diff --git a/src/dusk/console/console.c b/src/dusk/console/console.c index d43df5d..47eae34 100644 --- a/src/dusk/console/console.c +++ b/src/dusk/console/console.c @@ -15,7 +15,6 @@ console_t CONSOLE; void consoleInit() { memoryZero(&CONSOLE, sizeof(console_t)); - pthread_mutex_init(&CONSOLE.lock, NULL); // Initialize the mutex // Register the get and set command. CONSOLE.cmdGet = consoleRegCmd("get", cmdGet); @@ -26,10 +25,8 @@ void consoleInit() { } consolecmd_t * consoleRegCmd(const char_t *name, consolecmdfunc_t function) { - pthread_mutex_lock(&CONSOLE.lock); // Lock consolecmd_t *cmd = &CONSOLE.commands[CONSOLE.commandCount++]; consoleCmdInit(cmd, name, function); - pthread_mutex_unlock(&CONSOLE.lock); // Unlock return cmd; } @@ -38,10 +35,8 @@ consolevar_t * consoleRegVar( const char_t *value, consolevarchanged_t event ) { - pthread_mutex_lock(&CONSOLE.lock); // Lock consolevar_t *var = &CONSOLE.variables[CONSOLE.variableCount++]; consoleVarInitListener(var, name, value, event); - pthread_mutex_unlock(&CONSOLE.lock); // Unlock return var; } @@ -70,7 +65,6 @@ void consolePrint(const char_t *message, ...) { } void consoleExec(const char_t *line) { - pthread_mutex_lock(&CONSOLE.lock); // Lock assertNotNull(line, "line must not be NULL"); assertTrue( CONSOLE.execBufferCount < CONSOLE_EXEC_BUFFER_MAX, @@ -270,23 +264,6 @@ void consoleExec(const char_t *line) { break; } } - - pthread_mutex_unlock(&CONSOLE.lock); // Unlock -} - -void consoleProcess() { - pthread_mutex_lock(&CONSOLE.lock); // Lock - for(uint32_t i = 0; i < CONSOLE.execBufferCount; i++) { - consolecmdexec_t *exec = &CONSOLE.execBuffer[i]; - - assertNotNull(exec->cmd, "Command execution has no command."); - - exec->cmd->function(exec); - } - - // Clear the exec buffer - CONSOLE.execBufferCount = 0; - pthread_mutex_unlock(&CONSOLE.lock); // Unlock } void cmdGet(const consolecmdexec_t *exec) { @@ -334,62 +311,71 @@ void cmdEcho(const consolecmdexec_t *exec) { // May move these later void consoleUpdate() { - if(inputIsPressed(INPUT_TOGGLE_CONSOLE)) { - CONSOLE.open = !CONSOLE.open; - } else if(CONSOLE.open) { - switch(INPUT.keyPressed) { - case 0: - break; - - case KEY_ENTER: - consoleExec(CONSOLE.inputBuffer); - CONSOLE.inputIndex = 0; - CONSOLE.inputBuffer[0] = '\0'; - break; - - case KEY_BACKSPACE: - if(CONSOLE.inputIndex > 0) { - CONSOLE.inputIndex--; - CONSOLE.inputBuffer[CONSOLE.inputIndex] = '\0'; - } - break; - - default: - if( - INPUT.keyPressed >= 32 && - INPUT.keyPressed <= 126 && - CONSOLE.inputIndex < CONSOLE_LINE_MAX - 1 - ) { - CONSOLE.inputBuffer[CONSOLE.inputIndex++] = INPUT.charPressed; - CONSOLE.inputBuffer[CONSOLE.inputIndex] = '\0'; - } - break; - } + for(uint32_t i = 0; i < CONSOLE.execBufferCount; i++) { + consolecmdexec_t *exec = &CONSOLE.execBuffer[i]; + assertNotNull(exec->cmd, "Command execution has no command."); + exec->cmd->function(exec); } - consoleProcess(); + // #if DUSK_KEYBOARD_SUPPORT == 1 + // uint8_t key; + // while((key = inputKeyboardPop()) != 0) { + // printf("Key pressed: %c\n", key); + // switch(key) { + // case 0: + // break; + + // case INPUT_KEY_ENTER: + // consoleExec(CONSOLE.inputBuffer); + // CONSOLE.inputIndex = 0; + // CONSOLE.inputBuffer[0] = '\0'; + // break; + + // case INPUT_KEY_BACKSPACE: + // if(CONSOLE.inputIndex > 0) { + // CONSOLE.inputIndex--; + // CONSOLE.inputBuffer[CONSOLE.inputIndex] = '\0'; + // } + // break; + + // default: + // if( + // key >= INPUT_KEY_ASCII_START && key <= INPUT_KEY_ASCII_END && + // CONSOLE.inputIndex < CONSOLE_LINE_MAX - 1 + // ) { + // CONSOLE.inputBuffer[CONSOLE.inputIndex++] = key; + // CONSOLE.inputBuffer[CONSOLE.inputIndex] = '\0'; + // } + // break; + + // } + // } + // #endif + + // Clear the exec buffer + CONSOLE.execBufferCount = 0; } -void consoleDraw() { - if(!CONSOLE.open) return; +// void consoleDraw() { +// if(!CONSOLE.open) return; - size_t i = 0; - char_t *line; - int32_t fontSize = 10; - do { - line = CONSOLE.line[i]; - if(line[0] == '\0') { - i++; - continue; - } - DrawText(line, 0, i*fontSize, fontSize, YELLOW); - i++; - } while(i < CONSOLE_HISTORY_MAX); +// size_t i = 0; +// char_t *line; +// int32_t fontSize = 10; +// do { +// line = CONSOLE.line[i]; +// if(line[0] == '\0') { +// i++; +// continue; +// } +// DrawText(line, 0, i*fontSize, fontSize, YELLOW); +// i++; +// } while(i < CONSOLE_HISTORY_MAX); - DrawText( - CONSOLE.inputBuffer, 0, - (CONSOLE_HISTORY_MAX + 1) * fontSize, - fontSize, - PINK - ); -} \ No newline at end of file +// DrawText( +// CONSOLE.inputBuffer, 0, +// (CONSOLE_HISTORY_MAX + 1) * fontSize, +// fontSize, +// PINK +// ); +// } \ No newline at end of file diff --git a/src/dusk/console/console.h b/src/dusk/console/console.h index 59ee0e8..b09b248 100644 --- a/src/dusk/console/console.h +++ b/src/dusk/console/console.h @@ -37,13 +37,14 @@ typedef struct { consolecmd_t *cmdGet; consolecmd_t *cmdSet; - pthread_mutex_t lock; // Mutex for thread safety + + bool_t visible; // May move these later - char_t inputBuffer[CONSOLE_LINE_MAX]; - int32_t inputIndex; - - bool_t open; + // #if DUSK_KEYBOARD_SUPPORT == 1 + // char_t inputBuffer[CONSOLE_LINE_MAX]; + // int32_t inputIndex; + // #endif } console_t; extern console_t CONSOLE; @@ -97,18 +98,8 @@ void consoleExec(const char_t *line); /** * Processes the console's pending commands. */ -void consoleProcess(); - -/** - * Updates the console's input buffer and handles user input. - */ void consoleUpdate(); -/** - * Draws the console's output. - */ -void consoleDraw(); - void cmdGet(const consolecmdexec_t *exec); void cmdSet(const consolecmdexec_t *exec); void cmdEcho(const consolecmdexec_t *exec); \ No newline at end of file diff --git a/src/dusk/dusk.h b/src/dusk/dusk.h index 3bbf93a..5f6c373 100644 --- a/src/dusk/dusk.h +++ b/src/dusk/dusk.h @@ -12,6 +12,9 @@ #include #include #include +#include +#include +#include typedef bool bool_t; typedef int int_t; diff --git a/src/dusk/game.c b/src/dusk/game.c index 0c49948..d02d7df 100644 --- a/src/dusk/game.c +++ b/src/dusk/game.c @@ -13,8 +13,10 @@ #include "input.h" #include "event/event.h" #include "ui/uitextbox.h" +#include "console/console.h" void gameInit(void) { + consoleInit(); inputInit(); eventInit(); uiTextboxInit(); @@ -28,6 +30,7 @@ void gameUpdate(void) { uiTextboxUpdate(); eventUpdate(); + consoleUpdate(); inputUpdate(); } diff --git a/src/dusk/game.h b/src/dusk/game.h index e117ea9..cff44a6 100644 --- a/src/dusk/game.h +++ b/src/dusk/game.h @@ -20,6 +20,7 @@ * - If your system handles time dynamically, it should be ready to be used. * * The systems called (in order) are; + * - Console. * - Input system (Not the platforms input, but the game's input system). * - Time system (if applicable). * - Event System diff --git a/src/dusk/input.c b/src/dusk/input.c index f4654e8..2c1720a 100644 --- a/src/dusk/input.c +++ b/src/dusk/input.c @@ -18,6 +18,11 @@ void inputInit(void) { void inputUpdate(void) { INPUT.previous = INPUT.current; INPUT.current = inputStateGet(); + + #if DUSK_KEYBOARD_SUPPORT == 1 + + + #endif } bool_t inputIsDown(const uint8_t bind) { diff --git a/src/dusk/input.h b/src/dusk/input.h index 5a28a28..0fc2bc0 100644 --- a/src/dusk/input.h +++ b/src/dusk/input.h @@ -18,7 +18,7 @@ typedef struct { uint8_t current; - uint8_t previous; + uint8_t previous; } input_t; extern input_t INPUT; diff --git a/src/duskraylib/CMakeLists.txt b/src/duskraylib/CMakeLists.txt index ff85a44..0bc094a 100644 --- a/src/duskraylib/CMakeLists.txt +++ b/src/duskraylib/CMakeLists.txt @@ -3,6 +3,12 @@ # This software is released under the MIT License. # https://opensource.org/licenses/MIT +# Compile defs +target_compile_definitions(${DUSK_TARGET_NAME} + PUBLIC + # DUSK_KEYBOARD_SUPPORT=1 +) + # Libs find_package(raylib REQUIRED) @@ -11,6 +17,7 @@ target_link_libraries(${DUSK_TARGET_NAME} raylib ) + # Includes target_include_directories(${DUSK_TARGET_NAME} PUBLIC @@ -21,6 +28,7 @@ target_include_directories(${DUSK_TARGET_NAME} target_sources(${DUSK_TARGET_NAME} PRIVATE input.c + main.c ) # Subdirs diff --git a/src/duskraylib/display/draw/CMakeLists.txt b/src/duskraylib/display/draw/CMakeLists.txt index f01b9ec..47e66bf 100644 --- a/src/duskraylib/display/draw/CMakeLists.txt +++ b/src/duskraylib/display/draw/CMakeLists.txt @@ -9,4 +9,5 @@ target_sources(${DUSK_TARGET_NAME} drawscene.c drawoverworld.c drawui.c + drawconsole.c ) \ No newline at end of file diff --git a/src/duskraylib/display/draw/drawconsole.c b/src/duskraylib/display/draw/drawconsole.c new file mode 100644 index 0000000..c439ee9 --- /dev/null +++ b/src/duskraylib/display/draw/drawconsole.c @@ -0,0 +1,33 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "drawconsole.h" +#include "console/console.h" + +void drawConsoleInit(void) { +} + +void drawConsoleDraw(void) { + CONSOLE.visible = true; // Set console to visible by default + if(!CONSOLE.visible) return; + + BeginBlendMode(BLEND_ALPHA); + size_t i = 0; + char_t *line; + int32_t fontSize = 10; + do { + line = CONSOLE.line[i]; + if(line[0] == '\0') { + i++; + continue; + } + DrawText(line, 0, i * fontSize, fontSize, ORANGE); + i++; + } while(i < CONSOLE_HISTORY_MAX); + + EndBlendMode(); +} \ No newline at end of file diff --git a/src/duskraylib/display/draw/drawconsole.h b/src/duskraylib/display/draw/drawconsole.h new file mode 100644 index 0000000..2016a20 --- /dev/null +++ b/src/duskraylib/display/draw/drawconsole.h @@ -0,0 +1,19 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "duskraylib.h" + +/** + * Initializes the console drawing system. + */ +void drawConsoleInit(void); + +/** + * Draws the console to the screen. + */ +void drawConsoleDraw(void); \ No newline at end of file diff --git a/src/duskraylib/display/render.c b/src/duskraylib/display/render.c index 31e1306..70d0ac4 100644 --- a/src/duskraylib/display/render.c +++ b/src/duskraylib/display/render.c @@ -9,6 +9,7 @@ #include "display/draw/drawscene.h" #include "display/draw/drawoverworld.h" #include "display/draw/drawui.h" +#include "display/draw/drawconsole.h" #include "assert/assert.h" RenderTexture2D RENDER_SCREEN_TEXTURE; @@ -36,6 +37,7 @@ void renderInit(void) { drawOverworldInit(); drawUIInit(); + drawConsoleInit(); } void renderDraw(void) { @@ -75,10 +77,11 @@ void renderDraw(void) { 0.0f, WHITE ); + + drawConsoleDraw(); EndDrawing(); EndBlendMode(); - } void renderDispose(void) { diff --git a/src/duskraylib/input.c b/src/duskraylib/input.c index 3642592..4a9e488 100644 --- a/src/duskraylib/input.c +++ b/src/duskraylib/input.c @@ -18,6 +18,11 @@ typedef struct { uint8_t bind; } inputgpmap_t; +typedef struct { + int32_t key; + uint32_t bind; +} inputkeyboardmap_t; + inputkbmap_t INPUT_KB_MAP[] = { { KEY_UP, INPUT_BIND_UP }, { KEY_W, INPUT_BIND_UP }, @@ -71,4 +76,14 @@ uint8_t inputStateGet() { } return state; -} \ No newline at end of file +} + +// uint32_t inputKeyboardPop(void) { +// while(true) { +// int32_t key = GetKeyPressed(); +// if(key == 0) return 0; +// printf("Got key: %d\n", key); +// return (uint32_t)key; +// } +// return 0; +// } \ No newline at end of file diff --git a/src/duskraylib/main.c b/src/duskraylib/main.c index f60b3ac..321bdb0 100644 --- a/src/duskraylib/main.c +++ b/src/duskraylib/main.c @@ -9,6 +9,7 @@ #include "game.h" void main(void) { + renderInit(); gameInit(); while(!WindowShouldClose()) {