From 2c4ccafa0a4f76cd766ec3a646f64908279a804b Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sun, 3 Jul 2022 10:44:56 +0300 Subject: [PATCH 56/56] sdl2: Do not pass copies of SDL_Rect so much Use pointers instead of copying entire structure. See osdn #44931 Signed-off-by: Marko Lindqvist --- client/gui-sdl2/citydlg.c | 4 ++-- client/gui-sdl2/graphics.c | 27 ++++++++++++++------------- client/gui-sdl2/graphics.h | 4 ++-- client/gui-sdl2/gui_main.c | 28 +++++++++++++++------------- client/gui-sdl2/widget.c | 13 +++++++------ client/gui-sdl2/widget_icon.c | 18 +++++++++++------- 6 files changed, 51 insertions(+), 43 deletions(-) diff --git a/client/gui-sdl2/citydlg.c b/client/gui-sdl2/citydlg.c index 8182d5944b..0f852d501c 100644 --- a/client/gui-sdl2/citydlg.c +++ b/client/gui-sdl2/citydlg.c @@ -202,7 +202,7 @@ static int city_dlg_callback(struct widget *pwindow) && city_owner(pcity_dlg->pcity) == client.conn.playing) { if (is_in_rect_area(main_data.event.motion.x, main_data.event.motion.y, - pcity_dlg->spec_area)) { + &(pcity_dlg->spec_area))) { city_rotate_specialist(pcity_dlg->pcity, (main_data.event.motion.x - pcity_dlg->spec_area.x) / pcity_dlg->citizen_step); @@ -618,7 +618,7 @@ static SDL_Surface *create_unit_surface(struct unit *punit, bool support, /* Get unit sprite width, and crop top. Bottom might get restored in 'support' * case below. */ - src_rect = get_smaller_surface_rect(destcanvas->surf); + get_smaller_surface_rect(destcanvas->surf, &src_rect); if (support) { int i, step; diff --git a/client/gui-sdl2/graphics.c b/client/gui-sdl2/graphics.c index 62e815f9c7..7da1ae6782 100644 --- a/client/gui-sdl2/graphics.c +++ b/client/gui-sdl2/graphics.c @@ -1377,10 +1377,10 @@ bool correct_rect_region(SDL_Rect *prect) /**********************************************************************//** Return whether coordinates are in rectangle. **************************************************************************/ -bool is_in_rect_area(int x, int y, SDL_Rect rect) +bool is_in_rect_area(int x, int y, const SDL_Rect *rect) { - return ((x >= rect.x) && (x < rect.x + rect.w) - && (y >= rect.y) && (y < rect.y + rect.h)); + return ((x >= rect->x) && (x < rect->x + rect->w) + && (y >= rect->y) && (y < rect->y + rect->h)); } /* ===================================================================== */ @@ -1388,15 +1388,15 @@ bool is_in_rect_area(int x, int y, SDL_Rect rect) /**********************************************************************//** Get visible rectangle from surface. **************************************************************************/ -SDL_Rect get_smaller_surface_rect(SDL_Surface *surf) +void get_smaller_surface_rect(SDL_Surface *surf, SDL_Rect *rect) { - SDL_Rect src; int w, h, x, y; Uint16 minX, maxX, minY, maxY; Uint32 colorkey; Uint32 mask; fc_assert(surf != NULL); + fc_assert(rect != NULL); minX = surf->w; maxX = 0; @@ -1418,6 +1418,7 @@ SDL_Rect get_smaller_surface_rect(SDL_Surface *surf) { Uint8 *pixel = (Uint8 *)surf->pixels; Uint8 *start = pixel; + x = 0; y = 0; w = surf->w; @@ -1667,12 +1668,10 @@ SDL_Rect get_smaller_surface_rect(SDL_Surface *surf) } unlock_surf(surf); - src.x = minX; - src.y = minY; - src.w = maxX - minX + 1; - src.h = maxY - minY + 1; - - return src; + rect->x = minX; + rect->y = minY; + rect->w = maxX - minX + 1; + rect->h = maxY - minY + 1; } /**********************************************************************//** @@ -1680,7 +1679,9 @@ SDL_Rect get_smaller_surface_rect(SDL_Surface *surf) **************************************************************************/ SDL_Surface *crop_visible_part_from_surface(SDL_Surface *psrc) { - SDL_Rect src = get_smaller_surface_rect(psrc); + SDL_Rect src; + + get_smaller_surface_rect(psrc, &src); return crop_rect_from_surface(psrc, &src); } @@ -1808,7 +1809,7 @@ void create_frame(SDL_Surface *dest, Sint16 left, Sint16 top, struct color gsdl2_color = { .color = pcolor }; struct sprite *vertical = create_sprite(1, height, &gsdl2_color); struct sprite *horizontal = create_sprite(width, 1, &gsdl2_color); - SDL_Rect tmp,dst = { left, top, 0, 0 }; + SDL_Rect tmp, dst = { .x = left, .y = top, .w = 0, .h = 0 }; tmp = dst; alphablit(vertical->psurface, NULL, dest, &tmp, 255); diff --git a/client/gui-sdl2/graphics.h b/client/gui-sdl2/graphics.h index c378e80e5c..579687eebe 100644 --- a/client/gui-sdl2/graphics.h +++ b/client/gui-sdl2/graphics.h @@ -287,7 +287,7 @@ int main_window_height(void); /* Rect */ bool correct_rect_region(SDL_Rect *prect); -bool is_in_rect_area(int x, int y, SDL_Rect rect); +bool is_in_rect_area(int x, int y, const SDL_Rect *rect); int fill_rect_alpha(SDL_Surface *surf, SDL_Rect *prect, SDL_Color *pcolor); @@ -305,7 +305,7 @@ SDL_Surface *resize_surface_box(const SDL_Surface *psrc, bool absolute_dimensions); SDL_Surface *crop_visible_part_from_surface(SDL_Surface *psrc); -SDL_Rect get_smaller_surface_rect(SDL_Surface *psrc); +void get_smaller_surface_rect(SDL_Surface *surf, SDL_Rect *rect); #define map_rgba(format, color) \ SDL_MapRGBA(format, (color).r, (color).g, (color).b, (color).a) diff --git a/client/gui-sdl2/gui_main.c b/client/gui-sdl2/gui_main.c index 8ce84dd208..8691c63f0c 100644 --- a/client/gui-sdl2/gui_main.c +++ b/client/gui-sdl2/gui_main.c @@ -486,33 +486,35 @@ static void update_button_hold_state(void) **************************************************************************/ static int check_scroll_area(int x, int y) { - SDL_Rect rect_north = {0, 0, main_data.map->w, SCROLL_MAP_AREA}; - SDL_Rect rect_east = {main_data.map->w - SCROLL_MAP_AREA, 0, SCROLL_MAP_AREA, main_data.map->h}; - SDL_Rect rect_south = {0, main_data.map->h - SCROLL_MAP_AREA, main_data.map->w, SCROLL_MAP_AREA}; - SDL_Rect rect_west = {0, 0, SCROLL_MAP_AREA, main_data.map->h}; - - if (is_in_rect_area(x, y, rect_north)) { + SDL_Rect rect_north = {.x = 0, .y = 0, .w = main_data.map->w, .y = SCROLL_MAP_AREA}; + SDL_Rect rect_east = {.x = main_data.map->w - SCROLL_MAP_AREA, .y = 0, + .w = SCROLL_MAP_AREA, .h = main_data.map->h}; + SDL_Rect rect_south = {.x = 0, .y = main_data.map->h - SCROLL_MAP_AREA, + .w = main_data.map->w, .h = SCROLL_MAP_AREA}; + SDL_Rect rect_west = {.x = 0, .y = 0, .w = SCROLL_MAP_AREA, .h = main_data.map->h}; + + if (is_in_rect_area(x, y, &rect_north)) { is_map_scrolling = TRUE; - if (is_in_rect_area(x, y, rect_west)) { + if (is_in_rect_area(x, y, &rect_west)) { scroll_dir = DIR8_NORTHWEST; - } else if (is_in_rect_area(x, y, rect_east)) { + } else if (is_in_rect_area(x, y, &rect_east)) { scroll_dir = DIR8_NORTHEAST; } else { scroll_dir = DIR8_NORTH; } - } else if (is_in_rect_area(x, y, rect_south)) { + } else if (is_in_rect_area(x, y, &rect_south)) { is_map_scrolling = TRUE; - if (is_in_rect_area(x, y, rect_west)) { + if (is_in_rect_area(x, y, &rect_west)) { scroll_dir = DIR8_SOUTHWEST; - } else if (is_in_rect_area(x, y, rect_east)) { + } else if (is_in_rect_area(x, y, &rect_east)) { scroll_dir = DIR8_SOUTHEAST; } else { scroll_dir = DIR8_SOUTH; } - } else if (is_in_rect_area(x, y, rect_east)) { + } else if (is_in_rect_area(x, y, &rect_east)) { is_map_scrolling = TRUE; scroll_dir = DIR8_EAST; - } else if (is_in_rect_area(x, y, rect_west)) { + } else if (is_in_rect_area(x, y, &rect_west)) { is_map_scrolling = TRUE; scroll_dir = DIR8_WEST; } else { diff --git a/client/gui-sdl2/widget.c b/client/gui-sdl2/widget.c index 0d546003dd..abafc4e353 100644 --- a/client/gui-sdl2/widget.c +++ b/client/gui-sdl2/widget.c @@ -222,17 +222,18 @@ SDL_Surface *create_bcgnd_surf(SDL_Surface *ptheme, enum widget_state state, struct widget *find_next_widget_at_pos(struct widget *start_widget, int x, int y) { - SDL_Rect area = {0, 0, 0, 0}; struct widget *pwidget; pwidget = start_widget ? start_widget : begin_main_widget_list; while (pwidget) { - area.x = pwidget->dst->dest_rect.x + pwidget->size.x; - area.y = pwidget->dst->dest_rect.y + pwidget->size.y; - area.w = pwidget->size.w; - area.h = pwidget->size.h; - if (is_in_rect_area(x, y, area) + SDL_Rect area = { + .x = pwidget->dst->dest_rect.x + pwidget->size.x, + .y = pwidget->dst->dest_rect.y + pwidget->size.y, + .w = pwidget->size.w, + .h = pwidget->size.h}; + + if (is_in_rect_area(x, y, &area) && !((get_wflags(pwidget) & WF_HIDDEN) == WF_HIDDEN)) { return (struct widget *) pwidget; } diff --git a/client/gui-sdl2/widget_icon.c b/client/gui-sdl2/widget_icon.c index f1af81df7f..e5c90e895e 100644 --- a/client/gui-sdl2/widget_icon.c +++ b/client/gui-sdl2/widget_icon.c @@ -153,14 +153,18 @@ void set_new_icon_theme(struct widget *icon_widget, SDL_Surface *new_theme) } /**********************************************************************//** - Ugly hack to create 4-state icon theme from static icon; + Ugly hack to create 4-state icon theme from static icon. **************************************************************************/ SDL_Surface *create_icon_theme_surf(SDL_Surface *icon) { SDL_Color bg_color = { 255, 255, 255, 128 }; - SDL_Rect dest, src = get_smaller_surface_rect(icon); - SDL_Surface *ptheme = create_surf((src.w + adj_size(4)) * 4, src.h + adj_size(4), - SDL_SWSURFACE); + SDL_Rect dest; + SDL_Rect src; + SDL_Surface *ptheme; + + get_smaller_surface_rect(icon, &src); + ptheme = create_surf((src.w + adj_size(4)) * 4, src.h + adj_size(4), + SDL_SWSURFACE); dest.x = adj_size(2); dest.y = (ptheme->h - src.h) / 2; @@ -264,9 +268,9 @@ int draw_icon(struct widget *icon, Sint16 start_x, Sint16 start_y) int draw_icon_from_theme(SDL_Surface *icon_theme, Uint8 state, struct gui_layer *pdest, Sint16 start_x, Sint16 start_y) { - SDL_Rect src, des = {start_x, start_y, 0, 0}; + SDL_Rect src, des = {.x = start_x, .y = start_y, .w = 0, .h = 0}; - if (!icon_theme) { + if (icon_theme == NULL) { return -3; } @@ -295,7 +299,7 @@ SDL_Surface *create_icon_from_theme(SDL_Surface *icon_theme, Uint8 state) { SDL_Rect src; - if (!icon_theme) { + if (icon_theme == NULL) { return NULL; } -- 2.35.1