Custom font rendering.
This commit is contained in:
@ -9,6 +9,7 @@
|
||||
#include "ui/uitextbox.h"
|
||||
#include "util/memory.h"
|
||||
#include "assert/assert.h"
|
||||
#include "display/render.h"
|
||||
|
||||
void drawUIInit(void) {
|
||||
}
|
||||
@ -20,22 +21,21 @@ void drawUI() {
|
||||
void drawUITextbox() {
|
||||
if(!UI_TEXTBOX.visible) return;
|
||||
|
||||
uitextbox_t *textbox = &UI_TEXTBOX;
|
||||
|
||||
// Semi-transparent dark blue
|
||||
Color background = (Color){ 0, 0, 128, 128 };
|
||||
|
||||
uint32_t x = 0;
|
||||
uint32_t y = RENDER_HEIGHT - UI_TEXTBOX_HEIGHT;
|
||||
|
||||
DrawRectangle(
|
||||
0, RENDER_HEIGHT - UI_TEXTBOX_HEIGHT,
|
||||
x, y,
|
||||
UI_TEXTBOX_WIDTH, UI_TEXTBOX_HEIGHT,
|
||||
background
|
||||
);
|
||||
|
||||
|
||||
BeginBlendMode(BLEND_ALPHA);
|
||||
|
||||
if(UI_TEXTBOX.charsRevealed > 0) {
|
||||
char_t buffer[UI_TEXTBOX_CHARS_PER_LINE + 1];// +1 for null term
|
||||
uint8_t charsRendered = 0;
|
||||
uitextbox_t *textbox = &UI_TEXTBOX;
|
||||
|
||||
// For each line
|
||||
for(uint8_t i = 0; i < UI_TEXTBOX_LINES_PER_PAGE; i++) {
|
||||
@ -55,33 +55,66 @@ void drawUITextbox() {
|
||||
// Update how many rendered
|
||||
charsRendered += lineChars;
|
||||
|
||||
// Copy string from VN Textbox...
|
||||
memoryCopy(
|
||||
buffer,
|
||||
UI_TEXTBOX.text + (UI_TEXTBOX.page * UI_TEXTBOX_CHARS_PER_PAGE) +
|
||||
(i * UI_TEXTBOX_CHARS_PER_LINE),
|
||||
lineChars
|
||||
);
|
||||
// Null term string
|
||||
buffer[lineChars] = '\0'; // Null-terminate the string
|
||||
|
||||
// Render the text
|
||||
DrawText(
|
||||
buffer,
|
||||
UI_TEXTBOX_PADDING_X + UI_TEXTBOX_BORDER_WIDTH,
|
||||
(
|
||||
(RENDER_HEIGHT - UI_TEXTBOX_HEIGHT) +
|
||||
UI_TEXTBOX_PADDING_Y + UI_TEXTBOX_BORDER_HEIGHT +
|
||||
(i * FONT_LINE_HEIGHT)
|
||||
),
|
||||
FONT_HEIGHT,
|
||||
WHITE
|
||||
);
|
||||
for(uint8_t j = 0; j < lineChars; j++) {
|
||||
drawUIChar(
|
||||
UI_TEXTBOX.text[
|
||||
(i * UI_TEXTBOX_CHARS_PER_LINE) + j +
|
||||
(UI_TEXTBOX.page * UI_TEXTBOX_CHARS_PER_PAGE)
|
||||
],
|
||||
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
|
||||
),
|
||||
WHITE
|
||||
);
|
||||
}
|
||||
|
||||
// Check if we're done rendering text
|
||||
if(UI_TEXTBOX.charsRevealed - charsRendered == 0) break;
|
||||
}
|
||||
}
|
||||
|
||||
EndBlendMode();
|
||||
}
|
||||
|
||||
void drawUIText(
|
||||
const uint8_t *text,
|
||||
const uint16_t length,
|
||||
const int32_t x,
|
||||
const int32_t y,
|
||||
const Color color
|
||||
) {
|
||||
assertNotNull(text, "Text to draw cannot be NULL");
|
||||
if(length == 0) return;
|
||||
|
||||
for(uint16_t i = 0; i < length; i++) {
|
||||
drawUIChar(text[i], x + (i * FONT_TILE_WIDTH), y, color);
|
||||
}
|
||||
}
|
||||
|
||||
void drawUIChar(
|
||||
const char_t c,
|
||||
const int32_t x,
|
||||
const int32_t y,
|
||||
const Color color
|
||||
) {
|
||||
if(c < ' ' || c > '~') return; // Only print printable ASCII characters
|
||||
|
||||
int32_t charIndex = TILE_INDEXES[c - FONT_CHAR_START];
|
||||
int32_t charX = (charIndex % FONT_COLUMN_COUNT) * FONT_TILE_WIDTH;
|
||||
int32_t charY = (charIndex / FONT_COLUMN_COUNT) * FONT_TILE_HEIGHT;
|
||||
|
||||
// FLip Y coordinate for raylib texture coordinates
|
||||
DrawTextureRec(
|
||||
RENDER_FONT_TEXTURE,
|
||||
(Rectangle) {
|
||||
(float) charX,
|
||||
(float) charY, // Flip Y by adding height
|
||||
(float) FONT_TILE_WIDTH,
|
||||
(float) FONT_TILE_HEIGHT // Negative height to flip the texture
|
||||
},
|
||||
(Vector2) { x, y }, // Add FONT_HEIGHT to flip Y
|
||||
color
|
||||
);
|
||||
}
|
@ -21,4 +21,36 @@ void drawUI(void);
|
||||
/**
|
||||
* Draws the UI textbox to the screen.
|
||||
*/
|
||||
void drawUITextbox(void);
|
||||
void drawUITextbox(void);
|
||||
|
||||
/**
|
||||
* Draws text to the screen at the specified position with the given color.
|
||||
*
|
||||
* @param text The text to draw.
|
||||
* @param length The length of the text to draw.
|
||||
* @param x The x-coordinate to draw the text at.
|
||||
* @param y The y-coordinate to draw the text at.
|
||||
* @param color The color of the text.
|
||||
*/
|
||||
void drawUIText(
|
||||
const uint8_t *text,
|
||||
const uint16_t length,
|
||||
const int32_t x,
|
||||
const int32_t y,
|
||||
const Color color
|
||||
);
|
||||
|
||||
/**
|
||||
* Draws a single character to the screen at the specified position with the given color.
|
||||
*
|
||||
* @param c The character to draw.
|
||||
* @param x The x-coordinate to draw the character at.
|
||||
* @param y The y-coordinate to draw the character at.
|
||||
* @param color The color of the character.
|
||||
*/
|
||||
void drawUIChar(
|
||||
const char_t c,
|
||||
const int32_t x,
|
||||
const int32_t y,
|
||||
const Color color
|
||||
);
|
@ -14,6 +14,7 @@
|
||||
RenderTexture2D RENDER_SCREEN_TEXTURE;
|
||||
Texture2D RENDER_TILEMAP_TEXTURE;
|
||||
Texture2D RENDER_ENTITIES_TEXTURE;
|
||||
Texture2D RENDER_FONT_TEXTURE;
|
||||
|
||||
void renderInit(void) {
|
||||
InitWindow(
|
||||
@ -31,6 +32,7 @@ void renderInit(void) {
|
||||
RENDER_SCREEN_TEXTURE = LoadRenderTexture(RENDER_WIDTH, RENDER_HEIGHT);
|
||||
RENDER_TILEMAP_TEXTURE = LoadTexture("../data/tilemap.png");
|
||||
RENDER_ENTITIES_TEXTURE = LoadTexture("../data/entities.png");
|
||||
RENDER_FONT_TEXTURE = LoadTexture("../data/minogram_6x10.png");
|
||||
|
||||
drawOverworldInit();
|
||||
drawUIInit();
|
||||
|
@ -11,4 +11,5 @@
|
||||
|
||||
extern RenderTexture2D RENDER_SCREEN_TEXTURE;
|
||||
extern Texture2D RENDER_TILEMAP_TEXTURE;
|
||||
extern Texture2D RENDER_ENTITIES_TEXTURE;
|
||||
extern Texture2D RENDER_ENTITIES_TEXTURE;
|
||||
extern Texture2D RENDER_FONT_TEXTURE;
|
Reference in New Issue
Block a user