Time -> manager
Some checks failed
Build Dusk / build-linux (push) Failing after 1m14s
Build Dusk / build-psp (push) Failing after 1m21s

This commit is contained in:
2025-12-22 14:33:26 +10:00
parent 2e0f5f302b
commit ee3d27fda3
14 changed files with 135 additions and 117 deletions

View File

@@ -7,7 +7,6 @@
#include "Engine.hpp" #include "Engine.hpp"
#include "util/memory.hpp" #include "util/memory.hpp"
#include "time/time.hpp"
#include "input/input.hpp" #include "input/input.hpp"
#include "locale/localemanager.hpp" #include "locale/localemanager.hpp"
#include "display/display.hpp" #include "display/display.hpp"
@@ -26,7 +25,8 @@ Engine Engine::ENGINE;
Engine::Engine() : Engine::Engine() :
running(false), running(false),
argc(0), 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; this->argv = argv;
// Init systems. Order is important. // Init systems. Order is important.
timeInit();
inputInit(); inputInit();
assetInit(); assetInit();
localeManagerInit(); localeManagerInit();
@@ -54,7 +53,7 @@ void Engine::init(const int32_t argc, const char_t **argv) {
} }
void Engine::update() { void Engine::update() {
timeUpdate(); this->time.update();
inputUpdate(); inputUpdate();
rpgUpdate(); rpgUpdate();

View File

@@ -8,6 +8,8 @@
#pragma once #pragma once
#include "display/display.hpp"// Important to be included first. #include "display/display.hpp"// Important to be included first.
#include "time/TimeManager.hpp"
namespace Dusk { namespace Dusk {
struct Engine { struct Engine {
private: private:
@@ -15,9 +17,12 @@ namespace Dusk {
public: public:
static Engine ENGINE; static Engine ENGINE;
int32_t argc; int32_t argc;
const char_t **argv; const char_t **argv;
TimeManager time;
/** /**
* Initializes the engine. * Initializes the engine.
*/ */

View File

@@ -10,7 +10,9 @@
#include "util/memory.hpp" #include "util/memory.hpp"
#include "util/string.hpp" #include "util/string.hpp"
#include "util/math.hpp" #include "util/math.hpp"
#include "time/time.hpp" #include "engine/Engine.hpp"
using namespace Dusk;
input_t INPUT; input_t INPUT;
@@ -86,7 +88,7 @@ void inputUpdate(void) {
#if TIME_FIXED == 0 #if TIME_FIXED == 0
action->lastDynamicValue = action->currentDynamicValue; action->lastDynamicValue = action->currentDynamicValue;
action->currentDynamicValue = 0.0f; action->currentDynamicValue = 0.0f;
if(!TIME.dynamicUpdate) { if(!Engine::ENGINE.time.dynamicUpdate) {
action->lastValue = action->currentValue; action->lastValue = action->currentValue;
action->currentValue = 0.0f; action->currentValue = 0.0f;
} }
@@ -114,7 +116,7 @@ void inputUpdate(void) {
INPUT.actions[cur->action].currentDynamicValue = mathMax( INPUT.actions[cur->action].currentDynamicValue = mathMax(
cur->curVal, INPUT.actions[cur->action].currentDynamicValue cur->curVal, INPUT.actions[cur->action].currentDynamicValue
); );
if(!TIME.dynamicUpdate) { if(!Engine::ENGINE.time.dynamicUpdate) {
INPUT.actions[cur->action].currentValue = mathMax( INPUT.actions[cur->action].currentValue = mathMax(
cur->curVal, INPUT.actions[cur->action].currentValue cur->curVal, INPUT.actions[cur->action].currentValue
); );
@@ -131,7 +133,9 @@ void inputUpdate(void) {
float_t inputGetCurrentValue(const inputaction_t action) { float_t inputGetCurrentValue(const inputaction_t action) {
#if TIME_FIXED == 0 #if TIME_FIXED == 0
if(TIME.dynamicUpdate) return inputGetCurrentValueDynamic(action); if(Engine::ENGINE.time.dynamicUpdate) {
return inputGetCurrentValueDynamic(action);
}
#endif #endif
assertTrue(action < INPUT_ACTION_COUNT, "Input action out of bounds"); 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) { float_t inputGetLastValue(const inputaction_t action) {
#if TIME_FIXED == 0 #if TIME_FIXED == 0
if(TIME.dynamicUpdate) return inputGetLastValueDynamic(action); if(Engine::ENGINE.time.dynamicUpdate) {
return inputGetLastValueDynamic(action);
}
#endif #endif
assertTrue(action < INPUT_ACTION_COUNT, "Input action out of bounds"); assertTrue(action < INPUT_ACTION_COUNT, "Input action out of bounds");

View File

@@ -6,7 +6,7 @@
*/ */
#pragma once #pragma once
#include "time/time.hpp" #include "time/TimeManager.hpp"
typedef uint8_t inputaction_t; typedef uint8_t inputaction_t;

View File

@@ -7,7 +7,9 @@
#include "rpg/cutscene/cutscenesystem.hpp" #include "rpg/cutscene/cutscenesystem.hpp"
#include "input/input.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) { void cutsceneItemStart(const cutsceneitem_t *item, cutsceneitemdata_t *data) {
switch(item->type) { switch(item->type) {
@@ -44,7 +46,7 @@ void cutsceneItemUpdate(const cutsceneitem_t *item, cutsceneitemdata_t *data) {
break; break;
case CUTSCENE_ITEM_TYPE_WAIT: case CUTSCENE_ITEM_TYPE_WAIT:
data->wait -= TIME.delta; data->wait -= Engine::ENGINE.time.delta;
if(data->wait <= 0) cutsceneSystemNext(); if(data->wait <= 0) cutsceneSystemNext();
break; break;

View File

@@ -8,10 +8,12 @@
#include "entity.hpp" #include "entity.hpp"
#include "assert/assert.hpp" #include "assert/assert.hpp"
#include "util/memory.hpp" #include "util/memory.hpp"
#include "time/time.hpp"
#include "util/math.hpp" #include "util/math.hpp"
#include "rpg/cutscene/cutscenemode.hpp" #include "rpg/cutscene/cutscenemode.hpp"
#include "rpg/world/map.hpp" #include "rpg/world/map.hpp"
#include "engine/Engine.hpp"
using namespace Dusk;
entity_t ENTITIES[ENTITY_COUNT]; entity_t ENTITIES[ENTITY_COUNT];
@@ -39,7 +41,7 @@ void entityUpdate(entity_t *entity) {
// What state is the entity in? // What state is the entity in?
if(entity->animation != ENTITY_ANIM_IDLE) { if(entity->animation != ENTITY_ANIM_IDLE) {
// Entity is mid animation, tick it (down). // Entity is mid animation, tick it (down).
entity->animTime -= TIME.delta; entity->animTime -= Engine::ENGINE.time.delta;
if(entity->animTime <= 0) { if(entity->animTime <= 0) {
entity->animation = ENTITY_ANIM_IDLE; entity->animation = ENTITY_ANIM_IDLE;
entity->animTime = 0; entity->animTime = 0;

View File

@@ -9,7 +9,6 @@
#include "assert/assert.hpp" #include "assert/assert.hpp"
#include "rpg/rpgcamera.hpp" #include "rpg/rpgcamera.hpp"
#include "util/memory.hpp" #include "util/memory.hpp"
#include "time/time.hpp"
void playerInit(entity_t *entity) { void playerInit(entity_t *entity) {
assertNotNull(entity, "Entity pointer cannot be NULL"); assertNotNull(entity, "Entity pointer cannot be NULL");

View File

@@ -9,11 +9,13 @@
#include "entity/entity.hpp" #include "entity/entity.hpp"
#include "rpg/world/map.hpp" #include "rpg/world/map.hpp"
#include "rpg/cutscene/cutscenesystem.hpp" #include "rpg/cutscene/cutscenesystem.hpp"
#include "time/time.hpp"
#include "rpgcamera.hpp" #include "rpgcamera.hpp"
#include "rpgtextbox.hpp" #include "rpgtextbox.hpp"
#include "util/memory.hpp" #include "util/memory.hpp"
#include "assert/assert.hpp" #include "assert/assert.hpp"
#include "engine/Engine.hpp"
using namespace Dusk;
void rpgInit(void) { void rpgInit(void) {
memoryZero(ENTITIES, sizeof(ENTITIES)); memoryZero(ENTITIES, sizeof(ENTITIES));
@@ -38,7 +40,7 @@ void rpgInit(void) {
void rpgUpdate(void) { void rpgUpdate(void) {
#if TIME_FIXED == 0 #if TIME_FIXED == 0
if(TIME.dynamicUpdate) return; if(Engine::ENGINE.time.dynamicUpdate) return;
#endif #endif
// TODO: Do not update if the scene is not the map scene? // TODO: Do not update if the scene is not the map scene?

View File

@@ -6,7 +6,7 @@
# Sources # Sources
target_sources(${DUSK_TARGET_NAME} target_sources(${DUSK_TARGET_NAME}
PRIVATE PRIVATE
time.cpp TimeManager.cpp
) )
# Compiler defs # Compiler defs

49
src/time/TimeManager.cpp Normal file
View File

@@ -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 <SDL2/SDL.h>
#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
}

38
src/time/TimeManager.hpp Normal file
View File

@@ -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();
};
}

View File

@@ -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 <SDL2/SDL.h>
#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
}

View File

@@ -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);

View File

@@ -6,13 +6,14 @@
*/ */
#include "uidebug.hpp" #include "uidebug.hpp"
#include "time/time.hpp"
#include "util/string.hpp" #include "util/string.hpp"
#include "ui/uitext.hpp" #include "ui/uitext.hpp"
#include "display/screen.hpp" #include "display/screen.hpp"
#include "display/spritebatch.hpp" #include "display/spritebatch.hpp"
#include "rpg/entity/entity.hpp" #include "rpg/entity/entity.hpp"
#include "engine/Engine.hpp"
using namespace Dusk;
bool_t UI_DEBUG_DRAW = true; bool_t UI_DEBUG_DRAW = true;
void uiDebugRender(const tileset_t *tileset, texture_t *texture) { void uiDebugRender(const tileset_t *tileset, texture_t *texture) {
@@ -24,15 +25,23 @@ void uiDebugRender(const tileset_t *tileset, texture_t *texture) {
// FPS Meter // FPS Meter
#if TIME_FIXED == 0 #if TIME_FIXED == 0
float_t fpsDynamic = TIME.dynamicDelta > 0.0f ? (1.0f / TIME.dynamicDelta) : 0.0f; float_t fpsDynamic = (
float_t fpsFixed = TIME.delta > 0.0f ? (1.0f / TIME.delta) : 0.0f; 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( snprintf(
buffer, buffer,
sizeof(buffer), sizeof(buffer),
"%.2f/%.2f/%d/%d", "%.2f/%.2f/%d/%d",
TIME.dynamicDelta * 1000.0f, Engine::ENGINE.time.dynamicDelta * 1000.0f,
TIME.delta * 1000.0f, Engine::ENGINE.time.delta * 1000.0f,
TIME.dynamicUpdate, Engine::ENGINE.time.dynamicUpdate,
(int32_t)fpsDynamic (int32_t)fpsDynamic
); );