From 3f37b7cdb5d0525a047e43565f4f4cd3664089b8 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Wed, 3 Sep 2025 11:57:48 -0500 Subject: [PATCH] Beginning input refactor --- assets/font_minogram.png | Bin 1161 -> 1148 bytes assets/init.cfg | 1 + src/asset/type/assetalphaimage.c | 1 - src/asset/type/assetpaletteimage.c | 1 - src/console/console.c | 8 +- src/display/display.c | 6 +- src/display/scene/overworld/sceneoverworld.c | 1 - src/display/ui/CMakeLists.txt | 3 +- src/display/ui/ui.c | 54 ++++++++----- src/display/ui/ui.h | 24 +++++- src/display/ui/uiconsole.c | 29 +++++++ src/display/ui/uiconsole.h | 11 +++ src/display/ui/{rendertext.c => uitext.c} | 66 ++++++++-------- src/display/ui/{rendertext.h => uitext.h} | 26 +++--- src/engine/engine.c | 3 + src/input/CMakeLists.txt | 18 ++++- src/input/input.c | 48 +++++++++-- src/input/input.h | 78 ++++++++++++++---- src/rpg/entity/player.c | 79 ++++++++++--------- 19 files changed, 320 insertions(+), 137 deletions(-) create mode 100644 assets/init.cfg create mode 100644 src/display/ui/uiconsole.c create mode 100644 src/display/ui/uiconsole.h rename src/display/ui/{rendertext.c => uitext.c} (52%) rename src/display/ui/{rendertext.h => uitext.h} (79%) diff --git a/assets/font_minogram.png b/assets/font_minogram.png index a41c43611ed879a3e08c41496ce5d5ca6215c385..6aa3b6524c3b892eee504821b39471068356a1eb 100644 GIT binary patch delta 1127 zcmV-t1ep7Y3H%6?4|_pbB`sOpMj@_&kEeHAiqP!6Sk71|%^ z2U@8X4)D2xkA4`r0!qPyp&QkmQp=o`p;x3F;Ao{vRPY($Qz_-&0A!3>D?rbzXy(rb z+9x7pXScV%p!QC(C=1F!&n&A^Ju9;tG1B!0IV+z9`Mz&|>k1xFYb_h{k0mR0bQ=0( ztO%29R=`Rq5`Xx{s(yG$AIf7>*u}Lt}`M_#^`(UUbx{%gitTFM*f`SFL1+h zLUt&Ud&rP7YZCPM(-YS-&oKRg{Csc3UK+Yf_HZYxDBuw>y~?Bh`Do+iC^C-e2AR4` zCUjV2syOljbQsXNyP;$Ck^4L|0Fh&o(dy@lAxC_HU4P1qoyd0tuM?53)?qvUW5PrO z&^4+T^|Kgh9}Pk(q>#^jgdja*q$8!Amwh(ov7uvzot}ZQRsw>jhXxX6K|Px-8y6$} zY#Vna2AQyxxp$I%nQ#Y2lD1#Rgm(iXViqH1m@v|}vWOmroMqY)5pnZaZ4UuN{-1no zn=qT+MSl%5@{-7dZvg!8Eh&^C7GoEFC7Dq^d;Q`W#Ui2_wwoupWPxgI0 zQpMUz72|ZHR|wCQG?W=U1kc}W$9Z5wQ`7x`EXJX70)wZfQJIcoY;~b01bNJpovqQ# z&sG=pcb2j1m5}k%LdyRy4_J&Z6J8AVJm9}a_J6F{($7NvE=ZsDc&9*&KB}gco8`#hVWcy_kftmV8;&P1VcCwH-6$(#>p9zCbQ(Gv7JhxM)C}z znTzaZiei@Ct#IY%=`(>k+6p_7jjhp+Br_ppq`ntY(6d=Y%r;Ywo&wE;C6)hGSj7YM zCV%|vI({|2wV=iDHsPxyGn!>Gn=W(T(woM1J*+OqNfv$e<(XvQ%Ka&@PQ)^&QOqe7 zlQl8t2T{A0ZSQ`R2{FD{{)GBgEyyzJ)q*TLp1#zxYDtklP42BA>pq)?%J=K7l|lUx z<>=l#dgQrm7TcuTBL;x!#N{uluubI_F@Ir+Ofk-~bCo$q(B%rI4c*qrLZfHnB!2=A z=$)lrEM5WF{;o%oBg)+?%jPtmumWzus9I1~f7U;2dI~aZeF|1H;g!hj3DeJn5A`YP zsvFPfqi64uBR{u7#DwKNi|IAYCRP?3_4FG?WJFWmkNuZr@_Tb+_N*Gko{@e}NMM;V zxqA12I}YU$bRCJ+-xd)QM)0bDDEId4qZw_58pK#V`)H4_+KDi2AR7_Oly-aSKHf8) tJ_>>8?~MPfUp;zh?8=0**q-rP_ycfF`=xZ}>ly$6002ovPDHLkV1k;NGy?zt delta 1140 zcmV-)1dIFp2#E=hB!5szL_t(|ob6oglB6&QG+Xojuk8Lgt;j&r1dO_8S6%H+LI~yq zXq?-&uIuvRI(+{9?02DcziaJs$42F~oo}EW>6bQmdV2KomL4>Y)uY<~Q3M!8(TSP% z9+3yOU~Lcsb`08VPv^QUOZyp^ltrbknffE^VBHYfuYY_I6@Qk?s?vk>Gj1+pPxN^- zdE~kd=r=&SO4fr#TmeGF1<5{U^E4oP>4hUexuzfSxrxb}(#GPc=@skKSzQnxWkNRi zNO`1(J+0o!kJz~?zi)@h(<5&}#7+z9yzS)y*Fw6W7n3oX1^qW@P4_mI=dk20Y((s! zqn}4<&&qFoQGX^`*{<0mpCJ+phqI>b22?OxOFqL$&j3T3qC{*MXU2e8i`_4RJS19s zkIN5*SB$d|O+(LyMW7sfU6;hAJ0)1QJD1FUQzTle?mwK+5PSh`WamkBYpSoZAgXals`j-@nO z6tncp@=8oI>KnHF+JTra|4(~yjyxZy>c62!aX9z zI8=krFn?zcvRuK;A?K}Ty7ku&9=#)PA|4db=&GU1ES-vG^@Stm$qw^BMQYe9M<@~Az9)|xCKMAKGRUJJtB z;D3?aS?fo4^US0C@wlTdu{8FLXwtK$U&3fW{&_nN+$Aj0=hw$&B~L_GvB3CeDCWKIu2C)E@K~ zvqlz9`zd8c$`)5b@{Lma&<&8Os7GcbDUv@&YBHHR4jQKsI60@6l9vE&04f6@w{4^u z+v{Qv%pKJ6 z>DF8ja#<;i{;2{BQA49Qz^j;Mf#TdL?(i=#;k#u`A^ZZrPT$nYGXwho0000cmd, "Command execution has no command."); @@ -382,5 +389,4 @@ void consoleDispose(void) { pfd.revents = 0; } } - #endif \ No newline at end of file diff --git a/src/display/display.c b/src/display/display.c index ba75624..371df97 100644 --- a/src/display/display.c +++ b/src/display/display.c @@ -17,7 +17,11 @@ display_t DISPLAY; errorret_t displayInit(void) { #if DISPLAY_SDL2 - if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER) != 0) { + uint32_t flags = SDL_INIT_VIDEO; + #if INPUT_SUPPORT_GAMEPAD + flags |= SDL_INIT_GAMECONTROLLER; + #endif + if(SDL_Init(flags) != 0) { errorThrow("SDL Failed to Initialize: %s", SDL_GetError()); } diff --git a/src/display/scene/overworld/sceneoverworld.c b/src/display/scene/overworld/sceneoverworld.c index 07407b9..6dc283d 100644 --- a/src/display/scene/overworld/sceneoverworld.c +++ b/src/display/scene/overworld/sceneoverworld.c @@ -12,7 +12,6 @@ #include "display/scene/scenemanager.h" #include "display/mesh/quad.h" #include "asset/assetmanager.h" -#include "display/ui/rendertext.h" camera_t SCENE_OVERWORLD_CAMERA; asset_t *testAsset; diff --git a/src/display/ui/CMakeLists.txt b/src/display/ui/CMakeLists.txt index 1b0ffad..2e812f4 100644 --- a/src/display/ui/CMakeLists.txt +++ b/src/display/ui/CMakeLists.txt @@ -6,6 +6,7 @@ # Sources target_sources(${DUSK_TARGET_NAME} PRIVATE - rendertext.c + uitext.c ui.c + uiconsole.c ) \ No newline at end of file diff --git a/src/display/ui/ui.c b/src/display/ui/ui.c index ae9ccf5..be89f0c 100644 --- a/src/display/ui/ui.c +++ b/src/display/ui/ui.c @@ -6,44 +6,56 @@ */ #include "ui.h" -#include "rendertext.h" +#include "uitext.h" #include "display/framebuffer/framebuffer.h" +#include "display/spritebatch/spritebatch.h" +#include "uiconsole.h" +#include "util/memory.h" -camera_t UI_CAMERA; +ui_t UI; errorret_t uiInit(void) { - errorChain(renderTextInit()); + memoryZero(&UI, sizeof(ui_t)); + errorChain(uiTextInit()); - cameraInit(&UI_CAMERA); - UI_CAMERA.projType = CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC; - - UI_CAMERA.orthographic.left = 0.0f; - UI_CAMERA.orthographic.top = 0.0f; - UI_CAMERA.orthographic.right = frameBufferGetWidth(&FRAMEBUFFER_BACKBUFFER); - UI_CAMERA.orthographic.bottom = frameBufferGetHeight(&FRAMEBUFFER_BACKBUFFER); - UI_CAMERA.nearClip = -1.0f; - UI_CAMERA.farClip = 1.0f; + cameraInit(&UI.camera); + UI.camera.projType = CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC; - UI_CAMERA.viewType = CAMERA_VIEW_TYPE_MATRIX; - glm_mat4_identity(UI_CAMERA.view); + UI.camera.orthographic.left = 0.0f; + UI.camera.orthographic.top = 0.0f; + UI.camera.orthographic.right = frameBufferGetWidth(&FRAMEBUFFER_BACKBUFFER); + UI.camera.orthographic.bottom = frameBufferGetHeight(&FRAMEBUFFER_BACKBUFFER); + UI.camera.nearClip = -1.0f; + UI.camera.farClip = 1.0f; + + UI.camera.viewType = CAMERA_VIEW_TYPE_MATRIX; + glm_mat4_identity(UI.camera.view); + + UI.scale = 2.0f; errorOk(); } void uiUpdate(void) { + } void uiRender(void) { - UI_CAMERA.orthographic.right = frameBufferGetWidth(FRAMEBUFFER_BOUND); - UI_CAMERA.orthographic.bottom = frameBufferGetHeight(FRAMEBUFFER_BOUND); - - cameraPushMatrix(&UI_CAMERA); + UI.camera.orthographic.right = ( + frameBufferGetWidth(FRAMEBUFFER_BOUND) / UI.scale + ); + UI.camera.orthographic.bottom = ( + frameBufferGetHeight(FRAMEBUFFER_BOUND) / UI.scale + ); - renderTextDraw(10.0f, 10.0f, "Dusk Engine UI", 0xFF, 0xFF, 0xFF); - + cameraPushMatrix(&UI.camera); + + uiConsoleRender(); + + spriteBatchFlush(); cameraPopMatrix(); } void uiDispose(void) { - renderTextDispose(); + uiTextDispose(); } \ No newline at end of file diff --git a/src/display/ui/ui.h b/src/display/ui/ui.h index 6b01e89..5a9f081 100644 --- a/src/display/ui/ui.h +++ b/src/display/ui/ui.h @@ -9,9 +9,31 @@ #include "error/error.h" #include "display/camera.h" -extern camera_t UI_CAMERA; +typedef struct { + camera_t camera; + float_t scale; +} ui_t; +extern ui_t UI; + +/** + * Initializes the UI system. + * + * @return An errorret_t indicating success or failure. + */ errorret_t uiInit(void); + +/** + * Updates the UI system. Will not render anything. + */ void uiUpdate(void); + +/** + * Renders the UI system. + */ void uiRender(void); + +/** + * Disposes of the UI system. + */ void uiDispose(void); \ No newline at end of file diff --git a/src/display/ui/uiconsole.c b/src/display/ui/uiconsole.c new file mode 100644 index 0000000..6906001 --- /dev/null +++ b/src/display/ui/uiconsole.c @@ -0,0 +1,29 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "uiconsole.h" +#include "uitext.h" +#include "console/console.h" + +void uiConsoleRender(void) { + if(!CONSOLE.visible) return; + + int32_t i = 0; + char_t *line; + do { + line = CONSOLE.line[i]; + if(line[0] == '\0') { + i++; + continue; + } + uiTextDraw( + 0, i * UI_TEXT_TILE_HEIGHT, line, + 0xFF, 0xFF, 0xFF + ); + i++; + } while(i < CONSOLE_HISTORY_MAX); +} \ No newline at end of file diff --git a/src/display/ui/uiconsole.h b/src/display/ui/uiconsole.h new file mode 100644 index 0000000..88bed96 --- /dev/null +++ b/src/display/ui/uiconsole.h @@ -0,0 +1,11 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "dusk.h" + +void uiConsoleRender(void); \ No newline at end of file diff --git a/src/display/ui/rendertext.c b/src/display/ui/uitext.c similarity index 52% rename from src/display/ui/rendertext.c rename to src/display/ui/uitext.c index 48b048d..841870f 100644 --- a/src/display/ui/rendertext.c +++ b/src/display/ui/uitext.c @@ -5,32 +5,30 @@ // * https://opensource.org/licenses/MIT // */ -#include "rendertext.h" +#include "uitext.h" #include "asset/assetmanager.h" #include "assert/assert.h" #include "util/memory.h" #include "display/spritebatch/spritebatch.h" -// #include "display/display.h" -// #include "util/math.h" -rendertext_t RENDER_TEXT; +uitext_t UI_TEXT; -errorret_t renderTextInit(void) { - memoryZero(&RENDER_TEXT, sizeof(rendertext_t)); +errorret_t uiTextInit(void) { + memoryZero(&UI_TEXT, sizeof(uitext_t)); errorChain(assetManagerLoadAsset( - "font_minogram.dai", &RENDER_TEXT.asset, &RENDER_TEXT.assetRef + "font_minogram.dai", &UI_TEXT.asset, &UI_TEXT.assetRef )); errorOk(); } -void renderTextDispose(void) { - if(RENDER_TEXT.asset) { - assetUnlock(RENDER_TEXT.asset, RENDER_TEXT.assetRef); +void uiTextDispose(void) { + if(UI_TEXT.asset) { + assetUnlock(UI_TEXT.asset, UI_TEXT.assetRef); } } -void renderTextDrawChar( +void uiTextDrawChar( const float_t x, const float_t y, const char_t c, @@ -38,30 +36,34 @@ void renderTextDrawChar( const uint8_t g, const uint8_t b ) { - int32_t tileIndex = (int32_t)(c) - RENDER_TEXT_CHAR_START; + int32_t tileIndex = (int32_t)(c) - UI_TEXT_CHAR_START; + if(tileIndex < 0 || tileIndex >= UI_TEXT_TILE_COUNT) { + tileIndex = ((int32_t)'@') - UI_TEXT_CHAR_START; + } + assertTrue( - tileIndex >= 0 && tileIndex <= RENDER_TEXT_TILE_COUNT, + tileIndex >= 0 && tileIndex <= UI_TEXT_TILE_COUNT, "Character is out of bounds for font tiles" ); - const float_t w = (float)RENDER_TEXT.asset->alphaImage.texture.width; - const float_t h = (float)RENDER_TEXT.asset->alphaImage.texture.height; - const int32_t tileX = (tileIndex % RENDER_TEXT_COLUMN_COUNT); - const int32_t tileY = (tileIndex / RENDER_TEXT_COLUMN_COUNT); + const float_t w = (float)UI_TEXT.asset->alphaImage.texture.width; + const float_t h = (float)UI_TEXT.asset->alphaImage.texture.height; + const int32_t tileX = (tileIndex % UI_TEXT_COLUMN_COUNT); + const int32_t tileY = (tileIndex / UI_TEXT_COLUMN_COUNT); spriteBatchPush( - &RENDER_TEXT.asset->alphaImage.texture, + &UI_TEXT.asset->alphaImage.texture, x, y, - x + RENDER_TEXT_TILE_WIDTH, y + RENDER_TEXT_TILE_HEIGHT, + x + UI_TEXT_TILE_WIDTH, y + UI_TEXT_TILE_HEIGHT, r, g, b, 0xFF, - (tileX * RENDER_TEXT_TILE_WIDTH) / w, - (tileY * RENDER_TEXT_TILE_HEIGHT) / h, - ((tileX + 1) * RENDER_TEXT_TILE_WIDTH) / w, - ((tileY + 1) * RENDER_TEXT_TILE_HEIGHT) / h + (tileX * UI_TEXT_TILE_WIDTH) / w, + (tileY * UI_TEXT_TILE_HEIGHT) / h, + ((tileX + 1) * UI_TEXT_TILE_WIDTH) / w, + ((tileY + 1) * UI_TEXT_TILE_HEIGHT) / h ); } -void renderTextDraw( +void uiTextDraw( const float_t x, const float_t y, const char_t *text, @@ -79,21 +81,21 @@ void renderTextDraw( while((c = text[i++]) != '\0') { if(c == '\n') { posX = x; - posY += RENDER_TEXT_TILE_HEIGHT; + posY += UI_TEXT_TILE_HEIGHT; continue; } if(c == ' ') { - posX += RENDER_TEXT_TILE_WIDTH; + posX += UI_TEXT_TILE_WIDTH; continue; } - renderTextDrawChar(posX, posY, c, r, g, b); - posX += RENDER_TEXT_TILE_WIDTH; + uiTextDrawChar(posX, posY, c, r, g, b); + posX += UI_TEXT_TILE_WIDTH; } } -void renderTextMeasure( +void uiTextMeasure( const char_t *text, int32_t *outWidth, int32_t *outHeight @@ -103,7 +105,7 @@ void renderTextMeasure( assertNotNull(outHeight, "Output height pointer cannot be NULL"); int32_t width = 0; - int32_t height = RENDER_TEXT_TILE_HEIGHT; + int32_t height = UI_TEXT_TILE_HEIGHT; int32_t lineWidth = 0; char_t c; @@ -114,11 +116,11 @@ void renderTextMeasure( width = lineWidth; } lineWidth = 0; - height += RENDER_TEXT_TILE_HEIGHT; + height += UI_TEXT_TILE_HEIGHT; continue; } - lineWidth += RENDER_TEXT_TILE_WIDTH; + lineWidth += UI_TEXT_TILE_WIDTH; } if(lineWidth > width) { diff --git a/src/display/ui/rendertext.h b/src/display/ui/uitext.h similarity index 79% rename from src/display/ui/rendertext.h rename to src/display/ui/uitext.h index 9da17bb..13d5bee 100644 --- a/src/display/ui/rendertext.h +++ b/src/display/ui/uitext.h @@ -8,26 +8,26 @@ #pragma once #include "asset/assetmanager.h" -#define RENDER_TEXT_CHAR_START '@' +#define UI_TEXT_CHAR_START '!' -#define RENDER_TEXT_COLUMN_COUNT 16 -#define RENDER_TEXT_ROW_COUNT 6 -#define RENDER_TEXT_TILE_COUNT (RENDER_TEXT_COLUMN_COUNT*RENDER_TEXT_ROW_COUNT) +#define UI_TEXT_COLUMN_COUNT 16 +#define UI_TEXT_ROW_COUNT 6 +#define UI_TEXT_TILE_COUNT (UI_TEXT_COLUMN_COUNT*UI_TEXT_ROW_COUNT) -#define RENDER_TEXT_TILE_WIDTH 6.0f -#define RENDER_TEXT_TILE_HEIGHT 10.0f +#define UI_TEXT_TILE_WIDTH 6.0f +#define UI_TEXT_TILE_HEIGHT 10.0f typedef struct { ref_t assetRef; asset_t *asset; -} rendertext_t; +} uitext_t; -extern rendertext_t RENDER_TEXT; +extern uitext_t UI_TEXT; /** * Initializes the text rendering system. */ -errorret_t renderTextInit(void); +errorret_t uiTextInit(void); /** * Draws a single character at the specified position. @@ -39,7 +39,7 @@ errorret_t renderTextInit(void); * @param g The green component of the color (0-255). * @param b The blue component of the color (0-255). */ -void renderTextDrawChar( +void uiTextDrawChar( const float_t x, const float_t y, const char_t c, @@ -58,7 +58,7 @@ void renderTextDrawChar( * @param g The green component of the color (0-255). * @param b The blue component of the color (0-255). */ -void renderTextDraw( +void uiTextDraw( const float_t x, const float_t y, const char_t *text, @@ -74,7 +74,7 @@ void renderTextDraw( * @param outWidth Pointer to store the measured width in pixels. * @param outHeight Pointer to store the measured height in pixels. */ -void renderTextMeasure( +void uiTextMeasure( const char_t *text, int32_t *outWidth, int32_t *outHeight @@ -83,4 +83,4 @@ void renderTextMeasure( /** * Disposes of the text rendering system, freeing any allocated resources. */ -void renderTextDispose(void); \ No newline at end of file +void uiTextDispose(void); \ No newline at end of file diff --git a/src/engine/engine.c b/src/engine/engine.c index 562a84d..9134db7 100644 --- a/src/engine/engine.c +++ b/src/engine/engine.c @@ -8,6 +8,7 @@ #include "engine.h" #include "util/memory.h" #include "time/time.h" +#include "input/input.h" #include "console/console.h" #include "display/display.h" #include "asset/assetmanager.h" @@ -22,6 +23,7 @@ errorret_t engineInit(void) { // Init systems. Order is important. timeInit(); + inputInit(); consoleInit(); errorChain(assetManagerInit()); errorChain(displayInit()); @@ -32,6 +34,7 @@ errorret_t engineInit(void) { errorret_t engineUpdate(void) { timeUpdate(); + inputUpdate(); consoleUpdate(); assetManagerUpdate(); errorChain(displayUpdate()); diff --git a/src/input/CMakeLists.txt b/src/input/CMakeLists.txt index 2ee57d2..8b7dc59 100644 --- a/src/input/CMakeLists.txt +++ b/src/input/CMakeLists.txt @@ -7,4 +7,20 @@ target_sources(${DUSK_TARGET_NAME} PRIVATE input.c -) \ No newline at end of file +) + +if(DUSK_TARGET_SYSTEM STREQUAL "linux") + target_compile_definitions(${DUSK_TARGET_NAME} + PRIVATE + INPUT_SDL2=1 + INPUT_KEYBOARD=1 + INPUT_MOUSE=1 + INPUT_GAMEPAD=1 + ) +elseif(DUSK_TARGET_SYSTEM STREQUAL "psp") + target_compile_definitions(${DUSK_TARGET_NAME} + PRIVATE + INPUT_SDL=1 + INPUT_GAMEPAD=1 + ) +endif() \ No newline at end of file diff --git a/src/input/input.c b/src/input/input.c index 967d635..6359430 100644 --- a/src/input/input.c +++ b/src/input/input.c @@ -13,25 +13,57 @@ input_t INPUT; void inputInit(void) { memoryZero(&INPUT, sizeof(input_t)); + + INPUT.binds[INPUT_BIND_UP].keyboard[0] = SDL_SCANCODE_W; + INPUT.binds[INPUT_BIND_UP].keyboard[1] = SDL_SCANCODE_UP; + INPUT.binds[INPUT_BIND_CONSOLE].keyboard[0] = SDL_SCANCODE_GRAVE; } void inputUpdate(void) { - INPUT.previous = INPUT.current; - INPUT.current = inputStateGet(); + #if INPUT_SDL2 == 1 + const uint8_t *keyboardState = SDL_GetKeyboardState(NULL); + #endif + + // For each input bind... + inputbinddata_t *data = INPUT.binds; + do { + data->lastValue = data->currentValue; + data->currentValue = 0.0f; + + // Handle keyboard + #if INPUT_KEYBOARD == 1 + for(uint32_t i = 0; i < INPUT_BIND_KEYBOARD_BUTTONS_MAX; i++) { + if(data->keyboard[i] == 0) break; + + #if INPUT_SDL2 == 1 + if(keyboardState[data->keyboard[i]]) { + data->currentValue = 1.0f; + break; + } + #endif + } + #endif + + data++; + } while(data < INPUT.binds + INPUT_BIND_COUNT); } -inputstate_t inputStateGet(void) { - return 0; +float_t inputGetCurrentValue(const inputbind_t bind) { + assertTrue(bind < INPUT_BIND_COUNT, "Input bind out of bounds"); + return INPUT.binds[bind].currentValue; +} + +float_t inputGetLast(const inputbind_t bind) { + assertTrue(bind < INPUT_BIND_COUNT, "Input bind out of bounds"); + return INPUT.binds[bind].lastValue; } bool_t inputIsDown(const inputbind_t bind) { - assertTrue(bind < INPUT_BIND_COUNT, "Input bind out of bounds"); - return (INPUT.current & bind) != 0; + return inputGetCurrentValue(bind) > 0.0f; } bool_t inputWasDown(const inputbind_t bind) { - assertTrue(bind < INPUT_BIND_COUNT, "Input bind out of bounds"); - return (INPUT.previous & bind) != 0; + return inputGetLast(bind) > 0.0f; } bool_t inputPressed(const inputbind_t bind) { diff --git a/src/input/input.h b/src/input/input.h index 5dc43ba..6a546d3 100644 --- a/src/input/input.h +++ b/src/input/input.h @@ -8,23 +8,60 @@ #pragma once #include "dusk.h" -typedef uint8_t inputbind_t; -typedef inputbind_t inputstate_t; +#if INPUT_SDL2 == 1 + #include +#else + #error "No input backend defined" +#endif -#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_CONSOLE (1 << 6) -#define INPUT_BIND_QUIT (1 << 7) +// Keyboard defs +#if INPUT_KEYBOARD == 1 + #define INPUT_BIND_KEYBOARD_BUTTONS_MAX 32 -#define INPUT_BIND_COUNT (INPUT_BIND_QUIT + 1) + #if INPUT_SDL2 == 1 + typedef SDL_Scancode inputscancode_t; + #endif +#endif + +// Gamepad defs +#if INPUT_GAMEPAD == 1 + #define INPUT_GAMEPAD_BUTTON_NAME_MAX 8 + #define INPUT_BIND_GAMEPAD_BUTTONS_MAX 8 + + #if INPUT_SDL2 == 1 + typedef SDL_GameControllerButton inputgamepadbutton_t; + #endif +#endif + +typedef enum { + INPUT_BIND_UP, + INPUT_BIND_DOWN, + INPUT_BIND_LEFT, + INPUT_BIND_RIGHT, + INPUT_BIND_ACCEPT, + INPUT_BIND_CANCEL, + INPUT_BIND_CONSOLE, + + INPUT_BIND_COUNT +} inputbind_t; typedef struct { - uint8_t current; - uint8_t previous; + inputbind_t bind; + float_t lastValue; + float_t currentValue; + + #if INPUT_GAMEPAD == 1 + inputgamepadbutton_t gamepad[INPUT_BIND_GAMEPAD_BUTTONS_MAX]; + uint8_t gamepadButtonCount; + #endif + + #if INPUT_KEYBOARD == 1 + inputscancode_t keyboard[INPUT_BIND_KEYBOARD_BUTTONS_MAX]; + #endif +} inputbinddata_t; + +typedef struct { + inputbinddata_t binds[INPUT_BIND_COUNT]; } input_t; extern input_t INPUT; @@ -40,11 +77,20 @@ void inputInit(void); void inputUpdate(void); /** - * Gets the current input state as a bitmask. + * Gets the current value of a specific input bind. * - * @return The current input state as a bitmask. + * @param bind The input bind to get the value for. + * @return The current value of the bind (0.0f to 1.0f). */ -inputstate_t inputStateGet(void); +float_t inputGetCurrentValue(const inputbind_t bind); + +/** + * Gets the last value of a specific input bind. + * + * @param bind The input bind to get the value for. + * @return The last value of the bind (0.0f to 1.0f). + */ +float_t inputGetLast(const inputbind_t bind); /** * Checks if a specific input bind is currently pressed. diff --git a/src/rpg/entity/player.c b/src/rpg/entity/player.c index 5c23adb..f13135e 100644 --- a/src/rpg/entity/player.c +++ b/src/rpg/entity/player.c @@ -36,51 +36,52 @@ void playerEntityUpdate(entity_t *entity) { // if(UI_TEXTBOX.visible) return; if(entityIsMoving(entity)) return; - const uint8_t moveSpeed = inputIsDown(INPUT_BIND_CANCEL) ? PLAYER_SPEED_RUN : PLAYER_SPEED_WALK; + // const uint8_t moveSpeed = inputIsDown(INPUT_BIND_CANCEL) ? PLAYER_SPEED_RUN : PLAYER_SPEED_WALK; + const uint8_t moveSpeed = PLAYER_SPEED_WALK; - if(inputIsDown(INPUT_BIND_UP)) { - if(entity->dir != DIRECTION_NORTH) { - entityTurn(entity, DIRECTION_NORTH); - return; - } - entityMove(entity, moveSpeed); - return; + // if(inputIsDown(INPUT_BIND_UP)) { + // if(entity->dir != DIRECTION_NORTH) { + // entityTurn(entity, DIRECTION_NORTH); + // return; + // } + // entityMove(entity, moveSpeed); + // return; - } else if(inputIsDown(INPUT_BIND_DOWN)) { - if(entity->dir != DIRECTION_SOUTH) { - entityTurn(entity, DIRECTION_SOUTH); - return; - } + // } else if(inputIsDown(INPUT_BIND_DOWN)) { + // if(entity->dir != DIRECTION_SOUTH) { + // entityTurn(entity, DIRECTION_SOUTH); + // return; + // } - entityMove(entity, moveSpeed); - return; - } else if(inputIsDown(INPUT_BIND_LEFT)) { - if(entity->dir != DIRECTION_WEST) { - entityTurn(entity, DIRECTION_WEST); - return; - } - entityMove(entity, moveSpeed); - return; + // entityMove(entity, moveSpeed); + // return; + // } else if(inputIsDown(INPUT_BIND_LEFT)) { + // if(entity->dir != DIRECTION_WEST) { + // entityTurn(entity, DIRECTION_WEST); + // return; + // } + // entityMove(entity, moveSpeed); + // return; - } else if(inputIsDown(INPUT_BIND_RIGHT)) { - if(entity->dir != DIRECTION_EAST) { - entityTurn(entity, DIRECTION_EAST); - return; - } + // } else if(inputIsDown(INPUT_BIND_RIGHT)) { + // if(entity->dir != DIRECTION_EAST) { + // entityTurn(entity, DIRECTION_EAST); + // return; + // } - entityMove(entity, moveSpeed); - return; - } + // entityMove(entity, moveSpeed); + // return; + // } // Interact - if(inputPressed(INPUT_BIND_ACTION)) { - int8_t x, y; - directionGetCoordinates(entity->dir, &x, &y); - entity_t *ent = entityGetAt(entity->x + x, entity->y + y); + // if(inputPressed(INPUT_BIND_ACTION)) { + // int8_t x, y; + // directionGetCoordinates(entity->dir, &x, &y); + // entity_t *ent = entityGetAt(entity->x + x, entity->y + y); - // if(ent != NULL && ENTITY_CALLBACKS[ent->type].interact != NULL) { - // assertTrue(ent->type < ENTITY_TYPE_COUNT, "Entity type out of bounds"); - // ENTITY_CALLBACKS[ent->type].interact(entity, ent); - // } - } + // // if(ent != NULL && ENTITY_CALLBACKS[ent->type].interact != NULL) { + // // assertTrue(ent->type < ENTITY_TYPE_COUNT, "Entity type out of bounds"); + // // ENTITY_CALLBACKS[ent->type].interact(entity, ent); + // // } + // } } \ No newline at end of file