From ee3d27fda32a0573d00e1d0c29c656aed4978554 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Mon, 22 Dec 2025 14:33:26 +1000 Subject: [PATCH] Time -> manager --- src/engine/Engine.cpp | 7 ++-- src/engine/Engine.hpp | 5 +++ src/input/input.cpp | 16 +++++--- src/input/inputaction.hpp | 2 +- src/rpg/cutscene/item/cutsceneitem.cpp | 6 ++- src/rpg/entity/entity.cpp | 6 ++- src/rpg/entity/player.cpp | 1 - src/rpg/rpg.cpp | 6 ++- src/time/CMakeLists.txt | 2 +- src/time/TimeManager.cpp | 49 +++++++++++++++++++++++ src/time/TimeManager.hpp | 38 ++++++++++++++++++ src/time/time.cpp | 54 -------------------------- src/time/time.hpp | 39 ------------------- src/ui/uidebug.cpp | 21 +++++++--- 14 files changed, 135 insertions(+), 117 deletions(-) create mode 100644 src/time/TimeManager.cpp create mode 100644 src/time/TimeManager.hpp delete mode 100644 src/time/time.cpp delete mode 100644 src/time/time.hpp diff --git a/src/engine/Engine.cpp b/src/engine/Engine.cpp index 64a0ce9..f282d5f 100644 --- a/src/engine/Engine.cpp +++ b/src/engine/Engine.cpp @@ -7,7 +7,6 @@ #include "Engine.hpp" #include "util/memory.hpp" -#include "time/time.hpp" #include "input/input.hpp" #include "locale/localemanager.hpp" #include "display/display.hpp" @@ -26,7 +25,8 @@ Engine Engine::ENGINE; Engine::Engine() : running(false), argc(0), - argv(nullptr) + argv(nullptr), + time() { } @@ -36,7 +36,6 @@ void Engine::init(const int32_t argc, const char_t **argv) { this->argv = argv; // Init systems. Order is important. - timeInit(); inputInit(); assetInit(); localeManagerInit(); @@ -54,7 +53,7 @@ void Engine::init(const int32_t argc, const char_t **argv) { } void Engine::update() { - timeUpdate(); + this->time.update(); inputUpdate(); rpgUpdate(); diff --git a/src/engine/Engine.hpp b/src/engine/Engine.hpp index d061c7d..1d71549 100644 --- a/src/engine/Engine.hpp +++ b/src/engine/Engine.hpp @@ -8,6 +8,8 @@ #pragma once #include "display/display.hpp"// Important to be included first. +#include "time/TimeManager.hpp" + namespace Dusk { struct Engine { private: @@ -15,9 +17,12 @@ namespace Dusk { public: static Engine ENGINE; + int32_t argc; const char_t **argv; + TimeManager time; + /** * Initializes the engine. */ diff --git a/src/input/input.cpp b/src/input/input.cpp index fac1166..51463d2 100644 --- a/src/input/input.cpp +++ b/src/input/input.cpp @@ -10,7 +10,9 @@ #include "util/memory.hpp" #include "util/string.hpp" #include "util/math.hpp" -#include "time/time.hpp" +#include "engine/Engine.hpp" + +using namespace Dusk; input_t INPUT; @@ -86,7 +88,7 @@ void inputUpdate(void) { #if TIME_FIXED == 0 action->lastDynamicValue = action->currentDynamicValue; action->currentDynamicValue = 0.0f; - if(!TIME.dynamicUpdate) { + if(!Engine::ENGINE.time.dynamicUpdate) { action->lastValue = action->currentValue; action->currentValue = 0.0f; } @@ -114,7 +116,7 @@ void inputUpdate(void) { INPUT.actions[cur->action].currentDynamicValue = mathMax( cur->curVal, INPUT.actions[cur->action].currentDynamicValue ); - if(!TIME.dynamicUpdate) { + if(!Engine::ENGINE.time.dynamicUpdate) { INPUT.actions[cur->action].currentValue = mathMax( cur->curVal, INPUT.actions[cur->action].currentValue ); @@ -131,7 +133,9 @@ void inputUpdate(void) { float_t inputGetCurrentValue(const inputaction_t action) { #if TIME_FIXED == 0 - if(TIME.dynamicUpdate) return inputGetCurrentValueDynamic(action); + if(Engine::ENGINE.time.dynamicUpdate) { + return inputGetCurrentValueDynamic(action); + } #endif assertTrue(action < INPUT_ACTION_COUNT, "Input action out of bounds"); @@ -140,7 +144,9 @@ float_t inputGetCurrentValue(const inputaction_t action) { float_t inputGetLastValue(const inputaction_t action) { #if TIME_FIXED == 0 - if(TIME.dynamicUpdate) return inputGetLastValueDynamic(action); + if(Engine::ENGINE.time.dynamicUpdate) { + return inputGetLastValueDynamic(action); + } #endif assertTrue(action < INPUT_ACTION_COUNT, "Input action out of bounds"); diff --git a/src/input/inputaction.hpp b/src/input/inputaction.hpp index c3ac86f..197adb9 100644 --- a/src/input/inputaction.hpp +++ b/src/input/inputaction.hpp @@ -6,7 +6,7 @@ */ #pragma once -#include "time/time.hpp" +#include "time/TimeManager.hpp" typedef uint8_t inputaction_t; diff --git a/src/rpg/cutscene/item/cutsceneitem.cpp b/src/rpg/cutscene/item/cutsceneitem.cpp index 3ed73fc..5a69e0d 100644 --- a/src/rpg/cutscene/item/cutsceneitem.cpp +++ b/src/rpg/cutscene/item/cutsceneitem.cpp @@ -7,7 +7,9 @@ #include "rpg/cutscene/cutscenesystem.hpp" #include "input/input.hpp" -#include "time/time.hpp" +#include "engine/Engine.hpp" + +using namespace Dusk; void cutsceneItemStart(const cutsceneitem_t *item, cutsceneitemdata_t *data) { switch(item->type) { @@ -44,7 +46,7 @@ void cutsceneItemUpdate(const cutsceneitem_t *item, cutsceneitemdata_t *data) { break; case CUTSCENE_ITEM_TYPE_WAIT: - data->wait -= TIME.delta; + data->wait -= Engine::ENGINE.time.delta; if(data->wait <= 0) cutsceneSystemNext(); break; diff --git a/src/rpg/entity/entity.cpp b/src/rpg/entity/entity.cpp index ed1326a..c5e0e48 100644 --- a/src/rpg/entity/entity.cpp +++ b/src/rpg/entity/entity.cpp @@ -8,10 +8,12 @@ #include "entity.hpp" #include "assert/assert.hpp" #include "util/memory.hpp" -#include "time/time.hpp" #include "util/math.hpp" #include "rpg/cutscene/cutscenemode.hpp" #include "rpg/world/map.hpp" +#include "engine/Engine.hpp" + +using namespace Dusk; entity_t ENTITIES[ENTITY_COUNT]; @@ -39,7 +41,7 @@ void entityUpdate(entity_t *entity) { // What state is the entity in? if(entity->animation != ENTITY_ANIM_IDLE) { // Entity is mid animation, tick it (down). - entity->animTime -= TIME.delta; + entity->animTime -= Engine::ENGINE.time.delta; if(entity->animTime <= 0) { entity->animation = ENTITY_ANIM_IDLE; entity->animTime = 0; diff --git a/src/rpg/entity/player.cpp b/src/rpg/entity/player.cpp index 0030b0c..1ef76c3 100644 --- a/src/rpg/entity/player.cpp +++ b/src/rpg/entity/player.cpp @@ -9,7 +9,6 @@ #include "assert/assert.hpp" #include "rpg/rpgcamera.hpp" #include "util/memory.hpp" -#include "time/time.hpp" void playerInit(entity_t *entity) { assertNotNull(entity, "Entity pointer cannot be NULL"); diff --git a/src/rpg/rpg.cpp b/src/rpg/rpg.cpp index b354c6e..27f7fdb 100644 --- a/src/rpg/rpg.cpp +++ b/src/rpg/rpg.cpp @@ -9,11 +9,13 @@ #include "entity/entity.hpp" #include "rpg/world/map.hpp" #include "rpg/cutscene/cutscenesystem.hpp" -#include "time/time.hpp" #include "rpgcamera.hpp" #include "rpgtextbox.hpp" #include "util/memory.hpp" #include "assert/assert.hpp" +#include "engine/Engine.hpp" + +using namespace Dusk; void rpgInit(void) { memoryZero(ENTITIES, sizeof(ENTITIES)); @@ -38,7 +40,7 @@ void rpgInit(void) { void rpgUpdate(void) { #if TIME_FIXED == 0 - if(TIME.dynamicUpdate) return; + if(Engine::ENGINE.time.dynamicUpdate) return; #endif // TODO: Do not update if the scene is not the map scene? diff --git a/src/time/CMakeLists.txt b/src/time/CMakeLists.txt index 66bf87b..c43821c 100644 --- a/src/time/CMakeLists.txt +++ b/src/time/CMakeLists.txt @@ -6,7 +6,7 @@ # Sources target_sources(${DUSK_TARGET_NAME} PRIVATE - time.cpp + TimeManager.cpp ) # Compiler defs diff --git a/src/time/TimeManager.cpp b/src/time/TimeManager.cpp new file mode 100644 index 0000000..10e153f --- /dev/null +++ b/src/time/TimeManager.cpp @@ -0,0 +1,49 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "TimeManager.hpp" +#include "assert/assert.hpp" + +#if TIME_SDL2 + #include +#endif + +using namespace Dusk; + +TimeManager::TimeManager(void) : + time(TIME_STEP), + delta(TIME_STEP), + #if TIME_FIXED == 0 + dynamicUpdate(false), + dynamicDelta(TIME_STEP), + dynamicTime(TIME_STEP) + #endif +{ +} + +void TimeManager::update(void) { + #if TIME_FIXED == 0 + #if TIME_SDL2 + float_t newTime = (float_t)SDL_GetTicks() / 1000.0f; + this->dynamicDelta = newTime - this->dynamicTime; + this->dynamicTime = newTime; + this->dynamicUpdate = true; + #else + #error "No time platform defined" + #endif + + assertTrue(this->dynamicDelta >= 0.0f, "Time delta is negative"); + if(this->dynamicTime - this->time >= TIME_STEP) { + this->dynamicUpdate = false; + this->delta = TIME_STEP; + this->time += TIME_STEP; + } + #else + this->delta = TIME_STEP; + this->time += TIME_STEP; + #endif +} \ No newline at end of file diff --git a/src/time/TimeManager.hpp b/src/time/TimeManager.hpp new file mode 100644 index 0000000..7085530 --- /dev/null +++ b/src/time/TimeManager.hpp @@ -0,0 +1,38 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "dusk.hpp" + +#define TIME_STEP (1.0f / 60.0f) // 60 Ticks per second (what we are aiming for) + +#ifndef TIME_FIXED + #define TIME_FIXED 0 +#endif + +namespace Dusk { + struct TimeManager { + float_t delta; + float_t time; + + #if TIME_FIXED == 0 + bool_t dynamicUpdate; + float_t dynamicDelta; + float_t dynamicTime; + #endif + + /** + * Constructor + */ + TimeManager(); + + /** + * Update the time manager + */ + void update(); + }; +} \ No newline at end of file diff --git a/src/time/time.cpp b/src/time/time.cpp deleted file mode 100644 index dfced72..0000000 --- a/src/time/time.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/** - * Copyright (c) 2025 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#include "time.hpp" -#include "util/memory.hpp" -#include "assert/assert.hpp" - -#if TIME_SDL2 - #include -#endif - -dusktime_t TIME; - -void timeInit(void) { - memoryZero(&TIME, sizeof(TIME)); - - // Set these to something non-zero. - TIME.time = TIME_STEP; - TIME.delta = TIME_STEP; - - #if TIME_FIXED == 0 - TIME.dynamicTime = TIME_STEP; - TIME.dynamicDelta = TIME_STEP; - TIME.dynamicUpdate = false; - #endif -} - -void timeUpdate(void) { - - #if TIME_FIXED == 0 - #if TIME_SDL2 - float_t newTime = (float_t)SDL_GetTicks() / 1000.0f; - TIME.dynamicDelta = newTime - TIME.dynamicTime; - TIME.dynamicTime = newTime; - TIME.dynamicUpdate = true; - #else - #error "No time platform defined" - #endif - - assertTrue(TIME.dynamicDelta >= 0.0f, "Time delta is negative"); - if(TIME.dynamicTime - TIME.time >= TIME_STEP) { - TIME.dynamicUpdate = false; - TIME.delta = TIME_STEP; - TIME.time += TIME_STEP; - } - #else - TIME.delta = TIME_STEP; - TIME.time += TIME_STEP; - #endif -} \ No newline at end of file diff --git a/src/time/time.hpp b/src/time/time.hpp deleted file mode 100644 index c237750..0000000 --- a/src/time/time.hpp +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Copyright (c) 2025 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#pragma once -#include "dusk.hpp" - -#define TIME_STEP (1.0f / 60.0f) // 60 Ticks per second (what we are aiming for) - -#ifndef TIME_FIXED - #define TIME_FIXED 0 -#endif - -typedef struct { - float_t delta; - float_t time; - - #if TIME_FIXED == 0 - bool_t dynamicUpdate; - float_t dynamicDelta; - float_t dynamicTime; - #endif -} dusktime_t; - -extern dusktime_t TIME; - - -/** - * Initializes the time system. - */ -void timeInit(void); - -/** - * Updates the time system - */ -void timeUpdate(void); \ No newline at end of file diff --git a/src/ui/uidebug.cpp b/src/ui/uidebug.cpp index 499744e..5e6863b 100644 --- a/src/ui/uidebug.cpp +++ b/src/ui/uidebug.cpp @@ -6,13 +6,14 @@ */ #include "uidebug.hpp" -#include "time/time.hpp" #include "util/string.hpp" #include "ui/uitext.hpp" #include "display/screen.hpp" #include "display/spritebatch.hpp" #include "rpg/entity/entity.hpp" +#include "engine/Engine.hpp" +using namespace Dusk; bool_t UI_DEBUG_DRAW = true; void uiDebugRender(const tileset_t *tileset, texture_t *texture) { @@ -24,15 +25,23 @@ void uiDebugRender(const tileset_t *tileset, texture_t *texture) { // FPS Meter #if TIME_FIXED == 0 - float_t fpsDynamic = TIME.dynamicDelta > 0.0f ? (1.0f / TIME.dynamicDelta) : 0.0f; - float_t fpsFixed = TIME.delta > 0.0f ? (1.0f / TIME.delta) : 0.0f; + float_t fpsDynamic = ( + Engine::ENGINE.time.dynamicDelta > 0.0f ? + (1.0f / Engine::ENGINE.time.dynamicDelta) : + 0.0f + ); + float_t fpsFixed = ( + Engine::ENGINE.time.delta > 0.0f ? + (1.0f / Engine::ENGINE.time.delta) : + 0.0f + ); snprintf( buffer, sizeof(buffer), "%.2f/%.2f/%d/%d", - TIME.dynamicDelta * 1000.0f, - TIME.delta * 1000.0f, - TIME.dynamicUpdate, + Engine::ENGINE.time.dynamicDelta * 1000.0f, + Engine::ENGINE.time.delta * 1000.0f, + Engine::ENGINE.time.dynamicUpdate, (int32_t)fpsDynamic );