summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/candy_gear.cpp33
-rw-r--r--src/candy_gear.hpp7
-rw-r--r--src/core.cpp193
-rw-r--r--src/core.hpp13
-rw-r--r--src/graphic.cpp83
-rw-r--r--src/graphic.hpp29
-rw-r--r--src/main.cpp118
-rw-r--r--src/vk/graphics_pipeline_2d.cpp10
-rw-r--r--src/vk/graphics_pipeline_3d.cpp14
-rw-r--r--src/vk/renderer.cpp8
-rw-r--r--src/vk/swapchain.cpp2
-rw-r--r--test/config/init.yaml4
-rw-r--r--test/src/main.rb6
-rwxr-xr-xtest/test2
14 files changed, 345 insertions, 177 deletions
diff --git a/src/candy_gear.cpp b/src/candy_gear.cpp
index 7ed1d13..f9c5528 100644
--- a/src/candy_gear.cpp
+++ b/src/candy_gear.cpp
@@ -22,6 +22,7 @@
#include <yaml-cpp/yaml.h>
+#include "core.hpp"
#include "view_2d.hpp"
#include "view_3d.hpp"
@@ -60,6 +61,17 @@ parse_node(mrb_state *mrb, const YAML::Node &node)
}
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;
@@ -146,6 +158,27 @@ cg_mCandyGear_quit(mrb_state *mrb, mrb_value 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;
diff --git a/src/candy_gear.hpp b/src/candy_gear.hpp
index 44c6fb5..3ec92b3 100644
--- a/src/candy_gear.hpp
+++ b/src/candy_gear.hpp
@@ -19,6 +19,13 @@
#include "core.hpp"
+// Provides the basic interface the game needs to configure the engine's
+// initialization.
+void
+cg_candy_gear_init_config(mrb_state *mrb);
+void
+cg_candy_gear_finish_config(mrb_state *mrb);
+
void
cg_candy_gear_init(mrb_state *mrb);
diff --git a/src/core.cpp b/src/core.cpp
index 8d828d6..1efaedc 100644
--- a/src/core.cpp
+++ b/src/core.cpp
@@ -16,6 +16,20 @@
#include "core.hpp"
+#include "candy_gear.hpp"
+#include "graphic.hpp"
+#include "key.hpp"
+#include "mesh.hpp"
+#include "model.hpp"
+#include "rotation_3d.hpp"
+#include "sound.hpp"
+#include "sprite.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
@@ -25,8 +39,11 @@
std::random_device random_seed;
std::mt19937 random_number_generator;
+namespace
+{
+
#ifdef DEBUG
-static VKAPI_ATTR VkBool32 VKAPI_CALL
+VKAPI_ATTR VkBool32 VKAPI_CALL
vk_debug_callback(
VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
VkDebugUtilsMessageTypeFlagsEXT message_type,
@@ -51,36 +68,71 @@ vk_debug_callback(
}
#endif
-static void
-load_variables(void *obj)
+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)
{
using namespace std::chrono;
- YAML::Node root = YAML::LoadFile(cg_core.config_file);
+ FILE *fp;
+ mrb_value main_obj = mrb_obj_iv_inspect(cg_core.mrb, cg_core.mrb->top_self);
- std::string game_name = root["name"].as<std::string>().c_str();
- cg_core.game_name = new char[game_name.size() + 1];
- strcpy(cg_core.game_name, game_name.c_str());
+ cg_core.game_name = "CandyGear Game";
- cg_core.screen_width = root["screen"]["width"].as<int>();
- cg_core.screen_height = root["screen"]["height"].as<int>();
+ cg_core.display_width = 800;
+ cg_core.display_height = 600;
cg_core.game_version_major = 0;
cg_core.game_version_minor = 1;
cg_core.game_version_patch = 0;
cg_core.fps = 30;
+
+ 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(), "r");
+ 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);
+
cg_core.max_frame_duration =
duration<long long, std::milli>(1000 / cg_core.fps);
}
-static void
-unload_variables(void *obj)
-{
- delete[] cg_core.game_name;
-}
-
-static void
+void
load_sdl(void *obj)
{
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
@@ -99,14 +151,14 @@ load_sdl(void *obj)
}
}
-static void
+void
unload_sdl(void *obj)
{
SDL_Vulkan_UnloadLibrary();
SDL_Quit();
}
-static void
+void
load_sdl_image(void *obj)
{
int flags = IMG_INIT_JPG|IMG_INIT_PNG|IMG_INIT_TIF;
@@ -118,13 +170,13 @@ load_sdl_image(void *obj)
}
}
-static void
+void
unload_sdl_image(void *obj)
{
IMG_Quit();
}
-static void
+void
load_sdl_mixer(void *obj)
{
int flags = MIX_INIT_OGG|MIX_INIT_MOD;
@@ -137,13 +189,13 @@ load_sdl_mixer(void *obj)
}
}
-static void
+void
unload_sdl_mixer(void *obj)
{
while(Mix_Init(0)) Mix_Quit();
}
-static void
+void
load_sdl_open_audio(void *obj)
{
if(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 1024) == -1)
@@ -154,19 +206,20 @@ load_sdl_open_audio(void *obj)
}
}
-static void
+void
unload_sdl_open_audio(void *obj)
{
Mix_CloseAudio();
}
-static void
+void
load_window(void *obj)
{
cg_core.window = NULL;
cg_core.window = SDL_CreateWindow(
- cg_core.game_name, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
- cg_core.screen_width, cg_core.screen_height, SDL_WINDOW_VULKAN);
+ cg_core.game_name.c_str(), SDL_WINDOWPOS_UNDEFINED,
+ SDL_WINDOWPOS_UNDEFINED, cg_core.display_width, cg_core.display_height,
+ SDL_WINDOW_VULKAN);
if(cg_core.window == NULL)
{
std::string error{"Window could not be created! SDL_Error → "};
@@ -175,13 +228,13 @@ load_window(void *obj)
}
}
-static void
+void
unload_window(void *obj)
{
SDL_DestroyWindow(cg_core.window);
}
-static void
+void
load_vk_instance(void *obj)
{
std::vector<const char*> vk_extensions;
@@ -279,7 +332,7 @@ load_vk_instance(void *obj)
VkApplicationInfo app_info;
app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
app_info.pNext = nullptr;
- app_info.pApplicationName = cg_core.game_name;
+ app_info.pApplicationName = cg_core.game_name.c_str();
app_info.applicationVersion = VK_MAKE_VERSION(
cg_core.game_version_major,
cg_core.game_version_minor,
@@ -320,13 +373,13 @@ load_vk_instance(void *obj)
}
}
-static void
+void
unload_vk_instance(void *obj)
{
vkDestroyInstance(cg_core.vk_instance, nullptr);
}
-static void
+void
load_window_surface(void *obj)
{
if(!SDL_Vulkan_CreateSurface(
@@ -338,14 +391,14 @@ load_window_surface(void *obj)
}
}
-static void
+void
unload_window_surface(void *obj)
{
vkDestroySurfaceKHR(cg_core.vk_instance, cg_core.window_surface, nullptr);
}
#ifdef DEBUG
-static void
+void
load_vk_debug_callback(void *obj)
{
PFN_vkCreateDebugUtilsMessengerEXT debug_messenger;
@@ -378,7 +431,7 @@ load_vk_debug_callback(void *obj)
CommandError{"Failed to setup debug callback for Vulkan."};
}
-static void
+void
unload_vk_debug_callback(void *obj)
{
PFN_vkDestroyDebugUtilsMessengerEXT debug_messenger;
@@ -391,7 +444,7 @@ unload_vk_debug_callback(void *obj)
}
#endif
-static void
+void
load_vk_devices(void *obj)
{
uint32_t devices_count;
@@ -425,7 +478,7 @@ load_vk_devices(void *obj)
}
}
-static void
+void
unload_vk_devices(void *obj)
{
cg_core.vk_devices.clear();
@@ -443,13 +496,13 @@ load_vk_swapchain(void *obj)
}
}
-static void
+void
unload_vk_swapchain(void *obj)
{
delete cg_core.vk_swapchain;
}
-static void
+void
load_vk_graphics_pipeline_3d_layout(void *obj)
{
try
@@ -463,13 +516,13 @@ load_vk_graphics_pipeline_3d_layout(void *obj)
}
}
-static void
+void
unload_vk_graphics_pipeline_3d_layout(void *obj)
{
delete cg_core.vk_graphics_pipeline_3d_layout;
}
-static void
+void
load_vk_graphics_pipeline_2d_layout(void *obj)
{
try
@@ -483,7 +536,7 @@ load_vk_graphics_pipeline_2d_layout(void *obj)
}
}
-static void
+void
unload_vk_graphics_pipeline_2d_layout(void *obj)
{
delete cg_core.vk_graphics_pipeline_2d_layout;
@@ -529,15 +582,15 @@ unload_vk_graphics_pipeline_2d(void *obj)
cg_core.vk_graphics_pipeline_2d = nullptr;
}
-static void
+void
load_vk_renderer(void *obj)
{
try
{
glm::vec4 region(
0.f, 0.f,
- static_cast<float>(cg_core.screen_width),
- static_cast<float>(cg_core.screen_height));
+ static_cast<float>(cg_core.display_width),
+ static_cast<float>(cg_core.display_height));
cg_core.vk_renderer = new VK::Renderer(
{},
{std::make_shared<VK::View3D>(region)});
@@ -548,38 +601,43 @@ load_vk_renderer(void *obj)
}
}
-static void
+void
unload_vk_renderer(void *obj)
{
delete cg_core.vk_renderer;
}
-static void
-load_mruby(void *obj)
-{
- cg_core.mrb = mrb_open();
- if (!cg_core.mrb) throw CommandError{"Failed to initialize mruby."};
-}
-
-static void
-unload_mruby(void *obj)
-{
- mrb_close(cg_core.mrb);
+void
+load_mruby_interface(void *obj)
+{
+ mrb_value main_obj = mrb_obj_iv_inspect(cg_core.mrb, cg_core.mrb->top_self);
+
+ cg_candy_gear_init(cg_core.mrb);
+ cg_key_init(cg_core.mrb);
+ cg_mesh_init(cg_core.mrb);
+ cg_model_init(cg_core.mrb);
+ cg_rotation_3d_init(cg_core.mrb);
+ cg_sound_init(cg_core.mrb);
+ cg_sprite_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);
+
+ mrb_funcall_id(cg_core.mrb, main_obj, cg_core.sym_init, 0);
+ if (cg_core.mrb->exc)
+ {
+ mrb_print_error(cg_core.mrb);
+ throw CommandError{"Error initializing game."};
+ }
}
-static void
-load_mruby_symbols(void *obj)
-{
- cg_core.sym_fatal = mrb_intern_cstr(cg_core.mrb, "fatal");
- cg_core.sym_error = mrb_intern_cstr(cg_core.mrb, "error");
- cg_core.sym_warning = mrb_intern_cstr(cg_core.mrb, "warning");
- cg_core.sym_information = mrb_intern_cstr(cg_core.mrb, "information");
- cg_core.sym_debug = mrb_intern_cstr(cg_core.mrb, "debug");
- cg_core.sym_trace = mrb_intern_cstr(cg_core.mrb, "trace");
}
const CommandChain cg_sCore::loader{
- {&load_variables, &unload_variables},
+ {&load_mruby_symbols, nullptr},
+ {&load_game, nullptr},
{&load_sdl, &unload_sdl},
{&load_sdl_image, &unload_sdl_image},
{&load_sdl_mixer, &unload_sdl_mixer},
@@ -601,6 +659,5 @@ const CommandChain cg_sCore::loader{
{&load_vk_graphics_pipeline_2d, &unload_vk_graphics_pipeline_2d},
{&load_vk_renderer, &unload_vk_renderer},
- {&load_mruby, &unload_mruby},
- {&load_mruby_symbols, nullptr}
+ {&load_mruby_interface, nullptr}
};
diff --git a/src/core.hpp b/src/core.hpp
index 7d1d62f..5a1556a 100644
--- a/src/core.hpp
+++ b/src/core.hpp
@@ -66,21 +66,22 @@ struct cg_sCore
mrb_state *mrb;
- /// mruby symbols
- mrb_sym sym_fatal, sym_error, sym_warning, sym_information, sym_debug,
- sym_trace;
+ std::string game_file;
- const char *config_file;
+ /// mruby symbols
+ mrb_sym sym_config, sym_debug, sym_error, sym_fatal, sym_information,
+ sym_init, sym_key_down, sym_key_up, sym_quit, sym_tick, sym_trace,
+ sym_warning;
/// Text displayed in the game window.
- char *game_name;
+ std::string game_name;
/**
* @{
* This is the ammount of pixel that the games uses when rendering to the
* screen.
*/
- uint32_t screen_width, screen_height;
+ uint32_t display_width, display_height;
/// @}
int game_version_major, game_version_minor, game_version_patch;
diff --git a/src/graphic.cpp b/src/graphic.cpp
new file mode 100644
index 0000000..f27fff7
--- /dev/null
+++ b/src/graphic.cpp
@@ -0,0 +1,83 @@
+/*
+ * 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 "graphic.hpp"
+
+#include "core.hpp"
+
+static mrb_value
+cg_mCandyGear_set_display_width(mrb_state *mrb, mrb_value self)
+{
+ mrb_int width;
+
+ mrb_get_args(mrb, "i", &width);
+ cg_core.display_width = width;
+
+ return self;
+}
+
+static mrb_value
+cg_mCandyGear_set_display_height(mrb_state *mrb, mrb_value self)
+{
+ mrb_int height;
+
+ mrb_get_args(mrb, "i", &height);
+ cg_core.display_height = height;
+
+ return self;
+}
+
+static mrb_value
+cg_mCandyGear_set_fps(mrb_state *mrb, mrb_value self)
+{
+ mrb_int fps;
+
+ mrb_get_args(mrb, "i", &fps);
+ cg_core.fps = fps;
+
+ return self;
+}
+
+void
+cg_graphic_init_config(mrb_state *mrb)
+{
+ struct RClass *cg_m, *cg_mGraphic;
+
+ cg_m = mrb_module_get(mrb, "CandyGear");
+ cg_mGraphic = mrb_define_module_under(mrb, cg_m, "Graphic");
+
+ mrb_define_class_method(
+ mrb, cg_mGraphic, "display_width=", cg_mCandyGear_set_display_width,
+ MRB_ARGS_REQ(1));
+ mrb_define_class_method(
+ mrb, cg_mGraphic, "display_height=", cg_mCandyGear_set_display_height,
+ MRB_ARGS_REQ(1));
+ mrb_define_class_method(
+ mrb, cg_mGraphic, "fps=", cg_mCandyGear_set_fps, MRB_ARGS_REQ(1));
+}
+
+void
+cg_graphic_finish_config(mrb_state *mrb)
+{
+ struct RClass *cg_m, *cg_mGraphic;
+
+ cg_m = mrb_module_get(mrb, "CandyGear");
+ cg_mGraphic = mrb_module_get_under(mrb, cg_m, "Graphic");
+
+ mrb_undef_class_method(mrb, cg_mGraphic, "display_width=");
+ mrb_undef_class_method(mrb, cg_mGraphic, "display_height=");
+ mrb_undef_class_method(mrb, cg_mGraphic, "fps=");
+}
diff --git a/src/graphic.hpp b/src/graphic.hpp
new file mode 100644
index 0000000..fbd9df8
--- /dev/null
+++ b/src/graphic.hpp
@@ -0,0 +1,29 @@
+/*
+ * 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.
+ */
+
+#ifndef CANDY_GEAR_GRAPHIC_H
+#define CANDY_GEAR_GRAPHIC_H 1
+
+#include "core.hpp"
+
+// Provides the basic interface the game needs to configure the engine's
+// initialization.
+void
+cg_graphic_init_config(mrb_state *mrb);
+void
+cg_graphic_finish_config(mrb_state *mrb);
+
+#endif /* CANDY_GEAR_GRAPHIC_H */
diff --git a/src/main.cpp b/src/main.cpp
index 178fcbb..7f02f49 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -17,28 +17,9 @@
#include <chrono>
#include <thread>
-#include "candy_gear.hpp"
#include "core.hpp"
-#include "key.hpp"
-#include "mesh.hpp"
-#include "model.hpp"
-#include "rotation_3d.hpp"
-#include "sound.hpp"
-#include "sprite.hpp"
-#include "texture.hpp"
-#include "vector_3d.hpp"
-#include "vector_4d.hpp"
-#include "view_2d.hpp"
-#include "view_3d.hpp"
-
cg_sCore cg_core;
-static void handle_error(mrb_state *mrb)
-{
- mrb_print_error(mrb);
- cg_core.quit_game = true;
-}
-
int main(int argc, char *argv[])
{
using namespace std::chrono;
@@ -49,50 +30,18 @@ int main(int argc, char *argv[])
// Random numbers
random_number_generator.seed(random_seed());
- mrb_value main_obj;
- mrb_sym sym_init, sym_key_down, sym_key_up, sym_quit, sym_tick;
- FILE *fp;
-
- cg_core.config_file = argv[1];
-
+ cg_core.game_file = argv[1];
+ cg_core.mrb = mrb_open();
+ if (!cg_core.mrb) throw CommandError{"Failed to initialize mruby."};
try{ cg_sCore::loader.execute(nullptr); }
catch(const CommandError &error)
{
cg_core.log.message(Log::Level::Fatal, error.what());
+ mrb_close(cg_core.mrb);
return 1;
}
- mrb_define_module(cg_core.mrb, "CandyGear");
- cg_candy_gear_init(cg_core.mrb);
- cg_key_init(cg_core.mrb);
- cg_mesh_init(cg_core.mrb);
- cg_model_init(cg_core.mrb);
- cg_rotation_3d_init(cg_core.mrb);
- cg_sound_init(cg_core.mrb);
- cg_sprite_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);
-
- main_obj = mrb_obj_iv_inspect(cg_core.mrb, cg_core.mrb->top_self);
- sym_init = mrb_intern_cstr(cg_core.mrb, "init");
- sym_key_down = mrb_intern_cstr(cg_core.mrb, "key_down");
- sym_key_up = mrb_intern_cstr(cg_core.mrb, "key_up");
- sym_quit = mrb_intern_cstr(cg_core.mrb, "quit");
- sym_tick = mrb_intern_cstr(cg_core.mrb, "tick");
-
- fp = fopen(argv[2], "r");
- mrb_load_irep_file(cg_core.mrb, fp);
- fclose(fp);
- if (cg_core.mrb->exc)
- handle_error(cg_core.mrb);
- else
- {
- mrb_funcall_id(cg_core.mrb, main_obj, sym_init, 0);
- if (cg_core.mrb->exc) handle_error(cg_core.mrb);
- }
+ mrb_value main_obj = mrb_obj_iv_inspect(cg_core.mrb, cg_core.mrb->top_self);
auto frame_start = steady_clock::now();
@@ -105,45 +54,52 @@ int main(int argc, char *argv[])
switch(event.type)
{
case SDL_KEYDOWN:
- mrb_funcall_id(
- cg_core.mrb, main_obj, sym_key_down, 1,
- mrb_int_value(cg_core.mrb, event.key.keysym.sym));
- break;
+ mrb_funcall_id(
+ cg_core.mrb, main_obj, cg_core.sym_key_down, 1,
+ mrb_int_value(cg_core.mrb, event.key.keysym.sym));
+ break;
case SDL_KEYUP:
- mrb_funcall_id(
- cg_core.mrb, main_obj, sym_key_up, 1,
- mrb_int_value(cg_core.mrb, event.key.keysym.sym));
- break;
+ mrb_funcall_id(
+ cg_core.mrb, main_obj, cg_core.sym_key_up, 1,
+ mrb_int_value(cg_core.mrb, event.key.keysym.sym));
+ break;
case SDL_MOUSEMOTION:
- break;
+ break;
case SDL_MOUSEBUTTONDOWN:
- break;
+ break;
case SDL_MOUSEBUTTONUP:
- break;
+ break;
case SDL_QUIT:
- mrb_funcall_id(cg_core.mrb, main_obj, sym_quit, 0);
- break;
+ mrb_funcall_id(cg_core.mrb, main_obj, cg_core.sym_quit, 0);
+ break;
}
}
- mrb_funcall_id(cg_core.mrb, main_obj, sym_tick, 0);
- if (cg_core.mrb->exc) handle_error(cg_core.mrb);
-
- cg_core.vk_renderer->draw();
-
- // Timer
+ mrb_funcall_id(cg_core.mrb, main_obj, cg_core.sym_tick, 0);
+ if (cg_core.mrb->exc)
+ {
+ mrb_print_error(cg_core.mrb);
+ cg_core.quit_game = true;
+ }
+ else
{
- auto frame_stop = steady_clock::now();
- auto frame_duration = frame_stop - frame_start;
+ cg_core.vk_renderer->draw();
+
+ // Timer
+ {
+ auto frame_stop = steady_clock::now();
+ auto frame_duration = frame_stop - frame_start;
- // If frame take less time than maximum allowed.
- if(cg_core.max_frame_duration > frame_duration)
- sleep_for(cg_core.max_frame_duration - frame_duration);
+ // If frame take less time than maximum allowed.
+ if(cg_core.max_frame_duration > frame_duration)
+ sleep_for(cg_core.max_frame_duration - frame_duration);
- frame_start = frame_stop;
+ frame_start = frame_stop;
+ }
}
}
+ mrb_close(cg_core.mrb);
cg_sCore::loader.revert(nullptr);
return 0;
diff --git a/src/vk/graphics_pipeline_2d.cpp b/src/vk/graphics_pipeline_2d.cpp
index 1509d2f..734a509 100644
--- a/src/vk/graphics_pipeline_2d.cpp
+++ b/src/vk/graphics_pipeline_2d.cpp
@@ -42,8 +42,8 @@ load_framebuffer(void *obj)
cg_core.vk_graphics_pipeline_2d_layout->render_pass;
framebuffer_info.attachmentCount = attachments.size();
framebuffer_info.pAttachments = attachments.data();
- framebuffer_info.width = cg_core.screen_width;
- framebuffer_info.height = cg_core.screen_height;
+ framebuffer_info.width = cg_core.display_width;
+ framebuffer_info.height = cg_core.display_height;
framebuffer_info.layers = 1;
if(vkCreateFramebuffer(
@@ -130,14 +130,14 @@ load_pipeline(void *obj)
VkViewport viewport = {};
viewport.x = 0;
viewport.y = 0;
- viewport.width = cg_core.screen_width;
- viewport.height = cg_core.screen_height;
+ viewport.width = cg_core.display_width;
+ viewport.height = cg_core.display_height;
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;
VkRect2D scissor = {};
scissor.offset = {0, 0};
- scissor.extent = {cg_core.screen_width, cg_core.screen_height};
+ scissor.extent = {cg_core.display_width, cg_core.display_height};
VkPipelineViewportStateCreateInfo viewport_state = {};
viewport_state.sType =
diff --git a/src/vk/graphics_pipeline_3d.cpp b/src/vk/graphics_pipeline_3d.cpp
index f3a54e7..f08e43a 100644
--- a/src/vk/graphics_pipeline_3d.cpp
+++ b/src/vk/graphics_pipeline_3d.cpp
@@ -187,8 +187,8 @@ load_depth_image(void *obj)
auto self = static_cast<VK::GraphicsPipeline3D*>(obj);
VkExtent3D extent3d{};
- extent3d.width = cg_core.screen_width;
- extent3d.height = cg_core.screen_height;
+ extent3d.width = cg_core.display_width;
+ extent3d.height = cg_core.display_height;
extent3d.depth = 1;
try
@@ -273,8 +273,8 @@ load_framebuffer(void *obj)
cg_core.vk_graphics_pipeline_3d_layout->render_pass;
framebuffer_info.attachmentCount = attachments.size();
framebuffer_info.pAttachments = attachments.data();
- framebuffer_info.width = cg_core.screen_width;
- framebuffer_info.height = cg_core.screen_height;
+ framebuffer_info.width = cg_core.display_width;
+ framebuffer_info.height = cg_core.display_height;
framebuffer_info.layers = 1;
@@ -371,14 +371,14 @@ load_pipeline(void *obj)
VkViewport viewport = {};
viewport.x = 0;
viewport.y = 0;
- viewport.width = cg_core.screen_width;
- viewport.height = cg_core.screen_height;
+ viewport.width = cg_core.display_width;
+ viewport.height = cg_core.display_height;
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;
VkRect2D scissor = {};
scissor.offset = {0, 0};
- scissor.extent = {cg_core.screen_width, cg_core.screen_height};
+ scissor.extent = {cg_core.display_width, cg_core.display_height};
VkPipelineViewportStateCreateInfo viewport_state = {};
viewport_state.sType =
diff --git a/src/vk/renderer.cpp b/src/vk/renderer.cpp
index 5ebc271..7492cce 100644
--- a/src/vk/renderer.cpp
+++ b/src/vk/renderer.cpp
@@ -212,8 +212,8 @@ Renderer::draw()
cg_core.vk_graphics_pipeline_3d->swapchain_framebuffers[image_index];
render_pass_begin.renderArea.offset = {0, 0};
render_pass_begin.renderArea.extent = {
- static_cast<uint32_t>(cg_core.screen_width),
- static_cast<uint32_t>(cg_core.screen_height)};
+ static_cast<uint32_t>(cg_core.display_width),
+ static_cast<uint32_t>(cg_core.display_height)};
render_pass_begin.clearValueCount = clear_values.size();
render_pass_begin.pClearValues = clear_values.data();
@@ -239,8 +239,8 @@ Renderer::draw()
cg_core.vk_graphics_pipeline_2d->swapchain_framebuffers[image_index];
render_pass_begin.renderArea.offset = {0, 0};
render_pass_begin.renderArea.extent = {
- static_cast<uint32_t>(cg_core.screen_width),
- static_cast<uint32_t>(cg_core.screen_height)};
+ static_cast<uint32_t>(cg_core.display_width),
+ static_cast<uint32_t>(cg_core.display_height)};
render_pass_begin.clearValueCount = 0;
render_pass_begin.pClearValues = nullptr;
diff --git a/src/vk/swapchain.cpp b/src/vk/swapchain.cpp
index 1dd19d9..2c67ebd 100644
--- a/src/vk/swapchain.cpp
+++ b/src/vk/swapchain.cpp
@@ -51,7 +51,7 @@ load_swapchain(void *obj)
swapchain_create_info.imageColorSpace = vk_surface_formats[0].colorSpace;
swapchain_create_info.imageExtent = {
- cg_core.screen_width, cg_core.screen_height};
+ cg_core.display_width, cg_core.display_height};
swapchain_create_info.imageArrayLayers = 1;
swapchain_create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchain_create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
diff --git a/test/config/init.yaml b/test/config/init.yaml
deleted file mode 100644
index 0a7fdcf..0000000
--- a/test/config/init.yaml
+++ /dev/null
@@ -1,4 +0,0 @@
-name: "Candy Gear Test"
-screen:
- width: 1280
- height: 720
diff --git a/test/src/main.rb b/test/src/main.rb
index aaffbfb..be0ae0b 100644
--- a/test/src/main.rb
+++ b/test/src/main.rb
@@ -16,6 +16,12 @@ CAMERA_ROTATION_SPEED = Math::PI/45;
BOX_ROTATION_SPEED = Math::PI/180;
TRANSLATION_SPEED = 0.5;
+def config()
+ CandyGear.game_name = "Candy Gear Test";
+ CandyGear::Graphic.display_width = 1280;
+ CandyGear::Graphic.display_height = 720;
+end
+
def init()
texture = CandyGear::Texture.from_image("textures/color_texture.png");
mesh = CandyGear::Mesh.new("meshes/cube.cgmesh");
diff --git a/test/test b/test/test
index 0d3dede..ec6c529 100755
--- a/test/test
+++ b/test/test
@@ -1,2 +1,2 @@
#!/bin/bash
-candy_gear config/init.yaml test.mrb
+candy_gear test.mrb