summaryrefslogtreecommitdiff
path: root/src/candy_gear/core.cpp
blob: 68979213fa68fa3cfd42433e214c965780d188da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Copyright 2022-2025 Frederico de Oliveira Linhares
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "core.hpp"

#include "candy_gear.hpp"
#include "font.hpp"
#include "graphic.hpp"
#include "key.hpp"
#include "orientation_3d.hpp"
#include "skeletal_model.hpp"
#include "skeletal_mesh.hpp"
#include "static_model.hpp"
#include "static_mesh.hpp"
#include "sound.hpp"
#include "sprite.hpp"
#include "sprite_3d.hpp"
#include "texture.hpp"
#include "vector_3d.hpp"
#include "vector_4d.hpp"
#include "view_2d.hpp"
#include "view_3d.hpp"

#ifdef DEBUG
#include <sstream>
#endif

namespace
{

void
load_mruby_symbols(void *obj)
{
  cg_core.sym_config = mrb_intern_cstr(cg_core.mrb, "config");
  cg_core.sym_debug = mrb_intern_cstr(cg_core.mrb, "debug");
  cg_core.sym_error = mrb_intern_cstr(cg_core.mrb, "error");
  cg_core.sym_fatal = mrb_intern_cstr(cg_core.mrb, "fatal");
  cg_core.sym_information = mrb_intern_cstr(cg_core.mrb, "information");
  cg_core.sym_init = mrb_intern_cstr(cg_core.mrb, "init");
  cg_core.sym_key_down = mrb_intern_cstr(cg_core.mrb, "key_down");
  cg_core.sym_key_up = mrb_intern_cstr(cg_core.mrb, "key_up");
  cg_core.sym_quit = mrb_intern_cstr(cg_core.mrb, "quit");
  cg_core.sym_tick = mrb_intern_cstr(cg_core.mrb, "tick");
  cg_core.sym_trace = mrb_intern_cstr(cg_core.mrb, "trace");
  cg_core.sym_warning = mrb_intern_cstr(cg_core.mrb, "warning");
}

void
load_game(void *obj)
{
  FILE *fp;
  mrb_value main_obj{mrb_top_self(cg_core.mrb)};

  mrb_define_module(cg_core.mrb, "CandyGear");
  cg_candy_gear_init_config(cg_core.mrb);
  cg_graphic_init_config(cg_core.mrb);

  fp = fopen(cg_core.game_file.c_str(), "rb");
  mrb_load_irep_file(cg_core.mrb, fp);
  fclose(fp);
  if (cg_core.mrb->exc)
  {
    mrb_print_error(cg_core.mrb);
    throw CommandError{"Error loading game."};
  }

  mrb_funcall_id(cg_core.mrb, main_obj, cg_core.sym_config, 0);
  if (cg_core.mrb->exc)
  {
    mrb_print_error(cg_core.mrb);
    throw CommandError{"Error configuring game."};
  }

  cg_candy_gear_finish_config(cg_core.mrb);
  cg_graphic_finish_config(cg_core.mrb);
}

void
load_blucat(void *obj)
{
	BluCat::INT::core.loader.execute(nullptr);
}

void
unload_blucat(void *obj)
{
	BluCat::INT::core.loader.revert(nullptr);
}

void
load_mruby_interface(void *obj)
{
  cg_candy_gear_init(cg_core.mrb);
  cg_font_init(cg_core.mrb);
  cg_key_init(cg_core.mrb);
  cg_orientation_3d_init(cg_core.mrb);
  cg_skeletal_model_init(cg_core.mrb);
  cg_skeletal_mesh_init(cg_core.mrb);
  cg_static_model_init(cg_core.mrb);
  cg_static_mesh_init(cg_core.mrb);
  cg_sound_init(cg_core.mrb);
  cg_sprite_init(cg_core.mrb);
  cg_sprite_3d_init(cg_core.mrb);
  cg_texture_init(cg_core.mrb);
  cg_vector_3d_init(cg_core.mrb);
  cg_vector_4d_init(cg_core.mrb);
  cg_view_2d_init(cg_core.mrb);
  cg_view_3d_init(cg_core.mrb);
}

}

const CommandChain cg_sCore::loader{
  {&load_mruby_symbols, nullptr},
  {&load_game, nullptr},
	{&load_blucat, &unload_blucat},
  {&load_mruby_interface, nullptr}
};