Update console

This commit is contained in:
2025-08-06 11:26:25 -05:00
parent d68fae0db7
commit 24fcf87fb4
14 changed files with 163 additions and 94 deletions

View File

@@ -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
);
}
// DrawText(
// CONSOLE.inputBuffer, 0,
// (CONSOLE_HISTORY_MAX + 1) * fontSize,
// fontSize,
// PINK
// );
// }

View File

@@ -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);

View File

@@ -12,6 +12,9 @@
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <errno.h>
#include <ctype.h>
#include <stdarg.h>
typedef bool bool_t;
typedef int int_t;

View File

@@ -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();
}

View File

@@ -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

View File

@@ -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) {

View File

@@ -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

View File

@@ -9,4 +9,5 @@ target_sources(${DUSK_TARGET_NAME}
drawscene.c
drawoverworld.c
drawui.c
drawconsole.c
)

View File

@@ -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();
}

View File

@@ -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);

View File

@@ -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) {
@@ -76,9 +78,10 @@ void renderDraw(void) {
WHITE
);
drawConsoleDraw();
EndDrawing();
EndBlendMode();
}
void renderDispose(void) {

View File

@@ -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 },
@@ -72,3 +77,13 @@ uint8_t inputStateGet() {
return state;
}
// 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;
// }

View File

@@ -9,6 +9,7 @@
#include "game.h"
void main(void) {
renderInit();
gameInit();
while(!WindowShouldClose()) {