Text
This commit is contained in:
@@ -11,6 +11,7 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
screen.c
|
||||
texture.c
|
||||
spritebatch.c
|
||||
text.c
|
||||
)
|
||||
|
||||
# Subdirectories
|
||||
|
||||
@@ -34,9 +34,9 @@ void cameraInitOrthographic(camera_t *camera) {
|
||||
|
||||
camera->projType = CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC;
|
||||
camera->orthographic.left = 0.0f;
|
||||
camera->orthographic.right = (float_t)frameBufferGetWidth(FRAMEBUFFER_BOUND);
|
||||
camera->orthographic.bottom = 0.0f;
|
||||
camera->orthographic.top = (float_t)frameBufferGetHeight(FRAMEBUFFER_BOUND);
|
||||
camera->orthographic.right = SCREEN.width;
|
||||
camera->orthographic.top = 0.0f;
|
||||
camera->orthographic.bottom = SCREEN.height;
|
||||
camera->nearClip = -1.0f;
|
||||
camera->farClip = 1.0f;
|
||||
|
||||
|
||||
@@ -1,113 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dusk.h"
|
||||
|
||||
typedef float_t colorchannelf_t;
|
||||
typedef uint8_t colorchannelb_t;
|
||||
|
||||
typedef struct {
|
||||
colorchannelf_t r;
|
||||
colorchannelf_t g;
|
||||
colorchannelf_t b;
|
||||
} color3f_t;
|
||||
|
||||
typedef struct {
|
||||
colorchannelf_t r;
|
||||
colorchannelf_t g;
|
||||
colorchannelf_t b;
|
||||
colorchannelf_t a;
|
||||
} color4f_t;
|
||||
|
||||
typedef struct {
|
||||
colorchannelb_t r;
|
||||
colorchannelb_t g;
|
||||
colorchannelb_t b;
|
||||
} color3b_t;
|
||||
|
||||
typedef struct {
|
||||
colorchannelb_t r;
|
||||
colorchannelb_t g;
|
||||
colorchannelb_t b;
|
||||
colorchannelb_t a;
|
||||
} color4b_t;
|
||||
|
||||
typedef color4b_t color_t;
|
||||
|
||||
#define color3f(r, g, b) ((color3f_t){r, g, b})
|
||||
#define color4f(r, g, b, a) ((color4f_t){r, g, b, a})
|
||||
#define color3b(r, g, b) ((color3b_t){r, g, b})
|
||||
#define color4b(r, g, b, a) ((color4b_t){r, g, b, a})
|
||||
#define color(r, g, b, a) ((color_t){r, g, b, a})
|
||||
|
||||
#define colorHex(hex) color( \
|
||||
((hex >> 24) & 0xFF), \
|
||||
((hex >> 16) & 0xFF), \
|
||||
((hex >> 8) & 0xFF), \
|
||||
(hex & 0xFF) \
|
||||
)
|
||||
|
||||
#define COLOR_WHITE_3F color3f(1.0f, 1.0f, 1.0f)
|
||||
#define COLOR_WHITE_4F color4f(1.0f, 1.0f, 1.0f, 1.0f)
|
||||
#define COLOR_WHITE_3B color3b(255, 255, 255)
|
||||
#define COLOR_WHITE_4B color4b(255, 255, 255, 255)
|
||||
#define COLOR_WHITE color(255, 255, 255, 255)
|
||||
|
||||
#define COLOR_BLACK_3F color3f(0.0f, 0.0f, 0.0f)
|
||||
#define COLOR_BLACK_4F color4f(0.0f, 0.0f, 0.0f, 1.0f)
|
||||
#define COLOR_BLACK_3B color3b(0, 0, 0)
|
||||
#define COLOR_BLACK_4B color4b(0, 0, 0, 255)
|
||||
#define COLOR_BLACK color(0, 0, 0, 255)
|
||||
|
||||
#define COLOR_RED_3F color3f(1.0f, 0.0f, 0.0f)
|
||||
#define COLOR_RED_4F color4f(1.0f, 0.0f, 0.0f, 1.0f)
|
||||
#define COLOR_RED_3B color3b(255, 0, 0)
|
||||
#define COLOR_RED_4B color4b(255, 0, 0, 255)
|
||||
#define COLOR_RED color(255, 0, 0, 255)
|
||||
|
||||
#define COLOR_GREEN_3F color3f(0.0f, 1.0f, 0.0f)
|
||||
#define COLOR_GREEN_4F color4f(0.0f, 1.0f, 0.0f, 1.0f)
|
||||
#define COLOR_GREEN_3B color3b(0, 255, 0)
|
||||
#define COLOR_GREEN_4B color4b(0, 255, 0, 255)
|
||||
#define COLOR_GREEN color(0, 255, 0, 255)
|
||||
|
||||
#define COLOR_BLUE_3F color3f(0.0f, 0.0f, 1.0f)
|
||||
#define COLOR_BLUE_4F color4f(0.0f, 0.0f, 1.0f, 1.0f)
|
||||
#define COLOR_BLUE_3B color3b(0, 0, 255)
|
||||
#define COLOR_BLUE_4B color4b(0, 0, 255, 255)
|
||||
#define COLOR_BLUE color(0, 0, 255, 255)
|
||||
|
||||
#define COLOR_YELLOW_3F color3f(1.0f, 1.0f, 0.0f)
|
||||
#define COLOR_YELLOW_4F color4f(1.0f, 1.0f, 0.0f, 1.0f)
|
||||
#define COLOR_YELLOW_3B color3b(255, 255, 0)
|
||||
#define COLOR_YELLOW_4B color4b(255, 255, 0, 255)
|
||||
#define COLOR_YELLOW color(255, 255, 0, 255)
|
||||
|
||||
#define COLOR_CYAN_3F color3f(0.0f, 1.0f, 1.0f)
|
||||
#define COLOR_CYAN_4F color4f(0.0f, 1.0f, 1.0f, 1.0f)
|
||||
#define COLOR_CYAN_3B color3b(0, 255, 255)
|
||||
#define COLOR_CYAN_4B color4b(0, 255, 255, 255)
|
||||
#define COLOR_CYAN color(0, 255, 255, 255)
|
||||
|
||||
#define COLOR_MAGENTA_3F color3f(1.0f, 0.0f, 1.0f)
|
||||
#define COLOR_MAGENTA_4F color4f(1.0f, 0.0f, 1.0f, 1.0f)
|
||||
#define COLOR_MAGENTA_3B color3b(255, 0, 255)
|
||||
#define COLOR_MAGENTA_4B color4b(255, 0, 255, 255)
|
||||
#define COLOR_MAGENTA color(255, 0, 255, 255)
|
||||
|
||||
#define COLOR_TRANSPARENT_3F color3f(0.0f, 0.0f, 0.0f)
|
||||
#define COLOR_TRANSPARENT_4F color4f(0.0f, 0.0f, 0.0f, 0.0f)
|
||||
#define COLOR_TRANSPARENT_3B color3b(0, 0, 0)
|
||||
#define COLOR_TRANSPARENT_4B color4b(0, 0, 0, 0)
|
||||
#define COLOR_TRANSPARENT color(0, 0, 0, 0)
|
||||
|
||||
#define COLOR_CORNFLOWER_BLUE_3F color3f(0.39f, 0.58f, 0.93f)
|
||||
#define COLOR_CORNFLOWER_BLUE_4F color4f(0.39f, 0.58f, 0.93f, 1.0f)
|
||||
#define COLOR_CORNFLOWER_BLUE_3B color3b(100, 149, 237)
|
||||
#define COLOR_CORNFLOWER_BLUE_4B color4b(100, 149, 237, 255)
|
||||
#define COLOR_CORNFLOWER_BLUE color(100, 149, 237, 255)
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "display/screen.h"
|
||||
#include "ui/ui.h"
|
||||
#include "debug/debug.h"
|
||||
#include "display/text.h"
|
||||
|
||||
display_t DISPLAY;
|
||||
|
||||
@@ -73,6 +74,7 @@ errorret_t displayInit(void) {
|
||||
quadInit();
|
||||
frameBufferInitBackbuffer();
|
||||
spriteBatchInit();
|
||||
errorChain(textInit());
|
||||
screenInit();
|
||||
|
||||
errorOk();
|
||||
@@ -145,6 +147,7 @@ errorret_t displayUpdate(void) {
|
||||
errorret_t displayDispose(void) {
|
||||
spriteBatchDispose();
|
||||
screenDispose();
|
||||
textDispose();
|
||||
|
||||
#if DISPLAY_SDL2
|
||||
if(DISPLAY.glContext) {
|
||||
|
||||
124
src/display/text.c
Normal file
124
src/display/text.c
Normal file
@@ -0,0 +1,124 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "text.h"
|
||||
#include "assert/assert.h"
|
||||
#include "util/memory.h"
|
||||
#include "display/spritebatch.h"
|
||||
#include "asset/asset.h"
|
||||
|
||||
texture_t DEFAULT_FONT_TEXTURE;
|
||||
|
||||
errorret_t textInit(void) {
|
||||
errorChain(assetLoad(DEFAULT_FONT_TILESET.image, &DEFAULT_FONT_TEXTURE));
|
||||
errorOk();
|
||||
}
|
||||
|
||||
void textDispose(void) {
|
||||
textureDispose(&DEFAULT_FONT_TEXTURE);
|
||||
}
|
||||
|
||||
void textDrawChar(
|
||||
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) - TEXT_CHAR_START;
|
||||
if(tileIndex < 0 || tileIndex >= tileset->tileCount) {
|
||||
tileIndex = ((int32_t)'@') - 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 textDraw(
|
||||
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;
|
||||
}
|
||||
|
||||
textDrawChar(posX, posY, c, color, tileset, texture);
|
||||
posX += tileset->tileWidth;
|
||||
}
|
||||
}
|
||||
|
||||
void textMeasure(
|
||||
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;
|
||||
}
|
||||
80
src/display/text.h
Normal file
80
src/display/text.h
Normal file
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "asset/asset.h"
|
||||
#include "display/texture.h"
|
||||
#include "display/tileset/tileset.h"
|
||||
#include "display/tileset/tileset_minogram.h"
|
||||
|
||||
#define TEXT_CHAR_START '!'
|
||||
|
||||
extern texture_t DEFAULT_FONT_TEXTURE;
|
||||
#define DEFAULT_FONT_TILESET TILESET_MINOGRAM
|
||||
|
||||
/**
|
||||
* Initializes the text system.
|
||||
*/
|
||||
errorret_t textInit(void);
|
||||
|
||||
/**
|
||||
* Disposes of the text system.
|
||||
*/
|
||||
void textDispose(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 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 textDrawChar(
|
||||
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 textDraw(
|
||||
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 textMeasure(
|
||||
const char_t *text,
|
||||
const tileset_t *tileset,
|
||||
int32_t *outWidth,
|
||||
int32_t *outHeight
|
||||
);
|
||||
Reference in New Issue
Block a user