214 lines
6.6 KiB
C
Raw Normal View History

2017-01-04 02:07:19 -05:00
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2017-01-22 13:40:32 +01:00
* Copyright (C) 2011-2017 - Daniel De Matteis
2019-02-22 16:31:54 -05:00
* Copyright (C) 2016-2019 - Brad Parker
*
2017-01-04 02:07:19 -05:00
* 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/>.
*/
#include <stdlib.h>
#include <string/stdstring.h>
2017-01-05 22:17:48 -05:00
#include <encodings/utf.h>
#include <lists/string_list.h>
2017-01-04 02:07:19 -05:00
2020-03-05 14:01:34 +01:00
#include <windows.h>
#include <wingdi.h>
2017-01-04 02:07:19 -05:00
#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif
#include "../../retroarch.h"
2017-01-04 02:07:19 -05:00
#include "../common/gdi_common.h"
2017-01-05 22:17:48 -05:00
#include "../common/win32_common.h"
2020-03-05 14:01:34 +01:00
#include "../font_driver.h"
#include "../../configuration.h"
2020-03-05 14:01:34 +01:00
#include "../../verbosity.h"
2017-01-04 02:07:19 -05:00
typedef struct
{
const font_renderer_driver_t *font_driver;
void *font_data;
2017-01-04 02:07:19 -05:00
gdi_t *gdi;
} gdi_raster_t;
static void *gdi_font_init(void *data,
const char *font_path, float font_size,
bool is_threaded)
2017-01-04 02:07:19 -05:00
{
gdi_raster_t *font = (gdi_raster_t*)calloc(1, sizeof(*font));
if (!font)
return NULL;
font->gdi = (gdi_t*)data;
2018-07-14 00:54:14 +02:00
if (!font_renderer_create_default(
&font->font_driver,
&font->font_data, font_path, font_size))
2017-01-04 02:07:19 -05:00
{
RARCH_WARN("Couldn't initialize font renderer.\n");
return NULL;
}
return font;
}
static void gdi_font_free(void *data, bool is_threaded)
{
gdi_raster_t *font = (gdi_raster_t*)data;
if (!font)
return;
if (font->font_driver && font->font_data && font->font_driver->free)
font->font_driver->free(font->font_data);
free(font);
}
2017-01-04 02:07:19 -05:00
static void gdi_font_render_msg(
2020-03-09 21:34:14 +01:00
void *userdata,
void *data,
const char *msg,
const struct font_params *params)
2017-01-04 02:07:19 -05:00
{
2021-05-21 17:09:55 +02:00
char* msg_local;
2018-10-10 17:53:27 -04:00
float x, y, scale, drop_mod, drop_alpha;
int drop_x, drop_y, msg_strlen;
2018-10-10 16:18:29 +02:00
unsigned i;
unsigned new_x, new_y, new_drop_x, new_drop_y;
2017-01-21 15:46:46 -05:00
unsigned align;
unsigned red, green, blue;
2020-03-09 22:35:25 +01:00
gdi_t *gdi = (gdi_t*)userdata;
gdi_raster_t *font = (gdi_raster_t*)data;
2020-03-09 22:35:25 +01:00
unsigned width = gdi->video_width;
unsigned height = gdi->video_height;
2022-05-16 20:34:46 +02:00
SIZE text_size = {0};
2020-08-26 13:35:05 +02:00
struct string_list msg_list = {0};
2017-01-04 02:07:19 -05:00
if (!font || string_is_empty(msg) || !font->gdi)
2017-01-04 02:07:19 -05:00
return;
if (params)
{
2022-05-16 20:34:46 +02:00
x = params->x;
y = params->y;
drop_x = params->drop_x;
drop_y = params->drop_y;
drop_mod = params->drop_mod;
drop_alpha = params->drop_alpha;
scale = params->scale;
align = params->text_align;
red = FONT_COLOR_GET_RED(params->color);
green = FONT_COLOR_GET_GREEN(params->color);
blue = FONT_COLOR_GET_BLUE(params->color);
2017-01-04 02:07:19 -05: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;
drop_x = -2;
drop_y = -2;
drop_mod = 0.3f;
drop_alpha = 1.0f;
scale = 1.0f;
align = TEXT_ALIGN_LEFT;
red = video_msg_color_r * 255.0f;
green = video_msg_color_g * 255.0f;
blue = video_msg_color_b * 255.0f;
2017-01-04 02:07:19 -05:00
}
2022-05-16 20:34:46 +02:00
msg_local = utf8_to_local_string_alloc(msg);
msg_strlen = strlen(msg_local);
2021-04-22 20:38:58 +09:00
2022-05-16 20:34:46 +02:00
GetTextExtentPoint32(font->gdi->memDC, msg_local, msg_strlen, &text_size);
2017-01-21 15:46:46 -05:00
switch (align)
{
case TEXT_ALIGN_LEFT:
new_x = x * width * scale;
new_drop_x = drop_x * width * scale;
2017-01-21 15:46:46 -05:00
break;
case TEXT_ALIGN_RIGHT:
new_x = (x * width * scale) - text_size.cx;
new_drop_x = (drop_x * width * scale) - text_size.cx;
2017-01-21 15:46:46 -05:00
break;
case TEXT_ALIGN_CENTER:
new_x = (x * width * scale) - (text_size.cx / 2);
new_drop_x = (drop_x * width * scale) - (text_size.cx / 2);
2017-01-21 15:46:46 -05:00
break;
default:
new_x = 0;
new_drop_x = 0;
2017-01-21 15:46:46 -05:00
break;
}
new_y = height - (y * height * scale) - text_size.cy;
new_drop_y = height - (drop_y * height * scale) - text_size.cy;
2017-01-05 22:17:48 -05:00
2017-08-22 23:39:05 -04:00
font->gdi->bmp_old = (HBITMAP)SelectObject(font->gdi->memDC, font->gdi->bmp);
2017-08-22 23:39:05 -04:00
SetBkMode(font->gdi->memDC, TRANSPARENT);
2020-08-26 13:35:05 +02:00
string_list_initialize(&msg_list);
2021-04-22 20:38:58 +09:00
string_split_noalloc(&msg_list, msg_local, "\n");
if (drop_x || drop_y)
{
2022-05-16 19:41:00 +02:00
float dark_alpha = drop_alpha;
unsigned drop_red = red * drop_mod * dark_alpha;
unsigned drop_green = green * drop_mod * dark_alpha;
unsigned drop_blue = blue * drop_mod * dark_alpha;
SetTextColor(font->gdi->memDC, RGB(drop_red, drop_green, drop_blue));
2020-08-26 13:35:05 +02:00
for (i = 0; i < msg_list.size; i++)
TextOut(font->gdi->memDC, new_drop_x,
new_drop_y + (text_size.cy * i),
2020-08-26 13:35:05 +02:00
msg_list.elems[i].data,
2021-04-22 20:38:58 +09:00
strlen(msg_list.elems[i].data));
}
SetTextColor(font->gdi->memDC, RGB(red, green, blue));
2020-08-26 13:35:05 +02:00
for (i = 0; i < msg_list.size; i++)
TextOut(font->gdi->memDC, new_x, new_y + (text_size.cy * i),
2020-08-26 13:35:05 +02:00
msg_list.elems[i].data,
2021-04-22 20:38:58 +09:00
strlen(msg_list.elems[i].data));
2020-08-26 13:35:05 +02:00
string_list_deinitialize(&msg_list);
2021-04-22 20:38:58 +09:00
free(msg_local);
2017-08-22 23:39:05 -04:00
SelectObject(font->gdi->memDC, font->gdi->bmp_old);
2017-01-04 02:07:19 -05:00
}
font_renderer_t gdi_font = {
gdi_font_init,
gdi_font_free,
gdi_font_render_msg,
"gdi_font",
NULL, /* get_glyph */
NULL, /* bind_block */
NULL, /* flush */
NULL, /* get_message_width */
NULL /* get_line_metrics */
2017-01-04 02:07:19 -05:00
};