Render text working.

This commit is contained in:
2025-09-02 22:47:07 -05:00
parent 87f18d0e13
commit 1af2b8f47b
19 changed files with 340 additions and 157 deletions

View File

@@ -10,7 +10,7 @@
#include "display/framebuffer/framebuffer.h"
#include "display/scene/scenemanager.h"
#include "display/spritebatch/spritebatch.h"
#include "display/ui/rendertext.h"
#include "display/mesh/quad.h"
display_t DISPLAY;
@@ -62,6 +62,7 @@ errorret_t displayInit(void) {
quadInit();
frameBufferInitBackbuffer();
spriteBatchInit();
errorChain(renderTextInit());
sceneManagerInit();
errorOk();
@@ -117,6 +118,7 @@ errorret_t displayUpdate(void) {
errorret_t displayDispose(void) {
sceneManagerDispose();
renderTextDispose();
spriteBatchDispose();
#if DISPLAY_SDL2

View File

@@ -12,13 +12,25 @@
#include "display/scene/scenemanager.h"
#include "display/mesh/quad.h"
#include "asset/assetmanager.h"
#include "display/ui/rendertext.h"
camera_t SCENE_OVERWORLD_CAMERA;
asset_t *testAsset;
void sceneOverworldInit(void) {
cameraInit(&SCENE_OVERWORLD_CAMERA);
glm_vec3_copy((vec3){32.0f, 32.0f, 32.0f}, SCENE_OVERWORLD_CAMERA.lookat.position);
// glm_vec3_copy((vec3){32.0f, 32.0f, 32.0f}, SCENE_OVERWORLD_CAMERA.lookat.position);
SCENE_OVERWORLD_CAMERA.projType = CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC;
SCENE_OVERWORLD_CAMERA.orthographic.left = 0.0f;
SCENE_OVERWORLD_CAMERA.orthographic.right = 128.0f;
SCENE_OVERWORLD_CAMERA.orthographic.top = 0.0f;
SCENE_OVERWORLD_CAMERA.orthographic.bottom = 72.0f;
SCENE_OVERWORLD_CAMERA.nearClip = -1.0f;
SCENE_OVERWORLD_CAMERA.farClip = 1000.0f;
SCENE_OVERWORLD_CAMERA.viewType = CAMERA_VIEW_TYPE_MATRIX;
glm_mat4_identity(SCENE_OVERWORLD_CAMERA.view);
scene_t *scene = &SCENE_MANAGER_SCENES[SCENE_TYPE_OVERWORLD];
scene->flags |= SCENE_FLAG_ACTIVE | SCENE_FLAG_VISIBLE;
@@ -39,13 +51,14 @@ void sceneOverworldRender(void) {
// Draw entities
// Draw overlay layer.
renderTextDraw(0.0f, 0.0f, "Hello World", 0xFF, 0xFF, 0xFF);
spriteBatchPush(
&testAsset->paletteImage.texture,
0.0f, 0.0f, 12.0f, 12.0f,
0xFF, 0xFF, 0xFF, 0xFF,
0.0f, 0.0f, 1.0f, 1.0f
);
// spriteBatchPush(
// &testAsset->paletteImage.texture,
// 0.0f, 0.0f, 12.0f, 12.0f,
// 0xFF, 0xFF, 0xFF, 0xFF,
// 0.0f, 0.0f, 1.0f, 1.0f
// );
spriteBatchFlush();
cameraPopMatrix();

View File

@@ -5,156 +5,126 @@
// * https://opensource.org/licenses/MIT
// */
// #include "rendertext.h"
#include "rendertext.h"
#include "asset/assetmanager.h"
#include "assert/assert.h"
#include "util/memory.h"
#include "display/spritebatch/spritebatch.h"
// #include "display/display.h"
// #include "assert/assert.h"
// #include "display/spritebatch/spritebatch.h"
// #include "util/memory.h"
// #include "util/math.h"
// texture_t RENDER_TEXT_TEXTURE;
rendertext_t RENDER_TEXT;
// static mesh_t RENDER_TEXT_QUAD_MESH;
errorret_t renderTextInit(void) {
memoryZero(&RENDER_TEXT, sizeof(rendertext_t));
// 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;
errorChain(assetManagerLoadAsset(
"font_minogram.dai", &RENDER_TEXT.asset, &RENDER_TEXT.assetRef
));
errorOk();
}
// int32_t outputFontWidth = inputFontWidth;
// int32_t outputFontHeight = inputFontHeight;
void renderTextDispose(void) {
if(RENDER_TEXT.asset) {
assetUnlock(RENDER_TEXT.asset, RENDER_TEXT.assetRef);
}
}
// // // Round up to nearest power of 2
// // #if PSP
// // outputFontWidth = mathNextPowTwo(inputFontWidth);
// // outputFontHeight = mathNextPowTwo(inputFontHeight);
// // #endif
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) - RENDER_TEXT_CHAR_START;
assertTrue(
tileIndex >= 0 && tileIndex <= RENDER_TEXT_TILE_COUNT,
"Character is out of bounds for font tiles"
);
// uint8_t *pixels = (uint8_t *)memoryAllocate(
// outputFontWidth * outputFontHeight *
// sizeof(uint8_t)
// );
const float_t w = (float)RENDER_TEXT.asset->alphaImage.texture.width;
const float_t h = (float)RENDER_TEXT.asset->alphaImage.texture.height;
const int32_t tileX = (tileIndex % RENDER_TEXT_COLUMN_COUNT);
const int32_t tileY = (tileIndex / RENDER_TEXT_COLUMN_COUNT);
// // 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];
spriteBatchPush(
&RENDER_TEXT.asset->alphaImage.texture,
x, y,
x + RENDER_TEXT_TILE_WIDTH, y + RENDER_TEXT_TILE_HEIGHT,
r, g, b, 0xFF,
(tileX * RENDER_TEXT_TILE_WIDTH) / w,
(tileY * RENDER_TEXT_TILE_HEIGHT) / h,
((tileX + 1) * RENDER_TEXT_TILE_WIDTH) / w,
((tileY + 1) * RENDER_TEXT_TILE_HEIGHT) / h
);
}
// 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
// }
// }
// }
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");
// textureInit(
// &RENDER_TEXT_TEXTURE,
// outputFontWidth, outputFontHeight,
// TEXTURE_FORMAT_ALPHA, pixels
// );
// memoryFree(pixels);
// }
float_t posX = x;
float_t posY = y;
// 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"
// );
char_t c;
int32_t i = 0;
while((c = text[i++]) != '\0') {
if(c == '\n') {
posX = x;
posY += RENDER_TEXT_TILE_HEIGHT;
continue;
}
// 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);
if(c == ' ') {
posX += RENDER_TEXT_TILE_WIDTH;
continue;
}
// 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
// );
// }
renderTextDrawChar(posX, posY, c, r, g, b);
posX += RENDER_TEXT_TILE_WIDTH;
}
}
// 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");
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");
// float_t posX = x;
// float_t posY = y;
int32_t width = 0;
int32_t height = RENDER_TEXT_TILE_HEIGHT;
int32_t lineWidth = 0;
// char_t c;
// int32_t i = 0;
// while((c = text[i++]) != '\0') {
// if(c == '\n') {
// posX = x;
// posY += FONT_TILE_HEIGHT;
// continue;
// }
char_t c;
int32_t i = 0;
while((c = text[i++]) != '\0') {
if(c == '\n') {
if(lineWidth > width) {
width = lineWidth;
}
lineWidth = 0;
height += RENDER_TEXT_TILE_HEIGHT;
continue;
}
// renderTextDrawChar(posX, posY, c, r, g, b);
// posX += FONT_TILE_WIDTH;
// }
// }
lineWidth += RENDER_TEXT_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");
if(lineWidth > width) {
width = lineWidth;
}
// 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;
// }
// lineWidth += FONT_TILE_WIDTH;
// void renderTextDispose(void) {
// textureDispose(&RENDER_TEXT_TEXTURE);
// }
*outWidth = width;
*outHeight = height;
}

View File

@@ -6,14 +6,28 @@
*/
#pragma once
#include "display/texture/texture.h"
#include "asset/assetmanager.h"
extern texture_t RENDER_TEXT_TEXTURE;
#define RENDER_TEXT_CHAR_START '@'
#define RENDER_TEXT_COLUMN_COUNT 16
#define RENDER_TEXT_ROW_COUNT 6
#define RENDER_TEXT_TILE_COUNT (RENDER_TEXT_COLUMN_COUNT*RENDER_TEXT_ROW_COUNT)
#define RENDER_TEXT_TILE_WIDTH 6.0f
#define RENDER_TEXT_TILE_HEIGHT 10.0f
typedef struct {
ref_t assetRef;
asset_t *asset;
} rendertext_t;
extern rendertext_t RENDER_TEXT;
/**
* Initializes the text rendering system.
*/
void renderTextInit(void);
errorret_t renderTextInit(void);
/**
* Draws a single character at the specified position.