diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2023-11-30 11:31:45 -0500 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2023-11-30 11:31:45 -0500 |
| commit | 6e498a3781f5ccbbaa6cf5e407cf67722624760f (patch) | |
| tree | 8a4e95e4d962b3102d8d348a0f292f8ef8ee30df /src | |
| parent | 9830bc2425385de6e666251fa9df6318605c639c (diff) | |
prefer explicit callback system over events
Diffstat (limited to 'src')
| -rw-r--r-- | src/game-settings.cpp | 1 | ||||
| -rw-r--r-- | src/game-settings.hpp | 9 | ||||
| -rw-r--r-- | src/game.cpp | 20 | ||||
| -rw-r--r-- | src/game.hpp | 4 | ||||
| -rw-r--r-- | src/window.cpp | 8 | ||||
| -rw-r--r-- | src/window.hpp | 2 |
6 files changed, 32 insertions, 12 deletions
diff --git a/src/game-settings.cpp b/src/game-settings.cpp index 6f09432..54699dd 100644 --- a/src/game-settings.cpp +++ b/src/game-settings.cpp @@ -1,3 +1,4 @@ #include "game-settings.hpp" GameSettings pkeSettings{}; +PKEGameCallbacks pkeGameCallbacks{}; diff --git a/src/game-settings.hpp b/src/game-settings.hpp index 3456725..480ae8e 100644 --- a/src/game-settings.hpp +++ b/src/game-settings.hpp @@ -33,6 +33,15 @@ struct GameSettings { } mem; }; +struct PKEGameCallbacks { + void (*OnInit)() = nullptr; + void (*OnTick)(double delta) = nullptr; + void (*OnTeardown)() = nullptr; + void (*OnImGuiRender)() = nullptr; +}; + extern GameSettings pkeSettings; +extern PKEGameCallbacks pkeGameCallbacks; + #endif /* PKE_GAME_SETTINGS_HPP */ diff --git a/src/game.cpp b/src/game.cpp index 511003a..72228bf 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -439,7 +439,7 @@ const uint64_t consoleBufferCount = 30; const uint64_t consoleLineLength = 128; char consoleBuffer[consoleBufferCount][consoleLineLength]; long consoleBufferIndex = 0; -void RecordImGuiConsole() { +void Game_RecordImGui() { static bool scrollToBottom = true; if (!ImGui::Begin("Console", &pkeSettings.editorSettings.isShowingConsole)) { ImGui::End(); @@ -484,13 +484,15 @@ void Game_Tick(double delta) { PkeInput_Tick(delta); // TODO invoke external ticks here - Event_Dispatch<TickEvent, double>("GAME_TICK", delta); + if (pkeGameCallbacks.OnTick) { + pkeGameCallbacks.OnTick(delta); + } EntityType_Tick_Late(delta); ECS_Tick_Late(delta); } -void Game_Main(PKEWindowProperties *windowProps) { +void Game_Main(PKEWindowProperties windowProps) { fprintf(stdout, "Game_Main Entering\n"); try { AM_Init(); @@ -500,7 +502,9 @@ void Game_Main(PKEWindowProperties *windowProps) { CreateWindow(windowProps); PkeInput_Init(); EntityType_Init(); - Event_Dispatch<EventHandler>("GAME_INIT"); + if (pkeGameCallbacks.OnInit) { + pkeGameCallbacks.OnInit(); + } GameTimePoint lastTimePoint = pkeSettings.steadyClock.now(); double deltaTillNextRender = pkeSettings.deltaPerFrame; @@ -589,7 +593,9 @@ void Game_Main(PKEWindowProperties *windowProps) { #ifndef NDEBUG Pke_DebugPrint(); #endif - Event_Dispatch<EventHandler>("GAME_TEARDOWN"); + if (pkeGameCallbacks.OnTeardown) { + pkeGameCallbacks.OnTeardown(); + } Game_Teardown(); Event_Teardown(); EntityType_Teardown(); @@ -609,7 +615,9 @@ void Game_Init() { for (long i = 0; i < consoleBufferCount; ++i) { memset(consoleBuffer[i], '\0', consoleLineLength); } - Event_RegisterCallback("RenderImGui", RecordImGuiConsole); + if (!pkeGameCallbacks.OnImGuiRender) { + pkeGameCallbacks.OnImGuiRender = Game_RecordImGui; + } } void Game_Teardown() { } diff --git a/src/game.hpp b/src/game.hpp index 3e21c26..5697efa 100644 --- a/src/game.hpp +++ b/src/game.hpp @@ -1,12 +1,14 @@ #ifndef PKE_GAME_HPP #define PKE_GAME_HPP +#include "game-settings.hpp" #include "window-types.hpp" -void Game_Main(PKEWindowProperties *windowProps); +void Game_Main(PKEWindowProperties windowProps); void Game_Init(); void Game_Tick(double delta); void Game_Teardown(); +void Game_RecordImGui(); void Game_SaveSceneFile(const char *); void Game_LoadSceneFile(const char *); diff --git a/src/window.cpp b/src/window.cpp index 2b75cdd..8d40221 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -2427,12 +2427,12 @@ void RecordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) { vkCmdDraw(commandBuffer, 3, 1, 0, 0); // ImGui - if (isImGuiRenderActive) { + if (isImGuiRenderActive && pkeGameCallbacks.OnImGuiRender) { ImGui_ImplVulkan_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); - Event_Dispatch<EventHandler>("RenderImGui"); + pkeGameCallbacks.OnImGuiRender(); ImGui::Render(); auto drawData = ImGui::GetDrawData(); @@ -2547,7 +2547,7 @@ void FramebufferResizeCallback(GLFWwindow *window, int width, int height) { shouldRecreateSwapchain = true; } -void CreateWindow(PKEWindowProperties *wp) { +void CreateWindow(PKEWindowProperties wp) { if (vkInstance != nullptr) return; MemBkt_Vulkan = Pke_BeginTransientBucket(); vulkanAllocs = Pke_New<DynArray<pke_vkAllocData>>(MemBkt_Vulkan); @@ -2555,7 +2555,7 @@ void CreateWindow(PKEWindowProperties *wp) { vulkanAllocs->Reserve(2048); glfwInit(); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); - window = glfwCreateWindow(wp->width, wp->height, "Pikul", nullptr, nullptr); + window = glfwCreateWindow(wp.width, wp.height, "Pikul", nullptr, nullptr); InitVulkan(); glfwSetFramebufferSizeCallback(window, FramebufferResizeCallback); CreateSwapchain(); diff --git a/src/window.hpp b/src/window.hpp index 0194a18..73369d3 100644 --- a/src/window.hpp +++ b/src/window.hpp @@ -88,7 +88,7 @@ struct ImplementedPipelines { }; extern ImplementedPipelines pkePipelines; -void CreateWindow(PKEWindowProperties *wp); +void CreateWindow(PKEWindowProperties wp); void DestroyWindow(); VkShaderModule UploadShader(AssetHandle handle); void Render(); |
