all repos — mgba @ 6ee60dd79b6106385c5b2b26cfab00257fccf19a

mGBA Game Boy Advance Emulator

src/platform/3ds/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 "util/png-io.h"
  9#include "util/vfs.h"
 10#include "platform/3ds/ctr-gpu.h"
 11#include "icons.h"
 12#include "font.h"
 13
 14#define CELL_HEIGHT 16
 15#define CELL_WIDTH 16
 16#define GLYPH_HEIGHT 12
 17
 18struct GUIFont {
 19	struct ctrTexture texture;
 20	struct ctrTexture icons;
 21};
 22
 23struct GUIFont* GUIFontCreate(void) {
 24	struct GUIFont* guiFont = malloc(sizeof(struct GUIFont));
 25	if (!guiFont) {
 26		return 0;
 27	}
 28
 29	struct ctrTexture* tex = &guiFont->texture;
 30	ctrTexture_Init(tex);
 31	tex->data = vramAlloc(256 * 128 * 2);
 32	tex->format = GPU_RGBA5551;
 33	tex->width = 256;
 34	tex->height = 128;
 35
 36	GSPGPU_FlushDataCache(font, font_size);
 37	GX_RequestDma((u32*) font, tex->data, font_size);
 38	gspWaitForDMA();
 39
 40	tex = &guiFont->icons;
 41	ctrTexture_Init(tex);
 42	tex->data = vramAlloc(256 * 64 * 2);
 43	tex->format = GPU_RGBA5551;
 44	tex->width = 256;
 45	tex->height = 64;
 46
 47	GSPGPU_FlushDataCache(icons, icons_size);
 48	GX_RequestDma((u32*) icons, tex->data, icons_size);
 49	gspWaitForDMA();
 50
 51	return guiFont;
 52}
 53
 54void GUIFontDestroy(struct GUIFont* font) {
 55	vramFree(font->texture.data);
 56	vramFree(font->icons.data);
 57	free(font);
 58}
 59
 60unsigned GUIFontHeight(const struct GUIFont* font) {
 61	UNUSED(font);
 62	return GLYPH_HEIGHT;
 63}
 64
 65unsigned GUIFontGlyphWidth(const struct GUIFont* font, uint32_t glyph) {
 66	UNUSED(font);
 67	if (glyph > 0x7F) {
 68		glyph = 0;
 69	}
 70	return defaultFontMetrics[glyph].width;
 71}
 72
 73void GUIFontDrawGlyph(const struct GUIFont* font, int glyph_x, int glyph_y, uint32_t color, uint32_t glyph) {
 74	ctrActivateTexture(&font->texture);
 75
 76	if (glyph > 0x7F) {
 77		glyph = '?';
 78	}
 79
 80	struct GUIFontGlyphMetric metric = defaultFontMetrics[glyph];
 81	u16 x = glyph_x - metric.padding.left;
 82	u16 y = glyph_y - GLYPH_HEIGHT;
 83	u16 u = (glyph % 16u) * CELL_WIDTH;
 84	u16 v = (glyph / 16u) * CELL_HEIGHT;
 85
 86	ctrAddRect(color, x, y, u, v, CELL_WIDTH, CELL_HEIGHT);
 87}
 88
 89void GUIFontDrawIcon(const struct GUIFont* font, int x, int y, enum GUIAlignment align, enum GUIOrientation orient, uint32_t color, enum GUIIcon icon) {
 90	ctrActivateTexture(&font->icons);
 91
 92	if (icon >= GUI_ICON_MAX) {
 93		return;
 94	}
 95
 96	struct GUIIconMetric metric = defaultIconMetrics[icon];
 97	switch (align & GUI_ALIGN_HCENTER) {
 98	case GUI_ALIGN_HCENTER:
 99		x -= metric.width / 2;
100		break;
101	case GUI_ALIGN_RIGHT:
102		x -= metric.width;
103		break;
104	}
105	switch (align & GUI_ALIGN_VCENTER) {
106	case GUI_ALIGN_VCENTER:
107		y -= metric.height / 2;
108		break;
109	case GUI_ALIGN_BOTTOM:
110		y -= metric.height;
111		break;
112	}
113	switch (orient) {
114	case GUI_ORIENT_HMIRROR:
115		ctrAddRectScaled(color, x + metric.width, y, -metric.width, metric.height, metric.x, metric.y, metric.width, metric.height);
116		break;
117	case GUI_ORIENT_VMIRROR:
118		ctrAddRectScaled(color, x, y + metric.height, metric.width, -metric.height, metric.x, metric.y, metric.width, metric.height);
119		break;
120	case GUI_ORIENT_0:
121	default:
122		// TODO: Rotation
123		ctrAddRect(color, x, y, metric.x, metric.y, metric.width, metric.height);
124		break;
125	}
126}
127
128void GUIFontDrawIconSize(const struct GUIFont* font, int x, int y, int w, int h, uint32_t color, enum GUIIcon icon) {
129	ctrActivateTexture(&font->icons);
130
131	if (icon >= GUI_ICON_MAX) {
132		return;
133	}
134
135	struct GUIIconMetric metric = defaultIconMetrics[icon];
136	ctrAddRectScaled(color, x, y, w, h, metric.x, metric.y, metric.width, metric.height);
137}