From b89ae2391b82fb8693e95b71d42fba86911a8d1a Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Mon, 20 Apr 2026 14:31:22 -0500 Subject: [PATCH] Reg console. --- assets/init.lua | 5 +- .../cmd/{cmdtoggleconsole.h => cmdtime.h} | 5 +- src/dusk/console/console.c | 66 ++++++--- src/dusk/console/console.h | 38 +++++- src/dusk/console/consoledefs.h | 2 +- src/dusk/input/input.csv | 3 +- src/dusk/scene/scene.c | 127 +++++++++--------- 7 files changed, 160 insertions(+), 86 deletions(-) rename src/dusk/console/cmd/{cmdtoggleconsole.h => cmdtime.h} (63%) diff --git a/assets/init.lua b/assets/init.lua index d7875417..2e2ed46d 100644 --- a/assets/init.lua +++ b/assets/init.lua @@ -16,6 +16,7 @@ if PSP then inputBind("lstick_down", INPUT_ACTION_DOWN) inputBind("lstick_left", INPUT_ACTION_LEFT) inputBind("lstick_right", INPUT_ACTION_RIGHT) + inputBind("triangle", INPUT_ACTION_CONSOLE) elseif DOLPHIN then inputBind("up", INPUT_ACTION_UP) @@ -24,7 +25,8 @@ elseif DOLPHIN then inputBind("right", INPUT_ACTION_RIGHT) inputBind("b", INPUT_ACTION_CANCEL) inputBind("a", INPUT_ACTION_ACCEPT) - inputBind("z", INPUT_ACTION_RAGEQUIT) + -- inputBind("z", INPUT_ACTION_RAGEQUIT) + inputBind("z", INPUT_ACTION_CONSOLE) inputBind("lstick_up", INPUT_ACTION_UP) inputBind("lstick_down", INPUT_ACTION_DOWN) inputBind("lstick_left", INPUT_ACTION_LEFT) @@ -48,6 +50,7 @@ elseif LINUX then inputBind("q", INPUT_ACTION_CANCEL) inputBind("escape", INPUT_ACTION_RAGEQUIT) + inputBind("`", INPUT_ACTION_CONSOLE) end if INPUT_GAMEPAD then diff --git a/src/dusk/console/cmd/cmdtoggleconsole.h b/src/dusk/console/cmd/cmdtime.h similarity index 63% rename from src/dusk/console/cmd/cmdtoggleconsole.h rename to src/dusk/console/cmd/cmdtime.h index af366cd9..5463bd2d 100644 --- a/src/dusk/console/cmd/cmdtoggleconsole.h +++ b/src/dusk/console/cmd/cmdtime.h @@ -7,7 +7,8 @@ #pragma once #include "console/console.h" +#include "time/time.h" -void cmdToggleConsole(const consolecmdexec_t *exec) { - CONSOLE.visible = !CONSOLE.visible; +void cmdTime(const consolecmdexec_t *exec) { + consolePrint("Time: %.2f", TIME.time); } \ No newline at end of file diff --git a/src/dusk/console/console.c b/src/dusk/console/console.c index de8b1d87..e8b31983 100644 --- a/src/dusk/console/console.c +++ b/src/dusk/console/console.c @@ -18,6 +18,8 @@ #include "console/cmd/cmdget.h" #include "console/cmd/cmdquit.h" #include "console/cmd/cmdbind.h" +#include "console/cmd/cmdtime.h" + #include "display/shader/shaderunlit.h" #include "display/text/text.h" #include "display/spritebatch/spritebatch.h" @@ -28,9 +30,11 @@ void consoleInit() { memoryZero(&CONSOLE, sizeof(console_t)); // Register vars - consoleRegVar("fps", "0", NULL); + #define REG(name, value) consoleRegVar(name, value, NULL) + REG("console", "0"); + #undef REG - // Register cmds + // Register cmdss CONSOLE.cmdGet = consoleRegCmd("get", cmdGet); CONSOLE.cmdSet = consoleRegCmd("set", cmdSet); @@ -39,6 +43,7 @@ void consoleInit() { REG("quit", cmdQuit); REG("exit", cmdQuit); REG("bind", cmdBind); + REG("time", cmdTime); #undef REG #ifdef DUSK_CONSOLE_POSIX @@ -86,6 +91,31 @@ consolevar_t * consoleVarGet(const char_t *name) { return NULL; } +int32_t consoleVarGetInt(const char_t *name, const int32_t initial) { + consolevar_t *var = consoleVarGet(name); + if(var == NULL) return initial; + char *end; + int32_t value = (int32_t)strtol(var->value, &end, 10); + if(*end != '\0') return initial; + return value; +} + +bool_t consoleVarGetBool(const char_t *name, const bool_t initial) { + return consoleVarGetInt(name, initial ? 1 : 0) != 0; +} + +void consoleVarSetInt(const char_t *name, const int32_t value) { + consolevar_t *existing = consoleVarGet(name); + assertNotNull(existing, "Variable not found"); + char_t buffer[CONSOLE_VAR_VALUE_MAX]; + stringFormat(buffer, CONSOLE_VAR_VALUE_MAX, "%d", value); + consoleVarSetValue(existing, buffer); +} + +void consoleVarSetBool(const char_t *name, const bool_t value) { + consoleVarSetInt(name, value ? 1 : 0); +} + void consolePrint(const char_t *message, ...) { char_t buffer[CONSOLE_LINE_MAX]; @@ -337,14 +367,18 @@ void consoleExec(const char_t *line) { } void consoleUpdate() { + #ifdef DUSK_TIME_DYNAMIC + if(TIME.dynamicUpdate) return; + #endif + #ifdef DUSK_CONSOLE_POSIX threadMutexLock(&CONSOLE.execMutex); #endif // Toggle console - // if(inputPressed(INPUT_ACTION_CONSOLE)) { - // CONSOLE.visible = !CONSOLE.visible; - // } + if(inputPressed(INPUT_ACTION_CONSOLE)) { + consoleVarSetBool("console", !consoleVarGetBool("console", false)); + } // Anything to exec? if(CONSOLE.execBufferCount == 0) { @@ -379,21 +413,19 @@ void consoleUpdate() { } errorret_t consoleDraw() { - if(!CONSOLE.visible) { + if(!consoleVarGetBool("console", false)) { errorOk(); } - errorChain(shaderSetTexture( - &SHADER_UNLIT, SHADER_UNLIT_TEXTURE, &DEFAULT_FONT_TEXTURE - )); - errorChain(shaderSetColor(&SHADER_UNLIT, SHADER_UNLIT_COLOR, COLOR_WHITE)); - errorChain(textDraw( - 32, 32, - "Hello World", - COLOR_WHITE, - &DEFAULT_FONT_TILESET, - &DEFAULT_FONT_TEXTURE - )); + for(uint32_t i = 0; i < CONSOLE_HISTORY_MAX; i++) { + errorChain(textDraw( + 0, DEFAULT_FONT_TILESET.tileHeight * i, + CONSOLE.line[i], + COLOR_WHITE, + &DEFAULT_FONT_TILESET, + &DEFAULT_FONT_TEXTURE + )); + } errorChain(spriteBatchFlush()); errorOk(); } diff --git a/src/dusk/console/console.h b/src/dusk/console/console.h index e58b942b..c7a8a503 100644 --- a/src/dusk/console/console.h +++ b/src/dusk/console/console.h @@ -46,8 +46,6 @@ typedef struct { consolecmd_t *cmdGet; consolecmd_t *cmdSet; - bool_t visible; - #ifdef DUSK_CONSOLE_POSIX char_t inputBuffer[CONSOLE_LINE_MAX]; thread_t thread; @@ -94,6 +92,42 @@ consolevar_t * consoleRegVar( */ consolevar_t * consoleVarGet(const char_t *name); +/** + * Gets the value of a console variable as an int32_t. + * + * @param name The name of the variable. + * @param initial The default value to return if the variable is not found or + * invalid. + * @return The value of the variable, or default if not found or invalid. + */ +int32_t consoleVarGetInt(const char_t *name, const int32_t initial); + +/** + * Gets the value of a console variable as a bool_t. + * + * @param name The name of the variable. + * @param initial The default value to return if the variable is not found or + * invalid. + * @return The value of the variable, or default if not found or invalid. + */ +bool_t consoleVarGetBool(const char_t *name, const bool_t initial); + +/** + * Sets the value of a console variable as an int32_t. + * + * @param name The name of the variable. + * @param value The new value of the variable. + */ +void consoleVarSetInt(const char_t *name, const int32_t value); + +/** + * Sets the value of a console variable as a bool_t. + * + * @param name The name of the variable. + * @param value The new value of the variable. + */ +void consoleVarSetBool(const char_t *name, const bool_t value); + /** * Prints a message to the console. * diff --git a/src/dusk/console/consoledefs.h b/src/dusk/console/consoledefs.h index 6a7e2bb6..c0a8773c 100644 --- a/src/dusk/console/consoledefs.h +++ b/src/dusk/console/consoledefs.h @@ -18,5 +18,5 @@ #define CONSOLE_ALIAS_MAX 32 #define CONSOLE_VAR_NAME_MAX 32 -#define CONSOLE_VAR_VALUE_MAX 128 +#define CONSOLE_VAR_VALUE_MAX 32 #define CONSOLE_VAR_EVENTS_MAX 8 diff --git a/src/dusk/input/input.csv b/src/dusk/input/input.csv index f3418c63..42555928 100644 --- a/src/dusk/input/input.csv +++ b/src/dusk/input/input.csv @@ -5,6 +5,7 @@ LEFT, RIGHT, ACCEPT, CANCEL, -RAGEQUIT +RAGEQUIT, +CONSOLE, POINTERX, POINTERY, \ No newline at end of file diff --git a/src/dusk/scene/scene.c b/src/dusk/scene/scene.c index de8d54a9..57e7c3f9 100644 --- a/src/dusk/scene/scene.c +++ b/src/dusk/scene/scene.c @@ -41,76 +41,80 @@ errorret_t sceneRender(void) { entityid_t camCount = componentGetEntitiesWithComponent( COMPONENT_TYPE_CAMERA, camEnts, camComps ); - if(camCount == 0) errorOk(); - // Get meshes - entityid_t meshEnts[ENTITY_COUNT_MAX]; - componentid_t meshComps[ENTITY_COUNT_MAX]; - entityid_t meshCount = componentGetEntitiesWithComponent( - COMPONENT_TYPE_MESH, meshEnts, meshComps - ); - if(meshCount == 0) errorOk(); // Prep Matricies mat4 view, proj, model; - errorChain(shaderBind(&SHADER_UNLIT)); - // For each camera. - for(entityid_t camIndex = 0; camIndex < camCount; camIndex++) { - entityid_t camEnt = camEnts[camIndex]; - componentid_t camComp = camComps[camIndex]; - componentid_t camPos = entityGetComponent(camEnt, COMPONENT_TYPE_POSITION); - if(camPos == 0xFF) { - logError("Camera entity without entity position found\n"); - continue; - } + if(camCount > 0) { + // Get meshes + entityid_t meshEnts[ENTITY_COUNT_MAX]; + componentid_t meshComps[ENTITY_COUNT_MAX]; + entityid_t meshCount = componentGetEntitiesWithComponent( + COMPONENT_TYPE_MESH, meshEnts, meshComps + ); - entityCameraGetProjection(camEnt, camComp, proj); - entityPositionGetTransform(camEnt, camPos, view); + if(meshCount > 0) { + errorChain(shaderBind(&SHADER_UNLIT)); - // For each mesh. - for(entityid_t meshIndex = 0; meshIndex < meshCount; meshIndex++) { - entityid_t meshEnt = meshEnts[meshIndex]; + // For each camera. + for(entityid_t camIndex = 0; camIndex < camCount; camIndex++) { + entityid_t camEnt = camEnts[camIndex]; + componentid_t camComp = camComps[camIndex]; + componentid_t camPos = entityGetComponent(camEnt, COMPONENT_TYPE_POSITION); + if(camPos == 0xFF) { + logError("Camera entity without entity position found\n"); + continue; + } - componentid_t meshComp = meshComps[meshIndex]; - mesh_t *mesh = entityMeshGetMesh(meshEnt, meshComp); - if(mesh == NULL) { - continue; + entityCameraGetProjection(camEnt, camComp, proj); + entityPositionGetTransform(camEnt, camPos, view); + + // For each mesh. + for(entityid_t meshIndex = 0; meshIndex < meshCount; meshIndex++) { + entityid_t meshEnt = meshEnts[meshIndex]; + + componentid_t meshComp = meshComps[meshIndex]; + mesh_t *mesh = entityMeshGetMesh(meshEnt, meshComp); + if(mesh == NULL) { + continue; + } + + componentid_t meshPos = entityGetComponent( + meshEnt, COMPONENT_TYPE_POSITION + ); + if(meshPos == 0xFF) { + logError("Mesh entity without entity position found\n"); + continue; + } + + componentid_t meshMat = entityGetComponent( + meshEnt, COMPONENT_TYPE_MATERIAL + ); + if(meshMat == 0xFF) { + logError("Mesh entity without material component found\n"); + continue; + } + + shadermaterial_t *material = entityMaterialGetShaderMaterial( + meshEnt, meshMat + ); + shader_t *shader = entityMaterialGetShader(meshEnt, meshMat); + if(shader == NULL) { + logError("Mesh entity with material component without shader found\n"); + continue; + } + + entityPositionGetTransform(meshEnt, meshPos, model); + + errorChain(shaderBind(shader)); + errorChain(shaderSetMatrix(shader, SHADER_UNLIT_PROJECTION, proj)); + errorChain(shaderSetMatrix(shader, SHADER_UNLIT_VIEW, view)); + errorChain(shaderSetMatrix(shader, SHADER_UNLIT_MODEL, model)); + errorChain(shaderSetMaterial(shader, material)); + errorChain(meshDraw(mesh, 0, -1)); + } } - - componentid_t meshPos = entityGetComponent( - meshEnt, COMPONENT_TYPE_POSITION - ); - if(meshPos == 0xFF) { - logError("Mesh entity without entity position found\n"); - continue; - } - - componentid_t meshMat = entityGetComponent( - meshEnt, COMPONENT_TYPE_MATERIAL - ); - if(meshMat == 0xFF) { - logError("Mesh entity without material component found\n"); - continue; - } - - shadermaterial_t *material = entityMaterialGetShaderMaterial( - meshEnt, meshMat - ); - shader_t *shader = entityMaterialGetShader(meshEnt, meshMat); - if(shader == NULL) { - logError("Mesh entity with material component without shader found\n"); - continue; - } - - entityPositionGetTransform(meshEnt, meshPos, model); - - errorChain(shaderBind(shader)); - errorChain(shaderSetMatrix(shader, SHADER_UNLIT_PROJECTION, proj)); - errorChain(shaderSetMatrix(shader, SHADER_UNLIT_VIEW, view)); - errorChain(shaderSetMatrix(shader, SHADER_UNLIT_MODEL, model)); - errorChain(shaderSetMaterial(shader, material)); - errorChain(meshDraw(mesh, 0, -1)); } } @@ -133,7 +137,6 @@ errorret_t sceneRender(void) { errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_PROJECTION, proj)); errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_VIEW, view)); errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_MODEL, model)); - errorChain(consoleDraw()); errorOk();