summaryrefslogtreecommitdiff
path: root/src/candy_gear.cpp
blob: f9c55281ee126c5faa51ae84a5691a8f9e00bf6b (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * Copyright 2022 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 "candy_gear.hpp"

#include <mruby/array.h>
#include <mruby/hash.h>
#include <mruby/string.h>

#include <yaml-cpp/yaml.h>

#include "core.hpp"
#include "view_2d.hpp"
#include "view_3d.hpp"

static mrb_value
parse_node(mrb_state *mrb, const YAML::Node &node)
{
  mrb_value value;
  std::string scalar;

  switch(node.Type())
  {
  case YAML::NodeType::Null:
    return mrb_nil_value();

  case YAML::NodeType::Scalar:
    scalar = node.as<std::string>();
    return mrb_str_new(mrb, scalar.data(), scalar.size());

  case YAML::NodeType::Sequence:
    value = mrb_ary_new_capa(mrb, node.size());
    for (YAML::const_iterator it = node.begin(); it != node.end(); it++)
      mrb_ary_push(mrb, value, parse_node(mrb, *it));
    return value;

  case YAML::NodeType::Map:
    value = mrb_hash_new_capa(mrb, node.size());
    for(YAML::const_iterator it = node.begin(); it != node.end(); it++)
      mrb_hash_set(
	mrb, value, parse_node(mrb, it->first), parse_node(mrb, it->second));
    return value;

  case YAML::NodeType::Undefined:
  default:
    return mrb_nil_value();
  }
}

static mrb_value
cg_mCandyGear_set_game_name(mrb_state *mrb, mrb_value self)
{
  mrb_value name;

  mrb_get_args(mrb, "S", &name);
  cg_core.game_name = RSTRING_PTR(name);

  return self;
}

static mrb_value
cg_mCandyGear_load_yaml(mrb_state *mrb, mrb_value self)
{
  const char *file_path;

  mrb_get_args(mrb, "z", &file_path);

  YAML::Node root = YAML::LoadFile(file_path);

  return parse_node(mrb, root);
}

static mrb_value
cg_mCandyGear_set_views(mrb_state *mrb, mrb_value self)
{
  struct RClass *cg_m, *cg_cView2D, *cg_cView3D;
  mrb_value *array;
  mrb_int array_len;

  std::vector<std::shared_ptr<VK::View2D>> views_2d;
  std::vector<std::shared_ptr<VK::View3D>> views_3d;

  cg_m = mrb_module_get(mrb, "CandyGear");
  cg_cView2D = mrb_class_get_under(mrb, cg_m, "View2D");
  cg_cView3D = mrb_class_get_under(mrb, cg_m, "View3D");

  mrb_get_args(mrb, "a", &array, &array_len);
  for(mrb_int i{0}; i < array_len; i++)
  {
    if(mrb_obj_is_kind_of(mrb, array[i], cg_cView2D))
    {
      auto v = (std::shared_ptr<VK::View2D>*)DATA_PTR(array[i]);
      views_2d.push_back(*v);
    }
    else if(mrb_obj_is_kind_of(mrb, array[i], cg_cView3D))
    {
      auto v = (std::shared_ptr<VK::View3D>*)DATA_PTR(array[i]);
      views_3d.push_back(*v);
    }
  }

  // A Renderer need at least one view to work.
  if(views_2d.size() > 0 || views_3d.size() > 0)
  {
    delete cg_core.vk_renderer;
    cg_core.vk_renderer = new VK::Renderer({views_2d, views_3d});
  }

  return self;
}

static mrb_value
cg_mCandyGear_log(mrb_state *mrb, mrb_value self)
{
  const char *message;
  mrb_sym sym_log_level;
  Log::Level log_lvl;

  mrb_get_args(mrb, "nz", &sym_log_level, &message);

  if(sym_log_level == cg_core.sym_trace)
    log_lvl = Log::Level::Trace;
  else if(sym_log_level == cg_core.sym_debug)
    log_lvl = Log::Level::Debug;
  else if(sym_log_level == cg_core.sym_information)
    log_lvl = Log::Level::Information;
  else if(sym_log_level == cg_core.sym_warning)
    log_lvl = Log::Level::Warning;
  else if(sym_log_level == cg_core.sym_error)
    log_lvl = Log::Level::Error;
  else
    log_lvl = Log::Level::Fatal;

  cg_core.log.message(log_lvl, message);

  return self;
}

static mrb_value
cg_mCandyGear_quit(mrb_state *mrb, mrb_value self)
{
  cg_core.quit_game = true;

  return self;
}

void
cg_candy_gear_init_config(mrb_state *mrb)
{
  struct RClass *cg_m;

  cg_m = mrb_module_get(mrb, "CandyGear");

  mrb_define_class_method(
    mrb, cg_m, "game_name=", cg_mCandyGear_set_game_name, MRB_ARGS_REQ(1));
}

void
cg_candy_gear_finish_config(mrb_state *mrb)
{
  struct RClass *cg_m;

  cg_m = mrb_module_get(mrb, "CandyGear");

  mrb_undef_class_method(mrb, cg_m, "game_name=");
}

void
cg_candy_gear_init(mrb_state *mrb)
{
  struct RClass *cg_m;

  cg_m = mrb_module_get(mrb, "CandyGear");

  mrb_define_class_method(
    mrb, cg_m, "load_yaml", cg_mCandyGear_load_yaml, MRB_ARGS_REQ(1));
  mrb_define_class_method(
    mrb, cg_m, "views=", cg_mCandyGear_set_views, MRB_ARGS_REQ(1));
  mrb_define_class_method(
    mrb, cg_m, "log", cg_mCandyGear_log, MRB_ARGS_REQ(2));
  mrb_define_class_method(
    mrb, cg_m, "quit", cg_mCandyGear_quit, MRB_ARGS_NONE());
}