2019-01-07 21:49:55 +01:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2010-2019 - Francisco Javier Trujillo Mata
|
|
|
|
*
|
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-10-07 23:45:54 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2021-12-15 19:01:56 +01:00
|
|
|
#include <math.h>
|
2019-10-07 23:45:54 +02:00
|
|
|
#include <malloc.h>
|
2019-01-07 21:49:55 +01:00
|
|
|
#include <gsKit.h>
|
|
|
|
#include <dmaKit.h>
|
|
|
|
#include <gsToolkit.h>
|
2021-12-15 19:01:56 +01:00
|
|
|
#include <encodings/utf.h>
|
|
|
|
|
|
|
|
#include "../common/ps2_common.h"
|
2019-01-07 21:49:55 +01:00
|
|
|
|
|
|
|
#include "../font_driver.h"
|
|
|
|
|
2021-12-15 19:01:56 +01:00
|
|
|
#include "../../configuration.h"
|
|
|
|
#include "../../verbosity.h"
|
|
|
|
|
2021-01-15 22:29:53 +01:00
|
|
|
typedef struct
|
2019-01-07 21:49:55 +01:00
|
|
|
{
|
2021-01-15 22:29:53 +01:00
|
|
|
GSTEXTURE *texture;
|
|
|
|
const font_renderer_driver_t* font_driver;
|
|
|
|
void* font_data;
|
|
|
|
} ps2_font_t;
|
2019-01-07 21:49:55 +01:00
|
|
|
|
2022-06-26 18:01:43 +02:00
|
|
|
static void* ps2_font_init(void* data, const char* font_path,
|
2019-01-07 21:49:55 +01:00
|
|
|
float font_size, bool is_threaded)
|
|
|
|
{
|
2021-01-15 22:29:53 +01:00
|
|
|
uint32_t j;
|
2022-07-07 21:29:14 +02:00
|
|
|
int text_size, clut_size;
|
|
|
|
uint8_t *tex8;
|
|
|
|
uint32_t *clut32;
|
|
|
|
const struct font_atlas* atlas = NULL;
|
2021-01-15 22:29:53 +01:00
|
|
|
ps2_font_t* font = (ps2_font_t*)calloc(1, sizeof(*font));
|
|
|
|
|
|
|
|
if (!font)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!font_renderer_create_default(
|
|
|
|
&font->font_driver,
|
|
|
|
&font->font_data, font_path, font_size))
|
|
|
|
{
|
|
|
|
RARCH_WARN("Couldn't initialize font renderer.\n");
|
|
|
|
free(font);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-07-07 21:29:14 +02:00
|
|
|
atlas = font->font_driver->get_atlas(font->font_data);
|
|
|
|
font->texture = (GSTEXTURE*)calloc(1, sizeof(GSTEXTURE));
|
|
|
|
font->texture->Width = atlas->width;
|
|
|
|
font->texture->Height = atlas->height;
|
|
|
|
font->texture->PSM = GS_PSM_T8;
|
2021-01-15 22:29:53 +01:00
|
|
|
font->texture->ClutPSM = GS_PSM_CT32;
|
2022-07-07 21:29:14 +02:00
|
|
|
font->texture->Filter = GS_FILTER_NEAREST;
|
2021-01-15 22:29:53 +01:00
|
|
|
|
2022-07-07 21:29:14 +02:00
|
|
|
/* Convert to 8bit texture */
|
|
|
|
text_size = gsKit_texture_size_ee(atlas->width, atlas->height, GS_PSM_T8);
|
|
|
|
tex8 = (uint8_t*)malloc(text_size);
|
2021-01-15 22:29:53 +01:00
|
|
|
for (j = 0; j < atlas->width * atlas->height; j++ )
|
2022-07-07 21:29:14 +02:00
|
|
|
tex8[j] = atlas->buffer[j] & 0x000000FF;
|
|
|
|
font->texture->Mem = (u32 *)tex8;
|
|
|
|
|
|
|
|
/* Create 8bit CLUT */
|
|
|
|
clut_size = gsKit_texture_size_ee(16, 16, GS_PSM_CT32);
|
|
|
|
clut32 = (uint32_t*)malloc(clutSize);
|
|
|
|
for (j = 0; j < 256; j++)
|
|
|
|
clut32[j] = 0x01010101 * j;
|
2021-01-15 22:29:53 +01:00
|
|
|
font->texture->Clut = (u32 *)clut32;
|
|
|
|
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
2022-06-26 18:01:43 +02:00
|
|
|
static void ps2_font_free(void* data, bool is_threaded)
|
2021-01-15 22:29:53 +01:00
|
|
|
{
|
|
|
|
ps2_font_t* font = (ps2_font_t*)data;
|
|
|
|
|
|
|
|
if (!font)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (font->font_driver && font->font_data)
|
|
|
|
font->font_driver->free(font->font_data);
|
|
|
|
|
|
|
|
if (font->texture->Clut)
|
|
|
|
free(font->texture->Clut);
|
|
|
|
|
|
|
|
if (font->texture->Mem)
|
|
|
|
free(font->texture->Mem);
|
|
|
|
|
|
|
|
if (font->texture)
|
|
|
|
free(font->texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ps2_font_get_message_width(void* data, const char* msg,
|
|
|
|
unsigned msg_len, float scale)
|
|
|
|
{
|
2022-05-16 19:41:00 +02:00
|
|
|
const struct font_glyph* glyph_q = NULL;
|
2021-01-15 22:29:53 +01:00
|
|
|
unsigned i;
|
2022-05-16 19:41:00 +02:00
|
|
|
int delta_x = 0;
|
|
|
|
ps2_font_t* font = (ps2_font_t*)data;
|
2021-01-15 22:29:53 +01:00
|
|
|
|
|
|
|
if (!font)
|
|
|
|
return 0;
|
|
|
|
|
2022-05-16 19:41:00 +02:00
|
|
|
glyph_q = font->font_driver->get_glyph(font->font_data, '?');
|
|
|
|
|
2021-01-15 22:29:53 +01:00
|
|
|
for (i = 0; i < msg_len; i++)
|
|
|
|
{
|
2022-05-16 19:41:00 +02:00
|
|
|
const struct font_glyph* glyph;
|
2021-01-15 22:29:53 +01:00
|
|
|
const char* msg_tmp = &msg[i];
|
|
|
|
unsigned code = utf8_walk(&msg_tmp);
|
|
|
|
unsigned skip = msg_tmp - &msg[i];
|
|
|
|
|
|
|
|
if (skip > 1)
|
|
|
|
i += skip - 1;
|
|
|
|
|
2022-05-16 19:41:00 +02:00
|
|
|
/* Do something smarter here ... */
|
|
|
|
if (!(glyph =
|
|
|
|
font->font_driver->get_glyph(font->font_data, code)))
|
|
|
|
if (!(glyph = glyph_q))
|
|
|
|
continue;
|
2021-01-15 22:29:53 +01:00
|
|
|
|
|
|
|
delta_x += glyph->advance_x;
|
|
|
|
}
|
|
|
|
|
|
|
|
return delta_x * scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ps2_font_render_line(
|
|
|
|
ps2_video_t *ps2,
|
|
|
|
ps2_font_t* font, const char* msg, unsigned msg_len,
|
|
|
|
float scale, const unsigned int color, float pos_x,
|
|
|
|
float pos_y,
|
|
|
|
unsigned width, unsigned height, unsigned text_align)
|
|
|
|
{
|
|
|
|
unsigned i;
|
2022-05-16 19:41:00 +02:00
|
|
|
const struct font_glyph* glyph_q = NULL;
|
2021-01-15 22:29:53 +01:00
|
|
|
int x = roundf(pos_x * width);
|
|
|
|
int y = roundf((1.0f - pos_y) * height);
|
|
|
|
int delta_x = 0;
|
|
|
|
int delta_y = 0;
|
2022-07-07 19:51:36 +02:00
|
|
|
/* We need to >> 1, because GS_SETREG_RGBAQ expects 0x80 as max color */
|
2022-07-07 21:29:14 +02:00
|
|
|
int color_a = (int)(((color & 0xFF000000) >> 24) >> 2);
|
|
|
|
int color_b = (int)(((color & 0x00FF0000) >> 16) >> 1);
|
|
|
|
int color_g = (int)(((color & 0x0000FF00) >> 8) >> 1);
|
|
|
|
int color_r = (int)(((color & 0x000000FF) >> 0) >> 1);
|
2021-12-15 19:01:56 +01:00
|
|
|
|
2021-01-15 22:29:53 +01:00
|
|
|
/* Enable Alpha for font */
|
2021-09-08 01:05:41 +02:00
|
|
|
gsKit_set_primalpha(ps2->gsGlobal, GS_SETREG_ALPHA(0, 1, 0, 1, 0), 0);
|
2021-01-15 22:29:53 +01:00
|
|
|
ps2->gsGlobal->PrimAlphaEnable = GS_SETTING_ON;
|
|
|
|
gsKit_set_test(ps2->gsGlobal, GS_ATEST_ON);
|
2021-01-15 22:29:53 +01:00
|
|
|
|
|
|
|
switch (text_align)
|
|
|
|
{
|
|
|
|
case TEXT_ALIGN_RIGHT:
|
|
|
|
x -= ps2_font_get_message_width(font, msg, msg_len, scale);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TEXT_ALIGN_CENTER:
|
|
|
|
x -= ps2_font_get_message_width(font, msg, msg_len, scale) / 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-05-16 19:41:00 +02:00
|
|
|
glyph_q = font->font_driver->get_glyph(font->font_data, '?');
|
2021-01-15 22:29:53 +01:00
|
|
|
|
|
|
|
for (i = 0; i < msg_len; i++)
|
|
|
|
{
|
2022-05-16 19:41:00 +02:00
|
|
|
const struct font_glyph* glyph;
|
2021-01-15 22:29:53 +01:00
|
|
|
int off_x, off_y, tex_x, tex_y, width, height;
|
|
|
|
float x1, y1, u1, v1, x2, y2, u2, v2;
|
|
|
|
const char* msg_tmp = &msg[i];
|
|
|
|
unsigned code = utf8_walk(&msg_tmp);
|
|
|
|
unsigned skip = msg_tmp - &msg[i];
|
2020-03-09 04:13:41 +01:00
|
|
|
|
2021-01-15 22:29:53 +01:00
|
|
|
if (skip > 1)
|
|
|
|
i += skip - 1;
|
2019-01-07 21:49:55 +01:00
|
|
|
|
2022-05-16 19:41:00 +02:00
|
|
|
/* Do something smarter here ... */
|
|
|
|
if (!(glyph =
|
|
|
|
font->font_driver->get_glyph(font->font_data, code)))
|
|
|
|
if (!(glyph = glyph_q))
|
|
|
|
continue;
|
2021-01-15 22:29:53 +01:00
|
|
|
|
|
|
|
off_x = glyph->draw_offset_x;
|
|
|
|
off_y = glyph->draw_offset_y;
|
|
|
|
tex_x = glyph->atlas_offset_x;
|
|
|
|
tex_y = glyph->atlas_offset_y;
|
|
|
|
width = glyph->width;
|
|
|
|
height = glyph->height;
|
|
|
|
|
2022-05-16 19:41:00 +02:00
|
|
|
/* The -0.5 is needed to achieve pixel perfect.
|
2022-07-07 19:51:36 +02:00
|
|
|
* More info here (PS2 GSKit uses same logic as Direct3D9)
|
2022-05-16 19:41:00 +02:00
|
|
|
* https://docs.microsoft.com/en-us/windows/win32/direct3d10/d3d10-graphics-programming-guide-resources-coordinates
|
2021-01-15 22:29:53 +01:00
|
|
|
*/
|
2022-05-16 19:41:00 +02:00
|
|
|
x1 = -0.5f + x + (off_x + delta_x) * scale;
|
|
|
|
y1 = -0.5f + y + (off_y + delta_y) * scale;
|
|
|
|
u1 = tex_x;
|
|
|
|
v1 = tex_y;
|
|
|
|
x2 = x1 + width * scale;
|
|
|
|
y2 = y1 + height * scale;
|
|
|
|
u2 = u1 + width;
|
|
|
|
v2 = v1 + height;
|
2021-01-15 22:29:53 +01:00
|
|
|
|
|
|
|
gsKit_prim_sprite_texture(ps2->gsGlobal, font->texture,
|
|
|
|
x1, /* X1 */
|
|
|
|
y1, /* Y1 */
|
|
|
|
u1, /* U1 */
|
|
|
|
v1, /* V1 */
|
|
|
|
x2, /* X2 */
|
|
|
|
y2, /* Y2 */
|
|
|
|
u2, /* U2 */
|
|
|
|
v2, /* V2 */
|
|
|
|
5, /* Z */
|
2022-07-07 21:29:14 +02:00
|
|
|
GS_SETREG_RGBAQ(color_r, color_g, color_b, color_a, 0x00));
|
2021-01-15 22:29:53 +01:00
|
|
|
|
|
|
|
delta_x += glyph->advance_x;
|
|
|
|
delta_y += glyph->advance_y;
|
|
|
|
}
|
2019-01-07 21:49:55 +01:00
|
|
|
}
|
|
|
|
|
2021-01-15 22:29:53 +01:00
|
|
|
static void ps2_font_render_message(
|
|
|
|
ps2_video_t *ps2,
|
|
|
|
ps2_font_t* font, const char* msg, float scale,
|
|
|
|
const unsigned int color, float pos_x, float pos_y,
|
|
|
|
unsigned width, unsigned height, unsigned text_align)
|
2019-01-07 21:49:55 +01:00
|
|
|
{
|
2021-01-15 22:29:53 +01:00
|
|
|
struct font_line_metrics *line_metrics = NULL;
|
|
|
|
int lines = 0;
|
|
|
|
float line_height;
|
|
|
|
|
|
|
|
if (!msg || !*msg)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* If font line metrics are not supported just draw as usual */
|
|
|
|
if (!font->font_driver->get_line_metrics ||
|
|
|
|
!font->font_driver->get_line_metrics(font->font_data, &line_metrics))
|
|
|
|
{
|
|
|
|
ps2_font_render_line(ps2, font, msg, strlen(msg),
|
|
|
|
scale, color, pos_x, pos_y,
|
|
|
|
width, height, text_align);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
line_height = (float)line_metrics->height * scale / (float)height;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
const char* delim = strchr(msg, '\n');
|
|
|
|
unsigned msg_len = delim ?
|
|
|
|
(unsigned)(delim - msg) : strlen(msg);
|
|
|
|
|
|
|
|
/* Draw the line */
|
|
|
|
ps2_font_render_line(ps2, font, msg, msg_len,
|
|
|
|
scale, color, pos_x, pos_y - (float)lines * line_height,
|
|
|
|
width, height, text_align);
|
|
|
|
if (!delim)
|
|
|
|
break;
|
|
|
|
|
|
|
|
msg += msg_len + 1;
|
|
|
|
lines++;
|
|
|
|
}
|
2019-01-07 21:49:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ps2_font_render_msg(
|
2020-05-29 11:22:53 +02:00
|
|
|
void *userdata,
|
2021-01-15 22:29:53 +01:00
|
|
|
void* data, const char* msg,
|
2019-01-07 21:49:55 +01:00
|
|
|
const struct font_params *params)
|
|
|
|
{
|
2021-01-15 22:29:53 +01:00
|
|
|
float x, y, scale, drop_mod, drop_alpha;
|
|
|
|
int drop_x, drop_y;
|
|
|
|
enum text_alignment text_align;
|
2022-05-16 19:41:00 +02:00
|
|
|
unsigned color, r, g, b, alpha;
|
2021-01-15 22:29:53 +01:00
|
|
|
ps2_font_t * font = (ps2_font_t*)data;
|
|
|
|
ps2_video_t *ps2 = (ps2_video_t*)userdata;
|
|
|
|
unsigned width = ps2->vp.full_width;
|
|
|
|
unsigned height = ps2->vp.full_height;
|
2019-01-07 21:49:55 +01:00
|
|
|
|
2021-01-15 22:29:53 +01:00
|
|
|
if (!font || !msg || !*msg)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (params)
|
2020-03-07 01:41:00 +01:00
|
|
|
{
|
2022-05-16 20:34:46 +02:00
|
|
|
x = params->x;
|
|
|
|
y = params->y;
|
|
|
|
scale = params->scale;
|
|
|
|
text_align = params->text_align;
|
|
|
|
drop_x = params->drop_x;
|
|
|
|
drop_y = params->drop_y;
|
|
|
|
drop_mod = params->drop_mod;
|
|
|
|
drop_alpha = params->drop_alpha;
|
|
|
|
|
|
|
|
r = FONT_COLOR_GET_RED(params->color);
|
|
|
|
g = FONT_COLOR_GET_GREEN(params->color);
|
|
|
|
b = FONT_COLOR_GET_BLUE(params->color);
|
|
|
|
alpha = FONT_COLOR_GET_ALPHA(params->color);
|
|
|
|
|
|
|
|
color = COLOR_ABGR(r, g, b, alpha);
|
2019-01-07 21:49:55 +01:00
|
|
|
}
|
2021-01-15 22:29:53 +01:00
|
|
|
else
|
|
|
|
{
|
2022-05-16 20:34:46 +02:00
|
|
|
settings_t *settings = config_get_ptr();
|
|
|
|
float video_msg_pos_x = settings->floats.video_msg_pos_x;
|
|
|
|
float video_msg_pos_y = settings->floats.video_msg_pos_y;
|
|
|
|
float video_msg_color_r = settings->floats.video_msg_color_r;
|
|
|
|
float video_msg_color_g = settings->floats.video_msg_color_g;
|
|
|
|
float video_msg_color_b = settings->floats.video_msg_color_b;
|
|
|
|
x = video_msg_pos_x;
|
|
|
|
y = video_msg_pos_y;
|
|
|
|
scale = 1.0f;
|
|
|
|
text_align = TEXT_ALIGN_LEFT;
|
|
|
|
|
|
|
|
r = (video_msg_color_r * 255);
|
|
|
|
g = (video_msg_color_g * 255);
|
|
|
|
b = (video_msg_color_b * 255);
|
|
|
|
alpha = 255;
|
|
|
|
color = COLOR_ABGR(r, g, b, alpha);
|
|
|
|
|
|
|
|
drop_x = 1;
|
|
|
|
drop_y = -1;
|
|
|
|
drop_mod = 0.0f;
|
|
|
|
drop_alpha = 0.75f;
|
2021-01-15 22:29:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
gsKit_TexManager_bind(ps2->gsGlobal, font->texture);
|
|
|
|
|
|
|
|
if (drop_x || drop_y)
|
|
|
|
{
|
2022-05-16 19:41:00 +02:00
|
|
|
unsigned r_dark = r * drop_mod;
|
|
|
|
unsigned g_dark = g * drop_mod;
|
|
|
|
unsigned b_dark = b * drop_mod;
|
|
|
|
unsigned alpha_dark = alpha * drop_alpha;
|
|
|
|
unsigned color_dark = COLOR_ABGR(r_dark, g_dark, b_dark, alpha_dark);
|
2021-01-15 22:29:53 +01:00
|
|
|
ps2_font_render_message(ps2, font, msg, scale, color_dark,
|
|
|
|
x + scale * drop_x / width, y +
|
|
|
|
scale * drop_y / height,
|
|
|
|
width, height, text_align);
|
|
|
|
}
|
|
|
|
|
|
|
|
ps2_font_render_message(ps2, font, msg, scale,
|
|
|
|
color, x, y,
|
|
|
|
width, height, text_align);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct font_glyph* ps2_font_get_glyph(
|
|
|
|
void* data, uint32_t code)
|
|
|
|
{
|
|
|
|
ps2_font_t* font = (ps2_font_t*)data;
|
2022-05-16 19:41:00 +02:00
|
|
|
if (font && font->font_driver && font->font_driver->ident)
|
|
|
|
return font->font_driver->get_glyph((void*)font->font_driver, code);
|
|
|
|
return NULL;
|
2021-01-15 22:29:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool ps2_font_get_line_metrics(void* data, struct font_line_metrics **metrics)
|
|
|
|
{
|
|
|
|
ps2_font_t* font = (ps2_font_t*)data;
|
2022-05-16 19:41:00 +02:00
|
|
|
if (font && font->font_driver && font->font_data)
|
|
|
|
return font->font_driver->get_line_metrics(font->font_data, metrics);
|
2022-06-26 18:01:43 +02:00
|
|
|
return false;
|
2019-01-07 21:49:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
font_renderer_t ps2_font = {
|
2022-06-26 18:01:43 +02:00
|
|
|
ps2_font_init,
|
|
|
|
ps2_font_free,
|
2019-01-07 21:49:55 +01:00
|
|
|
ps2_font_render_msg,
|
2022-06-26 18:01:43 +02:00
|
|
|
"ps2_font",
|
2021-01-15 22:29:53 +01:00
|
|
|
ps2_font_get_glyph,
|
2019-01-07 21:49:55 +01:00
|
|
|
NULL, /* bind_block */
|
|
|
|
NULL, /* flush */
|
2021-01-15 22:29:53 +01:00
|
|
|
ps2_font_get_message_width,
|
|
|
|
ps2_font_get_line_metrics
|
2019-01-07 21:49:55 +01:00
|
|
|
};
|