Update console
This commit is contained in:
@@ -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
|
||||
// );
|
||||
// }
|
@@ -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);
|
@@ -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;
|
||||
|
@@ -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();
|
||||
}
|
||||
|
||||
|
@@ -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
|
||||
|
@@ -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) {
|
||||
|
@@ -18,7 +18,7 @@
|
||||
|
||||
typedef struct {
|
||||
uint8_t current;
|
||||
uint8_t previous;
|
||||
uint8_t previous;
|
||||
} input_t;
|
||||
|
||||
extern input_t INPUT;
|
||||
|
@@ -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
|
||||
|
@@ -9,4 +9,5 @@ target_sources(${DUSK_TARGET_NAME}
|
||||
drawscene.c
|
||||
drawoverworld.c
|
||||
drawui.c
|
||||
drawconsole.c
|
||||
)
|
33
src/duskraylib/display/draw/drawconsole.c
Normal file
33
src/duskraylib/display/draw/drawconsole.c
Normal 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();
|
||||
}
|
19
src/duskraylib/display/draw/drawconsole.h
Normal file
19
src/duskraylib/display/draw/drawconsole.h
Normal 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);
|
@@ -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) {
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
// }
|
@@ -9,6 +9,7 @@
|
||||
#include "game.h"
|
||||
|
||||
void main(void) {
|
||||
renderInit();
|
||||
gameInit();
|
||||
|
||||
while(!WindowShouldClose()) {
|
||||
|
Reference in New Issue
Block a user