Game updates
This commit is contained in:
12
src/ui/CMakeLists.txt
Normal file
12
src/ui/CMakeLists.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
# Copyright (c) 2025 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DUSK_TARGET_NAME}
|
||||
PRIVATE
|
||||
uitext.c
|
||||
uifps.c
|
||||
uiconsole.c
|
||||
)
|
||||
31
src/ui/uiconsole.c
Normal file
31
src/ui/uiconsole.c
Normal file
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* 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(const tileset_t *tileset, texture_t *texture) {
|
||||
if(!CONSOLE.visible) return;
|
||||
consolePrint("Test\n");
|
||||
|
||||
int32_t i = CONSOLE_HISTORY_MAX - 1;
|
||||
char_t *line;
|
||||
do {
|
||||
line = CONSOLE.line[i];
|
||||
if(line[0] == '\0') {
|
||||
i--;
|
||||
continue;
|
||||
}
|
||||
uiTextDraw(
|
||||
0, i * TILESET_MINOGRAM.tileHeight,
|
||||
line, COLOR_WHITE,
|
||||
tileset, texture
|
||||
);
|
||||
i--;
|
||||
} while(i > 0);
|
||||
}
|
||||
18
src/ui/uiconsole.h
Normal file
18
src/ui/uiconsole.h
Normal file
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "display/tileset.h"
|
||||
#include "display/texture/texture.h"
|
||||
|
||||
/**
|
||||
* Renders the console UI.
|
||||
*
|
||||
* @param tileset The tileset to use for rendering text.
|
||||
* @param texture The texture associated with the tileset.
|
||||
*/
|
||||
void uiConsoleRender(const tileset_t *tileset, texture_t *texture);
|
||||
36
src/ui/uifps.c
Normal file
36
src/ui/uifps.c
Normal file
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "uifps.h"
|
||||
#include "time/time.h"
|
||||
#include "console/console.h"
|
||||
#include "util/string.h"
|
||||
#include "ui/uitext.h"
|
||||
|
||||
void uiFPSRender(const tileset_t *tileset, texture_t *texture) {
|
||||
if(stringCompare(consoleVarGet("fps")->value, "0") == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
float_t fps = TIME.delta > 0.0f ? (1.0f / TIME.delta) : 0.0f;
|
||||
char_t buffer[64];
|
||||
snprintf(
|
||||
buffer,
|
||||
sizeof(buffer),
|
||||
"%.2f/%d",
|
||||
TIME.delta * 1000.0f,
|
||||
(int32_t)fps
|
||||
);
|
||||
|
||||
color_t color = (
|
||||
fps >= 50.0f ? COLOR_GREEN :
|
||||
fps >= 30.0f ? COLOR_YELLOW :
|
||||
COLOR_RED
|
||||
);
|
||||
|
||||
uiTextDraw(0, 0, buffer, color, tileset, texture);
|
||||
}
|
||||
18
src/ui/uifps.h
Normal file
18
src/ui/uifps.h
Normal file
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "display/tileset.h"
|
||||
#include "display/texture/texture.h"
|
||||
|
||||
/**
|
||||
* Renders the FPS counter UI.
|
||||
*
|
||||
* @param tileset The tileset to use for rendering text.
|
||||
* @param texture The texture associated with the tileset.
|
||||
*/
|
||||
void uiFPSRender(const tileset_t *tileset, texture_t *texture);
|
||||
112
src/ui/uitext.c
Normal file
112
src/ui/uitext.c
Normal file
@@ -0,0 +1,112 @@
|
||||
// /**
|
||||
// * Copyright (c) 2025 Dominic Masters
|
||||
// *
|
||||
// * This software is released under the MIT License.
|
||||
// * https://opensource.org/licenses/MIT
|
||||
// */
|
||||
|
||||
#include "uitext.h"
|
||||
#include "asset/assetmanager.h"
|
||||
#include "assert/assert.h"
|
||||
#include "util/memory.h"
|
||||
#include "display/spritebatch/spritebatch.h"
|
||||
|
||||
void uiTextDrawChar(
|
||||
const float_t x,
|
||||
const float_t y,
|
||||
const char_t c,
|
||||
const color_t color,
|
||||
const tileset_t *tileset,
|
||||
texture_t *texture
|
||||
) {
|
||||
int32_t tileIndex = (int32_t)(c) - UI_TEXT_CHAR_START;
|
||||
if(tileIndex < 0 || tileIndex >= tileset->tileCount) {
|
||||
tileIndex = ((int32_t)'@') - UI_TEXT_CHAR_START;
|
||||
}
|
||||
|
||||
assertTrue(
|
||||
tileIndex >= 0 && tileIndex <= tileset->tileCount,
|
||||
"Character is out of bounds for font tiles"
|
||||
);
|
||||
|
||||
vec4 uv;
|
||||
tilesetTileGetUV(tileset, tileIndex, uv);
|
||||
|
||||
spriteBatchPush(
|
||||
texture,
|
||||
x, y,
|
||||
x + tileset->tileWidth,
|
||||
y + tileset->tileHeight,
|
||||
color,
|
||||
uv[0], uv[1], uv[2], uv[3]
|
||||
);
|
||||
}
|
||||
|
||||
void uiTextDraw(
|
||||
const float_t x,
|
||||
const float_t y,
|
||||
const char_t *text,
|
||||
const color_t color,
|
||||
const tileset_t *tileset,
|
||||
texture_t *texture
|
||||
) {
|
||||
assertNotNull(text, "Text cannot be NULL");
|
||||
|
||||
float_t posX = x;
|
||||
float_t posY = y;
|
||||
|
||||
char_t c;
|
||||
int32_t i = 0;
|
||||
while((c = text[i++]) != '\0') {
|
||||
if(c == '\n') {
|
||||
posX = x;
|
||||
posY += tileset->tileHeight;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(c == ' ') {
|
||||
posX += tileset->tileWidth;
|
||||
continue;
|
||||
}
|
||||
|
||||
uiTextDrawChar(posX, posY, c, color, tileset, texture);
|
||||
posX += tileset->tileWidth;
|
||||
}
|
||||
}
|
||||
|
||||
void uiTextMeasure(
|
||||
const char_t *text,
|
||||
const tileset_t *tileset,
|
||||
int32_t *outWidth,
|
||||
int32_t *outHeight
|
||||
) {
|
||||
assertNotNull(text, "Text cannot be NULL");
|
||||
assertNotNull(outWidth, "Output width pointer cannot be NULL");
|
||||
assertNotNull(outHeight, "Output height pointer cannot be NULL");
|
||||
|
||||
int32_t width = 0;
|
||||
int32_t height = tileset->tileHeight;
|
||||
int32_t lineWidth = 0;
|
||||
|
||||
char_t c;
|
||||
int32_t i = 0;
|
||||
while((c = text[i++]) != '\0') {
|
||||
if(c == '\n') {
|
||||
if(lineWidth > width) {
|
||||
width = lineWidth;
|
||||
}
|
||||
lineWidth = 0;
|
||||
height += tileset->tileHeight;
|
||||
continue;
|
||||
}
|
||||
|
||||
lineWidth += tileset->tileWidth;
|
||||
}
|
||||
|
||||
if(lineWidth > width) {
|
||||
width = lineWidth;
|
||||
}
|
||||
|
||||
*outWidth = width;
|
||||
*outHeight = height;
|
||||
}
|
||||
65
src/ui/uitext.h
Normal file
65
src/ui/uitext.h
Normal file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "asset/assetmanager.h"
|
||||
#include "display/tileset/tileset_minogram.h"
|
||||
|
||||
#define UI_TEXT_CHAR_START '!'
|
||||
|
||||
/**
|
||||
* Draws a single character at the specified position.
|
||||
*
|
||||
* @param x The x-coordinate to draw the character at.
|
||||
* @param y The y-coordinate to draw the character at.
|
||||
* @param c The character to draw.
|
||||
* @param color The color to draw the character in.
|
||||
* @param tileset Font tileset to use for rendering.
|
||||
* @param texture Texture containing the font tileset image.
|
||||
*/
|
||||
void uiTextDrawChar(
|
||||
const float_t x,
|
||||
const float_t y,
|
||||
const char_t c,
|
||||
const color_t color,
|
||||
const tileset_t *tileset,
|
||||
texture_t *texture
|
||||
);
|
||||
|
||||
/**
|
||||
* Draws a string of text at the specified position.
|
||||
*
|
||||
* @param x The x-coordinate to draw the text at.
|
||||
* @param y The y-coordinate to draw the text at.
|
||||
* @param text The null-terminated string of text to draw.
|
||||
* @param color The color to draw the text in.
|
||||
* @param tileset Font tileset to use for rendering.
|
||||
* @param texture Texture containing the font tileset image.
|
||||
*/
|
||||
void uiTextDraw(
|
||||
const float_t x,
|
||||
const float_t y,
|
||||
const char_t *text,
|
||||
const color_t color,
|
||||
const tileset_t *tileset,
|
||||
texture_t *texture
|
||||
);
|
||||
|
||||
/**
|
||||
* Measures the width and height of the given text string when rendered.
|
||||
*
|
||||
* @param text The null-terminated string of text to measure.
|
||||
* @param tileset Font tileset to use for measurement.
|
||||
* @param outWidth Pointer to store the measured width in pixels.
|
||||
* @param outHeight Pointer to store the measured height in pixels.
|
||||
*/
|
||||
void uiTextMeasure(
|
||||
const char_t *text,
|
||||
const tileset_t *tileset,
|
||||
int32_t *outWidth,
|
||||
int32_t *outHeight
|
||||
);
|
||||
Reference in New Issue
Block a user