Skip to content

Commit 1533924

Browse files
backwardsEricNickMcConnell
authored andcommitted
SDL2: respond to SDL_RENDER_TARGETS_RESET/SDL_RENDER_DEVICE_RESET events
May help with angband/angband#5512 and angband/angband#5497 .
1 parent c795013 commit 1533924

File tree

3 files changed

+186
-19
lines changed

3 files changed

+186
-19
lines changed

src/main-sdl2.c

Lines changed: 144 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ static void keyboard_event_to_angband_key(const SDL_KeyboardEvent *key,
531531
bool kp_as_mod, keycode_t *ch, uint8_t *mods);
532532
static void textinput_event_to_angband_key(const SDL_TextInputEvent *key,
533533
bool kp_as_mod, keycode_t *ch, uint8_t *mods);
534+
static void recreate_textures(struct my_app *a, bool all);
534535

535536
/* Global variables. */
536537

@@ -1973,6 +1974,7 @@ static void show_shortcut_editor(struct sdlpui_window *w, int x, int y)
19731974

19741975
w->shorte->ftb = &shortcut_editor_funcs;
19751976
w->shorte->pop_callback = hide_shortcut_editor;
1977+
w->shorte->recreate_textures_callback = NULL;
19761978
w->shorte->next = NULL;
19771979
w->shorte->prev = NULL;
19781980
w->shorte->texture = NULL;
@@ -2005,13 +2007,45 @@ static void hide_about(struct sdlpui_dialog *d, struct sdlpui_window *w,
20052007
}
20062008
}
20072009

