Correctly align text rendering to baseline

This commit is contained in:
Doyle Thai 2016-07-07 22:30:06 +10:00
parent 5ef54e16be
commit 13a2152bf6
5 changed files with 21 additions and 12 deletions

Binary file not shown.

View File

@ -9,6 +9,12 @@
#include "Dengine/Platform.h" #include "Dengine/Platform.h"
#include "Dengine/AssetManager.h" #include "Dengine/AssetManager.h"
//#define WT_RENDER_FONT_FILE
#ifdef WT_RENDER_FONT_FILE
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <STB/stb_image_write.h>
#endif
Texture *asset_getTexture(AssetManager *assetManager, const enum TexList type) Texture *asset_getTexture(AssetManager *assetManager, const enum TexList type)
{ {
if (type < texlist_count) if (type < texlist_count)
@ -251,6 +257,9 @@ const i32 asset_loadTTFont(AssetManager *assetManager, const char *filePath)
i32 glyphsRemaining = numGlyphs; i32 glyphsRemaining = numGlyphs;
i32 glyphsOnCurrRow = glyphsPerRow; i32 glyphsOnCurrRow = glyphsPerRow;
// TODO(doyle): We copy over the bitmap direct to the font sheet, should we
// align the baselines up so we don't need to do baseline adjusting at
// render?
i32 atlasIndex = 0; i32 atlasIndex = 0;
for (i32 row = 0; row < MAX_TEXTURE_SIZE; row++) for (i32 row = 0; row < MAX_TEXTURE_SIZE; row++)
{ {
@ -331,6 +340,12 @@ const i32 asset_loadTTFont(AssetManager *assetManager, const char *filePath)
CAST(u8 *)fontBitmap); CAST(u8 *)fontBitmap);
assetManager->textures[texlist_font] = tex; assetManager->textures[texlist_font] = tex;
#ifdef WT_RENDER_FONT_FILE
/* save out a 4 channel image */
stbi_write_png("out.png", MAX_TEXTURE_SIZE, MAX_TEXTURE_SIZE, 4, fontBitmap,
MAX_TEXTURE_SIZE * 4);
#endif
font->tex = &assetManager->textures[texlist_font]; font->tex = &assetManager->textures[texlist_font];
font->atlas = &assetManager->texAtlas[texlist_font]; font->atlas = &assetManager->texAtlas[texlist_font];

View File

@ -1,6 +1,8 @@
#include "Dengine/OpenGL.h" #include "Dengine/OpenGL.h"
#include "Dengine/Renderer.h" #include "Dengine/Renderer.h"
#define RENDER_BOUNDING_BOX FALSE
void renderer_entity(Renderer *renderer, Entity *entity, f32 rotate, v3 color) void renderer_entity(Renderer *renderer, Entity *entity, f32 rotate, v3 color)
{ {
renderer_object(renderer, entity->pos, entity->size, rotate, color, renderer_object(renderer, entity->pos, entity->size, rotate, color,
@ -28,7 +30,7 @@ void renderer_object(Renderer *renderer, v2 pos, v2 size, f32 rotate, v3 color,
// TODO(doyle): Unimplemented // TODO(doyle): Unimplemented
// this->shader->uniformSetVec3f("spriteColor", color); // this->shader->uniformSetVec3f("spriteColor", color);
#if 1 #if RENDER_BOUNDING_BOX
glBindVertexArray(renderer->vao); glBindVertexArray(renderer->vao);
glDrawArrays(GL_TRIANGLE_STRIP, 0, renderer->numVertexesInVbo); glDrawArrays(GL_TRIANGLE_STRIP, 0, renderer->numVertexesInVbo);
glBindVertexArray(0); glBindVertexArray(0);

View File

@ -318,7 +318,6 @@ void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt)
RenderQuad worldQuads[ARRAY_COUNT(world->tiles)] = {0}; RenderQuad worldQuads[ARRAY_COUNT(world->tiles)] = {0};
i32 quadIndex = 0; i32 quadIndex = 0;
/* Render background tiles */ /* Render background tiles */
const v2 tileSize = V2(CAST(f32) state->tileSize, CAST(f32) state->tileSize); const v2 tileSize = V2(CAST(f32) state->tileSize, CAST(f32) state->tileSize);
for (i32 i = 0; i < ARRAY_COUNT(world->tiles); i++) for (i32 i = 0; i < ARRAY_COUNT(world->tiles); i++)
@ -358,16 +357,7 @@ void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt)
i32 codepoint = string[i]; i32 codepoint = string[i];
i32 relativeIndex = codepoint - font->codepointRange.x; i32 relativeIndex = codepoint - font->codepointRange.x;
CharMetrics charMetric = font->charMetrics[relativeIndex]; CharMetrics charMetric = font->charMetrics[relativeIndex];
yPosOnScreen = baseline - charMetric.offset.y;
i32 charBaseline = (font->maxSize.h - charMetric.trueSize.h);
if (codepoint == 'a')
{
yPosOnScreen = baseline - charBaseline;
}
else
{
yPosOnScreen = baseline;
}
const v4 charRectOnScreen = getRect( const v4 charRectOnScreen = getRect(
V2(xPosOnScreen, yPosOnScreen), V2(xPosOnScreen, yPosOnScreen),

View File

@ -32,6 +32,7 @@ typedef struct TexAtlas
v4 texRect[128]; v4 texRect[128];
} TexAtlas; } TexAtlas;
// TODO(doyle): We only use the offset and advance metric at the moment, remove?
typedef struct FontMetrics typedef struct FontMetrics
{ {
i32 ascent; i32 ascent;
@ -43,6 +44,7 @@ typedef struct CharMetrics
{ {
i32 advance; i32 advance;
i32 leftSideBearing; i32 leftSideBearing;
// TODO(doyle): Utilise kerning
i32 *kerning; i32 *kerning;
v2i offset; v2i offset;
v2i trueSize; v2i trueSize;