<URL: http://bugs.freeciv.org/Ticket/Display.html?id=38392 >

On 18/03/07, Marko Lindqvist <[EMAIL PROTECTED]> wrote:
>
>  This patch frees old city style sprites before loading new ones (or
> more typically: loading same ones again)

 - Updated against svn


 - ML

diff -Nurd -X.diff_ignore freeciv/client/gui-gtk-2.0/gui_main.c freeciv/client/gui-gtk-2.0/gui_main.c
--- freeciv/client/gui-gtk-2.0/gui_main.c	2007-08-01 17:17:28.000000000 +0300
+++ freeciv/client/gui-gtk-2.0/gui_main.c	2007-08-01 23:04:41.000000000 +0300
@@ -1470,6 +1470,7 @@
     gdk_gc_set_foreground(mask_bg_gc, &pixel);
   }
 
+  tileset_init(tileset);
   tileset_load_tiles(tileset);
 
   /* keep the icon of the executable on Windows (see PR#36491) */
diff -Nurd -X.diff_ignore freeciv/client/gui-sdl/gui_main.c freeciv/client/gui-sdl/gui_main.c
--- freeciv/client/gui-sdl/gui_main.c	2007-07-09 22:39:44.000000000 +0300
+++ freeciv/client/gui-sdl/gui_main.c	2007-08-01 23:04:41.000000000 +0300
@@ -927,7 +927,8 @@
                        CF_DRAW_PLAYERS_CEASEFIRE_STATUS|
                        CF_DRAW_PLAYERS_PEACE_STATUS|
                        CF_DRAW_PLAYERS_ALLIANCE_STATUS);
-                       
+
+  tileset_init(tileset);
   tileset_load_tiles(tileset);
   tileset_use_prefered_theme(tileset);
       
diff -Nurd -X.diff_ignore freeciv/client/gui-win32/gui_main.c freeciv/client/gui-win32/gui_main.c
--- freeciv/client/gui-win32/gui_main.c	2007-03-05 21:12:09.000000000 +0200
+++ freeciv/client/gui-win32/gui_main.c	2007-08-01 23:04:41.000000000 +0300
@@ -746,6 +746,7 @@
   SetMenu(root_window,create_mainmenu());
   ShowWindow(root_window,SW_SHOWNORMAL);
   UpdateWindow(root_window);
+  tileset_init(tileset);
   tileset_load_tiles(tileset);
   init_fog_bmp();
   load_cursors();
diff -Nurd -X.diff_ignore freeciv/client/gui-xaw/gui_main.c freeciv/client/gui-xaw/gui_main.c
--- freeciv/client/gui-xaw/gui_main.c	2007-08-01 17:17:29.000000000 +0300
+++ freeciv/client/gui-xaw/gui_main.c	2007-08-01 23:04:41.000000000 +0300
@@ -444,6 +444,7 @@
      setup_widgets() has enough colors available:  (on 256-colour systems)
   */
   setup_widgets();
+  tileset_init(tileset);
   tileset_load_tiles(tileset);
   load_intro_gfx();
   load_cursors();
diff -Nurd -X.diff_ignore freeciv/client/tilespec.c freeciv/client/tilespec.c
--- freeciv/client/tilespec.c	2007-08-01 17:17:38.000000000 +0300
+++ freeciv/client/tilespec.c	2007-08-01 23:07:20.000000000 +0300
@@ -150,6 +150,7 @@
       struct sprite *sprite;
     } *thresholds;
   } *styles;
+  int num_styles;
 };
 
 struct named_sprites {
@@ -1969,9 +1970,13 @@
 {
   /* get style and match the best tile based on city size */
   int style = style_of_city(pcity);
-  int num_thresholds = city_sprite->styles[style].num_thresholds;
+  int num_thresholds;
   int t;
 
+  assert(style < city_sprite->num_styles);
+
+  num_thresholds = city_sprite->styles[style].num_thresholds;
+
   if (num_thresholds == 0) {
     return NULL;
   }
@@ -2001,10 +2006,14 @@
   int style, size;
   char buffer[128];
 
-  city_sprite->styles = fc_malloc(game.control.styles_count
+  /* Store number of styles we have allocated memory for.
+   * game.control.styles_count might change if client disconnects from
+   * server and connects new one. */
+  city_sprite->num_styles = game.control.styles_count;
+  city_sprite->styles = fc_malloc(city_sprite->num_styles
 				  * sizeof(*city_sprite->styles));
 
-  for (style = 0; style < game.control.styles_count; style++) {
+  for (style = 0; style < city_sprite->num_styles; style++) {
     int thresholds = 0;
     struct sprite *sprite;
     char *graphic = city_styles[style].graphic;
@@ -2050,7 +2059,7 @@
   if (!city_sprite) {
     return;
   }
-  for (style = 0; style < game.control.styles_count; style++) {
+  for (style = 0; style < city_sprite->num_styles; style++) {
     if (city_sprite->styles[style].thresholds) {
       free(city_sprite->styles[style].thresholds);
     }
@@ -4489,6 +4498,12 @@
 void tileset_setup_city_tiles(struct tileset *t, int style)
 {
   if (style == game.control.styles_count - 1) {
+
+    /* Free old sprites */
+    free_city_sprite(t->sprites.city.tile);
+    free_city_sprite(t->sprites.city.wall);
+    free_city_sprite(t->sprites.city.occupied);
+
     t->sprites.city.tile = load_city_sprite(t, "city");
     t->sprites.city.wall = load_city_sprite(t, "wall");
     t->sprites.city.occupied = load_city_sprite(t, "occupied");
@@ -4989,3 +5004,14 @@
 		       "default");
   gui_clear_theme();
 }
+
+/****************************************************************************
+  Initialize tileset structure
+****************************************************************************/
+void tileset_init(struct tileset *t)
+{
+  /* We currently have no city sprites loaded. */
+  t->sprites.city.tile     = NULL;
+  t->sprites.city.wall     = NULL;
+  t->sprites.city.occupied = NULL;
+}
diff -Nurd -X.diff_ignore freeciv/client/tilespec.h freeciv/client/tilespec.h
--- freeciv/client/tilespec.h	2007-08-01 17:17:38.000000000 +0300
+++ freeciv/client/tilespec.h	2007-08-01 23:04:41.000000000 +0300
@@ -116,6 +116,7 @@
 const char **get_tileset_list(void);
 
 struct tileset *tileset_read_toplevel(const char *tileset_name, bool verbose);
+void tileset_init(struct tileset *t);
 void tileset_free(struct tileset *tileset);
 void tileset_load_tiles(struct tileset *t);
 void tileset_free_tiles(struct tileset *t);
_______________________________________________
Freeciv-dev mailing list
Freeciv-dev@gna.org
https://mail.gna.org/listinfo/freeciv-dev

Reply via email to