Complete.Org: Mailing Lists: Archives: freeciv-dev: April 2005:
[Freeciv-Dev] (PR#12751) move nation sprites into the tileset
Home

[Freeciv-Dev] (PR#12751) move nation sprites into the tileset

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#12751) move nation sprites into the tileset
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 9 Apr 2005 20:20:43 -0700
Reply-to: bugs@xxxxxxxxxxx

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

This patch moves nation sprites into the tileset, out of the nation struct.

It's a bit more complicated than the other patches of this type because:

1.  There is already a get_nation_flag_sprite, but it has the wrong 
interface (we need the nation ID but this can't be found from the nation 
pointer).

2.  There is no limit on the number of nations.

As a side effect I removed a duplicated function get_flag() from 
gui-gtk-2.0.

-jason

Index: client/tilespec.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/tilespec.c,v
retrieving revision 1.287
diff -u -r1.287 tilespec.c
--- client/tilespec.c   10 Apr 2005 01:59:31 -0000      1.287
+++ client/tilespec.c   10 Apr 2005 02:59:47 -0000
@@ -133,6 +133,8 @@
   struct sprite *government[G_MAGIC];
   struct sprite *unittype[U_LAST];
 
+  struct sprite_vector nation_flag;
+
   struct citizen_graphic {
     /* Each citizen type has up to MAX_NUM_CITIZEN_SPRITES different
      * sprites, as defined by the tileset. */
@@ -2149,6 +2151,7 @@
   /* no other place to initialize these variables */
   t->sprites.city.tile_wall = NULL;
   t->sprites.city.tile = NULL;
+  sprite_vector_init(&t->sprites.nation_flag);
 }
 
 /**************************************************************************
@@ -2502,32 +2505,37 @@
                  nation->flag_graphic_alt,
                  "f.unknown", NULL};
   int i;
+  struct sprite *sprite = NULL;
 
-  nation->flag_sprite = NULL;
-  for (i = 0; tags[i] && !nation->flag_sprite; i++) {
-    nation->flag_sprite = load_sprite(t, tags[i]);
+  for (i = 0; tags[i] && !sprite; i++) {
+    sprite = load_sprite(t, tags[i]);
   }
-  if (!nation->flag_sprite) {
+  if (!sprite) {
     /* Should never get here because of the f.unknown fallback. */
     freelog(LOG_FATAL, "No national flag for %s.", nation->name);
     exit(EXIT_FAILURE);
   }
+
+  sprite_vector_reserve(&t->sprites.nation_flag, game.nation_count);
+  t->sprites.nation_flag.p[id] = sprite;
 }
 
 /**********************************************************************
   Return the flag graphic to be used by the city.
 ***********************************************************************/
-static struct sprite *get_city_nation_flag_sprite(const struct city *pcity)
+static struct sprite *get_city_nation_flag_sprite(const struct tileset *t,
+                                                 const struct city *pcity)
 {
-  return get_nation_by_plr(city_owner(pcity))->flag_sprite;
+  return get_nation_flag_sprite(t, city_owner(pcity)->nation);
 }
 
 /**********************************************************************
   Return a sprite for the national flag for this unit.
 ***********************************************************************/
