all repos — mgba @ 73e190ff8215fc01e22179a9b4c55f52faed081b

mGBA Game Boy Advance Emulator

src/platform/psp2/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 <vita2d.h>
11
12#define CELL_HEIGHT 16
13#define CELL_WIDTH 16
14#define GLYPH_HEIGHT 12
15
16struct GUIFont {
17	vita2d_texture* tex;
18};
19
20struct GUIFont* GUIFontCreate(void) {
21	struct GUIFont* guiFont = malloc(sizeof(struct GUIFont));
22	if (!guiFont) {
23		return 0;
24	}
25	guiFont->tex = vita2d_load_PNG_buffer(font);
26	return guiFont;
27}
28
29void GUIFontDestroy(struct GUIFont* font) {
30	vita2d_free_texture(font->tex);
31	free(font);
32}
33
34unsigned GUIFontHeight(const struct GUIFont* font) {
35	UNUSED(font);
36	return GLYPH_HEIGHT;
37}
38
39void GUIFontPrintf(const struct GUIFont* font, int x, int y, enum GUITextAlignment align, uint32_t color, const char* text, ...) {
40	UNUSED(align); // TODO
41	char buffer[256];
42	va_list args;
43	va_start(args, text);
44	int len = vsnprintf(buffer, sizeof(buffer), text, args);
45	va_end(args);
46	int i;
47	for (i = 0; i < len; ++i) {
48		char c = buffer[i];
49		if (c > 0x7F) {
50			c = 0;
51		}
52		struct GUIFontGlyphMetric metric = defaultFontMetrics[c];
53		vita2d_draw_texture_tint_part(font->tex, x, y - GLYPH_HEIGHT + metric.padding.top,
54			                          (c & 15) * CELL_WIDTH + metric.padding.left,
55			                          (c >> 4) * CELL_HEIGHT + metric.padding.top,
56			                          CELL_WIDTH - (metric.padding.left + metric.padding.right),
57			                          CELL_HEIGHT - (metric.padding.top + metric.padding.bottom),
58			                          color);
59		x += metric.width;
60	}
61}