src/platform/wii/gui-font.c (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "util/gui/font.h"
7#include "font.h"
8
9#include <ogc/tpl.h>
10
11#define GLYPH_HEIGHT 11
12#define GLYPH_WIDTH 14
13#define FONT_TRACKING 10
14#define CELL_HEIGHT 16
15#define CELL_WIDTH 16
16
17struct GUIFont {
18 TPLFile tdf;
19};
20
21struct GUIFont* GUIFontCreate(void) {
22 struct GUIFont* guiFont = malloc(sizeof(struct GUIFont));
23 if (!guiFont) {
24 return 0;
25 }
26
27 // libogc's TPL code modifies and frees this itself...
28 void* fontTpl = memalign(32, font_size);
29 if (!fontTpl) {
30 free(guiFont);
31 return 0;
32 }
33 memcpy(fontTpl, font, font_size);
34 TPL_OpenTPLFromMemory(&guiFont->tdf, fontTpl, font_size);
35 return guiFont;
36}
37
38void GUIFontDestroy(struct GUIFont* font) {
39 TPL_CloseTPLFile(&font->tdf);
40 free(font);
41}
42
43int GUIFontHeight(const struct GUIFont* font) {
44 UNUSED(font);
45 return GLYPH_HEIGHT;
46}
47
48void GUIFontPrintf(const struct GUIFont* font, int x, int y, enum GUITextAlignment align, uint32_t color, const char* text, ...) {
49 UNUSED(align); // TODO
50 char buffer[256];
51 va_list args;
52 va_start(args, text);
53 int len = vsnprintf(buffer, sizeof(buffer), text, args);
54 va_end(args);
55 int i;
56 GXTexObj tex;
57 // Grumble grumble, libogc is bad about const-correctness
58 struct GUIFont* ncfont = font;
59 TPL_GetTexture(&ncfont->tdf, 0, &tex);
60 GX_LoadTexObj(&tex, GX_TEXMAP0);
61
62 GX_SetBlendMode(GX_BM_BLEND, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_NOOP);
63 GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
64
65 for (i = 0; i < len; ++i) {
66 char c = buffer[i];
67 if (c > 0x7F) {
68 c = 0;
69 }
70 s16 tx = (c & 15) * CELL_WIDTH + ((CELL_WIDTH - GLYPH_WIDTH) >> 1);
71 s16 ty = (c >> 4) * CELL_HEIGHT + ((CELL_HEIGHT - GLYPH_HEIGHT) >> 1) - 1;
72 GX_Begin(GX_QUADS, GX_VTXFMT0, 4);
73 GX_Position2s16(x, y - GLYPH_HEIGHT);
74 GX_TexCoord2f32(tx / 256.f, ty / 128.f);
75
76 GX_Position2s16(x + GLYPH_WIDTH, y - GLYPH_HEIGHT);
77 GX_TexCoord2f32((tx + CELL_WIDTH) / 256.f, ty / 128.f);
78
79 GX_Position2s16(x + GLYPH_WIDTH, y);
80 GX_TexCoord2f32((tx + CELL_WIDTH) / 256.f, (ty + CELL_HEIGHT) / 128.f);
81
82 GX_Position2s16(x, y);
83 GX_TexCoord2f32(tx / 256.f, (ty + CELL_HEIGHT) / 128.f);
84 GX_End();
85 x += FONT_TRACKING;
86 }
87}