all repos — mgba @ 77ead17e290ce40fa7ef4f1d7a859e20d18fc3eb

mGBA Game Boy Advance Emulator

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 <malloc.h>
11#include <ogc/tpl.h>
12
13#define GLYPH_HEIGHT 12
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
43unsigned GUIFontHeight(const struct GUIFont* font) {
44	UNUSED(font);
45	return GLYPH_HEIGHT;
46}
47
48unsigned GUIFontGlyphWidth(const struct GUIFont* font, uint32_t glyph) {
49	UNUSED(font);
50	if (glyph > 0x7F) {
51		glyph = 0;
52	}
53	return defaultFontMetrics[glyph].width;
54}
55
56void GUIFontDrawGlyph(const struct GUIFont* font, int x, int y, uint32_t color, uint32_t glyph) {
57	color = (color >> 24) | (color << 8);
58	GXTexObj tex;
59	// Grumble grumble, libogc is bad about const-correctness
60	struct GUIFont* ncfont = font;
61	TPL_GetTexture(&ncfont->tdf, 0, &tex);
62	GX_LoadTexObj(&tex, GX_TEXMAP0);
63
64	GX_SetBlendMode(GX_BM_BLEND, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_NOOP);
65	GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
66	GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
67
68	if (glyph > 0x7F) {
69		glyph = 0;
70	}
71	struct GUIFontGlyphMetric metric = defaultFontMetrics[glyph];
72	s16 tx = (glyph & 15) * CELL_WIDTH + metric.padding.left;
73	s16 ty = (glyph >> 4) * CELL_HEIGHT + metric.padding.top;
74	GX_Begin(GX_QUADS, GX_VTXFMT0, 4);
75	GX_Position2s16(x, y - GLYPH_HEIGHT + metric.padding.top);
76	GX_Color1u32(color);
77	GX_TexCoord2f32(tx / 256.f, ty / 128.f);
78
79	GX_Position2s16(x + CELL_WIDTH - (metric.padding.left + metric.padding.right), y - GLYPH_HEIGHT + metric.padding.top);
80	GX_Color1u32(color);
81	GX_TexCoord2f32((tx + CELL_WIDTH - (metric.padding.left + metric.padding.right)) / 256.f, ty / 128.f);
82
83	GX_Position2s16(x + CELL_WIDTH - (metric.padding.left + metric.padding.right), y - GLYPH_HEIGHT + CELL_HEIGHT - metric.padding.bottom);
84	GX_Color1u32(color);
85	GX_TexCoord2f32((tx + CELL_WIDTH - (metric.padding.left + metric.padding.right)) / 256.f, (ty + CELL_HEIGHT - (metric.padding.top + metric.padding.bottom)) / 128.f);
86
87	GX_Position2s16(x, y - GLYPH_HEIGHT + CELL_HEIGHT - metric.padding.bottom);
88	GX_Color1u32(color);
89	GX_TexCoord2f32(tx / 256.f, (ty + CELL_HEIGHT - (metric.padding.top + metric.padding.bottom)) / 128.f);
90	GX_End();
91}