-static struct sprite *get_unit_nation_flag_sprite(const struct unit *punit)
+static struct sprite *get_unit_nation_flag_sprite(const struct tileset *t,
+                                                 const struct unit *punit)
 {
-  return get_nation_by_plr(unit_owner(punit))->flag_sprite;
+  return get_nation_flag_sprite(t, unit_owner(punit)->nation);
 }
 
 /**************************************************************************
@@ -2641,7 +2649,7 @@
 
   if (backdrop) {
     if (!solid_color_behind_units) {
-      ADD_SPRITE(get_unit_nation_flag_sprite(punit), TRUE,
+      ADD_SPRITE(get_unit_nation_flag_sprite(t, punit), TRUE,
                 FULL_TILE_X_OFFSET + t->flag_offset_x,
                 FULL_TILE_Y_OFFSET + t->flag_offset_y);
     } else {
@@ -3778,7 +3786,7 @@
     /* City.  Some city sprites are drawn later. */
     if (pcity && draw_cities) {
       if (!solid_color_behind_units) {
-       ADD_SPRITE(get_city_nation_flag_sprite(pcity), TRUE,
+       ADD_SPRITE(get_city_nation_flag_sprite(t, pcity), TRUE,
                   FULL_TILE_X_OFFSET + t->flag_offset_x,
                   FULL_TILE_Y_OFFSET + t->flag_offset_y);
       }
@@ -4158,6 +4166,7 @@
   } specfile_list_iterate_end;
 
   sprite_vector_free(&t->sprites.explode.unit);
+  sprite_vector_free(&t->sprites.nation_flag);
 }
 
 /**************************************************************************
@@ -4196,9 +4205,13 @@
   Return the sprite for the nation.
 **************************************************************************/
 struct sprite *get_nation_flag_sprite(const struct tileset *t,
-                                     const struct nation_type *nation)
+                                     Nation_Type_id nation)
 {
-  return nation ? nation->flag_sprite : NULL;
+  if (nation < 0 || nation >= game.nation_count) {
+    assert(0);
+    return NULL;
+  }
+  return t->sprites.nation_flag.p[nation];
 }
 
 /**************************************************************************
Index: client/tilespec.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/tilespec.h,v
retrieving revision 1.146
diff -u -r1.146 tilespec.h
--- client/tilespec.h   10 Apr 2005 01:59:31 -0000      1.146
+++ client/tilespec.h   10 Apr 2005 02:59:47 -0000
@@ -179,7 +179,7 @@
                                  int citizen_index,
                                  const struct city *pcity);
 struct sprite *get_nation_flag_sprite(const struct tileset *t,
-                                     const struct nation_type *nation);
+                                     Nation_Type_id nation);
 struct sprite *get_tech_sprite(const struct tileset *t, Tech_Type_id tech);
 struct sprite *get_building_sprite(const struct tileset *t, Impr_Type_id b);
 struct sprite *get_government_sprite(const struct tileset *t,
Index: client/gui-gtk-2.0/dialogs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/dialogs.c,v
retrieving revision 1.98
diff -u -r1.98 dialogs.c
--- client/gui-gtk-2.0/dialogs.c        28 Mar 2005 16:59:14 -0000      1.98
+++ client/gui-gtk-2.0/dialogs.c        10 Apr 2005 02:59:47 -0000
@@ -1611,7 +1611,7 @@
 
     gtk_list_store_append(store, &it);
 
-    s = crop_blankspace(nation->flag_sprite);
+    s = crop_blankspace(get_nation_flag_sprite(tileset, i));
     img = sprite_get_pixbuf(s);
     gtk_list_store_set(store, &it, 0, i, 1, FALSE, 2, img, -1);
     free_sprite(s);
Index: client/gui-gtk-2.0/pages.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/pages.c,v
retrieving revision 1.23
diff -u -r1.23 pages.c
--- client/gui-gtk-2.0/pages.c  28 Mar 2005 16:59:14 -0000      1.23
+++ client/gui-gtk-2.0/pages.c  10 Apr 2005 02:59:47 -0000
@@ -31,17 +31,18 @@
 #include "support.h"
 #include "version.h"
 
-#include "chatline.h"
 #include "civclient.h"
 #include "clinet.h"
-#include "connectdlg.h"
 #include "connectdlg_common.h"
+#include "packhand.h"
+
+#include "chatline.h"
+#include "connectdlg.h"
 #include "graphics.h"
 #include "gui_main.h"
 #include "gui_stuff.h"
-#include "packhand.h"
 #include "pages.h"
-
+#include "plrdlg.h" /* for get_flag() */
 
 GtkWidget *start_message_area;
 GtkListStore *conn_model;       
@@ -1426,46 +1427,6 @@
   send_start_saved_game();
 }
 
