all repos — mgba @ 49e66da5c222fd957caf5026f8648ddd3d595f5d

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
12#include <sf2d.h>
13
14#define CELL_HEIGHT 16
15#define CELL_WIDTH 16
16#define GLYPH_HEIGHT 12
17
18struct GUIFont {
19	sf2d_texture* tex;
20};
21
22struct GUIFont* GUIFontCreate(void) {
23	struct GUIFont* guiFont = malloc(sizeof(struct GUIFont));
24	if (!guiFont) {
25		return 0;
26	}
27	guiFont->tex = sf2d_create_texture(256, 128, TEXFMT_RGB5A1, SF2D_PLACE_RAM);
28	memcpy(guiFont->tex->data, font, font_size);
29	guiFont->tex->tiled = 1;
30	return guiFont;
31}
32
33void GUIFontDestroy(struct GUIFont* font) {
34	sf2d_free_texture(font->tex);
35	free(font);
36}
37
38int GUIFontHeight(const struct GUIFont* font) {
39	UNUSED(font);
40	return GLYPH_HEIGHT;
41}
42
43void GUIFontPrintf(const struct GUIFont* font, int x, int y, enum GUITextAlignment align, uint32_t color, const char* text, ...) {
44	UNUSED(align); // TODO
45	char buffer[256];
46	va_list args;
47	va_start(args, text);
48	int len = vsnprintf(buffer, sizeof(buffer), text, args);
49	va_end(args);
50	int i;
51	for (i = 0; i < len; ++i) {
52		char c = buffer[i];
53		if (c > 0x7F) {
54			c = 0;
55		}
56		struct GUIFontGlyphMetric metric = defaultFontMetrics[c];
57		sf2d_draw_texture_part_blend(font->tex,
58		                             x - metric.padding.left,
59		                             y - GLYPH_HEIGHT,
60		                             (c & 15) * CELL_WIDTH,
61		                             (c >> 4) * CELL_HEIGHT,
62		                             CELL_WIDTH,
63		                             CELL_HEIGHT,
64		                             color);
65		x += metric.width;
66	}
67}