This commit is contained in:
2025-10-02 18:53:47 -05:00
parent 4b04fc65ad
commit c0cd4ead04
34 changed files with 684 additions and 35 deletions

View File

@@ -8,5 +8,6 @@ target_sources(${DUSK_TARGET_NAME}
PRIVATE
uitext.c
uifps.c
uiframe.c
uiconsole.c
)

View File

@@ -9,7 +9,12 @@
#include "uitext.h"
#include "console/console.h"
void uiConsoleRender(const tileset_t *tileset, texture_t *texture) {
void uiConsoleRender(
const float_t x,
const float_t y,
const tileset_t *tileset,
texture_t *texture
) {
if(!CONSOLE.visible) return;
int32_t i = CONSOLE_HISTORY_MAX - 1;
@@ -21,7 +26,7 @@ void uiConsoleRender(const tileset_t *tileset, texture_t *texture) {
continue;
}
uiTextDraw(
0, i * TILESET_MINOGRAM.tileHeight,
x, y + (i * TILESET_MINOGRAM.tileHeight),
line, COLOR_WHITE,
tileset, texture
);

View File

@@ -12,7 +12,14 @@
/**
* Renders the console UI.
*
* @param x The x-coordinate to start rendering the console.
* @param y The y-coordinate to start rendering the console.
* @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);
void uiConsoleRender(
const float_t x,
const float_t y,
const tileset_t *tileset,
texture_t *texture
);

384
src/ui/uiframe.c Normal file
View File

@@ -0,0 +1,384 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "uiframe.h"
#include "display/spritebatch/spritebatch.h"
#include "assert/assert.h"
#include <math.h>
void uiFrameDraw(
const float_t x,
const float_t y,
const float_t width,
const float_t height,
const tileset_t *tileset,
const uint16_t column,
const uint16_t row,
texture_t *texture
) {
assertNotNull(tileset, "Tileset cannot be NULL");
assertNotNull(texture, "Texture cannot be NULL");
if(height <= 0 || width <= 0) return;
// Top-Left
vec4 uv;
tilesetPositionGetUV(
tileset,
column,
row,
uv
);
spriteBatchPush(
texture,
x, y,
x + tileset->tileWidth,
y + tileset->tileHeight,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
// Top-Center
tilesetPositionGetUV(
tileset,
column + 1,
row,
uv
);
spriteBatchPush(
texture,
x + tileset->tileWidth, y,
x + width - tileset->tileWidth, y + tileset->tileHeight,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
// Top-Right
tilesetPositionGetUV(
tileset,
column + 2,
row,
uv
);
spriteBatchPush(
texture,
x + width - tileset->tileWidth, y,
x + width, y + tileset->tileHeight,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
// Middle-Left
tilesetPositionGetUV(
tileset,
column,
row + 1,
uv
);
spriteBatchPush(
texture,
x, y + tileset->tileHeight,
x + tileset->tileWidth, y + height - tileset->tileHeight,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
// Middle-Center
tilesetPositionGetUV(
tileset,
column + 1,
row + 1,
uv
);
spriteBatchPush(
texture,
x + tileset->tileWidth, y + tileset->tileHeight,
x + width - tileset->tileWidth, y + height - tileset->tileHeight,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
// Middle-Right
tilesetPositionGetUV(
tileset,
column + 2,
row + 1,
uv
);
spriteBatchPush(
texture,
x + width - tileset->tileWidth, y + tileset->tileHeight,
x + width, y + height - tileset->tileHeight,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
// Bottom-Left
tilesetPositionGetUV(
tileset,
column,
row + 2,
uv
);
spriteBatchPush(
texture,
x, y + height - tileset->tileHeight,
x + tileset->tileWidth, y + height,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
// Bottom-Center
tilesetPositionGetUV(
tileset,
column + 1,
row + 2,
uv
);
spriteBatchPush(
texture,
x + tileset->tileWidth, y + height - tileset->tileHeight,
x + width - tileset->tileWidth, y + height,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
// Bottom-Right
tilesetPositionGetUV(
tileset,
column + 2,
row + 2,
uv
);
spriteBatchPush(
texture,
x + width - tileset->tileWidth, y + height - tileset->tileHeight,
x + width, y + height,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
}
void uiFrameDrawTiled(
const float_t x,
const float_t y,
const float_t width,
const float_t height,
const tileset_t *tileset,
const uint16_t column,
const uint16_t row,
texture_t *texture
) {
assertNotNull(tileset, "Tileset cannot be NULL");
assertNotNull(texture, "Texture cannot be NULL");
if(height <= 0 || width <= 0) return;
uint32_t segmentsX, segmentsY;
float_t remainderX, remainderY;
segmentsX = (width - (tileset->tileWidth * 2)) / tileset->tileWidth;
segmentsY = (height - (tileset->tileHeight * 2)) / tileset->tileHeight;
remainderX = fmodf(width - (tileset->tileWidth * 2), tileset->tileWidth);
remainderY = fmodf(height - (tileset->tileHeight * 2), tileset->tileHeight);
// Corners
vec4 uv;
// Top-Left
tilesetPositionGetUV(
tileset,
column,
row,
uv
);
spriteBatchPush(
texture,
x, y,
x + tileset->tileWidth,
y + tileset->tileHeight,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
// Top-Right
tilesetPositionGetUV(
tileset,
column + 2,
row,
uv
);
spriteBatchPush(
texture,
x + width - tileset->tileWidth, y,
x + width, y + tileset->tileHeight,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
// Bottom-Left
tilesetPositionGetUV(
tileset,
column,
row + 2,
uv
);
spriteBatchPush(
texture,
x, y + height - tileset->tileHeight,
x + tileset->tileWidth, y + height,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
// Bottom-Right
tilesetPositionGetUV(
tileset,
column + 2,
row + 2,
uv
);
spriteBatchPush(
texture,
x + width - tileset->tileWidth, y + height - tileset->tileHeight,
x + width, y + height,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
// Top and Bottom Edges (Tiled)
tilesetPositionGetUV(tileset, column + 1, row, uv); // Top edge
float_t edgeX = x + tileset->tileWidth;
for(uint32_t i = 0; i < segmentsX; ++i, edgeX += tileset->tileWidth) {
spriteBatchPush(
texture,
edgeX, y,
edgeX + tileset->tileWidth, y + tileset->tileHeight,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
}
if(remainderX) {
spriteBatchPush(
texture,
edgeX, y,
edgeX + remainderX, y + tileset->tileHeight,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
}
tilesetPositionGetUV(tileset, column + 1, row + 2, uv); // Bottom edge
edgeX = x + tileset->tileWidth;
for(uint32_t i = 0; i < segmentsX; ++i, edgeX += tileset->tileWidth) {
spriteBatchPush(
texture,
edgeX, y + height - tileset->tileHeight,
edgeX + tileset->tileWidth, y + height,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
}
if(remainderX) {
spriteBatchPush(
texture,
edgeX, y + height - tileset->tileHeight,
edgeX + remainderX, y + height,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
}
// Left and Right Edges (Tiled)
tilesetPositionGetUV(tileset, column, row + 1, uv); // Left edge
float_t edgeY = y + tileset->tileHeight;
for(uint32_t i = 0; i < segmentsY; ++i, edgeY += tileset->tileHeight) {
spriteBatchPush(
texture,
x, edgeY,
x + tileset->tileWidth, edgeY + tileset->tileHeight,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
}
if(remainderY) {
spriteBatchPush(
texture,
x, edgeY,
x + tileset->tileWidth, edgeY + remainderY,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
}
tilesetPositionGetUV(tileset, column + 2, row + 1, uv); // Right edge
edgeY = y + tileset->tileHeight;
for(uint32_t i = 0; i < segmentsY; ++i, edgeY += tileset->tileHeight) {
spriteBatchPush(
texture,
x + width - tileset->tileWidth, edgeY,
x + width, edgeY + tileset->tileHeight,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
}
if(remainderY) {
spriteBatchPush(
texture,
x + width - tileset->tileWidth, edgeY,
x + width, edgeY + remainderY,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
}
// Center (Tiled)
tilesetPositionGetUV(tileset, column + 1, row + 1, uv); // Center tile
float_t centerY = y + tileset->tileHeight;
for(uint32_t j = 0; j < segmentsY; ++j, centerY += tileset->tileHeight) {
float_t centerX = x + tileset->tileWidth;
for(uint32_t i = 0; i < segmentsX; ++i, centerX += tileset->tileWidth) {
spriteBatchPush(
texture,
centerX, centerY,
centerX + tileset->tileWidth, centerY + tileset->tileHeight,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
}
if(remainderX) {
spriteBatchPush(
texture,
centerX, centerY,
centerX + remainderX, centerY + tileset->tileHeight,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
}
}
if(remainderY) {
float_t centerX = x + tileset->tileWidth;
for(uint32_t i = 0; i < segmentsX; ++i, centerX += tileset->tileWidth) {
spriteBatchPush(
texture,
centerX, centerY,
centerX + tileset->tileWidth, centerY + remainderY,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
}
if(remainderX) {
spriteBatchPush(
texture,
centerX, centerY,
centerX + remainderX, centerY + remainderY,
COLOR_WHITE,
uv[0], uv[1], uv[2], uv[3]
);
}
}
}

57
src/ui/uiframe.h Normal file
View File

@@ -0,0 +1,57 @@
/**
* 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"
/**
* Draw a UI Frame (using the 9-slice technique).
*
* @param x The x position to draw the frame at.
* @param y The y position to draw the frame at.
* @param width The width of the frame.
* @param height The height of the frame.
* @param tileset The tileset to use for rendering the frame.
* @param column The column in the tileset to use for the frame.
* @param row The row in the tileset to use for the frame.
* @param texture The texture associated with the tileset.
*/
void uiFrameDraw(
const float_t x,
const float_t y,
const float_t width,
const float_t height,
const tileset_t *tileset,
const uint16_t column,
const uint16_t row,
texture_t *texture
);
/**
* Draw a tiled UI Frame (using the 9-slice technique). This will tile the
* center components to allow them to repeat without distortion.
*
* @param x The x position to draw the frame at.
* @param y The y position to draw the frame at.
* @param width The width of the frame.
* @param height The height of the frame.
* @param tileset The tileset to use for rendering the frame.
* @param column The column in the tileset to use for the frame.
* @param row The row in the tileset to use for the frame.
* @param texture The texture associated with the tileset.
*/
void uiFrameDrawTiled(
const float_t x,
const float_t y,
const float_t width,
const float_t height,
const tileset_t *tileset,
const uint16_t column,
const uint16_t row,
texture_t *texture
);