all repos — mgba @ 0511d0a69e4e86f111cdd5bf60ad46f7bdb0ff5f

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 "icons.h"
  9#include "font.h"
 10
 11#include <malloc.h>
 12#include <ogc/tpl.h>
 13
 14#define GLYPH_HEIGHT 24
 15#define CELL_HEIGHT 32
 16#define CELL_WIDTH 32
 17
 18struct GUIFont {
 19	TPLFile tdf;
 20	TPLFile iconsTdf;
 21};
 22
 23struct GUIFont* GUIFontCreate(void) {
 24	struct GUIFont* guiFont = malloc(sizeof(struct GUIFont));
 25	if (!guiFont) {
 26		return 0;
 27	}
 28
 29	// libogc's TPL code modifies and frees this itself...
 30	void* fontTpl = memalign(32, font_size);
 31	if (!fontTpl) {
 32		free(guiFont);
 33		return 0;
 34	}
 35	memcpy(fontTpl, font, font_size);
 36	TPL_OpenTPLFromMemory(&guiFont->tdf, fontTpl, font_size);
 37
 38	void* iconsTpl = memalign(32, icons_size);
 39	if (!iconsTpl) {
 40		TPL_CloseTPLFile(&guiFont->tdf);
 41		free(guiFont);
 42		return 0;
 43	}
 44	memcpy(iconsTpl, icons, icons_size);
 45	TPL_OpenTPLFromMemory(&guiFont->iconsTdf, iconsTpl, icons_size);
 46	return guiFont;
 47}
 48
 49void GUIFontDestroy(struct GUIFont* font) {
 50	TPL_CloseTPLFile(&font->tdf);
 51	TPL_CloseTPLFile(&font->iconsTdf);
 52	free(font);
 53}
 54
 55unsigned GUIFontHeight(const struct GUIFont* font) {
 56	UNUSED(font);
 57	return GLYPH_HEIGHT;
 58}
 59
 60unsigned GUIFontGlyphWidth(const struct GUIFont* font, uint32_t glyph) {
 61	UNUSED(font);
 62	if (glyph > 0x7F) {
 63		glyph = '?';
 64	}
 65	return defaultFontMetrics[glyph].width * 2;
 66}
 67
 68void GUIFontDrawGlyph(const struct GUIFont* font, int x, int y, uint32_t color, uint32_t glyph) {
 69	color = (color >> 24) | (color << 8);
 70	GXTexObj tex;
 71	// Grumble grumble, libogc is bad about const-correctness
 72	struct GUIFont* ncfont = font;
 73	TPL_GetTexture(&ncfont->tdf, 0, &tex);
 74	GX_LoadTexObj(&tex, GX_TEXMAP0);
 75
 76	GX_SetBlendMode(GX_BM_BLEND, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_NOOP);
 77	GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
 78	GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
 79
 80	if (glyph > 0x7F) {
 81		glyph = '?';
 82	}
 83	struct GUIFontGlyphMetric metric = defaultFontMetrics[glyph];
 84	s16 tx = (glyph & 15) * CELL_WIDTH + metric.padding.left * 2;
 85	s16 ty = (glyph >> 4) * CELL_HEIGHT + metric.padding.top * 2;
 86	GX_Begin(GX_QUADS, GX_VTXFMT0, 4);
 87	GX_Position2s16(x, y - GLYPH_HEIGHT + metric.padding.top * 2);
 88	GX_Color1u32(color);
 89	GX_TexCoord2f32(tx / 512.f, ty / 256.f);
 90
 91	GX_Position2s16(x + CELL_WIDTH - (metric.padding.left + metric.padding.right) * 2, y - GLYPH_HEIGHT + metric.padding.top * 2);
 92	GX_Color1u32(color);
 93	GX_TexCoord2f32((tx + CELL_WIDTH - (metric.padding.left + metric.padding.right) * 2) / 512.f, ty / 256.f);
 94
 95	GX_Position2s16(x + CELL_WIDTH - (metric.padding.left + metric.padding.right) * 2, y - GLYPH_HEIGHT + CELL_HEIGHT - metric.padding.bottom * 2);
 96	GX_Color1u32(color);
 97	GX_TexCoord2f32((tx + CELL_WIDTH - (metric.padding.left + metric.padding.right) * 2) / 512.f, (ty + CELL_HEIGHT - (metric.padding.top + metric.padding.bottom) * 2) / 256.f);
 98
 99	GX_Position2s16(x, y - GLYPH_HEIGHT + CELL_HEIGHT - metric.padding.bottom * 2);
100	GX_Color1u32(color);
101	GX_TexCoord2f32(tx / 512.f, (ty + CELL_HEIGHT - (metric.padding.top + metric.padding.bottom) * 2) / 256.f);
102	GX_End();
103}
104
105void GUIFontDrawIcon(const struct GUIFont* font, int x, int y, enum GUIAlignment align, enum GUIOrientation orient, uint32_t color, enum GUIIcon icon) {
106	if (icon >= GUI_ICON_MAX) {
107		return;
108	}
109
110	color = (color >> 24) | (color << 8);
111	GXTexObj tex;
112
113	struct GUIFont* ncfont = font;
114	TPL_GetTexture(&ncfont->iconsTdf, 0, &tex);
115	GX_LoadTexObj(&tex, GX_TEXMAP0);
116
117	GX_SetBlendMode(GX_BM_BLEND, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_NOOP);
118	GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
119	GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
120
121	struct GUIIconMetric metric = defaultIconMetrics[icon];
122	switch (align & GUI_ALIGN_HCENTER) {
123	case GUI_ALIGN_HCENTER:
124		x -= metric.width;
125		break;
126	case GUI_ALIGN_RIGHT:
127		x -= metric.width * 2;
128		break;
129	}
130	switch (align & GUI_ALIGN_VCENTER) {
131	case GUI_ALIGN_VCENTER:
132		y -= metric.height;
133		break;
134	case GUI_ALIGN_BOTTOM:
135		y -= metric.height * 2;
136		break;
137	}
138
139	float u[4];
140	float v[4];
141
142	switch (orient) {
143	case GUI_ORIENT_0:
144	default:
145		// TODO: Rotations
146		u[0] = u[3] = metric.x / 256.f;
147		u[1] = u[2] = (metric.x + metric.width) / 256.f;
148		v[0] = v[1] = (metric.y + metric.height) / 64.f;
149		v[2] = v[3] = metric.y / 64.f;
150		break;
151	case GUI_ORIENT_HMIRROR:
152		u[0] = u[3] = (metric.x + metric.width) / 256.f;
153		u[1] = u[2] = metric.x / 256.f;
154		v[0] = v[1] = (metric.y + metric.height) / 64.f;
155		v[2] = v[3] = metric.y / 64.f;
156		break;
157	case GUI_ORIENT_VMIRROR:
158		u[0] = u[3] = metric.x / 256.f;
159		u[1] = u[2] = (metric.x + metric.width) / 256.f;
160		v[0] = v[1] = metric.y / 64.f;
161		v[2] = v[3] = (metric.y + metric.height) / 64.f;
162		break;
163	}
164
165	GX_Begin(GX_QUADS, GX_VTXFMT0, 4);
166	GX_Position2s16(x, y + metric.height * 2);
167	GX_Color1u32(color);
168	GX_TexCoord2f32(u[0], v[0]);
169
170	GX_Position2s16(x + metric.width * 2, y + metric.height * 2);
171	GX_Color1u32(color);
172	GX_TexCoord2f32(u[1], v[1]);
173
174	GX_Position2s16(x + metric.width * 2, y);
175	GX_Color1u32(color);
176	GX_TexCoord2f32(u[2], v[2]);
177
178	GX_Position2s16(x, y);
179	GX_Color1u32(color);
180	GX_TexCoord2f32(u[3], v[3]);
181	GX_End();
182}