all repos — mgba @ d53004650d5fbf3bc5b3f830e4fce604bee70132

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
  9#include <vita2d.h>
 10
 11#define CELL_HEIGHT 32
 12#define CELL_WIDTH 32
 13#define GLYPH_HEIGHT 24
 14
 15extern const uint8_t _binary_font2x_png_start[];
 16extern const uint8_t _binary_icons2x_png_start[];
 17
 18struct GUIFont {
 19	vita2d_texture* tex;
 20	vita2d_texture* icons;
 21};
 22
 23struct GUIFont* GUIFontCreate(void) {
 24	struct GUIFont* font = malloc(sizeof(struct GUIFont));
 25	if (!font) {
 26		return 0;
 27	}
 28	font->tex = vita2d_load_PNG_buffer(_binary_font2x_png_start);
 29	font->icons = vita2d_load_PNG_buffer(_binary_icons2x_png_start);
 30	return font;
 31}
 32
 33void GUIFontDestroy(struct GUIFont* font) {
 34	vita2d_free_texture(font->tex);
 35	vita2d_free_texture(font->icons);
 36	free(font);
 37}
 38
 39unsigned GUIFontHeight(const struct GUIFont* font) {
 40	UNUSED(font);
 41	return GLYPH_HEIGHT;
 42}
 43
 44unsigned GUIFontGlyphWidth(const struct GUIFont* font, uint32_t glyph) {
 45	UNUSED(font);
 46	if (glyph > 0x7F) {
 47		glyph = '?';
 48	}
 49	return defaultFontMetrics[glyph].width * 2;
 50}
 51
 52void GUIFontDrawGlyph(const struct GUIFont* font, int x, int y, uint32_t color, uint32_t glyph) {
 53	if (glyph > 0x7F) {
 54		glyph = '?';
 55	}
 56	struct GUIFontGlyphMetric metric = defaultFontMetrics[glyph];
 57	vita2d_draw_texture_tint_part(font->tex, x, y - GLYPH_HEIGHT + metric.padding.top * 2,
 58	                                    (glyph & 15) * CELL_WIDTH + metric.padding.left * 2,
 59	                                    (glyph >> 4) * CELL_HEIGHT + metric.padding.top * 2,
 60	                                    CELL_WIDTH - (metric.padding.left + metric.padding.right) * 2,
 61	                                    CELL_HEIGHT - (metric.padding.top + metric.padding.bottom) * 2,
 62	                                    color);
 63}
 64
 65void GUIFontDrawIcon(const struct GUIFont* font, int x, int y, enum GUIAlignment align, enum GUIOrientation orient, uint32_t color, enum GUIIcon icon) {
 66	if (icon >= GUI_ICON_MAX) {
 67		return;
 68	}
 69	struct GUIIconMetric metric = defaultIconMetrics[icon];
 70	switch (align & GUI_ALIGN_HCENTER) {
 71	case GUI_ALIGN_HCENTER:
 72		x -= metric.width;
 73		break;
 74	case GUI_ALIGN_RIGHT:
 75		x -= metric.width * 2;
 76		break;
 77	}
 78	switch (align & GUI_ALIGN_VCENTER) {
 79	case GUI_ALIGN_VCENTER:
 80		y -= metric.height;
 81		break;
 82	case GUI_ALIGN_BOTTOM:
 83		y -= metric.height * 2;
 84		break;
 85	}
 86
 87	switch (orient) {
 88	case GUI_ORIENT_HMIRROR:
 89		vita2d_draw_texture_tint_part_scale(font->icons, x + metric.width * 2, y,
 90		                                    metric.x * 2, metric.y * 2,
 91		                                    metric.width * 2, metric.height * 2,
 92		                                    -1, 1, color);
 93		return;
 94	case GUI_ORIENT_VMIRROR:
 95		vita2d_draw_texture_tint_part_scale(font->icons, x, y + metric.height * 2,
 96		                                    metric.x * 2, metric.y * 2,
 97		                                    metric.width * 2, metric.height * 2,
 98		                                    1, -1, color);
 99		return;
100	case GUI_ORIENT_0:
101	default:
102		// TOOD: Rotate
103		vita2d_draw_texture_tint_part(font->icons, x, y,
104		                                    metric.x * 2, metric.y * 2,
105		                                    metric.width * 2, metric.height * 2,
106		                                    color);
107		break;
108	}
109}
110
111void GUIFontDrawIconSize(const struct GUIFont* font, int x, int y, int w, int h, uint32_t color, enum GUIIcon icon) {
112	if (icon >= GUI_ICON_MAX) {
113		return;
114	}
115	struct GUIIconMetric metric = defaultIconMetrics[icon];
116	vita2d_draw_texture_tint_part_scale(font->icons, x, y,
117	                                    metric.x * 2, metric.y * 2,
118	                                    metric.width * 2, metric.height * 2,
119	                                    w / (float) metric.width, h / (float) metric.height, color);
120}