Time -> manager
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "time/time.hpp"
|
||||
#include "time/TimeManager.hpp"
|
||||
|
||||
typedef uint8_t inputaction_t;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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?
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
# Sources
|
||||
target_sources(${DUSK_TARGET_NAME}
|
||||
PRIVATE
|
||||
time.cpp
|
||||
TimeManager.cpp
|
||||
)
|
||||
|
||||
# Compiler defs
|
||||
|
||||
49
src/time/TimeManager.cpp
Normal file
49
src/time/TimeManager.cpp
Normal 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
38
src/time/TimeManager.hpp
Normal 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();
|
||||
};
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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);
|
||||
@@ -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
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user