From 3dacb0c095d837d2e035308bd824c3a4f35e0ede Mon Sep 17 00:00:00 2001 From: Karl Date: Fri, 30 Apr 2021 23:11:01 +0200 Subject: [PATCH] Add ability for client to override fixed and dynamic update --- cpp/Application.cpp | 8 +++----- include/Gedeng/Application.h | 22 ++++++++++++++++++++-- include/Gedeng/Input.h | 3 ++- test/test-app/main.cpp | 15 +++++++++++++-- 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/cpp/Application.cpp b/cpp/Application.cpp index 723195b..e73cb69 100644 --- a/cpp/Application.cpp +++ b/cpp/Application.cpp @@ -9,7 +9,7 @@ namespace Gedeng { void Application::run() { // Setup Rendering // FIXME: Make these parameters variable, maybe move this to a different function - RenderBackend::initialize_window(1920, 1080, String("Application")); + RenderBackend::initialize_window(WINDOW_SIZE_X, WINDOW_SIZE_Y, WINDOW_NAME); Input::initialize(RenderBackend::get_window()); unsigned long previous_time_ms = Time::get_time_ms(); @@ -26,13 +26,11 @@ void Application::run() { // Update fixed time step while (time_until_fixed_update_ms >= MS_PER_UPDATE) { - // Fixed Update - GG_CORE_INFO("Fixed Update"); + fixed_update(static_cast(MS_PER_UPDATE) / 1000.0); time_until_fixed_update_ms -= MS_PER_UPDATE; } - // Variable update - GG_CORE_INFO("Variable Update"); + dynamic_update(static_cast(elapsed_time_ms) / 1000.0); } } diff --git a/include/Gedeng/Application.h b/include/Gedeng/Application.h index d3f737d..05eb7c1 100644 --- a/include/Gedeng/Application.h +++ b/include/Gedeng/Application.h @@ -1,18 +1,36 @@ #pragma once +#include "String.h" + namespace Gedeng { class Application { public: - Application() = default; + Application(unsigned long ms_per_update, unsigned int window_size_x, unsigned int window_size_y, String window_name) + : MS_PER_UPDATE(ms_per_update), WINDOW_SIZE_X(window_size_x), WINDOW_SIZE_Y(window_size_y), + WINDOW_NAME(window_name) { + } // Virtual since this class will be inherited by user-created applications virtual ~Application() = default; + // Game Loop void run(); + // Primarily for gameplay and physics + // To be overridden by client applications + virtual void fixed_update(double delta) = 0; + + // Primarily for rendering + // To be overridden by client applications + virtual void dynamic_update(double delta) = 0; + private: - const unsigned long MS_PER_UPDATE = 20; + // TODO: These will probably become a separate Settings struct + const unsigned long MS_PER_UPDATE; + const unsigned int WINDOW_SIZE_X; + const unsigned int WINDOW_SIZE_Y; + const String WINDOW_NAME; }; // To be defined in client applications diff --git a/include/Gedeng/Input.h b/include/Gedeng/Input.h index b304615..1212999 100644 --- a/include/Gedeng/Input.h +++ b/include/Gedeng/Input.h @@ -17,7 +17,8 @@ class Input { glfwSetKeyCallback(window, key_callback); } - // FIXME: Ignore warnings produced by these unused variables -- they're required for the callback to work + // FIXME: Ignore warnings produced by these unused variables -- they're required for the callback to work: + // https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) { if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GLFW_TRUE); diff --git a/test/test-app/main.cpp b/test/test-app/main.cpp index 7d2e221..94172a1 100644 --- a/test/test-app/main.cpp +++ b/test/test-app/main.cpp @@ -4,12 +4,23 @@ class TestApp : public Gedeng::Application { public: - TestApp() = default; + TestApp(unsigned long ms_per_update, unsigned int window_size_x, unsigned int window_size_y, + Gedeng::String window_name) + : Application(ms_per_update, window_size_x, window_size_y, window_name) { + } ~TestApp() = default; + + void fixed_update(double delta) override { + GG_CLIENT_INFO("Fixed update"); + } + + void dynamic_update(double delta) override { + GG_CLIENT_INFO("Dynamic update"); + } }; Gedeng::Application *Gedeng::create_application() { GG_CLIENT_INFO("Creating Application"); - return new TestApp(); + return new TestApp(20, 900, 600, String("Test App")); }