Time stuff

This commit is contained in:
2025-08-17 16:11:22 -05:00
parent 6d6a0e4886
commit 3d4317260f
20 changed files with 176 additions and 17 deletions

View File

@@ -20,6 +20,7 @@ target_sources(${DUSK_TARGET_NAME}
PRIVATE
game.c
input.c
time.c
)
# Subdirs

View File

@@ -14,12 +14,16 @@
#include "event/event.h"
#include "ui/uitextbox.h"
#include "console/console.h"
#include "util/memory.h"
#include "time.h"
game_t GAME;
void gameInit(void) {
memoryZero(&GAME, sizeof(game_t));
GAME.running = true;
timeInit();
consoleInit();
inputInit();
eventInit();
@@ -28,12 +32,15 @@ void gameInit(void) {
}
void gameUpdate(void) {
timeUpdate();
sceneUpdate();
uiTextboxUpdate();
eventUpdate();
consoleUpdate();
inputUpdate();
if(inputPressed(INPUT_BIND_QUIT)) consoleExec("quit");
}
void gameDispose(void) {

27
src/dusk/time.c Normal file
View File

@@ -0,0 +1,27 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "time.h"
#include "util/memory.h"
#include "assert/assert.h"
dusktime_t TIME;
void timeInit(void) {
memoryZero(&TIME, sizeof(TIME));
}
void timeUpdate(void) {
#if DUSK_DYNAMIC_TIME
TIME.delta = timeDeltaGet();
#else
TIME.delta = DUSK_TIME_STEP;
#endif
TIME.time += TIME.delta;
assertTrue(TIME.delta >= 0.0f, "Time delta is negative");
}

47
src/dusk/time.h Normal file
View File

@@ -0,0 +1,47 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
typedef struct {
float_t delta;
float_t time;
} dusktime_t;
extern dusktime_t TIME;
#ifndef DUSK_DYNAMIC_TIME
#define DUSK_DYNAMIC_TIME 1
#endif
#if DUSK_DYNAMIC_TIME == 0
#ifndef DUSK_TIME_STEP
#define DUSK_TIME_STEP (1.0f / 60.0f)
#endif
#endif
/**
* Initializes the time system.
*/
void timeInit(void);
/**
* Updates the time system
*/
void timeUpdate(void);
#if DUSK_DYNAMIC_TIME == 1
/**
* Gets the time delta since the last frame, in seconds. Tied to the
* platform.
*
* This will only get called once per gameUpdate.
*/
float_t timeDeltaGet(void);
#endif

View File

@@ -7,16 +7,17 @@
#pragma once
#include "tile.h"
#define CHUNK_MAP_WIDTH 4
#define CHUNK_MAP_HEIGHT 3
#define CHUNK_MAP_COUNT (CHUNK_MAP_WIDTH * CHUNK_MAP_HEIGHT)
#include "display/render.h"
#define CHUNK_WIDTH 8
#define CHUNK_HEIGHT 8
#define CHUNK_TILE_COUNT (CHUNK_WIDTH * CHUNK_HEIGHT)
#define CHUNK_ENTITY_COUNT_MAX 8
#define CHUNK_MAP_WIDTH (((RENDER_WIDTH / TILE_WIDTH_HEIGHT)/CHUNK_WIDTH)+2)
#define CHUNK_MAP_HEIGHT (((RENDER_HEIGHT / TILE_WIDTH_HEIGHT)/CHUNK_HEIGHT)+2)
#define CHUNK_MAP_COUNT (CHUNK_MAP_WIDTH * CHUNK_MAP_HEIGHT)
typedef struct {
uint16_t x, y;
tile_t tilesBase[CHUNK_TILE_COUNT];

View File

@@ -20,6 +20,7 @@ target_compile_definitions(${DUSK_TARGET_NAME}
RENDER_HEIGHT=272
RENDER_WINDOW_WIDTH_DEFAULT=480
RENDER_WINDOW_HEIGHT_DEFAULT=272
DUSK_DYNAMIC_TIME=0
)
# Includes

View File

@@ -32,6 +32,7 @@ target_sources(${DUSK_TARGET_NAME}
PRIVATE
dusksdl2input.c
main.c
time.c
)
# Subdirs

View File

@@ -8,8 +8,6 @@ target_sources(${DUSK_TARGET_NAME}
PRIVATE
render.c
renderbackbuffer.c
rendertext.c
renderconsole.c
)
# Subdirs
@@ -19,4 +17,5 @@ add_subdirectory(mesh)
add_subdirectory(overworld)
add_subdirectory(texture)
add_subdirectory(spritebatch)
add_subdirectory(scene)
add_subdirectory(scene)
add_subdirectory(ui)

View File

@@ -41,7 +41,6 @@ void renderOverworldDraw(void) {
for(uint8_t i = 0; i < CHUNK_MAP_COUNT; i++) {
renderchunk_t *chunk = &RENDER_OVERWORLD.chunks[i];
// meshDraw(&chunk->meshBase, 0, CHUNK_TILE_COUNT * QUAD_VERTEX_COUNT);
meshDraw(&chunk->meshBase, -1, -1);
}
@@ -53,8 +52,8 @@ void renderOverworldDraw(void) {
// Draw the entity
spriteBatchPush(
NULL,
entity->x, entity->y,
entity->x + TILE_WIDTH_HEIGHT, entity->y + TILE_WIDTH_HEIGHT,
floorf(entity->x), floorf(entity->y),
floorf(entity->x + TILE_WIDTH_HEIGHT), floorf(entity->y + TILE_WIDTH_HEIGHT),
0xFF, 0x00, 0xFF, 0XFF,
0.0f, 0.0f, 1.0f, 1.0f
);

View File

@@ -5,11 +5,13 @@
* https://opensource.org/licenses/MIT
*/
#include "dusksdl2input.h"
#include "render.h"
#include "assert/assert.h"
#include "renderbackbuffer.h"
#include "rendertext.h"
#include "renderconsole.h"
#include "display/ui/rendertext.h"
#include "display/ui/renderconsole.h"
#include "display/ui/renderfps.h"
#include "console/console.h"
#include "display/scene/renderscene.h"
#include "display/spritebatch/spritebatch.h"
@@ -23,6 +25,7 @@ errorret_t renderInit(void) {
// Init SDL
uint32_t flags = SDL_INIT_VIDEO;
#if INPUT_SUPPORT_GAMEPAD
printf("Gamepad support enabled.\n");
flags |= SDL_INIT_GAMECONTROLLER;
#endif
@@ -100,6 +103,7 @@ errorret_t renderDraw(void) {
// Draw UI
cameraUIPush();
renderConsoleDraw();
renderFPSDraw();
spriteBatchFlush();
cameraUIPop();

View File

@@ -0,0 +1,12 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_TARGET_NAME}
PRIVATE
renderconsole.c
renderfps.c
rendertext.c
)

View File

@@ -7,7 +7,7 @@
#include "renderconsole.h"
#include "console/console.h"
#include "rendertext.h"
#include "display/ui/rendertext.h"
void renderConsoleDraw(void) {
if(!CONSOLE.visible) return;

View File

@@ -0,0 +1,28 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "renderfps.h"
#include "display/ui/rendertext.h"
#include "time.h"
void renderFPSDraw(void) {
float_t fps = 1.0f / TIME.delta;
char_t buffer[32];
snprintf(buffer, sizeof(buffer), "FPS: %.1f", fps);
if(fps >= 50.0f) {
// Green
renderTextDraw(0, 0, buffer, 0x00, 0xFF, 0x00);
} else if(fps >= 30.0f) {
// Yellow
renderTextDraw(0, 0, buffer, 0xFF, 0xFF, 0x00);
} else {
// Red
renderTextDraw(0, 0, buffer, 0xFF, 0x00, 0x00);
}
}

View File

@@ -0,0 +1,13 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
/**
* Draws the FPS overlay.
*/
void renderFPSDraw(void);

View File

@@ -6,7 +6,7 @@
*/
#include "rendertext.h"
#include "render.h"
#include "display/render.h"
#include "assert/assert.h"
#include "display/spritebatch/spritebatch.h"
#include "util/memory.h"

View File

@@ -30,7 +30,7 @@
{ SDL_CONTROLLER_BUTTON_DPAD_RIGHT, INPUT_BIND_RIGHT },
{ SDL_CONTROLLER_BUTTON_A, INPUT_BIND_ACTION },
{ SDL_CONTROLLER_BUTTON_B, INPUT_BIND_CANCEL },
{ SDL_CONTROLLER_BUTTON_MISC1, INPUT_BIND_CONSOLE },
{ SDL_CONTROLLER_BUTTON_BACK, INPUT_BIND_CONSOLE },
{ 0, 0 }
};
#endif

View File

@@ -7,7 +7,6 @@
#include "display/render.h"
#include "game.h"
#include "console/console.h"
#include "input.h"
#define mainError(ret) \
@@ -26,7 +25,6 @@ int main(int argc, char *argv[]) {
gameUpdate();
mainError(renderDraw());
if(inputPressed(INPUT_BIND_QUIT)) consoleExec("quit");
if(!GAME.running) break;
}

21
src/dusksdl2/time.c Normal file
View File

@@ -0,0 +1,21 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "time.h"
#include "dusksdl2.h"
#if DUSK_DYNAMIC_TIME
uint32_t TIME_LAST = 0;
float_t timeDeltaGet(void) {
// Get the current time in milliseconds
uint32_t currentTime = SDL_GetTicks();
float_t delta = (currentTime - TIME_LAST) / 1000.0f;
TIME_LAST = currentTime;
return delta;
}
#endif