2010+
static void recreate_about_dialog_textures(struct sdlpui_dialog *d,
2011+
struct sdlpui_window *w, bool all)
2012+
{
2013+
struct sdlpui_simple_info *psi;
2014+
int i;
2015+
2016+
if (!all) {
2017+
return;
2018+
}
2019+
2020+
SDL_assert(d->type_code == SDLPUI_DIALOG_SIMPLE_INFO);
2021+
psi = (struct sdlpui_simple_info*)d->priv;
2022+
2023+
/* Recreate the static texture for the one image in the dialog. */
2024+
for (i = 0; i < psi->number; ++i) {
2025+
if (psi->labels[i].type_code == SDLPUI_CTRL_IMAGE) {
2026+
char path[4096];
2027+
struct sdlpui_image *pi =
2028+
(struct sdlpui_image*)psi->labels[i].priv;
2029+
2030+
if (pi->image) {
2031+
SDL_DestroyTexture(pi->image);
2032+
}
2033+
path_build(path, sizeof(path), DEFAULT_ABOUT_ICON_DIR,
2034+
DEFAULT_ABOUT_ICON);
2035+
pi->image = load_image(w, path);
2036+
break;
2037+
}
2038+
}
2039+
}
2040+
20082041
static void show_about(struct sdlpui_window *window, int x, int y)
20092042
{
20102043
if (!window->infod) {
20112044
char path[4096];
20122045
SDL_Texture *texture;
20132046

2014-
window->infod = sdlpui_start_simple_info("Ok", NULL, 0);
2047+
window->infod = sdlpui_start_simple_info("Ok", NULL,
2048+
recreate_about_dialog_textures, 0);
20152049
path_build(path, sizeof(path), DEFAULT_ABOUT_ICON_DIR,
20162050
DEFAULT_ABOUT_ICON);
20172051
texture = load_image(window, path);
@@ -2146,7 +2180,7 @@ static struct sdlpui_dialog *handle_menu_windows(struct sdlpui_control *ctrl,
21462180
int ul_x_win, int ul_y_win)
21472181
{
21482182
struct sdlpui_dialog *result = sdlpui_start_simple_menu(
2149-
dlg, ctrl, MAX_WINDOWS, true, false, NULL, 0);
2183+
dlg, ctrl, MAX_WINDOWS, true, false, NULL, NULL, 0);
21502184
unsigned int i;
21512185

21522186
for (i = 1; i < MAX_WINDOWS; ++i) {
@@ -2332,8 +2366,8 @@ static struct sdlpui_dialog *handle_menu_tile_sizes(struct sdlpui_control *ctrl,
23322366
*/
23332367
bool disabled = (window->graphics.id == GRAPHICS_NONE
23342368
|| !character_generated || !inkey_flag);
2335-
struct sdlpui_dialog *result =
2336-
sdlpui_start_simple_menu(dlg, ctrl, 2, true, false, NULL, 0);
2369+
struct sdlpui_dialog *result = sdlpui_start_simple_menu(dlg, ctrl, 2,
2370+
true, false, NULL, NULL, 0);
23372371
struct sdlpui_control *c;
23382372

23392373
c = sdlpui_get_simple_menu_next_unused(result, SDLPUI_MFLG_NONE);
@@ -2365,7 +2399,7 @@ static struct sdlpui_dialog *handle_menu_tile_sets(struct sdlpui_control *ctrl,
23652399
*/
23662400
bool disabled = !character_generated || !inkey_flag;
23672401
struct sdlpui_dialog *result = sdlpui_start_simple_menu(dlg, ctrl,
2368-
0, true, false, NULL, 0);
2402+
0, true, false, NULL, NULL, 0);
23692403
graphics_mode *mode = graphics_modes;
23702404

23712405
while (mode) {
@@ -2389,7 +2423,7 @@ static struct sdlpui_dialog *handle_menu_tiles(struct sdlpui_control *ctrl,
23892423
int ul_x_win, int ul_y_win)
23902424
{
23912425
struct sdlpui_dialog *result = sdlpui_start_simple_menu(dlg, ctrl, 2,
2392-
true, false, NULL, 0);
2426+
true, false, NULL, NULL, 0);
23932427
struct sdlpui_control *c;
23942428

23952429
c = sdlpui_get_simple_menu_next_unused(result, SDLPUI_MFLG_NONE);
@@ -2585,7 +2619,8 @@ static struct sdlpui_dialog *handle_menu_font_sizes(
25852619
calculate_subwindow_font_size_bounds(subwindow, NULL,
25862620
&subwindow->min_font_size, &subwindow->max_font_size);
25872621
SDL_assert(subwindow);
2588-
result = sdlpui_start_simple_menu(dlg, ctrl, 2, true, false, NULL, 0);
2622+
result = sdlpui_start_simple_menu(dlg, ctrl, 2, true, false, NULL,
2623+
NULL, 0);
25892624
c = sdlpui_get_simple_menu_next_unused(result, SDLPUI_MFLG_NONE);
25902625
sdlpui_create_menu_ranged_int(c, "- %2d points +", SDLPUI_HOR_LEFT,
25912626
handle_menu_font_size, tag, !is_vector_font
@@ -2647,7 +2682,7 @@ static struct sdlpui_dialog *handle_menu_font_names(struct sdlpui_control *ctrl,
26472682
count = window->app->font_count - start;
26482683
}
26492684
result = sdlpui_start_simple_menu(dlg, ctrl,
2650-
count + ((more_nesting) ? 1 : 0), true, false, NULL, 0);
2685+
count + ((more_nesting) ? 1 : 0), true, false, NULL, NULL, 0);
26512686
if (more_nesting) {
26522687
c = sdlpui_get_simple_menu_next_unused(result,
26532688
SDLPUI_MFLG_NONE);
@@ -2683,7 +2718,7 @@ static struct sdlpui_dialog *handle_menu_purpose(struct sdlpui_control *ctrl,
26832718
subw_idx = (*ctrl->ftb->get_tag)(ctrl);
26842719
SDL_assert(subw_idx >= 0 && subw_idx != MAIN_SUBWINDOW);
26852720
result = sdlpui_start_simple_menu(dlg, ctrl,
2686-
(int)N_ELEMENTS(window_flag_desc), true, false, NULL, 0);
2721+
(int)N_ELEMENTS(window_flag_desc), true, false, NULL, NULL, 0);
26872722
while (i < (int)N_ELEMENTS(window_flag_desc)) {
26882723
if (window_flag_desc[i]) {
26892724
struct sdlpui_control *c =
@@ -2714,7 +2749,8 @@ static struct sdlpui_dialog *handle_menu_font(struct sdlpui_control *ctrl,
27142749

27152750
SDL_assert(ctrl->ftb->get_tag);
27162751
tag = (*ctrl->ftb->get_tag)(ctrl);
2717-
result = sdlpui_start_simple_menu(dlg, ctrl, 2, true, false, NULL, 0);
2752+
result = sdlpui_start_simple_menu(dlg, ctrl, 2, true, false, NULL,
2753+
NULL, 0);
27182754
c = sdlpui_get_simple_menu_next_unused(result, SDLPUI_MFLG_NONE);
27192755
sdlpui_create_submenu_button(c, "Name", SDLPUI_HOR_LEFT,
27202756
handle_menu_font_names, SDLPUI_CHILD_MENU_RIGHT, tag, false);
@@ -2813,7 +2849,7 @@ static struct sdlpui_dialog *handle_menu_alpha(struct sdlpui_control *ctrl,
28132849
nstep = 1 + ((100 - DEFAULT_ALPHA_LOWEST + (DEFAULT_ALPHA_STEP - 1))
28142850
/ DEFAULT_ALPHA_STEP);
28152851
result = sdlpui_start_simple_menu(dlg, ctrl, nstep, true, false,
2816-
NULL, 0);
2852+
NULL, NULL, 0);
28172853
for (i = 0; i < nstep; ++i) {
28182854
int alpha_pct = MIN(100,
28192855
DEFAULT_ALPHA_LOWEST + i * DEFAULT_ALPHA_STEP);
@@ -2879,7 +2915,7 @@ static struct sdlpui_dialog *handle_menu_terms(struct sdlpui_control *ctrl,
28792915
SDL_assert(subwindow);
28802916
result = sdlpui_start_simple_menu(dlg, ctrl,
28812917
(subwindow->index == MAIN_SUBWINDOW) ? 4 : 5, true, false,
2882-
handle_menu_term_pop, tag);
2918+
handle_menu_term_pop, NULL, tag);
28832919
c = sdlpui_get_simple_menu_next_unused(result, SDLPUI_MFLG_NONE);
28842920
sdlpui_create_submenu_button(c, "Font", SDLPUI_HOR_LEFT,
28852921
handle_menu_font, SDLPUI_CHILD_MENU_RIGHT, tag, false);
@@ -2919,7 +2955,7 @@ static struct sdlpui_dialog *handle_menu_button(struct sdlpui_control *ctrl,
29192955
struct sdlpui_dialog *result = sdlpui_start_simple_menu(
29202956
parent, ctrl, 3 + (int)N_ELEMENTS(angband_term_name)
29212957
+ ((window->index == MAIN_WINDOW) ? 2 : 0), true, false,
2922-
NULL, 0);
2958+
NULL, NULL, 0);
29232959
unsigned int i;
29242960
struct sdlpui_control *c;
29252961

@@ -4091,6 +4127,12 @@ static void wait_anykey(struct my_app *a)
40914127
case SDL_QUIT:
40924128
handle_quit();
40934129
break;
4130+
case SDL_RENDER_TARGETS_RESET:
4131+
recreate_textures(a, false);
4132+
break;
4133+
case SDL_RENDER_DEVICE_RESET:
4134+
recreate_textures(a, true);
4135+
break;
40944136
case SDL_WINDOWEVENT:
40954137
handle_windowevent(a, &event.window);
40964138
return;
@@ -4135,6 +4177,12 @@ static bool get_event(struct my_app *a)
41354177
case SDL_WINDOWEVENT:
41364178
handle_windowevent(a, &event.window);
41374179
return false;
4180+
case SDL_RENDER_TARGETS_RESET:
4181+
recreate_textures(a, false);
4182+
return false;
4183+
case SDL_RENDER_DEVICE_RESET:
4184+
recreate_textures(a, true);
4185+
return false;
41384186
case SDL_QUIT:
41394187
handle_quit();
41404188
return false;
@@ -5088,6 +5136,88 @@ static void free_font(struct font *font)
50885136
mem_free(font);
50895137
}
50905138

5139+
static void recreate_textures(struct my_app *a, bool all)
5140+
{
5141+
int i;
5142+
5143+
reload_all_graphics(a, current_graphics_mode);
5144+
for (i = 0; i < MAX_WINDOWS; ++i) {
5145+
struct sdlpui_window *w = &a->windows[i];
5146+
struct sdlpui_dialog *d;
5147+
int j;
5148+
5149+
if (!w->loaded) {
5150+
continue;
5151+
}
5152+
5153+
/*
5154+
* Recreate the dynamic texture used to cache the dialog font.
5155+
*/
5156+
if (w->dialog_font->cache.texture) {
5157+
SDL_DestroyTexture(w->dialog_font->cache.texture);
5158+
w->dialog_font->cache.texture = NULL;
5159+
make_font_cache(w, w->dialog_font);
5160+
}
5161+
5162+
/* Recreate the static texture used for the stipple pattern. */
5163+
if (all && w->stipple.texture) {
5164+
SDL_DestroyTexture(w->stipple.texture);
5165+
w->stipple = sdlpui_compute_stipple(w->renderer);
5166+
}
5167+
5168+
/*
5169+
* Recreate the texture used for the wallpaper. That texture
5170+
* is dynamic if the wallpapering mode is WALLPAPER_TILED;
5171+
* otherwise, that texture is static.
5172+
*/
5173+
if (w->wallpaper.mode != WALLPAPER_DONT_SHOW
5174+
&& w->wallpaper.texture && (all
5175+
|| w->wallpaper.mode == WALLPAPER_TILED)) {
5176+
SDL_DestroyTexture(w->wallpaper.texture);
5177+
w->wallpaper.texture = NULL;
5178+
if (w->config) {
5179+
load_wallpaper(w, w->config->wallpaper_path);
5180+
} else {
5181+
load_default_wallpaper(w);
5182+
}
5183+
}
5184+
5185+
/*
5186+
* Recreate the textures for subwindows; those are all
5187+
* dynamic.
5188+
*/
5189+
for (j = 0; j < MAX_SUBWINDOWS; ++j) {
5190+
struct subwindow *sw = w->subwindows[j];
5191+
5192+
if (!sw) {
5193+
continue;
5194+
}
5195+
if (!sw->texture) {
5196+
SDL_DestroyTexture(sw->texture);
5197+
sw->texture = make_subwindow_texture(w,
5198+
sw->full_rect.w, sw->full_rect.h);
5199+
SDL_assert(sw->texture);
5200+
}
5201+
if (sw->font->cache.texture) {
5202+
SDL_DestroyTexture(sw->font->cache.texture);
5203+
sw->font->cache.texture = NULL;
5204+
make_font_cache(w, sw->font);
5205+
}
5206+
}
5207+
5208+
/*
5209+
* Recreate any textures used by dialogs or menus in the window.
5210+
*/
5211+
for (d = w->d_head; d; d = d->next) {
5212+
if (d->recreate_textures_callback) {
5213+
(*d->recreate_textures_callback)(d, w, all);
5214+
}
5215+
}
5216+
}
5217+
5218+
refresh_angband_terms(a);
5219+
}
5220+
50915221
static bool is_ok_col_row(const struct subwindow *subwindow,
50925222
const SDL_Rect *rect, int cell_w, int cell_h)
50935223
{
@@ -5332,7 +5462,7 @@ static void load_status_bar(struct sdlpui_window *window)
53325462
window->status_bar = sdlpui_start_simple_menu(NULL, NULL,
53335463
2 + N_ELEMENTS(window->subwindows)
53345464
+ ((window->index == MAIN_WINDOW) ? 1 : 0), false, true,
5335-
NULL, 0);
5465+
NULL, NULL, 0);
53365466
c = sdlpui_get_simple_menu_next_unused(window->status_bar,
53375467
SDLPUI_MFLG_NONE);
53385468
sdlpui_create_submenu_button(c, "Menu", SDLPUI_HOR_CENTER,

src/sdl2/pui-dlg.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,13 +2073,24 @@ void sdlpui_menu_handle_loses_key(struct sdlpui_dialog *d,
20732073
* be layed out in a single column; if false, it causes the controls to layed
20742074
* out in a single row.
20752075
* \param border will, if true, cause a border to be drawn about the menu.
2076+
* \param pop_callback will, if not NULL, be the function called when the
2077+
* menu is popped up or down.
2078+
* \param recreate_textures_callback will, if not NULL, be the function
2079+
* called by the controlling application in response to
2080+
* SDL_RENDER_TARGETS_RESET (all set to false) or SDL_RENDER_DEVICE_RESET
2081+
* (all set to true) events.
2082+
* \param tag sets the tag field of the generated menu so different menus using
2083+
* the same callbacks can be distinguished.
20762084
* \return a pointer to the structure describing the menu.
20772085
*/
20782086
struct sdlpui_dialog *sdlpui_start_simple_menu(struct sdlpui_dialog *parent,
20792087
struct sdlpui_control *parent_ctrl, int preallocated,
20802088
bool vertical, bool border, void (*pop_callback)(
20812089
struct sdlpui_dialog *d, struct sdlpui_window *w,
2082-
bool up), int tag)
2090+
bool up),
2091+
void (*recreate_textures_callback)(struct sdlpui_dialog *d,
2092+
struct sdlpui_window *w, bool all),
2093+
int tag)
20832094
{
20842095
struct sdlpui_dialog *result = SDL_malloc(sizeof(*result));
20852096
struct sdlpui_simple_menu *psm = SDL_malloc(sizeof(*psm));
@@ -2106,6 +2117,7 @@ struct sdlpui_dialog *sdlpui_start_simple_menu(struct sdlpui_dialog *parent,
21062117

21072118
result->ftb = &simple_menu_funcs;
21082119
result->pop_callback = pop_callback;
2120+
result->recreate_textures_callback = recreate_textures_callback;
21092121
result->next = NULL;
21102122
result->prev = NULL;
21112123
result->texture = NULL;
@@ -2208,11 +2220,20 @@ void sdlpui_complete_simple_menu(struct sdlpui_dialog *d,
22082220
*
22092221
* \param button_label is the text label to use for the button that dismisses
22102222
* the dialog.
2223+
* \param pop_callback will, if not NULL, be the function called when the
2224+
* dialog is popped up or down.
2225+
* \param recreate_textures_callback will, if not NULL, be the function
2226+
* called by the controlling application in response to
2227+
* SDL_RENDER_TARGETS_RESET (all set to false) or SDL_RENDER_DEVICE_RESET
2228+
* (all set to true) events.
22112229
* \return a pointer to the structure describing the dialog.
22122230
*/
22132231
struct sdlpui_dialog *sdlpui_start_simple_info(const char *button_label,
22142232
void (*pop_callback)(struct sdlpui_dialog *d,
2215-
struct sdlpui_window *w, bool up), int tag)
2233+
struct sdlpui_window *w, bool up),
2234+
void (*recreate_textures_callback)(struct sdlpui_dialog *d,
2235+
struct sdlpui_window *w, bool all),
2236+
int tag)
22162237
{
22172238
struct sdlpui_dialog *result = SDL_malloc(sizeof(*result));
22182239
struct sdlpui_simple_info *psi = SDL_malloc(sizeof(*psi));
@@ -2226,6 +2247,7 @@ struct sdlpui_dialog *sdlpui_start_simple_info(const char *button_label,
22262247

22272248
result->ftb = &simple_info_funcs;
22282249
result->pop_callback = pop_callback;
2250+
result->recreate_textures_callback = recreate_textures_callback;
22292251
result->next = NULL;
22302252
result->prev = NULL;
22312253
result->texture = NULL;

0 commit comments

Comments
 (0)