all repos — mgba @ 87a05e3ed1828cea1ac54e9397f5e69825e92efc

mGBA Game Boy Advance Emulator

GUI: Refactor font code to be more central
Jeffrey Pfau jeffrey@endrift.com
Wed, 26 Aug 2015 21:41:49 -0700
commit

87a05e3ed1828cea1ac54e9397f5e69825e92efc

parent

73e190ff8215fc01e22179a9b4c55f52faed081b

M src/platform/3ds/gui-font.csrc/platform/3ds/gui-font.c

@@ -40,28 +40,25 @@ UNUSED(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); }
M src/platform/3ds/main.csrc/platform/3ds/main.c

@@ -113,7 +113,7 @@ if (!selectFile(&params, "/", path, currentPath, sizeof(path), GBAIsROM)) {

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;
M src/platform/psp2/gui-font.csrc/platform/psp2/gui-font.c

@@ -36,26 +36,23 @@ UNUSED(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); }
M src/platform/wii/gui-font.csrc/platform/wii/gui-font.c

@@ -44,14 +44,15 @@ UNUSED(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 @@

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); + 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 + 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 + 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; - } + 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(); }
M src/platform/wii/main.csrc/platform/wii/main.c

@@ -324,7 +324,7 @@ }

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);
A src/util/gui/font.c

@@ -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); +}
M src/util/gui/font.hsrc/util/gui/font.h

@@ -30,7 +30,11 @@ } padding;

}; 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