Complete.Org: Mailing Lists: Archives: freeciv-dev: February 2005:
[Freeciv-Dev] (PR#12386) encapsulate and vectorize sprites.explode.unit
Home

[Freeciv-Dev] (PR#12386) encapsulate and vectorize sprites.explode.unit

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#12386) encapsulate and vectorize sprites.explode.unit
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 28 Feb 2005 17:16:47 -0800
Reply-to: bugs@xxxxxxxxxxx

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

This patch encapsulates and vectorizes the unit explosion graphics.

* Instead of a manually allocated array with a separate array-size 
value, we have a sprite_vector.  Much simpler.

* mapview_common (which uses this animation) accesses the data via an 
accessor function.  Because the value is a sprite_vector this is rather 
easy.

At some point in the future we should have a general structure for 
animations.  An animation includes not just the sprite_vector but also 
an animation time (taken from the tileset and at runtime modified by the 
client-option animation speed value).  Alternately we could have a fixed 
animation time (like 30 or 60 FPS) and the tileset controlls the runtime 
simply by changing the number of sprites.

-jason

Index: client/mapview_common.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/mapview_common.c,v
retrieving revision 1.199
diff -u -r1.199 mapview_common.c
--- client/mapview_common.c     28 Feb 2005 04:01:52 -0000      1.199
+++ client/mapview_common.c     1 Mar 2005 01:13:45 -0000
@@ -1930,6 +1930,8 @@
                             struct unit *punit1, int hp1)
 {
   static struct timer *anim_timer = NULL; 
+  struct sprite_vector *anim = get_unit_explode_animation();
+  const int num_tiles_explode_unit = sprite_vector_size(anim);
   struct unit *losing_unit = (hp0 == 0 ? punit0 : punit1);
   int canvas_x, canvas_y, i;
 
@@ -1966,8 +1968,9 @@
 
     for (i = 0; i < num_tiles_explode_unit; i++) {
       int w, h;
+      struct Sprite *sprite = *sprite_vector_get(anim, i);
 
-      get_sprite_dimensions(sprites.explode.unit[i], &w, &h);
+      get_sprite_dimensions(sprite, &w, &h);
       anim_timer = renew_timer_start(anim_timer, TIMER_USER, TIMER_ACTIVE);
 
       /* We first draw the explosion onto the unit and draw draw the
@@ -1979,7 +1982,7 @@
       canvas_put_sprite_full(mapview.store,
                             canvas_x + NORMAL_TILE_WIDTH / 2 - w / 2,
                             canvas_y + NORMAL_TILE_HEIGHT / 2 - h / 2,
-                            sprites.explode.unit[i]);
+                            sprite);
       dirty_rect(canvas_x, canvas_y, NORMAL_TILE_WIDTH, NORMAL_TILE_HEIGHT);
 
       flush_dirty();
Index: client/tilespec.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/tilespec.c,v
retrieving revision 1.248
diff -u -r1.248 tilespec.c
--- client/tilespec.c   28 Feb 2005 20:24:38 -0000      1.248
+++ client/tilespec.c   1 Mar 2005 01:13:46 -0000
@@ -82,8 +82,6 @@
 
 static int city_names_font_size, city_productions_font_size;
 
-int num_tiles_explode_unit=0;
-
 static int roadstyle;
 enum fog_style fogstyle;
 
@@ -1386,23 +1384,16 @@
 
   SET_SPRITE(explode.nuke, "explode.nuke");
 
-  num_tiles_explode_unit = 0;
-  do {
-    my_snprintf(buffer, sizeof(buffer), "explode.unit_%d",
-               num_tiles_explode_unit++);
-  } while (sprite_exists(buffer));
-  num_tiles_explode_unit--;
-    
-  if (num_tiles_explode_unit==0) {
-    sprites.explode.unit = NULL;
-  } else {
-    sprites.explode.unit = fc_calloc(num_tiles_explode_unit,
-                                    sizeof(struct Sprite *));
+  sprite_vector_init(&sprites.explode.unit);
+  for (i = 0; ; i++) {
+    struct Sprite *sprite;
 
-    for (i = 0; i < num_tiles_explode_unit; i++) {
-      my_snprintf(buffer, sizeof(buffer), "explode.unit_%d", i);
-      SET_SPRITE(explode.unit[i], buffer);
+    my_snprintf(buffer, sizeof(buffer), "explode.unit_%d", i);
+    sprite = load_sprite(buffer);
+    if (!sprite) {
+      break;
     }
+    sprite_vector_append(&sprites.explode.unit, &sprite);
   }
 
   SET_SPRITE(unit.auto_attack,  "unit.auto_attack");
@@ -3513,9 +3504,7 @@
     free(sf);
   } specfile_list_iterate_end;
 
-  if (num_tiles_explode_unit > 0) {
-    free(sprites.explode.unit);
-  }
+  sprite_vector_free(&sprites.explode.unit);
 }
 
 /**************************************************************************
@@ -3589,6 +3578,15 @@
 }
 
 /**************************************************************************
+  Return a sprite_vector containing the animation sprites for a unit
+  explosion.
+**************************************************************************/
+struct sprite_vector *get_unit_explode_animation(void)
+{
+  return &sprites.explode.unit;
+}
+
+/**************************************************************************
   Loads the sprite. If the sprite is already loaded a reference
   counter is increased. Can return NULL if the sprite couldn't be
   loaded.
Index: client/tilespec.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/tilespec.h,v
retrieving revision 1.112
diff -u -r1.112 tilespec.h
--- client/tilespec.h   28 Feb 2005 20:24:38 -0000      1.112
+++ client/tilespec.h   1 Mar 2005 01:13:46 -0000
@@ -252,7 +252,7 @@
       *total[MAX_INDEX_VALID];     /* includes all possibilities */
   } road, rail;
   struct {
-    struct Sprite **unit;
+    struct sprite_vector unit;
     struct Sprite *nuke;
   } explode;
   struct {
@@ -359,6 +359,7 @@
 struct Sprite *get_arrow_sprite(void);
 struct Sprite *get_tax_sprite(Output_type_id otype);
 struct Sprite *get_treaty_thumb_sprite(bool on_off);
+struct sprite_vector *get_unit_explode_animation(void);
 
 /* full pathnames: */
 extern char *main_intro_filename;
@@ -402,8 +403,6 @@
 #define OVERVIEW_TILE_WIDTH ((MAP_IS_ISOMETRIC ? 2 : 1) * OVERVIEW_TILE_SIZE)
 #define OVERVIEW_TILE_HEIGHT OVERVIEW_TILE_SIZE
 
-extern int num_tiles_explode_unit;
-
 /* Tileset accessor functions. */
 bool tileset_is_isometric(void);
 int tileset_hex_width(void);

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#12386) encapsulate and vectorize sprites.explode.unit, Jason Short <=