mirror of https://github.com/mgba-emu/mgba.git
GUI: Refactor font code to be more central
This commit is contained in:
parent
73e190ff82
commit
87a05e3ed1
|
@ -40,28 +40,25 @@ unsigned GUIFontHeight(const struct GUIFont* font) {
|
||||||
return GLYPH_HEIGHT;
|
return GLYPH_HEIGHT;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GUIFontPrintf(const struct GUIFont* font, int x, int y, enum GUITextAlignment align, uint32_t color, const char* text, ...) {
|
unsigned GUIFontGlyphWidth(const struct GUIFont* font, uint32_t glyph) {
|
||||||
UNUSED(align); // TODO
|
UNUSED(font);
|
||||||
char buffer[256];
|
if (glyph > 0x7F) {
|
||||||
va_list args;
|
glyph = 0;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -113,7 +113,7 @@ int main() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
_drawStart();
|
_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();
|
_drawEnd();
|
||||||
if (!GBAContextLoadROM(&context, path, true)) {
|
if (!GBAContextLoadROM(&context, path, true)) {
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -36,26 +36,23 @@ unsigned GUIFontHeight(const struct GUIFont* font) {
|
||||||
return GLYPH_HEIGHT;
|
return GLYPH_HEIGHT;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GUIFontPrintf(const struct GUIFont* font, int x, int y, enum GUITextAlignment align, uint32_t color, const char* text, ...) {
|
unsigned GUIFontGlyphWidth(const struct GUIFont* font, uint32_t glyph) {
|
||||||
UNUSED(align); // TODO
|
UNUSED(font);
|
||||||
char buffer[256];
|
if (glyph > 0x7F) {
|
||||||
va_list args;
|
glyph = 0;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,14 +44,15 @@ unsigned GUIFontHeight(const struct GUIFont* font) {
|
||||||
return GLYPH_HEIGHT;
|
return GLYPH_HEIGHT;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GUIFontPrintf(const struct GUIFont* font, int x, int y, enum GUITextAlignment align, uint32_t color, const char* text, ...) {
|
unsigned GUIFontGlyphWidth(const struct GUIFont* font, uint32_t glyph) {
|
||||||
UNUSED(align); // TODO
|
UNUSED(font);
|
||||||
char buffer[256];
|
if (glyph > 0x7F) {
|
||||||
va_list args;
|
glyph = 0;
|
||||||
va_start(args, text);
|
}
|
||||||
int len = vsnprintf(buffer, sizeof(buffer), text, args);
|
return defaultFontMetrics[glyph].width;
|
||||||
va_end(args);
|
}
|
||||||
int i;
|
|
||||||
|
void GUIFontDrawGlyph(const struct GUIFont* font, int x, int y, uint32_t color, uint32_t glyph) {
|
||||||
GXTexObj tex;
|
GXTexObj tex;
|
||||||
// Grumble grumble, libogc is bad about const-correctness
|
// Grumble grumble, libogc is bad about const-correctness
|
||||||
struct GUIFont* ncfont = font;
|
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_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);
|
GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
|
||||||
|
|
||||||
for (i = 0; i < len; ++i) {
|
if (glyph > 0x7F) {
|
||||||
char c = buffer[i];
|
glyph = 0;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
|
|
|
@ -324,7 +324,7 @@ static void GBAWiiFrame(void) {
|
||||||
|
|
||||||
bool GBAWiiLoadGame(const char* path) {
|
bool GBAWiiLoadGame(const char* path) {
|
||||||
_drawStart();
|
_drawStart();
|
||||||
GUIFontPrintf(font, 0, 30, GUI_TEXT_CENTER, 0xFFFFFFFF, "Loading...");
|
GUIFontPrintf(font, 176, 120, GUI_TEXT_CENTER, 0xFFFFFFFF, "Loading...");
|
||||||
_drawEnd();
|
_drawEnd();
|
||||||
|
|
||||||
return GBAContextLoadROM(&context, path, true);
|
return GBAContextLoadROM(&context, path, true);
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
|
@ -30,7 +30,11 @@ struct GUIFontGlyphMetric {
|
||||||
};
|
};
|
||||||
|
|
||||||
unsigned GUIFontHeight(const struct GUIFont*);
|
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 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
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue