Beginning input refactor

This commit is contained in:
2025-09-03 11:57:48 -05:00
parent 059ccf41b6
commit 3f37b7cdb5
19 changed files with 320 additions and 137 deletions

View File

@@ -6,6 +6,7 @@
# Sources
target_sources(${DUSK_TARGET_NAME}
PRIVATE
rendertext.c
uitext.c
ui.c
uiconsole.c
)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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