From 7246d0d053909c67dc921a0fd061cd39a39299fe Mon Sep 17 00:00:00 2001 From: natinusala Date: Fri, 13 Mar 2020 17:12:20 +0100 Subject: [PATCH] gfx_widgets: move generic message to its own file --- Makefile.common | 3 +- gfx/gfx_widgets.c | 79 ++------------ gfx/gfx_widgets.h | 4 +- gfx/widgets/gfx_widget_generic_message.c | 131 +++++++++++++++++++++++ griffin/griffin.c | 1 + retroarch.c | 2 +- 6 files changed, 146 insertions(+), 74 deletions(-) create mode 100644 gfx/widgets/gfx_widget_generic_message.c diff --git a/Makefile.common b/Makefile.common index d8bba67d2a..d127eafecb 100644 --- a/Makefile.common +++ b/Makefile.common @@ -842,7 +842,8 @@ endif ifeq ($(HAVE_GFX_WIDGETS), 1) OBJ += gfx/gfx_widgets.o \ gfx/widgets/gfx_widget_screenshot.o \ - gfx/widgets/gfx_widget_volume.o + gfx/widgets/gfx_widget_volume.o \ + gfx/widgets/gfx_widget_generic_message.o endif ifeq ($(HAVE_OVERLAY), 1) diff --git a/gfx/gfx_widgets.c b/gfx/gfx_widgets.c index 78364bb26f..1fa822e4e3 100644 --- a/gfx/gfx_widgets.c +++ b/gfx/gfx_widgets.c @@ -286,11 +286,6 @@ static unsigned ai_service_overlay_height = 0; static uintptr_t ai_service_overlay_texture = 0; #endif -/* Generic message */ -static gfx_timer_t generic_message_timer; -static float generic_message_alpha = 0.0f; -static char generic_message[256] = {'\0'}; - /* Libretro message */ static gfx_timer_t libretro_message_timer; @@ -351,7 +346,12 @@ static unsigned msg_queue_task_rect_start_x; static unsigned msg_queue_task_hourglass_x; /* Used for both generic and libretro messages */ -static unsigned generic_message_height; +static unsigned generic_message_height; + +unsigned gfx_widgets_get_generic_message_height(void) +{ + return generic_message_height; +} static unsigned divider_width_1px = 1; @@ -371,7 +371,8 @@ unsigned gfx_widgets_get_last_video_height(void) /* Widgets list */ const static gfx_widget_t* const widgets[] = { &gfx_widget_screenshot, - &gfx_widget_volume + &gfx_widget_volume, + &gfx_widget_generic_message }; static const size_t widgets_len = sizeof(widgets) / sizeof(widgets[0]); @@ -1606,27 +1607,6 @@ void gfx_widgets_frame(void *data) 1, false, 0, false); } - /* Generic message */ - if (generic_message_alpha > 0.0f) - { - unsigned text_color = COLOR_TEXT_ALPHA(0xffffffff, (unsigned)(generic_message_alpha*255.0f)); - gfx_display_set_alpha(gfx_widgets_backdrop_orig, generic_message_alpha); - - gfx_display_draw_quad(userdata, - video_width, video_height, - 0, video_height - generic_message_height, - video_width, generic_message_height, - video_width, video_height, - gfx_widgets_backdrop_orig); - - gfx_display_draw_text(font_regular, generic_message, - video_width/2, - video_height - generic_message_height/2 + widget_font_size/4, - video_width, video_height, - text_color, TEXT_ALIGN_CENTER, - 1, false, 0, false); - } - #ifdef HAVE_CHEEVOS /* Achievement notification */ if (cheevo_popup_queue_read_index >= 0 && cheevo_popup_queue[cheevo_popup_queue_read_index].title) @@ -2253,9 +2233,6 @@ static void gfx_widgets_free(void) font_driver_bind_block(NULL, NULL); /* Reset state of all other widgets */ - /* Generic message*/ - generic_message_alpha = 0.0f; - /* Libretro message */ libretro_tag = (uintptr_t) &libretro_message_timer; libretro_message_alpha = 0.0f; @@ -2569,46 +2546,6 @@ void gfx_widgets_push_achievement(const char *title, const char *badge) } #endif -static void gfx_widgets_generic_message_fadeout(void *userdata) -{ - gfx_animation_ctx_entry_t entry; - gfx_animation_ctx_tag tag = (uintptr_t) &generic_message_timer; - - /* Start fade out animation */ - entry.cb = NULL; - entry.duration = MSG_QUEUE_ANIMATION_DURATION; - entry.easing_enum = EASING_OUT_QUAD; - entry.subject = &generic_message_alpha; - entry.tag = tag; - entry.target_value = 0.0f; - entry.userdata = NULL; - - gfx_animation_push(&entry); -} - -void gfx_widgets_set_message(char *msg) -{ - gfx_timer_ctx_entry_t timer; - gfx_animation_ctx_tag tag = (uintptr_t) &generic_message_timer; - - if (!widgets_active) - return; - - strlcpy(generic_message, msg, sizeof(generic_message)); - - generic_message_alpha = DEFAULT_BACKDROP; - - /* Kill and restart the timer / animation */ - gfx_timer_kill(&generic_message_timer); - gfx_animation_kill_by_tag(&tag); - - timer.cb = gfx_widgets_generic_message_fadeout; - timer.duration = GENERIC_MESSAGE_DURATION; - timer.userdata = NULL; - - gfx_timer_start(&generic_message_timer, &timer); -} - static void gfx_widgets_libretro_message_fadeout(void *userdata) { gfx_animation_ctx_entry_t entry; diff --git a/gfx/gfx_widgets.h b/gfx/gfx_widgets.h index 05f4170be7..1b33d042eb 100644 --- a/gfx/gfx_widgets.h +++ b/gfx/gfx_widgets.h @@ -99,6 +99,7 @@ font_data_t* gfx_widgets_get_font_bold(void); float* gfx_widgets_get_backdrop_orig(void); unsigned gfx_widgets_get_last_video_width(void); unsigned gfx_widgets_get_last_video_height(void); +unsigned gfx_widgets_get_generic_message_height(void); float gfx_widgets_get_thumbnail_scale_factor( const float dst_width, const float dst_height, @@ -120,6 +121,7 @@ typedef struct gfx_widget gfx_widget_t; extern const gfx_widget_t gfx_widget_screenshot; extern const gfx_widget_t gfx_widget_volume; +extern const gfx_widget_t gfx_widget_generic_message; bool gfx_widgets_active(void); void gfx_widgets_set_persistence(bool persist); @@ -169,7 +171,7 @@ void gfx_widgets_cleanup_load_content_animation(void); void gfx_widgets_push_achievement(const char *title, const char *badge); /* Warning: not thread safe! */ -void gfx_widgets_set_message(char *message); +void gfx_widget_set_message(char *message); /* Warning: not thread safe! */ void gfx_widgets_set_libretro_message(const char *message, unsigned duration); diff --git a/gfx/widgets/gfx_widget_generic_message.c b/gfx/widgets/gfx_widget_generic_message.c new file mode 100644 index 0000000000..37cdb05b08 --- /dev/null +++ b/gfx/widgets/gfx_widget_generic_message.c @@ -0,0 +1,131 @@ +/* RetroArch - A frontend for libretro. + * Copyright (C) 2014-2017 - Jean-André Santoni + * Copyright (C) 2015-2018 - Andre Leiradella + * Copyright (C) 2018-2020 - natinusala + * + * 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 . + */ + +#include "../gfx_widgets.h" +#include "../gfx_animation.h" +#include "../gfx_display.h" +#include "../../retroarch.h" + +struct gfx_widget_generic_message_state +{ + gfx_timer_t timer; + float alpha; + char message[256]; +}; + +typedef struct gfx_widget_generic_message_state gfx_widget_generic_message_state_t; + +static gfx_widget_generic_message_state_t p_w_generic_message_st = { + 0.0f, + 0.0f, + {'\0'} +}; + +static gfx_widget_generic_message_state_t* gfx_widget_generic_message_get_ptr() +{ + return &p_w_generic_message_st; +} + +static void gfx_widget_generic_message_fadeout(void *userdata) +{ + gfx_widget_generic_message_state_t* state = gfx_widget_generic_message_get_ptr(); + gfx_animation_ctx_entry_t entry; + gfx_animation_ctx_tag tag = (uintptr_t) &state->timer; + + /* Start fade out animation */ + entry.cb = NULL; + entry.duration = MSG_QUEUE_ANIMATION_DURATION; + entry.easing_enum = EASING_OUT_QUAD; + entry.subject = &state->alpha; + entry.tag = tag; + entry.target_value = 0.0f; + entry.userdata = NULL; + + gfx_animation_push(&entry); +} + +void gfx_widget_set_message(char *msg) +{ + gfx_widget_generic_message_state_t* state = gfx_widget_generic_message_get_ptr(); + gfx_timer_ctx_entry_t timer; + gfx_animation_ctx_tag tag = (uintptr_t) &state->timer; + + if (!gfx_widgets_active()) + return; + + strlcpy(state->message, msg, sizeof(state->message)); + + state->alpha = DEFAULT_BACKDROP; + + /* Kill and restart the timer / animation */ + gfx_timer_kill(&state->timer); + gfx_animation_kill_by_tag(&tag); + + timer.cb = gfx_widget_generic_message_fadeout; + timer.duration = GENERIC_MESSAGE_DURATION; + timer.userdata = NULL; + + gfx_timer_start(&state->timer, &timer); +} + +static void gfx_widget_generic_message_frame(void* data) +{ + gfx_widget_generic_message_state_t* state = gfx_widget_generic_message_get_ptr(); + + if (state->alpha > 0.0f) + { + video_frame_info_t* video_info = (video_frame_info_t*)data; + void* userdata = video_info->userdata; + unsigned video_width = video_info->width; + unsigned video_height = video_info->height; + + unsigned height = gfx_widgets_get_generic_message_height(); + + unsigned text_color = COLOR_TEXT_ALPHA(0xffffffff, (unsigned)(state->alpha*255.0f)); + gfx_display_set_alpha(gfx_widgets_get_backdrop_orig(), state->alpha); + + gfx_display_draw_quad(userdata, + video_width, video_height, + 0, video_height - height, + video_width, height, + video_width, video_height, + gfx_widgets_get_backdrop_orig()); + + gfx_display_draw_text(gfx_widgets_get_font_regular(), state->message, + video_width/2, + video_height - height/2 + gfx_widgets_get_font_size()/4, + video_width, video_height, + text_color, TEXT_ALIGN_CENTER, + 1, false, 0, false); + } +} + +static void gfx_widget_generic_message_free(void) +{ + gfx_widget_generic_message_state_t* state = gfx_widget_generic_message_get_ptr(); + state->alpha = 0.0f; +} + +const gfx_widget_t gfx_widget_generic_message = { + NULL, /* init */ + gfx_widget_generic_message_free, + NULL, /* context_reset*/ + NULL, /* context_destroy */ + NULL, /* layout */ + NULL, /* iterate */ + gfx_widget_generic_message_frame +}; diff --git a/griffin/griffin.c b/griffin/griffin.c index f54fb38f0c..dbf6e13170 100644 --- a/griffin/griffin.c +++ b/griffin/griffin.c @@ -1263,6 +1263,7 @@ MENU #include "../gfx/gfx_widgets.c" #include "../gfx/widgets/gfx_widget_screenshot.c" #include "../gfx/widgets/gfx_widget_volume.c" +#include "../gfx/widgets/gfx_widget_generic_message.c" #endif #include "../input/input_osk.c" diff --git a/retroarch.c b/retroarch.c index dfd4fe8525..ec896da25f 100644 --- a/retroarch.c +++ b/retroarch.c @@ -5555,7 +5555,7 @@ bool retroarch_apply_shader(enum rarch_shader_type type, const char *preset_path preset_file ? preset_file : "None"); #ifdef HAVE_GFX_WIDGETS if (gfx_widgets_active()) - gfx_widgets_set_message(msg); + gfx_widget_set_message(msg); else #endif runloop_msg_queue_push(msg, 1, 120, true, NULL,