From 1b6db0c6434570084812e87172f549d63bf74d2b Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Tue, 10 Jun 2025 17:09:33 -0500 Subject: [PATCH] Render test --- CMakeLists.txt | 3 - src/CMakeLists.txt | 28 +---- src/display/color.h | 22 ---- src/display/render.c | 71 ------------ src/display/term.c | 59 ---------- src/display/term.h | 48 -------- src/dusk/CMakeLists.txt | 29 +++++ src/{ => dusk}/assert/CMakeLists.txt | 0 src/{ => dusk}/assert/assert.c | 0 src/{ => dusk}/assert/assert.h | 0 {lib => src/dusk/display}/CMakeLists.txt | 5 +- src/{ => dusk}/display/render.h | 10 +- src/{ => dusk}/dusk.h | 0 src/dusk/input.c | 29 +++++ src/{ => dusk}/input.h | 15 +-- src/{ => dusk}/main.c | 9 +- src/{ => dusk}/rpg/CMakeLists.txt | 0 src/{ => dusk}/rpg/entity/CMakeLists.txt | 0 src/{ => dusk}/rpg/entity/entity.c | 0 src/{ => dusk}/rpg/entity/entity.h | 6 +- src/{ => dusk}/rpg/entity/player.c | 0 src/{ => dusk}/rpg/entity/player.h | 0 src/{ => dusk}/rpg/event/event.h | 0 src/{ => dusk}/rpg/item/CMakeLists.txt | 0 src/{ => dusk}/rpg/item/inventory.c | 0 src/{ => dusk}/rpg/item/inventory.h | 0 src/{ => dusk}/rpg/item/itemstack.h | 0 src/{ => dusk}/rpg/item/itemtype.c | 0 src/{ => dusk}/rpg/item/itemtype.h | 0 src/{ => dusk}/rpg/quest/CMakeLists.txt | 0 src/{ => dusk}/rpg/quest/quest.c | 0 src/{ => dusk}/rpg/quest/quest.h | 0 src/{ => dusk}/rpg/world/CMakeLists.txt | 0 src/{ => dusk}/rpg/world/map.c | 0 src/{ => dusk}/rpg/world/map.h | 0 src/{ => dusk}/util/CMakeLists.txt | 0 src/{ => dusk}/util/memory.c | 0 src/{ => dusk}/util/memory.h | 0 src/{ => dusk}/util/random.c | 0 src/{ => dusk}/util/random.h | 0 src/{ => dusk}/util/string.c | 0 src/{ => dusk}/util/string.h | 0 src/duskraylib/CMakeLists.txt | 34 ++++++ src/{ => duskraylib}/display/CMakeLists.txt | 1 - src/duskraylib/display/render.c | 53 +++++++++ src/duskraylib/input.c | 47 ++++++++ src/input.c | 120 -------------------- 47 files changed, 219 insertions(+), 370 deletions(-) delete mode 100644 src/display/color.h delete mode 100644 src/display/render.c delete mode 100644 src/display/term.c delete mode 100644 src/display/term.h create mode 100644 src/dusk/CMakeLists.txt rename src/{ => dusk}/assert/CMakeLists.txt (100%) rename src/{ => dusk}/assert/assert.c (100%) rename src/{ => dusk}/assert/assert.h (100%) rename {lib => src/dusk/display}/CMakeLists.txt (69%) rename src/{ => dusk}/display/render.h (62%) rename src/{ => dusk}/dusk.h (100%) create mode 100644 src/dusk/input.c rename src/{ => dusk}/input.h (91%) rename src/{ => dusk}/main.c (75%) rename src/{ => dusk}/rpg/CMakeLists.txt (100%) rename src/{ => dusk}/rpg/entity/CMakeLists.txt (100%) rename src/{ => dusk}/rpg/entity/entity.c (100%) rename src/{ => dusk}/rpg/entity/entity.h (95%) rename src/{ => dusk}/rpg/entity/player.c (100%) rename src/{ => dusk}/rpg/entity/player.h (100%) rename src/{ => dusk}/rpg/event/event.h (100%) rename src/{ => dusk}/rpg/item/CMakeLists.txt (100%) rename src/{ => dusk}/rpg/item/inventory.c (100%) rename src/{ => dusk}/rpg/item/inventory.h (100%) rename src/{ => dusk}/rpg/item/itemstack.h (100%) rename src/{ => dusk}/rpg/item/itemtype.c (100%) rename src/{ => dusk}/rpg/item/itemtype.h (100%) rename src/{ => dusk}/rpg/quest/CMakeLists.txt (100%) rename src/{ => dusk}/rpg/quest/quest.c (100%) rename src/{ => dusk}/rpg/quest/quest.h (100%) rename src/{ => dusk}/rpg/world/CMakeLists.txt (100%) rename src/{ => dusk}/rpg/world/map.c (100%) rename src/{ => dusk}/rpg/world/map.h (100%) rename src/{ => dusk}/util/CMakeLists.txt (100%) rename src/{ => dusk}/util/memory.c (100%) rename src/{ => dusk}/util/memory.h (100%) rename src/{ => dusk}/util/random.c (100%) rename src/{ => dusk}/util/random.h (100%) rename src/{ => dusk}/util/string.c (100%) rename src/{ => dusk}/util/string.h (100%) create mode 100644 src/duskraylib/CMakeLists.txt rename src/{ => duskraylib}/display/CMakeLists.txt (94%) create mode 100644 src/duskraylib/display/render.c create mode 100644 src/duskraylib/input.c delete mode 100644 src/input.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ce5e57..eaaafb5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,9 +39,6 @@ project(${DUSK_TARGET_NAME} # Executable add_executable(${DUSK_TARGET_NAME}) -# Get Libraries -add_subdirectory(lib) - # Add tools add_subdirectory(tools) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b01cd93..fe911e6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,29 +1,7 @@ # Copyright (c) 2025 Dominic Masters -# +# # This software is released under the MIT License. # https://opensource.org/licenses/MIT -# Libs -target_link_libraries(${DUSK_TARGET_NAME} - PUBLIC - m -) - -# Includes -target_include_directories(${DUSK_TARGET_NAME} - PUBLIC - ${CMAKE_CURRENT_LIST_DIR} -) - -# Sources -target_sources(${DUSK_TARGET_NAME} - PRIVATE - main.c - input.c -) - -# Subdirs -add_subdirectory(assert) -add_subdirectory(display) -add_subdirectory(rpg) -add_subdirectory(util) \ No newline at end of file +add_subdirectory(dusk) +add_subdirectory(duskraylib) \ No newline at end of file diff --git a/src/display/color.h b/src/display/color.h deleted file mode 100644 index 3bc7adf..0000000 --- a/src/display/color.h +++ /dev/null @@ -1,22 +0,0 @@ -/** - * Copyright (c) 2025 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#pragma once -#include "dusk.h" - -typedef enum { - COLOR_BLACK, - COLOR_RED, - COLOR_GREEN, - COLOR_YELLOW, - COLOR_BLUE, - COLOR_MAGENTA, - COLOR_CYAN, - COLOR_WHITE, -} color_t; - -#define COLOR_COUNT (COLOR_WHITE + 1) \ No newline at end of file diff --git a/src/display/render.c b/src/display/render.c deleted file mode 100644 index 7f2a2f1..0000000 --- a/src/display/render.c +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Copyright (c) 2025 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#include "assert/assert.h" -#include "render.h" -#include "term.h" -#include "rpg/entity/entity.h" - -void renderInit() { - termInit(); -} - -void renderUpdate() { - char_t c; - color_t color, colorCurrent; - entity_t *ent; - - termUpdate(); - termClear(); - - colorCurrent = COLOR_WHITE; - termPushColor(colorCurrent); - - for(int32_t y = 0; y < TERM.height; y++) { - for(int32_t x = 0; x < TERM.width; x++) { - color = COLOR_WHITE; - c = ' '; - - ent = entityGetAt(x, y); - if(ent) { - color = COLOR_RED; - - switch(ent->dir) { - case ENTITY_DIR_UP: - c = '^'; - color = COLOR_GREEN; - break; - case ENTITY_DIR_DOWN: - c = 'v'; - color = COLOR_RED; - break; - case ENTITY_DIR_LEFT: - c = '<'; - color = COLOR_YELLOW; - break; - case ENTITY_DIR_RIGHT: - c = '>'; - color = COLOR_BLUE; - break; - default: - assertUnreachable("Invalid entity direction"); - break; - } - } - - if(c == ' ') { - termPushChar(' '); - continue; - } - - if(color != colorCurrent) termPushColor((colorCurrent = color)); - termPushChar(c); - } - } - - termFlush(); -} \ No newline at end of file diff --git a/src/display/term.c b/src/display/term.c deleted file mode 100644 index f79192d..0000000 --- a/src/display/term.c +++ /dev/null @@ -1,59 +0,0 @@ -/** - * Copyright (c) 2025 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#include "assert/assert.h" -#include "term.h" -#include "util/memory.h" -#include - -term_t TERM; - -const char_t* TERM_COLORS[COLOR_COUNT] = { - [COLOR_BLACK] = "\033[30m", - [COLOR_RED] = "\033[31m", - [COLOR_GREEN] = "\033[32m", - [COLOR_YELLOW] = "\033[33m", - [COLOR_BLUE] = "\033[34m", - [COLOR_MAGENTA] = "\033[35m", - [COLOR_CYAN] = "\033[36m", - [COLOR_WHITE] = "\033[37m" -}; - -void termInit() { - memoryZero(&TERM, sizeof(term_t)); - - struct winsize w; - if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0) { - TERM.width = w.ws_col; - TERM.height = w.ws_row; - } -} - -void termUpdate() { - struct winsize w; - if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0) { - TERM.width = w.ws_col; - TERM.height = w.ws_row; - } -} - -void termClear() { - printf("\033[2J\033[H"); -} - -void termPushColor(const color_t color) { - assertTrue(color < COLOR_COUNT, "Invalid color index"); - printf("%s", TERM_COLORS[color]); -} - -void termPushChar(const char_t c) { - putchar(c); -} - -void termFlush() { - fflush(stdout); -} \ No newline at end of file diff --git a/src/display/term.h b/src/display/term.h deleted file mode 100644 index af9f261..0000000 --- a/src/display/term.h +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Copyright (c) 2025 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#pragma once -#include "dusk.h" -#include "display/color.h" - -typedef struct { - int32_t width; - int32_t height; -} term_t; - -extern term_t TERM; - -extern const char_t* TERM_COLORS[COLOR_COUNT]; - -/** - * Initialize the terminal system. - */ -void termInit(); - -/** - * Update the terminal system, typically called each frame. - */ -void termUpdate(); - -/** - * Clear the terminal screen and reset cursor position. - */ -void termClear(); - -void termPushColor(const color_t color); - -/** - * Push a character to the terminal output buffer. - * - * @param c The character to push. - */ -void termPushChar(const char_t c); - -/** - * Flush the terminal output buffer to the terminal. - */ -void termFlush(); \ No newline at end of file diff --git a/src/dusk/CMakeLists.txt b/src/dusk/CMakeLists.txt new file mode 100644 index 0000000..b01cd93 --- /dev/null +++ b/src/dusk/CMakeLists.txt @@ -0,0 +1,29 @@ +# Copyright (c) 2025 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Libs +target_link_libraries(${DUSK_TARGET_NAME} + PUBLIC + m +) + +# Includes +target_include_directories(${DUSK_TARGET_NAME} + PUBLIC + ${CMAKE_CURRENT_LIST_DIR} +) + +# Sources +target_sources(${DUSK_TARGET_NAME} + PRIVATE + main.c + input.c +) + +# Subdirs +add_subdirectory(assert) +add_subdirectory(display) +add_subdirectory(rpg) +add_subdirectory(util) \ No newline at end of file diff --git a/src/assert/CMakeLists.txt b/src/dusk/assert/CMakeLists.txt similarity index 100% rename from src/assert/CMakeLists.txt rename to src/dusk/assert/CMakeLists.txt diff --git a/src/assert/assert.c b/src/dusk/assert/assert.c similarity index 100% rename from src/assert/assert.c rename to src/dusk/assert/assert.c diff --git a/src/assert/assert.h b/src/dusk/assert/assert.h similarity index 100% rename from src/assert/assert.h rename to src/dusk/assert/assert.h diff --git a/lib/CMakeLists.txt b/src/dusk/display/CMakeLists.txt similarity index 69% rename from lib/CMakeLists.txt rename to src/dusk/display/CMakeLists.txt index 2737231..fb0e252 100644 --- a/lib/CMakeLists.txt +++ b/src/dusk/display/CMakeLists.txt @@ -3,4 +3,7 @@ # This software is released under the MIT License. # https://opensource.org/licenses/MIT -include(FetchContent) \ No newline at end of file +# Sources +target_sources(${DUSK_TARGET_NAME} + PRIVATE +) \ No newline at end of file diff --git a/src/display/render.h b/src/dusk/display/render.h similarity index 62% rename from src/display/render.h rename to src/dusk/display/render.h index fff8dad..a57ccf9 100644 --- a/src/display/render.h +++ b/src/dusk/display/render.h @@ -8,6 +8,9 @@ #pragma once #include "dusk.h" +extern const uint16_t RENDER_WIDTH; +extern const uint16_t RENDER_HEIGHT; + /** * Init the render system. */ @@ -16,4 +19,9 @@ void renderInit(); /** * Update the render system. */ -void renderUpdate(); \ No newline at end of file +bool_t renderUpdate(); + +/** + * Dispose of the render system. + */ +void renderDispose(); \ No newline at end of file diff --git a/src/dusk.h b/src/dusk/dusk.h similarity index 100% rename from src/dusk.h rename to src/dusk/dusk.h diff --git a/src/dusk/input.c b/src/dusk/input.c new file mode 100644 index 0000000..8a2b98e --- /dev/null +++ b/src/dusk/input.c @@ -0,0 +1,29 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "input.h" +#include "assert/assert.h" + +input_t INPUT; + +bool_t inputIsDown(const uint8_t key) { + assertTrue(key < INPUT_COUNT, "Invalid input key"); + return (INPUT.current & key) != 0; +} + +bool_t inputWasDown(const uint8_t key) { + assertTrue(key < INPUT_COUNT, "Invalid input key"); + return (INPUT.previous & key) != 0; +} + +bool_t inputWasPressed(const uint8_t key) { + return inputIsDown(key) && !inputWasDown(key); +} + +bool_t inputWasReleased(const uint8_t key) { + return !inputIsDown(key) && inputWasDown(key); +} \ No newline at end of file diff --git a/src/input.h b/src/dusk/input.h similarity index 91% rename from src/input.h rename to src/dusk/input.h index 96591c2..7c45fb3 100644 --- a/src/input.h +++ b/src/dusk/input.h @@ -15,25 +15,17 @@ #define INPUT_RIGHT (1 << 3) #define INPUT_ACTION (1 << 4) #define INPUT_CANCEL (1 << 5) +#define INPUT_COUNT (INPUT_CANCEL + 1) #define INPUT_BUFFER_SIZE 128 typedef struct { uint8_t current; uint8_t previous; - bool_t isRawMode; - struct termios origin; } input_t; extern input_t INPUT; -typedef struct { - char_t *key; - uint8_t value; -} inputmap_t; - -extern inputmap_t INPUT_MAP[]; - /** * Initializes the input system. */ @@ -44,6 +36,11 @@ void inputInit(); */ void inputUpdate(); +/** + * Disposes of the input system, restoring terminal settings. + */ +void inputDispose(); + /** * Returns true if the specified key is currently pressed down. * diff --git a/src/main.c b/src/dusk/main.c similarity index 75% rename from src/main.c rename to src/dusk/main.c index 905744a..314fd90 100644 --- a/src/main.c +++ b/src/dusk/main.c @@ -33,14 +33,7 @@ int32_t main(const int32_t argc, const char **argv) { entityUpdate(ent++); } while(ent < (ENTITIES + ENTITY_COUNT)); - if(inputIsDown(INPUT_DOWN)) { - printf("Input down pressed\n"); - } else { - printf("Input down not pressed\n"); - } - - // renderUpdate(); - usleep(16 * 1000); // Sleep for 16 milliseconds (60 FPS) + if(!renderUpdate()) break; } return EXIT_SUCCESS; diff --git a/src/rpg/CMakeLists.txt b/src/dusk/rpg/CMakeLists.txt similarity index 100% rename from src/rpg/CMakeLists.txt rename to src/dusk/rpg/CMakeLists.txt diff --git a/src/rpg/entity/CMakeLists.txt b/src/dusk/rpg/entity/CMakeLists.txt similarity index 100% rename from src/rpg/entity/CMakeLists.txt rename to src/dusk/rpg/entity/CMakeLists.txt diff --git a/src/rpg/entity/entity.c b/src/dusk/rpg/entity/entity.c similarity index 100% rename from src/rpg/entity/entity.c rename to src/dusk/rpg/entity/entity.c diff --git a/src/rpg/entity/entity.h b/src/dusk/rpg/entity/entity.h similarity index 95% rename from src/rpg/entity/entity.h rename to src/dusk/rpg/entity/entity.h index a56e927..40eb23c 100644 --- a/src/rpg/entity/entity.h +++ b/src/dusk/rpg/entity/entity.h @@ -8,7 +8,9 @@ #pragma once #include "player.h" -#define ENTITY_MOVE_SUBPIXEL 4 +#define ENTITY_WIDTH 16 +#define ENTITY_HEIGHT 16 +#define ENTITY_MOVE_SUBPIXEL ENTITY_WIDTH typedef enum { ENTITY_DIR_UP = 0, @@ -31,7 +33,7 @@ typedef enum { typedef struct _entity_t { entitytype_t type; uint8_t x, y; - uint8_t subX, subY; + int8_t subX, subY; entitydir_t dir; // Per type data diff --git a/src/rpg/entity/player.c b/src/dusk/rpg/entity/player.c similarity index 100% rename from src/rpg/entity/player.c rename to src/dusk/rpg/entity/player.c diff --git a/src/rpg/entity/player.h b/src/dusk/rpg/entity/player.h similarity index 100% rename from src/rpg/entity/player.h rename to src/dusk/rpg/entity/player.h diff --git a/src/rpg/event/event.h b/src/dusk/rpg/event/event.h similarity index 100% rename from src/rpg/event/event.h rename to src/dusk/rpg/event/event.h diff --git a/src/rpg/item/CMakeLists.txt b/src/dusk/rpg/item/CMakeLists.txt similarity index 100% rename from src/rpg/item/CMakeLists.txt rename to src/dusk/rpg/item/CMakeLists.txt diff --git a/src/rpg/item/inventory.c b/src/dusk/rpg/item/inventory.c similarity index 100% rename from src/rpg/item/inventory.c rename to src/dusk/rpg/item/inventory.c diff --git a/src/rpg/item/inventory.h b/src/dusk/rpg/item/inventory.h similarity index 100% rename from src/rpg/item/inventory.h rename to src/dusk/rpg/item/inventory.h diff --git a/src/rpg/item/itemstack.h b/src/dusk/rpg/item/itemstack.h similarity index 100% rename from src/rpg/item/itemstack.h rename to src/dusk/rpg/item/itemstack.h diff --git a/src/rpg/item/itemtype.c b/src/dusk/rpg/item/itemtype.c similarity index 100% rename from src/rpg/item/itemtype.c rename to src/dusk/rpg/item/itemtype.c diff --git a/src/rpg/item/itemtype.h b/src/dusk/rpg/item/itemtype.h similarity index 100% rename from src/rpg/item/itemtype.h rename to src/dusk/rpg/item/itemtype.h diff --git a/src/rpg/quest/CMakeLists.txt b/src/dusk/rpg/quest/CMakeLists.txt similarity index 100% rename from src/rpg/quest/CMakeLists.txt rename to src/dusk/rpg/quest/CMakeLists.txt diff --git a/src/rpg/quest/quest.c b/src/dusk/rpg/quest/quest.c similarity index 100% rename from src/rpg/quest/quest.c rename to src/dusk/rpg/quest/quest.c diff --git a/src/rpg/quest/quest.h b/src/dusk/rpg/quest/quest.h similarity index 100% rename from src/rpg/quest/quest.h rename to src/dusk/rpg/quest/quest.h diff --git a/src/rpg/world/CMakeLists.txt b/src/dusk/rpg/world/CMakeLists.txt similarity index 100% rename from src/rpg/world/CMakeLists.txt rename to src/dusk/rpg/world/CMakeLists.txt diff --git a/src/rpg/world/map.c b/src/dusk/rpg/world/map.c similarity index 100% rename from src/rpg/world/map.c rename to src/dusk/rpg/world/map.c diff --git a/src/rpg/world/map.h b/src/dusk/rpg/world/map.h similarity index 100% rename from src/rpg/world/map.h rename to src/dusk/rpg/world/map.h diff --git a/src/util/CMakeLists.txt b/src/dusk/util/CMakeLists.txt similarity index 100% rename from src/util/CMakeLists.txt rename to src/dusk/util/CMakeLists.txt diff --git a/src/util/memory.c b/src/dusk/util/memory.c similarity index 100% rename from src/util/memory.c rename to src/dusk/util/memory.c diff --git a/src/util/memory.h b/src/dusk/util/memory.h similarity index 100% rename from src/util/memory.h rename to src/dusk/util/memory.h diff --git a/src/util/random.c b/src/dusk/util/random.c similarity index 100% rename from src/util/random.c rename to src/dusk/util/random.c diff --git a/src/util/random.h b/src/dusk/util/random.h similarity index 100% rename from src/util/random.h rename to src/dusk/util/random.h diff --git a/src/util/string.c b/src/dusk/util/string.c similarity index 100% rename from src/util/string.c rename to src/dusk/util/string.c diff --git a/src/util/string.h b/src/dusk/util/string.h similarity index 100% rename from src/util/string.h rename to src/dusk/util/string.h diff --git a/src/duskraylib/CMakeLists.txt b/src/duskraylib/CMakeLists.txt new file mode 100644 index 0000000..0dbe76c --- /dev/null +++ b/src/duskraylib/CMakeLists.txt @@ -0,0 +1,34 @@ +# Copyright (c) 2025 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +include(FetchContent) + +# Libs +FetchContent_Declare( + raylib + URL https://github.com/raysan5/raylib/archive/refs/tags/5.5.tar.gz + URL_HASH MD5=61638c4c2c097fbca1d6a71e4da36c16 +) +FetchContent_MakeAvailable(raylib) + +target_link_libraries(${DUSK_TARGET_NAME} + PUBLIC + raylib +) + +# Includes +target_include_directories(${DUSK_TARGET_NAME} + PUBLIC + ${CMAKE_CURRENT_LIST_DIR} +) + +# Sources +target_sources(${DUSK_TARGET_NAME} + PRIVATE + input.c +) + +# Subdirs +add_subdirectory(display) \ No newline at end of file diff --git a/src/display/CMakeLists.txt b/src/duskraylib/display/CMakeLists.txt similarity index 94% rename from src/display/CMakeLists.txt rename to src/duskraylib/display/CMakeLists.txt index 944d412..b354f11 100644 --- a/src/display/CMakeLists.txt +++ b/src/duskraylib/display/CMakeLists.txt @@ -7,5 +7,4 @@ target_sources(${DUSK_TARGET_NAME} PRIVATE render.c - term.c ) \ No newline at end of file diff --git a/src/duskraylib/display/render.c b/src/duskraylib/display/render.c new file mode 100644 index 0000000..a3eb4fc --- /dev/null +++ b/src/duskraylib/display/render.c @@ -0,0 +1,53 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "assert/assert.h" +#include "display/render.h" +#include "raylib.h" +#include "rpg/entity/entity.h" + +const uint16_t RENDER_WIDTH = 800; +const uint16_t RENDER_HEIGHT = 600; + +void renderInit() { + InitWindow(RENDER_WIDTH, RENDER_HEIGHT, "Dusk"); + SetTargetFPS(60); +} + +bool_t renderUpdate() { + // End rendering? + if(WindowShouldClose()) return false; + + BeginDrawing(); + ClearBackground(RAYWHITE); + DrawText("Hello, Dusk!", 10, 10, 20, BLACK); + + entity_t *ent = ENTITIES; + do { + if(ent->type == ENTITY_TYPE_NULL) { + ent++; + continue; + } + + DrawRectangle( + (ent->x * ENTITY_WIDTH) + ent->subX, + (ent->y * ENTITY_HEIGHT) + ent->subY, + ENTITY_WIDTH, + ENTITY_HEIGHT, + RED + ); + ent++; + } while(ent < (ENTITIES + ENTITY_COUNT)); + + EndDrawing(); + + return true; +} + +void renderDispose() { + CloseWindow(); +} \ No newline at end of file diff --git a/src/duskraylib/input.c b/src/duskraylib/input.c new file mode 100644 index 0000000..ff5240d --- /dev/null +++ b/src/duskraylib/input.c @@ -0,0 +1,47 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "input.h" +#include "raylib.h" + +typedef struct { + int32_t key; + uint8_t flag; +} inputmap_t; + +const inputmap_t INPUT_MAP[] = { + { KEY_W, INPUT_UP }, + { KEY_S, INPUT_DOWN }, + { KEY_A, INPUT_LEFT }, + { KEY_D, INPUT_RIGHT }, + { KEY_SPACE, INPUT_ACTION }, + { KEY_ESCAPE, INPUT_CANCEL }, + { 0, 0 } +}; + +void inputInit() { + +} + +void inputUpdate() { + uint8_t state = 0; + inputmap_t *map = INPUT_MAP; + + do { + if(IsKeyDown(map->key)) { + state |= map->flag; + } + map++; + } while(map->key != 0); + + INPUT.previous = INPUT.current; + INPUT.current = state; +} + +void inputDispose() { + +} diff --git a/src/input.c b/src/input.c deleted file mode 100644 index 8fef90f..0000000 --- a/src/input.c +++ /dev/null @@ -1,120 +0,0 @@ -/** - * Copyright (c) 2025 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#include "input.h" -#include "util/memory.h" -#include "assert/assert.h" -#include - -input_t INPUT; - -inputmap_t INPUT_MAP[] = { - {"w", INPUT_UP}, - {"s", INPUT_DOWN}, - {"a", INPUT_LEFT}, - {"d", INPUT_RIGHT}, - {" ", INPUT_ACTION}, - {"e", INPUT_ACTION}, - {"\n", INPUT_ACTION}, // Enter key - {"\x1B", INPUT_CANCEL}, // Escape key - {"\033[A", INPUT_UP}, // Up arrow - {"\033[B", INPUT_DOWN}, // Down arrow - {"\033[C", INPUT_RIGHT}, // Right arrow - {"\033[D", INPUT_LEFT}, // Left arrow - { 0 } -}; - -void inputEnableRawMode() { - if (INPUT.isRawMode) return; - tcgetattr(STDIN_FILENO, &INPUT.origin); - struct termios raw = INPUT.origin; - raw.c_lflag &= ~(ICANON | ECHO); - tcsetattr(STDIN_FILENO, TCSANOW, &raw); - INPUT.isRawMode = 1; -} - -void inputDisableRawMode() { - if(!INPUT.isRawMode) return; - tcsetattr(STDIN_FILENO, TCSANOW, &INPUT.origin); - INPUT.isRawMode = 0; -} - -void inputSetNonBlocking() { - int flags = fcntl(STDIN_FILENO, F_GETFL, 0); - fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); -} - -void inputInit() { - memoryZero(&INPUT, sizeof(input_t)); - - inputEnableRawMode(); - inputSetNonBlocking(); -} - -void inputUpdate() { - char_t buffer[INPUT_BUFFER_SIZE]; - ssize_t n = read(STDIN_FILENO, buffer, sizeof(buffer) - 1); - - if(n < 0) { - INPUT.previous = INPUT.current; - INPUT.current = 0; - return; - } - - assertTrue(n < INPUT_BUFFER_SIZE, "Input buffer overflow"); - - uint8_t val = 0; - inputmap_t *map; - uint8_t len; - char_t *c; - for(uint8_t i = 0; i < n; i++) { - map = INPUT_MAP; - - do { - len = strlen(map->key); - - if(i + len > n) { - map++; - continue; - } - - c = buffer + i; - if(memoryCompare(c, map->key, len) != 0) { - map++; - continue; - } - - // Button was pressed - val |= map->value; - i += len - 1; - break; - } while(map->key); - } - - INPUT.previous = INPUT.current; - INPUT.current = val; -} - -bool_t inputIsDown(const uint8_t key) { - return (INPUT.current & key) != 0; -} - -bool_t inputWasDown(const uint8_t key) { - return (INPUT.previous & key) != 0; -} - -bool_t inputWasPressed(const uint8_t key) { - return inputIsDown(key) && !inputWasDown(key); -} - -bool_t inputWasReleased(const uint8_t key) { - return !inputIsDown(key) && inputWasDown(key); -} - -void inputDispose() { - inputDisableRawMode(); -} \ No newline at end of file