/* * 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 #include #include #include #include "rotation_3d.hpp" #include "vector_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(); 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_camera_position(mrb_state *mrb, mrb_value self) { std::shared_ptr *camera_position; mrb_get_args(mrb, "d", &camera_position, &cg_vector_3d_type); cg_core.vk_graphics_pipeline_3d->camera_position = (*camera_position); return self; } static mrb_value cg_mCandyGear_set_camera_rotation(mrb_state *mrb, mrb_value self) { std::shared_ptr *camera_rotation; mrb_get_args(mrb, "d", &camera_rotation, &cg_rotation_3d_type); cg_core.vk_graphics_pipeline_3d->camera_rotation = (*camera_rotation); 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_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(mrb_state *mrb) { struct RClass *cg_m; cg_m = mrb_module_get(mrb, "CandyGear"); mrb_define_class_method( mrb, cg_m, "camera_position=", cg_mCandyGear_set_camera_position, MRB_ARGS_REQ(1)); mrb_define_class_method( mrb, cg_m, "camera_rotation=", cg_mCandyGear_set_camera_rotation, MRB_ARGS_REQ(1)); mrb_define_class_method( mrb, cg_m, "load_yaml", cg_mCandyGear_load_yaml, 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()); }