archivemuh
This commit is contained in:
14
archive/dusksdl2/display/ui/CMakeLists.txt
Normal file
14
archive/dusksdl2/display/ui/CMakeLists.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
# 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
|
||||
renderconsole.c
|
||||
renderfps.c
|
||||
rendertext.c
|
||||
renderui.c
|
||||
rendertextbox.c
|
||||
)
|
29
archive/dusksdl2/display/ui/renderconsole.c
Normal file
29
archive/dusksdl2/display/ui/renderconsole.c
Normal file
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "renderconsole.h"
|
||||
#include "console/console.h"
|
||||
#include "display/ui/rendertext.h"
|
||||
|
||||
void renderConsoleDraw(void) {
|
||||
if(!CONSOLE.visible) return;
|
||||
|
||||
int32_t i = 0;
|
||||
char_t *line;
|
||||
do {
|
||||
line = CONSOLE.line[i];
|
||||
if(line[0] == '\0') {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
renderTextDraw(
|
||||
0, (CONSOLE_HISTORY_MAX - i - 1) * FONT_TILE_HEIGHT, line,
|
||||
0xFF, 0xFF, 0xFF
|
||||
);
|
||||
i++;
|
||||
} while(i < CONSOLE_HISTORY_MAX);
|
||||
}
|
14
archive/dusksdl2/display/ui/renderconsole.h
Normal file
14
archive/dusksdl2/display/ui/renderconsole.h
Normal file
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dusksdl2.h"
|
||||
|
||||
/**
|
||||
* Draws the console overlay.
|
||||
*/
|
||||
void renderConsoleDraw(void);
|
46
archive/dusksdl2/display/ui/renderfps.c
Normal file
46
archive/dusksdl2/display/ui/renderfps.c
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "renderfps.h"
|
||||
#include "display/render.h"
|
||||
#include "display/ui/rendertext.h"
|
||||
#include "time.h"
|
||||
#include "game.h"
|
||||
|
||||
float_t RENDER_FPS_AVG = -1.0f;
|
||||
float_t RENDER_TPS_AVG = -1.0f;
|
||||
|
||||
void renderFPSDraw(void) {
|
||||
|
||||
if(TIME.delta > 0) {
|
||||
float_t fps = 1.0f / TIME.realDelta;
|
||||
|
||||
if(RENDER_FPS_AVG == -1.0f) {
|
||||
RENDER_FPS_AVG = fps;
|
||||
} else {
|
||||
RENDER_FPS_AVG = (RENDER_FPS_AVG + fps) / 2.0f;
|
||||
}
|
||||
}
|
||||
|
||||
if(TIME.time != TIME.lastTick) {
|
||||
float_t timeSinceLastTick = TIME.realTime - TIME.lastTick;
|
||||
float_t tps = 1.0f / timeSinceLastTick;
|
||||
|
||||
if(RENDER_TPS_AVG == -1.0f) {
|
||||
RENDER_TPS_AVG = tps;
|
||||
} else {
|
||||
RENDER_TPS_AVG = (RENDER_TPS_AVG + tps) / 2.0f;
|
||||
}
|
||||
}
|
||||
|
||||
char_t buffer[64];
|
||||
snprintf(buffer, sizeof(buffer), "%.1f/%.1f", RENDER_FPS_AVG, RENDER_TPS_AVG);
|
||||
|
||||
int32_t width, height;
|
||||
renderTextMeasure(buffer, &width, &height);
|
||||
renderTextDraw(RENDER_WIDTH - width, 0, buffer, 0x00, 0xFF, 0x00);
|
||||
}
|
13
archive/dusksdl2/display/ui/renderfps.h
Normal file
13
archive/dusksdl2/display/ui/renderfps.h
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* Draws the FPS overlay.
|
||||
*/
|
||||
void renderFPSDraw(void);
|
159
archive/dusksdl2/display/ui/rendertext.c
Normal file
159
archive/dusksdl2/display/ui/rendertext.c
Normal file
@@ -0,0 +1,159 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "rendertext.h"
|
||||
#include "display/render.h"
|
||||
#include "assert/assert.h"
|
||||
#include "display/spritebatch/spritebatch.h"
|
||||
#include "util/memory.h"
|
||||
#include "util/math.h"
|
||||
|
||||
texture_t RENDER_TEXT_TEXTURE;
|
||||
|
||||
static mesh_t RENDER_TEXT_QUAD_MESH;
|
||||
|
||||
void renderTextInit(void) {
|
||||
const int32_t cols = FONT_COLUMN_COUNT;
|
||||
const int32_t rows = (FONT_TILE_COUNT + cols - 1) / cols;
|
||||
const int32_t inputFontWidth = cols * FONT_TILE_WIDTH;
|
||||
const int32_t inputFontHeight = rows * FONT_TILE_HEIGHT;
|
||||
|
||||
int32_t outputFontWidth = inputFontWidth;
|
||||
int32_t outputFontHeight = inputFontHeight;
|
||||
|
||||
// Round up to nearest power of 2
|
||||
#if PSP
|
||||
outputFontWidth = mathNextPowTwo(inputFontWidth);
|
||||
outputFontHeight = mathNextPowTwo(inputFontHeight);
|
||||
#endif
|
||||
|
||||
uint8_t *pixels = (uint8_t *)memoryAllocate(
|
||||
outputFontWidth * outputFontHeight *
|
||||
sizeof(uint8_t)
|
||||
);
|
||||
|
||||
// Buffer the pixels.
|
||||
for(int tileIndex = 0; tileIndex < FONT_TILE_COUNT; ++tileIndex) {
|
||||
const int32_t tileX = (tileIndex % FONT_COLUMN_COUNT) * FONT_TILE_WIDTH;
|
||||
const int32_t tileY = (tileIndex / FONT_COLUMN_COUNT) * FONT_TILE_HEIGHT;
|
||||
const uint8_t* tile = TILE_PIXEL_DATA[tileIndex];
|
||||
|
||||
for (int y = 0; y < FONT_TILE_HEIGHT; ++y) {
|
||||
for (int x = 0; x < FONT_TILE_WIDTH; ++x) {
|
||||
const int32_t pixel = (tileY + y) * outputFontWidth + (tileX + x);
|
||||
const int32_t pixelOffset = pixel;
|
||||
uint8_t value = tile[y * FONT_TILE_WIDTH + x];
|
||||
pixels[pixel] = value ? 0xFF : 0x00; // Alpha channel
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
textureInit(
|
||||
&RENDER_TEXT_TEXTURE,
|
||||
outputFontWidth, outputFontHeight,
|
||||
GL_ALPHA, pixels
|
||||
);
|
||||
memoryFree(pixels);
|
||||
}
|
||||
|
||||
void renderTextDrawChar(
|
||||
const float_t x,
|
||||
const float_t y,
|
||||
const char_t c,
|
||||
const uint8_t r,
|
||||
const uint8_t g,
|
||||
const uint8_t b
|
||||
) {
|
||||
int32_t tileIndex = (int32_t)(c) - FONT_CHAR_START;
|
||||
assertTrue(
|
||||
tileIndex >= 0 && tileIndex < FONT_TILE_COUNT,
|
||||
"Character is out of bounds for font tiles"
|
||||
);
|
||||
|
||||
const float_t w = (float)RENDER_TEXT_TEXTURE.width;
|
||||
const float_t h = (float)RENDER_TEXT_TEXTURE.height;
|
||||
const int32_t tileX = (tileIndex % FONT_COLUMN_COUNT);
|
||||
const int32_t tileY = (tileIndex / FONT_COLUMN_COUNT);
|
||||
|
||||
spriteBatchPush(
|
||||
&RENDER_TEXT_TEXTURE,
|
||||
x, y,
|
||||
x + FONT_TILE_WIDTH, y + FONT_TILE_HEIGHT,
|
||||
r, g, b, 0xFF,
|
||||
(tileX * FONT_TILE_WIDTH) / w,
|
||||
(tileY * FONT_TILE_HEIGHT) / h,
|
||||
((tileX + 1) * FONT_TILE_WIDTH) / w,
|
||||
((tileY + 1) * FONT_TILE_HEIGHT) / h
|
||||
);
|
||||
}
|
||||
|
||||
void renderTextDraw(
|
||||
const float_t x,
|
||||
const float_t y,
|
||||
const char_t *text,
|
||||
const uint8_t r,
|
||||
const uint8_t g,
|
||||
const uint8_t b
|
||||
) {
|
||||
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 += FONT_TILE_HEIGHT;
|
||||
continue;
|
||||
}
|
||||
|
||||
renderTextDrawChar(posX, posY, c, r, g, b);
|
||||
posX += FONT_TILE_WIDTH;
|
||||
}
|
||||
}
|
||||
|
||||
void renderTextMeasure(
|
||||
const char_t *text,
|
||||
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 = FONT_TILE_HEIGHT;
|
||||
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 += FONT_TILE_HEIGHT;
|
||||
continue;
|
||||
}
|
||||
|
||||
lineWidth += FONT_TILE_WIDTH;
|
||||
}
|
||||
|
||||
if(lineWidth > width) {
|
||||
width = lineWidth;
|
||||
}
|
||||
|
||||
*outWidth = width;
|
||||
*outHeight = height;
|
||||
}
|
||||
|
||||
void renderTextDispose(void) {
|
||||
textureDispose(&RENDER_TEXT_TEXTURE);
|
||||
}
|
74
archive/dusksdl2/display/ui/rendertext.h
Normal file
74
archive/dusksdl2/display/ui/rendertext.h
Normal file
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dusksdl2.h"
|
||||
#include "ui/font.h"
|
||||
#include "display/texture/texture.h"
|
||||
|
||||
extern texture_t RENDER_TEXT_TEXTURE;
|
||||
|
||||
/**
|
||||
* Initializes the text rendering system.
|
||||
*/
|
||||
void renderTextInit(void);
|
||||
|
||||
/**
|
||||
* 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 r The red component of the color (0-255).
|
||||
* @param g The green component of the color (0-255).
|
||||
* @param b The blue component of the color (0-255).
|
||||
*/
|
||||
void renderTextDrawChar(
|
||||
const float_t x,
|
||||
const float_t y,
|
||||
const char_t c,
|
||||
const uint8_t r,
|
||||
const uint8_t g,
|
||||
const uint8_t b
|
||||
);
|
||||
|
||||
/**
|
||||
* 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 r The red component of the color (0-255).
|
||||
* @param g The green component of the color (0-255).
|
||||
* @param b The blue component of the color (0-255).
|
||||
*/
|
||||
void renderTextDraw(
|
||||
const float_t x,
|
||||
const float_t y,
|
||||
const char_t *text,
|
||||
const uint8_t r,
|
||||
const uint8_t g,
|
||||
const uint8_t b
|
||||
);
|
||||
|
||||
/**
|
||||
* Measures the width and height of the given text string when rendered.
|
||||
*
|
||||
* @param text The null-terminated string of text to measure.
|
||||
* @param outWidth Pointer to store the measured width in pixels.
|
||||
* @param outHeight Pointer to store the measured height in pixels.
|
||||
*/
|
||||
void renderTextMeasure(
|
||||
const char_t *text,
|
||||
int32_t *outWidth,
|
||||
int32_t *outHeight
|
||||
);
|
||||
|
||||
/**
|
||||
* Disposes of the text rendering system, freeing any allocated resources.
|
||||
*/
|
||||
void renderTextDispose(void);
|
70
archive/dusksdl2/display/ui/rendertextbox.c
Normal file
70
archive/dusksdl2/display/ui/rendertextbox.c
Normal file
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "rendertextbox.h"
|
||||
#include "ui/uitextbox.h"
|
||||
#include "display/ui/rendertext.h"
|
||||
#include "display/spritebatch/spritebatch.h"
|
||||
#include "assert/assert.h"
|
||||
|
||||
void renderTextboxDraw(void) {
|
||||
if(!UI_TEXTBOX.visible) return;
|
||||
|
||||
// Background
|
||||
spriteBatchPush(
|
||||
NULL,
|
||||
0, RENDER_HEIGHT - UI_TEXTBOX_HEIGHT,
|
||||
RENDER_WIDTH, RENDER_HEIGHT,
|
||||
0x00, 0x00, 0x00, 0xFF,
|
||||
0.0f, 0.0f, 1.0f, 1.0f
|
||||
);
|
||||
|
||||
uint32_t x = 0;
|
||||
uint32_t y = RENDER_HEIGHT - UI_TEXTBOX_HEIGHT;
|
||||
|
||||
if(UI_TEXTBOX.charsRevealed > 0) {
|
||||
uint8_t charsRendered = 0;
|
||||
|
||||
// For each line
|
||||
for(uint8_t i = 0; i < UI_TEXTBOX_LINES_PER_PAGE; i++) {
|
||||
// Get count of chars in the line
|
||||
uint8_t lineLength = UI_TEXTBOX.lineLengths[
|
||||
i + (UI_TEXTBOX.page * UI_TEXTBOX_LINES_PER_PAGE)
|
||||
];
|
||||
if(lineLength == 0) continue;
|
||||
|
||||
// Determine how many chars left to render
|
||||
uint8_t lineChars = UI_TEXTBOX.charsRevealed - charsRendered;
|
||||
|
||||
// Don't render more than in line
|
||||
if(lineChars > lineLength) lineChars = lineLength;
|
||||
assertTrue(lineChars > 0, "Line chars must be greater than 0");
|
||||
|
||||
// Update how many rendered
|
||||
charsRendered += lineChars;
|
||||
|
||||
for(uint8_t j = 0; j < lineChars; j++) {
|
||||
renderTextDrawChar(
|
||||
x + UI_TEXTBOX_PADDING_X + UI_TEXTBOX_BORDER_WIDTH + (
|
||||
j * FONT_TILE_WIDTH
|
||||
),
|
||||
y + UI_TEXTBOX_PADDING_Y + UI_TEXTBOX_BORDER_HEIGHT + (
|
||||
i * FONT_TILE_HEIGHT
|
||||
),
|
||||
UI_TEXTBOX.text[
|
||||
(i * UI_TEXTBOX_CHARS_PER_LINE) + j +
|
||||
(UI_TEXTBOX.page * UI_TEXTBOX_CHARS_PER_PAGE)
|
||||
],
|
||||
0xFF, 0xFF, 0xFF
|
||||
);
|
||||
}
|
||||
|
||||
// Check if we're done rendering text
|
||||
if(UI_TEXTBOX.charsRevealed - charsRendered == 0) break;
|
||||
}
|
||||
}
|
||||
}
|
10
archive/dusksdl2/display/ui/rendertextbox.h
Normal file
10
archive/dusksdl2/display/ui/rendertextbox.h
Normal file
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
void renderTextboxDraw(void);
|
63
archive/dusksdl2/display/ui/renderui.c
Normal file
63
archive/dusksdl2/display/ui/renderui.c
Normal file
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "renderui.h"
|
||||
#include "display/ui/rendertext.h"
|
||||
#include "display/ui/renderconsole.h"
|
||||
#include "display/ui/renderfps.h"
|
||||
#include "display/ui/rendertextbox.h"
|
||||
#include "display/spritebatch/spritebatch.h"
|
||||
#include "display/camera/camera.h"
|
||||
|
||||
renderuicallback_t RENDER_UI_CALLBACKS[] = {
|
||||
{
|
||||
.init = renderTextInit,
|
||||
.dispose = renderTextDispose
|
||||
},
|
||||
|
||||
{
|
||||
.draw = renderTextboxDraw,
|
||||
},
|
||||
|
||||
{
|
||||
.draw = renderConsoleDraw,
|
||||
},
|
||||
|
||||
{
|
||||
.draw = renderFPSDraw,
|
||||
},
|
||||
};
|
||||
|
||||
#define RENDER_UI_CALLBACKS_COUNT ( \
|
||||
sizeof(RENDER_UI_CALLBACKS) / sizeof(RENDER_UI_CALLBACKS[0]) \
|
||||
)
|
||||
|
||||
void renderUIInit(void) {
|
||||
for (int32_t i = 0; i < RENDER_UI_CALLBACKS_COUNT; i++) {
|
||||
if(!RENDER_UI_CALLBACKS[i].init) continue;
|
||||
RENDER_UI_CALLBACKS[i].init();
|
||||
}
|
||||
}
|
||||
|
||||
void renderUIDraw(void) {
|
||||
cameraUIPush();
|
||||
|
||||
for (int32_t i = 0; i < RENDER_UI_CALLBACKS_COUNT; i++) {
|
||||
if(!RENDER_UI_CALLBACKS[i].draw) continue;
|
||||
RENDER_UI_CALLBACKS[i].draw();
|
||||
}
|
||||
|
||||
spriteBatchFlush();
|
||||
cameraUIPop();
|
||||
}
|
||||
|
||||
void renderUIDispose(void) {
|
||||
for (int32_t i = 0; i < RENDER_UI_CALLBACKS_COUNT; i++) {
|
||||
if(!RENDER_UI_CALLBACKS[i].dispose) continue;
|
||||
RENDER_UI_CALLBACKS[i].dispose();
|
||||
}
|
||||
}
|
32
archive/dusksdl2/display/ui/renderui.h
Normal file
32
archive/dusksdl2/display/ui/renderui.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dusksdl2.h"
|
||||
|
||||
typedef struct {
|
||||
void (*init)(void);
|
||||
void (*draw)(void);
|
||||
void (*dispose)(void);
|
||||
} renderuicallback_t;
|
||||
|
||||
extern renderuicallback_t RENDER_UI_CALLBACKS[];
|
||||
|
||||
/**
|
||||
* Initialize the UI rendering system.
|
||||
*/
|
||||
void renderUIInit(void);
|
||||
|
||||
/**
|
||||
* Draw the UI elements.
|
||||
*/
|
||||
void renderUIDraw(void);
|
||||
|
||||
/**
|
||||
* Dispose of the UI rendering system.
|
||||
*/
|
||||
void renderUIDispose(void);
|
Reference in New Issue
Block a user