From c3310a036fbed2facbb052009e52e08bbeabadf3 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sat, 16 Aug 2025 14:52:52 -0500 Subject: [PATCH] Nothing really changed --- src/dusk/console/console.c | 9 ++++ src/dusk/display/scene.c | 43 +++++++++++++++++- src/dusk/display/scene.h | 34 +++++++++++++- src/dusk/game.c | 8 ++-- src/dusk/input.c | 8 ++-- src/dusk/input.h | 17 ++++--- src/dusksdl2/display/CMakeLists.txt | 4 +- src/dusksdl2/display/render.c | 7 ++- src/dusksdl2/display/renderconsole.c | 4 +- src/dusksdl2/display/renderoverworld.h | 8 ++++ src/dusksdl2/display/renderscene.c | 20 --------- src/dusksdl2/display/scene/CMakeLists.txt | 13 ++++++ src/dusksdl2/display/scene/renderscene.c | 44 +++++++++++++++++++ .../display/{ => scene}/renderscene.h | 10 ++++- src/dusksdl2/dusksdl2input.c | 4 +- src/dusksdl2/dusksdl2input.h | 6 ++- 16 files changed, 188 insertions(+), 51 deletions(-) create mode 100644 src/dusksdl2/display/renderoverworld.h delete mode 100644 src/dusksdl2/display/renderscene.c create mode 100644 src/dusksdl2/display/scene/CMakeLists.txt create mode 100644 src/dusksdl2/display/scene/renderscene.c rename src/dusksdl2/display/{ => scene}/renderscene.h (64%) diff --git a/src/dusk/console/console.c b/src/dusk/console/console.c index 47eae34..defbe55 100644 --- a/src/dusk/console/console.c +++ b/src/dusk/console/console.c @@ -311,6 +311,15 @@ void cmdEcho(const consolecmdexec_t *exec) { // May move these later void consoleUpdate() { + if(inputPressed(INPUT_BIND_CONSOLE)) { + CONSOLE.visible = !CONSOLE.visible; + if(CONSOLE.visible) { + consolePrint("Console opened."); + } else { + consolePrint("Console closed."); + } + } + for(uint32_t i = 0; i < CONSOLE.execBufferCount; i++) { consolecmdexec_t *exec = &CONSOLE.execBuffer[i]; assertNotNull(exec->cmd, "Command execution has no command."); diff --git a/src/dusk/display/scene.c b/src/dusk/display/scene.c index e8e5023..b748374 100644 --- a/src/dusk/display/scene.c +++ b/src/dusk/display/scene.c @@ -6,5 +6,46 @@ */ #include "scene.h" +#include "world/overworld.h" -scene_t SCENE_CURRENT = SCENE_INITIAL; \ No newline at end of file +scene_t SCENE_CURRENT; +scenecallback_t SCENE_CALLBACKS[SCENE_COUNT] = { + [SCENE_INITIAL] = { + .init = NULL, + .update = NULL + }, + + [SCENE_OVERWORLD] = { + .init = overworldInit, + .update = overworldUpdate, + .dispose = NULL + } +}; + +void sceneInit(void) { + for(uint8_t i = 0; i < SCENE_COUNT; i++) { + if(SCENE_CALLBACKS[i].init) { + SCENE_CALLBACKS[i].init(); + } + } + + SCENE_CURRENT = SCENE_INITIAL; +} + +void sceneSet(const scene_t scene) { + SCENE_CURRENT = scene; +} + +void sceneUpdate(void) { + if(SCENE_CALLBACKS[SCENE_CURRENT].update) { + SCENE_CALLBACKS[SCENE_CURRENT].update(); + } +} + +void sceneDispose(void) { + for(uint8_t i = 0; i < SCENE_COUNT; i++) { + if(SCENE_CALLBACKS[i].dispose) { + SCENE_CALLBACKS[i].dispose(); + } + } +} \ No newline at end of file diff --git a/src/dusk/display/scene.h b/src/dusk/display/scene.h index 6f4a28e..1529f00 100644 --- a/src/dusk/display/scene.h +++ b/src/dusk/display/scene.h @@ -6,10 +6,42 @@ */ #pragma once +#include "dusk.h" typedef enum { SCENE_INITIAL, SCENE_OVERWORLD, + + SCENE_COUNT } scene_t; -extern scene_t SCENE_CURRENT; \ No newline at end of file +typedef struct { + void (*init)(void); + void (*update)(void); + void (*dispose)(void); +} scenecallback_t; + +extern scene_t SCENE_CURRENT; +extern scenecallback_t SCENE_CALLBACKS[SCENE_COUNT]; + +/** + * Initializes the scene module. + */ +void sceneInit(void); + +/** + * Sets the current scene. + * + * @param scene The scene to set. + */ +void sceneSet(const scene_t scene); + +/** + * Updates the current scene. + */ +void sceneUpdate(void); + +/** + * Disposes of the current scene. + */ +void sceneDispose(void); \ No newline at end of file diff --git a/src/dusk/game.c b/src/dusk/game.c index d02d7df..6f5849c 100644 --- a/src/dusk/game.c +++ b/src/dusk/game.c @@ -20,13 +20,11 @@ void gameInit(void) { inputInit(); eventInit(); uiTextboxInit(); - overworldInit(); - - SCENE_CURRENT = SCENE_OVERWORLD; + sceneInit(); } void gameUpdate(void) { - overworldUpdate(); + sceneUpdate(); uiTextboxUpdate(); eventUpdate(); @@ -35,5 +33,5 @@ void gameUpdate(void) { } void gameDispose(void) { - + sceneDispose(); } \ No newline at end of file diff --git a/src/dusk/input.c b/src/dusk/input.c index f4654e8..a2a5849 100644 --- a/src/dusk/input.c +++ b/src/dusk/input.c @@ -20,20 +20,20 @@ void inputUpdate(void) { INPUT.current = inputStateGet(); } -bool_t inputIsDown(const uint8_t bind) { +bool_t inputIsDown(const inputbind_t bind) { assertTrue(bind < INPUT_BIND_COUNT, "Input bind out of bounds"); return (INPUT.current & bind) != 0; } -bool_t inputWasDown(const uint8_t bind) { +bool_t inputWasDown(const inputbind_t bind) { assertTrue(bind < INPUT_BIND_COUNT, "Input bind out of bounds"); return (INPUT.previous & bind) != 0; } -bool_t inputPressed(const uint8_t bind) { +bool_t inputPressed(const inputbind_t bind) { return inputIsDown(bind) && !inputWasDown(bind); } -bool_t inputReleased(const uint8_t bind) { +bool_t inputReleased(const inputbind_t bind) { return !inputIsDown(bind) && inputWasDown(bind); } \ No newline at end of file diff --git a/src/dusk/input.h b/src/dusk/input.h index 0fc2bc0..f9d5a3e 100644 --- a/src/dusk/input.h +++ b/src/dusk/input.h @@ -8,13 +8,18 @@ #pragma once #include "dusk.h" +typedef uint8_t inputbind_t; +typedef inputbind_t inputstate_t; + #define INPUT_BIND_UP (1 << 0) #define INPUT_BIND_DOWN (1 << 1) #define INPUT_BIND_LEFT (1 << 2) #define INPUT_BIND_RIGHT (1 << 3) #define INPUT_BIND_ACTION (1 << 4) #define INPUT_BIND_CANCEL (1 << 5) -#define INPUT_BIND_COUNT (INPUT_BIND_CANCEL + 1) +#define INPUT_BIND_CONSOLE (1 << 6) + +#define INPUT_BIND_COUNT (INPUT_BIND_CONSOLE + 1) typedef struct { uint8_t current; @@ -38,7 +43,7 @@ void inputUpdate(void); * * @return The current input state as a bitmask. */ -uint8_t inputStateGet(void); +inputstate_t inputStateGet(void); /** * Checks if a specific input bind is currently pressed. @@ -46,7 +51,7 @@ uint8_t inputStateGet(void); * @param bind The input bind to check. * @return true if the bind is currently pressed, false otherwise. */ -bool_t inputIsDown(const uint8_t bind); +bool_t inputIsDown(const inputbind_t bind); /** * Checks if a specific input bind was pressed in the last update. @@ -54,7 +59,7 @@ bool_t inputIsDown(const uint8_t bind); * @param bind The input bind to check. * @return true if the bind was pressed in the last update, false otherwise. */ -bool_t inputWasDown(const uint8_t bind); +bool_t inputWasDown(const inputbind_t bind); /** * Checks if a specific input bind was down this frame but not in the the @@ -63,7 +68,7 @@ bool_t inputWasDown(const uint8_t bind); * @param bind The input bind to check. * @return true if the bind is currently pressed, false otherwise. */ -bool_t inputPressed(const uint8_t bind); +bool_t inputPressed(const inputbind_t bind); /** * Checks if a specific input bind was released this frame. @@ -71,4 +76,4 @@ bool_t inputPressed(const uint8_t bind); * @param bind The input bind to check. * @return true if the bind was released this frame, false otherwise. */ -bool_t inputReleased(const uint8_t bind); \ No newline at end of file +bool_t inputReleased(const inputbind_t bind); \ No newline at end of file diff --git a/src/dusksdl2/display/CMakeLists.txt b/src/dusksdl2/display/CMakeLists.txt index 2859449..4101afc 100644 --- a/src/dusksdl2/display/CMakeLists.txt +++ b/src/dusksdl2/display/CMakeLists.txt @@ -10,7 +10,6 @@ target_sources(${DUSK_TARGET_NAME} renderbackbuffer.c rendertext.c renderconsole.c - renderscene.c ) # Subdirs @@ -18,4 +17,5 @@ add_subdirectory(camera) add_subdirectory(framebuffer) add_subdirectory(mesh) add_subdirectory(texture) -add_subdirectory(spritebatch) \ No newline at end of file +add_subdirectory(spritebatch) +add_subdirectory(scene) \ No newline at end of file diff --git a/src/dusksdl2/display/render.c b/src/dusksdl2/display/render.c index f5f8587..ada3fbc 100644 --- a/src/dusksdl2/display/render.c +++ b/src/dusksdl2/display/render.c @@ -11,8 +11,7 @@ #include "rendertext.h" #include "renderconsole.h" #include "console/console.h" -#include "dusksdl2input.h" -#include "renderscene.h" +#include "display/scene/renderscene.h" #include "display/spritebatch/spritebatch.h" #include "display/camera/camera.h" @@ -95,10 +94,10 @@ errorret_t renderDraw(void) { spriteBatchClear(); renderBackBufferBind(); - // Draw everything + // Draw the scene renderSceneDraw(); - // UI + // Draw UI cameraUIPush(); renderConsoleDraw(); spriteBatchFlush(); diff --git a/src/dusksdl2/display/renderconsole.c b/src/dusksdl2/display/renderconsole.c index 77e4136..6b4df19 100644 --- a/src/dusksdl2/display/renderconsole.c +++ b/src/dusksdl2/display/renderconsole.c @@ -10,9 +10,7 @@ #include "rendertext.h" void renderConsoleDraw(void) { - // if(!CONSOLE.visible) return; - - // renderTextDraw(0, 0, "Dusk Console"); + if(!CONSOLE.visible) return; int32_t i = 0; char_t *line; diff --git a/src/dusksdl2/display/renderoverworld.h b/src/dusksdl2/display/renderoverworld.h new file mode 100644 index 0000000..f0a9ce0 --- /dev/null +++ b/src/dusksdl2/display/renderoverworld.h @@ -0,0 +1,8 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once \ No newline at end of file diff --git a/src/dusksdl2/display/renderscene.c b/src/dusksdl2/display/renderscene.c deleted file mode 100644 index 87c8633..0000000 --- a/src/dusksdl2/display/renderscene.c +++ /dev/null @@ -1,20 +0,0 @@ -/** - * Copyright (c) 2025 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#include "renderscene.h" - -void renderSceneInit(void) { - // Initialize scene-related resources here -} - -void renderSceneDraw(void) { - // Draw the current scene here -} - -void renderSceneDispose(void) { - // Dispose of scene-related resources here -} \ No newline at end of file diff --git a/src/dusksdl2/display/scene/CMakeLists.txt b/src/dusksdl2/display/scene/CMakeLists.txt new file mode 100644 index 0000000..43eb585 --- /dev/null +++ b/src/dusksdl2/display/scene/CMakeLists.txt @@ -0,0 +1,13 @@ +# 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 + renderscene.c +) + +# Subdirs +# add_subdirectory(draw) \ No newline at end of file diff --git a/src/dusksdl2/display/scene/renderscene.c b/src/dusksdl2/display/scene/renderscene.c new file mode 100644 index 0000000..cdc0ec2 --- /dev/null +++ b/src/dusksdl2/display/scene/renderscene.c @@ -0,0 +1,44 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "renderscene.h" + +renderscenecallback_t RENDER_SCENE_CALLBACKS[SCENE_COUNT] = { + [SCENE_INITIAL] = { + .init = NULL, + .draw = NULL, + .dispose = NULL + }, + + [SCENE_OVERWORLD] = { + .init = NULL, + .draw = NULL, + .dispose = NULL + }, +}; + +void renderSceneInit(void) { + for (int i = 0; i < SCENE_COUNT; i++) { + if (RENDER_SCENE_CALLBACKS[i].init) { + RENDER_SCENE_CALLBACKS[i].init(); + } + } +} + +void renderSceneDraw(void) { + if(RENDER_SCENE_CALLBACKS[SCENE_CURRENT].draw) { + RENDER_SCENE_CALLBACKS[SCENE_CURRENT].draw(); + } +} + +void renderSceneDispose(void) { + for (int i = 0; i < SCENE_COUNT; i++) { + if (RENDER_SCENE_CALLBACKS[i].dispose) { + RENDER_SCENE_CALLBACKS[i].dispose(); + } + } +} \ No newline at end of file diff --git a/src/dusksdl2/display/renderscene.h b/src/dusksdl2/display/scene/renderscene.h similarity index 64% rename from src/dusksdl2/display/renderscene.h rename to src/dusksdl2/display/scene/renderscene.h index 541a148..c21665f 100644 --- a/src/dusksdl2/display/renderscene.h +++ b/src/dusksdl2/display/scene/renderscene.h @@ -6,7 +6,15 @@ */ #pragma once -#include "dusk.h" +#include "display/scene.h" + +typedef struct { + void (*init)(void); + void (*draw)(void); + void (*dispose)(void); +} renderscenecallback_t; + +extern renderscenecallback_t RENDER_SCENE_CALLBACKS[SCENE_COUNT]; /** * Initializes the render scene module. diff --git a/src/dusksdl2/dusksdl2input.c b/src/dusksdl2/dusksdl2input.c index 9c22fc5..58d76ca 100644 --- a/src/dusksdl2/dusksdl2input.c +++ b/src/dusksdl2/dusksdl2input.c @@ -7,8 +7,8 @@ #include "dusksdl2input.h" -uint8_t inputStateGet() { - uint8_t state = 0; +inputstate_t inputStateGet() { + inputstate_t state = 0; #if INPUT_SUPPORT_GAMEPAD // Get gamepad state. diff --git a/src/dusksdl2/dusksdl2input.h b/src/dusksdl2/dusksdl2input.h index 4cd6e72..51b3fb0 100644 --- a/src/dusksdl2/dusksdl2input.h +++ b/src/dusksdl2/dusksdl2input.h @@ -20,7 +20,7 @@ #if INPUT_SUPPORT_GAMEPAD typedef struct { const SDL_GameControllerButton button; - const uint8_t bind; + const inputbind_t bind; } inputsdlbuttonmap_t; static const inputsdlbuttonmap_t INPUT_SDL_BUTTON_MAP[] = { @@ -30,6 +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 }, { 0, 0 } }; #endif @@ -37,7 +38,7 @@ #if INPUT_SUPPORT_KEYBOARD typedef struct { SDL_Scancode code; - uint8_t bind; + inputbind_t bind; } inputsdlkbmap_t; static const inputsdlkbmap_t INPUT_SDL_KEYBOARD_MAP[] = { @@ -47,6 +48,7 @@ { SDL_SCANCODE_D, INPUT_BIND_RIGHT }, { SDL_SCANCODE_SPACE, INPUT_BIND_ACTION }, { SDL_SCANCODE_ESCAPE, INPUT_BIND_CANCEL }, + { SDL_SCANCODE_GRAVE, INPUT_BIND_CONSOLE }, { 0, 0 } }; #endif \ No newline at end of file