GUI: Refactor font code to be more central

This commit is contained in:
Jeffrey Pfau 2015-08-26 21:41:49 -07:00
parent 73e190ff82
commit 87a05e3ed1
7 changed files with 115 additions and 76 deletions

View File

@ -40,28 +40,25 @@ unsigned GUIFontHeight(const struct GUIFont* font) {
return GLYPH_HEIGHT;
}
void GUIFontPrintf(const struct GUIFont* font, int x, int y, enum GUITextAlignment align, uint32_t color, const char* text, ...) {
UNUSED(align); // TODO
char buffer[256];
va_list args;
va_start(args, text);
int len = vsnprintf(buffer, sizeof(buffer), text, args);
va_end(args);
int i;
for (i = 0; i < len; ++i) {
char c = buffer[i];
if (c > 0x7F) {
c = 0;
}
struct GUIFontGlyphMetric metric = defaultFontMetrics[c];
sf2d_draw_texture_part_blend(font->tex,
x - metric.padding.left,
y - GLYPH_HEIGHT,
(c & 15) * CELL_WIDTH,
(c >> 4) * CELL_HEIGHT,
CELL_WIDTH,
CELL_HEIGHT,
color);
x += metric.width;
unsigned GUIFontGlyphWidth(const struct GUIFont* font, uint32_t glyph) {
UNUSED(font);
if (glyph > 0x7F) {
glyph = 0;
}
return defaultFontMetrics[glyph].width;
}
void GUIFontDrawGlyph(const struct GUIFont* font, int x, int y, uint32_t color, uint32_t glyph) {
if (glyph > 0x7F) {
glyph = 0;
}
struct GUIFontGlyphMetric metric = defaultFontMetrics[glyph];
sf2d_draw_texture_part_blend(font->tex,
x - metric.padding.left,
y - GLYPH_HEIGHT,
(glyph & 15) * CELL_WIDTH,
(glyph >> 4) * CELL_HEIGHT,
CELL_WIDTH,
CELL_HEIGHT,
color);
}

View File

@ -113,7 +113,7 @@ int main() {
break;
}
_drawStart();
GUIFontPrintf(font, 130, (GUIFontHeight(font) + 240) / 2, GUI_TEXT_LEFT, 0xFFFFFFFF, "Loading...");
GUIFontPrintf(font, 160, (GUIFontHeight(font) + 240) / 2, GUI_TEXT_CENTER, 0xFFFFFFFF, "Loading...");
_drawEnd();
if (!GBAContextLoadROM(&context, path, true)) {
continue;

View File

@ -36,26 +36,23 @@ unsigned GUIFontHeight(const struct GUIFont* font) {
return GLYPH_HEIGHT;
}
void GUIFontPrintf(const struct GUIFont* font, int x, int y, enum GUITextAlignment align, uint32_t color, const char* text, ...) {
UNUSED(align); // TODO
char buffer[256];
va_list args;
va_start(args, text);
int len = vsnprintf(buffer, sizeof(buffer), text, args);
va_end(args);
int i;
for (i = 0; i < len; ++i) {
char c = buffer[i];
if (c > 0x7F) {
c = 0;
}
struct GUIFontGlyphMetric metric = defaultFontMetrics[c];
vita2d_draw_texture_tint_part(font->tex, x, y - GLYPH_HEIGHT + metric.padding.top,
(c & 15) * CELL_WIDTH + metric.padding.left,
(c >> 4) * CELL_HEIGHT + metric.padding.top,
CELL_WIDTH - (metric.padding.left + metric.padding.right),
CELL_HEIGHT - (metric.padding.top + metric.padding.bottom),
color);
x += metric.width;
unsigned GUIFontGlyphWidth(const struct GUIFont* font, uint32_t glyph) {
UNUSED(font);
if (glyph > 0x7F) {
glyph = 0;
}
return defaultFontMetrics[glyph].width;
}
void GUIFontDrawGlyph(const struct GUIFont* font, int x, int y, uint32_t color, uint32_t glyph) {
if (glyph > 0x7F) {
glyph = 0;
}
struct GUIFontGlyphMetric metric = defaultFontMetrics[glyph];
vita2d_draw_texture_tint_part(font->tex, x, y - GLYPH_HEIGHT + metric.padding.top,
(glyph & 15) * CELL_WIDTH + metric.padding.left,
(glyph >> 4) * CELL_HEIGHT + metric.padding.top,
CELL_WIDTH - (metric.padding.left + metric.padding.right),
CELL_HEIGHT - (metric.padding.top + metric.padding.bottom),
color);
}

View File

@ -44,14 +44,15 @@ unsigned GUIFontHeight(const struct GUIFont* font) {
return GLYPH_HEIGHT;
}
void GUIFontPrintf(const struct GUIFont* font, int x, int y, enum GUITextAlignment align, uint32_t color, const char* text, ...) {
UNUSED(align); // TODO
char buffer[256];
va_list args;
va_start(args, text);
int len = vsnprintf(buffer, sizeof(buffer), text, args);
va_end(args);
int i;
unsigned GUIFontGlyphWidth(const struct GUIFont* font, uint32_t glyph) {
UNUSED(font);
if (glyph > 0x7F) {
glyph = 0;
}
return defaultFontMetrics[glyph].width;
}
void GUIFontDrawGlyph(const struct GUIFont* font, int x, int y, uint32_t color, uint32_t glyph) {
GXTexObj tex;
// Grumble grumble, libogc is bad about const-correctness
struct GUIFont* ncfont = font;
@ -61,27 +62,23 @@ void GUIFontPrintf(const struct GUIFont* font, int x, int y, enum GUITextAlignme
GX_SetBlendMode(GX_BM_BLEND, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_NOOP);
GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
for (i = 0; i < len; ++i) {
char c = buffer[i];
if (c > 0x7F) {
c = 0;
}
struct GUIFontGlyphMetric metric = defaultFontMetrics[c];
s16 tx = (c & 15) * CELL_WIDTH + metric.padding.left;
s16 ty = (c >> 4) * CELL_HEIGHT + metric.padding.top;
GX_Begin(GX_QUADS, GX_VTXFMT0, 4);
GX_Position2s16(x, y - GLYPH_HEIGHT + metric.padding.top);
GX_TexCoord2f32(tx / 256.f, ty / 128.f);
GX_Position2s16(x + CELL_WIDTH - (metric.padding.left + metric.padding.right), y - GLYPH_HEIGHT + metric.padding.top);
GX_TexCoord2f32((tx + CELL_WIDTH - (metric.padding.left + metric.padding.right)) / 256.f, ty / 128.f);
GX_Position2s16(x + CELL_WIDTH - (metric.padding.left + metric.padding.right), y - GLYPH_HEIGHT + CELL_HEIGHT - metric.padding.bottom);
GX_TexCoord2f32((tx + CELL_WIDTH - (metric.padding.left + metric.padding.right)) / 256.f, (ty + CELL_HEIGHT - (metric.padding.top + metric.padding.bottom)) / 128.f);
GX_Position2s16(x, y - GLYPH_HEIGHT + CELL_HEIGHT - metric.padding.bottom);
GX_TexCoord2f32(tx / 256.f, (ty + CELL_HEIGHT - (metric.padding.top + metric.padding.bottom)) / 128.f);
GX_End();
x += metric.width;
if (glyph > 0x7F) {
glyph = 0;
}
struct GUIFontGlyphMetric metric = defaultFontMetrics[glyph];
s16 tx = (glyph & 15) * CELL_WIDTH + metric.padding.left;
s16 ty = (glyph >> 4) * CELL_HEIGHT + metric.padding.top;
GX_Begin(GX_QUADS, GX_VTXFMT0, 4);
GX_Position2s16(x, y - GLYPH_HEIGHT + metric.padding.top);
GX_TexCoord2f32(tx / 256.f, ty / 128.f);
GX_Position2s16(x + CELL_WIDTH - (metric.padding.left + metric.padding.right), y - GLYPH_HEIGHT + metric.padding.top);
GX_TexCoord2f32((tx + CELL_WIDTH - (metric.padding.left + metric.padding.right)) / 256.f, ty / 128.f);
GX_Position2s16(x + CELL_WIDTH - (metric.padding.left + metric.padding.right), y - GLYPH_HEIGHT + CELL_HEIGHT - metric.padding.bottom);
GX_TexCoord2f32((tx + CELL_WIDTH - (metric.padding.left + metric.padding.right)) / 256.f, (ty + CELL_HEIGHT - (metric.padding.top + metric.padding.bottom)) / 128.f);
GX_Position2s16(x, y - GLYPH_HEIGHT + CELL_HEIGHT - metric.padding.bottom);
GX_TexCoord2f32(tx / 256.f, (ty + CELL_HEIGHT - (metric.padding.top + metric.padding.bottom)) / 128.f);
GX_End();
}

View File

@ -324,7 +324,7 @@ static void GBAWiiFrame(void) {
bool GBAWiiLoadGame(const char* path) {
_drawStart();
GUIFontPrintf(font, 0, 30, GUI_TEXT_CENTER, 0xFFFFFFFF, "Loading...");
GUIFontPrintf(font, 176, 120, GUI_TEXT_CENTER, 0xFFFFFFFF, "Loading...");
_drawEnd();
return GBAContextLoadROM(&context, path, true);

44
src/util/gui/font.c Normal file
View File

@ -0,0 +1,44 @@
/* Copyright (c) 2013-2015 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "util/gui/font.h"
unsigned GUIFontSpanWidth(const struct GUIFont* font, const char* text) {
unsigned width = 0;
size_t i;
for (i = 0; text[i]; ++i) {
char c = text[i];
width += GUIFontGlyphWidth(font, c);
}
return width;
}
void GUIFontPrint(const struct GUIFont* font, int x, int y, enum GUITextAlignment align, uint32_t color, const char* text) {
switch (align) {
case GUI_TEXT_CENTER:
x -= GUIFontSpanWidth(font, text) / 2;
break;
case GUI_TEXT_RIGHT:
x -= GUIFontSpanWidth(font, text);
break;
default:
break;
}
size_t i;
for (i = 0; text[i]; ++i) {
char c = text[i];
GUIFontDrawGlyph(font, x, y, color, c);
x += GUIFontGlyphWidth(font, c);
}
}
void GUIFontPrintf(const struct GUIFont* font, int x, int y, enum GUITextAlignment align, uint32_t color, const char* text, ...) {
char buffer[256];
va_list args;
va_start(args, text);
vsnprintf(buffer, sizeof(buffer), text, args);
va_end(args);
GUIFontPrint(font, x, y, align, color, buffer);
}

View File

@ -30,7 +30,11 @@ struct GUIFontGlyphMetric {
};
unsigned GUIFontHeight(const struct GUIFont*);
unsigned GUIFontGlyphWidth(const struct GUIFont*, uint32_t glyph);
unsigned GUIFontSpanWidth(const struct GUIFont*, const char* text);
void GUIFontPrintf(const struct GUIFont*, int x, int y, enum GUITextAlignment, uint32_t color, const char* text, ...);
void GUIFontPrint(const struct GUIFont*, int x, int y, enum GUITextAlignment, uint32_t color, const char* text);
void GUIFontDrawGlyph(const struct GUIFont*, int x, int y, uint32_t color, uint32_t glyph);
#endif