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 "util/gui/font-metrics.h"
8#include "font.h"
9
10#include <ogc/tpl.h>
11
12#define GLYPH_HEIGHT 12
13#define CELL_HEIGHT 16
14#define CELL_WIDTH 16
15
16struct GUIFont {
17 TPLFile tdf;
18};
19
20struct GUIFont* GUIFontCreate(void) {
21 struct GUIFont* guiFont = malloc(sizeof(struct GUIFont));
22 if (!guiFont) {
23 return 0;
24 }
25
26 // libogc's TPL code modifies and frees this itself...
27 void* fontTpl = memalign(32, font_size);
28 if (!fontTpl) {
29 free(guiFont);
30 return 0;
31 }
32 memcpy(fontTpl, font, font_size);
33 TPL_OpenTPLFromMemory(&guiFont->tdf, fontTpl, font_size);
34 return guiFont;
35}
36
37void GUIFontDestroy(struct GUIFont* font) {
38 TPL_CloseTPLFile(&font->tdf);
39 free(font);
40}
41
42unsigned GUIFontHeight(const struct GUIFont* font) {
43 UNUSED(font);
44 return GLYPH_HEIGHT;
45}
46
47unsigned GUIFontGlyphWidth(const struct GUIFont* font, uint32_t glyph) {
48 UNUSED(font);
49 if (glyph > 0x7F) {
50 glyph = 0;
51 }
52 return defaultFontMetrics[glyph].width;
53}
54
55void GUIFontDrawGlyph(const struct GUIFont* font, int x, int y, uint32_t color, uint32_t glyph) {
56 color = (color >> 24) | (color << 8);
57 GXTexObj tex;
58 // Grumble grumble, libogc is bad about const-correctness
59 struct GUIFont* ncfont = font;
60 TPL_GetTexture(&ncfont->tdf, 0, &tex);
61 GX_LoadTexObj(&tex, GX_TEXMAP0);
62
63 GX_SetBlendMode(GX_BM_BLEND, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_NOOP);
64 GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
65 GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
66
67 if (glyph > 0x7F) {
68 glyph = 0;
69 }
70 struct GUIFontGlyphMetric metric = defaultFontMetrics[glyph];
71 s16 tx = (glyph & 15) * CELL_WIDTH + metric.padding.left;
72 s16 ty = (glyph >> 4) * CELL_HEIGHT + metric.padding.top;
73 GX_Begin(GX_QUADS, GX_VTXFMT0, 4);
74 GX_Position2s16(x, y - GLYPH_HEIGHT + metric.padding.top);
75 GX_Color1u32(color);
76 GX_TexCoord2f32(tx / 256.f, ty / 128.f);
77
78 GX_Position2s16(x + CELL_WIDTH - (metric.padding.left + metric.padding.right), y - GLYPH_HEIGHT + metric.padding.top);
79 GX_Color1u32(color);
80 GX_TexCoord2f32((tx + CELL_WIDTH - (metric.padding.left + metric.padding.right)) / 256.f, ty / 128.f);
81
82 GX_Position2s16(x + CELL_WIDTH - (metric.padding.left + metric.padding.right), y - GLYPH_HEIGHT + CELL_HEIGHT - metric.padding.bottom);
83 GX_Color1u32(color);
84 GX_TexCoord2f32((tx + CELL_WIDTH - (metric.padding.left + metric.padding.right)) / 256.f, (ty + CELL_HEIGHT - (metric.padding.top + metric.padding.bottom)) / 128.f);
85
86 GX_Position2s16(x, y - GLYPH_HEIGHT + CELL_HEIGHT - metric.padding.bottom);
87 GX_Color1u32(color);
88 GX_TexCoord2f32(tx / 256.f, (ty + CELL_HEIGHT - (metric.padding.top + metric.padding.bottom)) / 128.f);
89 GX_End();
90}