all repos — mgba @ a623bcadc3933fcd21c31376b3ccc0264efa40fc

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 "font.h"
11#include "ctr-gpu.h"
12
13#define CELL_HEIGHT 16
14#define CELL_WIDTH 16
15#define GLYPH_HEIGHT 12
16
17struct GUIFont {
18	struct ctrTexture texture;
19};
20
21struct GUIFont* GUIFontCreate(void) {
22	struct GUIFont* guiFont = malloc(sizeof(struct GUIFont));
23	if (!guiFont) {
24		return 0;
25	}
26
27	struct ctrTexture* tex = &guiFont->texture;
28	ctrTexture_Init(tex);
29	tex->data = vramAlloc(256 * 128 * 2);
30	tex->format = GPU_RGBA5551;
31	tex->width = 256;
32	tex->height = 128;
33
34	GSPGPU_FlushDataCache(NULL, (u8*)font, font_size);
35	GX_RequestDma(NULL, (u32*)font, tex->data, font_size);
36	gspWaitForDMA();
37
38	return guiFont;
39}
40
41void GUIFontDestroy(struct GUIFont* font) {
42	vramFree(font->texture.data);
43	free(font);
44}
45
46unsigned GUIFontHeight(const struct GUIFont* font) {
47	UNUSED(font);
48	return GLYPH_HEIGHT;
49}
50
51unsigned GUIFontGlyphWidth(const struct GUIFont* font, uint32_t glyph) {
52	UNUSED(font);
53	if (glyph > 0x7F) {
54		glyph = 0;
55	}
56	return defaultFontMetrics[glyph].width;
57}
58
59void GUIFontDrawGlyph(const struct GUIFont* font, int glyph_x, int glyph_y, uint32_t color, uint32_t glyph) {
60	ctrActivateTexture(&font->texture);
61
62	if (glyph > 0x7F) {
63		glyph = 0;
64	}
65
66	struct GUIFontGlyphMetric metric = defaultFontMetrics[glyph];
67	u16 x = glyph_x - metric.padding.left;
68	u16 y = glyph_y - GLYPH_HEIGHT;
69	u16 u = (glyph % 16u) * CELL_WIDTH;
70	u16 v = (glyph / 16u) * CELL_HEIGHT;
71
72	ctrAddRect(color, x, y, u, v, CELL_WIDTH, CELL_HEIGHT);
73}