-#define MIN_DIMENSION 5
-/**************************************************************************
- FIXME: this is somewhat duplicated in plrdlg.c, 
-        should be somewhere else and non-static
-**************************************************************************/
-static GdkPixbuf *get_flag(const struct nation_type *nation)
-{
-  int x0, y0, x1, y1, w, h;
-  GdkPixbuf *im, *im2;
-  struct sprite *flag;
-
-  flag = get_nation_flag_sprite(tileset, nation);
-
-  if (!flag) {
-    return NULL;
-  }
-
-  /* calculate the bounding box ... */
-  sprite_get_bounding_box(flag, &x0, &y0, &x1, &y1);
-
-  assert(x0 != -1);
-  assert(y0 != -1);
-  assert(x1 != -1);
-  assert(y1 != -1);
-
-  w = (x1 - x0) + 1;
-  h = (y1 - y0) + 1;
-
-  /* if the flag is smaller then 5 x 5, something is wrong */
-  assert(w >= MIN_DIMENSION && h >= MIN_DIMENSION);
-
-  /* get the pixbuf and crop*/
-  im = gdk_pixbuf_new_subpixbuf(sprite_get_pixbuf(flag), x0, y0, w, h);
-  im2 = gdk_pixbuf_copy(im);
-  g_object_unref(im);
-
-  /* and finaly store the scaled flag pixbuf in the static flags array */
-  return im2;
-}
-
 /**************************************************************************
 ...
 **************************************************************************/
@@ -1496,7 +1457,7 @@
 
     /* set flag if we've got one to set. */
     if (packet->nations[i] != NO_NATION_SELECTED) {
-      GdkPixbuf *flag = get_flag(get_nation_by_idx(packet->nations[i]));
+      GdkPixbuf *flag = get_flag(packet->nations[i]);
 
       if (flag) {
        gtk_list_store_set(nation_store, &iter, 1, flag, -1);
Index: client/gui-gtk-2.0/plrdlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/plrdlg.c,v
retrieving revision 1.53
diff -u -r1.53 plrdlg.c
--- client/gui-gtk-2.0/plrdlg.c 28 Mar 2005 16:59:14 -0000      1.53
+++ client/gui-gtk-2.0/plrdlg.c 10 Apr 2005 02:59:47 -0000
@@ -474,13 +474,13 @@
 /**************************************************************************
  Builds the flag pixmap.
 **************************************************************************/
-static GdkPixbuf *get_flag(struct nation_type *nation)
+GdkPixbuf *get_flag(Nation_Type_id nation)
 {
   int x0, y0, x1, y1, w, h;
   GdkPixbuf *im, *im2;
   struct sprite *flag;
 
-  flag = nation->flag_sprite;
+  flag = get_nation_flag_sprite(tileset, nation);
 
   /* calculate the bounding box ... */
   sprite_get_bounding_box(flag, &x0, &y0, &x1, &y1);
@@ -527,7 +527,7 @@
        gtk_list_store_set(store, it, k, p, -1);
        break;
       case COL_FLAG:
-        pixbuf = get_flag(get_nation_by_plr(plr));
+        pixbuf = get_flag(plr->nation);
         gtk_list_store_set(store, it, k, pixbuf, -1);
         g_object_unref(pixbuf);
        break;
Index: client/gui-gtk-2.0/plrdlg.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/plrdlg.h,v
retrieving revision 1.4
diff -u -r1.4 plrdlg.h
--- client/gui-gtk-2.0/plrdlg.h 25 Nov 2004 06:57:17 -0000      1.4
+++ client/gui-gtk-2.0/plrdlg.h 10 Apr 2005 02:59:47 -0000
@@ -17,4 +17,7 @@
 
 void popdown_players_dialog(void);
 
+/* Misc helper functions */
+GdkPixbuf *get_flag(Nation_Type_id nation);
+
 #endif  /* FC__PLRDLG_H */
Index: common/nation.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/nation.h,v
retrieving revision 1.39
diff -u -r1.39 nation.h
--- common/nation.h     29 Mar 2005 12:28:26 -0000      1.39
+++ common/nation.h     10 Apr 2005 02:59:47 -0000
@@ -40,8 +40,6 @@
 typedef int Nation_Type_id;
 typedef int Team_Type_id;
 
-struct sprite;                 /* opaque; client-gui specific */
-
 /*
  * The city_name structure holds information about a default choice for
  * the city name.  The "name" field is, of course, just the name for
@@ -85,7 +83,6 @@
   struct leader *leaders;
   int city_style;
   struct city_name *city_names;                /* The default city names. */
-  struct sprite *flag_sprite;
   char *legend;                                /* may be empty */
 
   /* civilwar_nations is a NO_NATION_SELECTED-terminated list of index of

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#12751) move nation sprites into the tileset, Jason Short <=