Complete.Org: Mailing Lists: Archives: freeciv-dev: February 2005:
[Freeciv-Dev] (PR#12140) use sprites to draw the mapview grid
Home

[Freeciv-Dev] (PR#12140) use sprites to draw the mapview grid

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#12140) use sprites to draw the mapview grid
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 23 Feb 2005 18:39:31 -0800
Reply-to: bugs@xxxxxxxxxxx

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

Here is a new and hopefully final patch.  (It needs the v6 tarball for
the image files.)

-jason

? data/isotrident/grid.png
? data/isotrident/grid.spec
? data/misc/colors.png
? data/misc/colors.spec
? data/trident/grid.png
? data/trident/grid.spec
Index: client/mapview_common.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/mapview_common.h,v
retrieving revision 1.97
diff -u -r1.97 mapview_common.h
--- client/mapview_common.h     24 Feb 2005 01:56:40 -0000      1.97
+++ client/mapview_common.h     24 Feb 2005 02:29:44 -0000
@@ -154,11 +154,11 @@
            pedge->tile[1] = map_pos_to_tile((GRI_sum - 4) / 4, /* S */     \
                                             (GRI_diff + 2) / 4);           \
          } else {                                                          \
-           pedge->type = EDGE_EW;                                          \
+           pedge->type = EDGE_WE;                                          \
            pedge->tile[0] = map_pos_to_tile((GRI_sum - 6) / 4,             \
-                                            GRI_diff / 4); /* E */         \
-           pedge->tile[1] = map_pos_to_tile((GRI_sum - 2) / 4,             \
                                             GRI_diff / 4); /* W */         \
+           pedge->tile[1] = map_pos_to_tile((GRI_sum - 2) / 4,             \
+                                            GRI_diff / 4); /* E */         \
          }                                                                 \
        }                                                                   \
       } else {                                                             \
@@ -183,17 +183,17 @@
          /* Edge. */                                                       \
          pedge = &GRI_edge;                                                \
          if (GRI_y_itr % 2 == 0) {                                         \
-           pedge->type = EDGE_EW;                                          \
-           pedge->tile[0] = map_pos_to_tile(GRI_x_itr / 2 - 1,             \
-                                            (GRI_y_itr - 1) / 2);          \
-           pedge->tile[1] = map_pos_to_tile(GRI_x_itr / 2,                 \
-                                            (GRI_y_itr - 1) / 2);          \
-         } else {                                                          \
            pedge->type = EDGE_NS;                                          \
-           pedge->tile[0] = map_pos_to_tile((GRI_x_itr - 1) / 2,           \
+           pedge->tile[0] = map_pos_to_tile((GRI_x_itr - 1) / 2, /* N */   \
                                             GRI_y_itr / 2 - 1);            \
-           pedge->tile[1] = map_pos_to_tile((GRI_x_itr - 1) / 2,           \
+           pedge->tile[1] = map_pos_to_tile((GRI_x_itr - 1) / 2, /* S */   \
                                             GRI_y_itr / 2);                \
+         } else {                                                          \
+           pedge->type = EDGE_WE;                                          \
+           pedge->tile[0] = map_pos_to_tile(GRI_x_itr / 2 - 1, /* W */     \
+                                            (GRI_y_itr - 1) / 2);          \
+           pedge->tile[1] = map_pos_to_tile(GRI_x_itr / 2, /* E */         \
+                                            (GRI_y_itr - 1) / 2);          \
          }                                                                 \
        }                                                                   \
       }                                                                        
    \
Index: client/tilespec.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/tilespec.c,v
retrieving revision 1.240
diff -u -r1.240 tilespec.c
--- client/tilespec.c   24 Feb 2005 01:56:40 -0000      1.240
+++ client/tilespec.c   24 Feb 2005 02:29:45 -0000
@@ -1463,6 +1463,54 @@
   SET_SPRITE(tx.airbase,    "tx.airbase");
   SET_SPRITE(tx.fog,        "tx.fog");
 
+  if (load_sprite("grid.main.ns")) {
+    for (i = 0; i < MAX_NUM_PLAYERS + MAX_NUM_BARBARIANS; i++) {
+      my_snprintf(buffer, sizeof(buffer), "colors.player%d", i);
+      SET_SPRITE(colors.player[i], buffer);
+    }
+
+    for (i = 0; i < EDGE_COUNT; i++) {
+      char *name[EDGE_COUNT] = {"ns", "we"};
+      int j, p;
+
+      my_snprintf(buffer, sizeof(buffer), "grid.main.%s", name[i]);
+      SET_SPRITE(grid.main[i], buffer);
+
+      my_snprintf(buffer, sizeof(buffer), "grid.city.%s", name[i]);
+      SET_SPRITE(grid.city[i], buffer);
+
+      my_snprintf(buffer, sizeof(buffer), "grid.worked.%s", name[i]);
+      SET_SPRITE(grid.worked[i], buffer);
+
+      my_snprintf(buffer, sizeof(buffer), "grid.unavailable.%s", name[i]);
+      SET_SPRITE(grid.unavailable[i], buffer);
+
+      my_snprintf(buffer, sizeof(buffer), "grid.selected.%s", name[i]);
+      SET_SPRITE(grid.selected[i], buffer);
+
+      my_snprintf(buffer, sizeof(buffer), "grid.coastline.%s", name[i]);
+      SET_SPRITE(grid.coastline[i], buffer);
+
+      for (j = 0; j < 2; j++) {
+       struct Sprite *s;
+
+       my_snprintf(buffer, sizeof(buffer), "grid.borders.%c", name[i][j]);
+       SET_SPRITE(grid.borders[i][j], buffer);
+
+       for (p = 0; p < MAX_NUM_PLAYERS + MAX_NUM_BARBARIANS; p++) {
+         if (sprites.colors.player[p] && sprites.grid.borders[i][j]) {
+           s = crop_sprite(sprites.colors.player[p],
+                           0, 0, NORMAL_TILE_WIDTH, NORMAL_TILE_HEIGHT,
+                           sprites.grid.borders[i][j], 0, 0);
+         } else {
+           s = sprites.grid.borders[i][j];
+         }
+         sprites.grid.player_borders[p][i][j] = s;
+       }
+      }
+    }
+  }
+
   for (i = 0; i < num_index_cardinal; i++) {
     my_snprintf(buffer, sizeof(buffer), "tx.s_river_%s",
                cardinal_index_str(i));
@@ -2739,6 +2787,110 @@
 
 
 /****************************************************************************
+  Fill in the grid sprites for the given tile, city, and unit.
+****************************************************************************/
+static int fill_grid_sprite_array(struct drawn_sprite *sprs,
+                                 const struct tile *ptile,
+                                 const struct tile_edge *pedge,
+                                 const struct tile_corner *pcorner,
+                                 const struct unit *punit,
+                                 const struct city *pcity,
+                                 const struct city *citymode)
+{
+  struct drawn_sprite *saved_sprs = sprs;
+
+  if (!sprites.grid.main[EDGE_NS]) {
+    if (ptile && tile_get_known(ptile) != TILE_UNKNOWN) {
+      /* Add grid.  In classic view this is done later. */
+      ADD_GRID(ptile, citymode);
+    }
+  } else if (pedge) {
+    bool known[EDGE_COUNT], city[EDGE_COUNT], unit[EDGE_COUNT];
+    bool worked[EDGE_COUNT], blocked[EDGE_COUNT];
+    int i;
+    struct unit *pfocus = get_unit_in_focus();
+
+    for (i = 0; i < 2; i++) {
+      const struct tile *tile = pedge->tile[i];
+      struct player *powner = tile ? map_get_owner(tile) : NULL;
+      int dummy_x, dummy_y;
+
+      known[i] = tile && tile_get_known(tile) != TILE_UNKNOWN;
+      city[i] = tile && (powner == NULL || powner == game.player_ptr)
+       && player_in_city_radius(game.player_ptr, tile);
+      unit[i] = tile && pfocus && unit_flag(pfocus, F_CITIES)
+       && city_can_be_built_here(pfocus->tile, pfocus)
+       && base_map_to_city_map(&dummy_x, &dummy_y, pfocus->tile, tile);
+      worked[i] = blocked[i] = FALSE;
+      if (city[i]) {
+       enum city_tile_type ttype;
+       struct city *dummy;
+
+       get_worker_on_map_position(tile, &ttype, &dummy);
+       switch (ttype) {
+       case C_TILE_EMPTY:
+         break;
+       case C_TILE_WORKER:
+         worked[i] = TRUE;
+         break;
+       case C_TILE_UNAVAILABLE:
+         blocked[i] = TRUE;
+         break;
+       }
+      }
+    }
+
+    if ((pedge->tile[0]
+        && map_deco[pedge->tile[0]->index].hilite == HILITE_CITY)
+       || (pedge->tile[1]
+           && map_deco[pedge->tile[1]->index].hilite == HILITE_CITY)) {
+      ADD_SPRITE_SIMPLE(sprites.grid.selected[pedge->type]);
+    } else if (!draw_terrain && draw_coastline
+              && pedge->tile[0] && pedge->tile[1]
+              && known[0] && known[1]
+              && (is_ocean(pedge->tile[0]->terrain)
+                  ^ is_ocean(pedge->tile[1]->terrain))) {
+      ADD_SPRITE_SIMPLE(sprites.grid.coastline[pedge->type]);
+    } else if (draw_map_grid) {
+      if (blocked[0] || blocked[1]) {
+       ADD_SPRITE_SIMPLE(sprites.grid.unavailable[pedge->type]);
+      } else if (worked[0] || worked[1]) {
+       ADD_SPRITE_SIMPLE(sprites.grid.worked[pedge->type]);
+      } else if (city[0] || city[1]) {
+       ADD_SPRITE_SIMPLE(sprites.grid.city[pedge->type]);
+      } else if (known[0] || known[1]) {
+       ADD_SPRITE_SIMPLE(sprites.grid.main[pedge->type]);
+      }
+    } else if (draw_city_outlines) {
+      if (XOR(city[0], city[1])) {
+       ADD_SPRITE_SIMPLE(sprites.grid.city[pedge->type]);
+      }
+      if (XOR(unit[0], unit[1])) {
+       ADD_SPRITE_SIMPLE(sprites.grid.worked[pedge->type]);
+      }
+    }
+
+    if (draw_borders && game.borders > 0 && known[0] && known[1]) {
+      struct player *owner0 = map_get_owner(pedge->tile[0]);
+      struct player *owner1 = map_get_owner(pedge->tile[1]);
+
+      if (owner0 != owner1) {
+       if (owner0) {
+         ADD_SPRITE_SIMPLE(sprites.grid.player_borders
+                           [owner0->player_no][pedge->type][0]);
+       }
+       if (owner1) {
+         ADD_SPRITE_SIMPLE(sprites.grid.player_borders
+                           [owner1->player_no][pedge->type][1]);
+       }
+      }
+    }
+  }
+
+  return sprs - saved_sprs;
+}
+
+/****************************************************************************
   Fill in the sprite array for the given tile, city, and unit.
 
   ptile, if specified, gives the tile.  If specified the terrain and specials
@@ -2900,9 +3052,9 @@
     break;
 
   case LAYER_GRID1:
-    if (ptile && tile_get_known(ptile) != TILE_UNKNOWN && is_isometric) {
-      /* Add grid.  In classic view this is done later. */
-      ADD_GRID(ptile, citymode);
+    if (is_isometric) {
+      sprs += fill_grid_sprite_array(sprs, ptile, pedge, pcorner,
+                                    punit, pcity, citymode);
     }
     break;
 
@@ -2985,9 +3137,9 @@
     break;
 
   case LAYER_GRID2:
-    if (ptile && tile_get_known(ptile) != TILE_UNKNOWN && !is_isometric) {
-      /* Add grid.  In iso-view this is done earlier. */
-      ADD_GRID(ptile, citymode);
+    if (!is_isometric) {
+      sprs += fill_grid_sprite_array(sprs, ptile, pedge, pcorner,
+                                    punit, pcity, citymode);
     }
     break;
 
Index: client/tilespec.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/tilespec.h,v
retrieving revision 1.106
diff -u -r1.106 tilespec.h
--- client/tilespec.h   24 Feb 2005 01:56:40 -0000      1.106
+++ client/tilespec.h   24 Feb 2005 02:29:45 -0000
@@ -27,10 +27,12 @@
 struct Sprite;                 /* opaque; gui-dep */
 
 /* An edge is the border between two tiles.  This structure represents one
- * edge.  The tiles are in unspecified order for the moment. */
+ * edge.  The tiles are given in the same order as the enumeration name. */
 struct tile_edge {
   enum {
-    EDGE_NS, EDGE_EW
+    EDGE_NS, /* North and south */
+    EDGE_WE, /* West and east */
+    EDGE_COUNT
   } type;
 #define NUM_EDGE_TILES 2
   const struct tile *tile[NUM_EDGE_TILES];
@@ -322,6 +324,20 @@
       *darkness[MAX_INDEX_CARDINAL],         /* first unused */
       *river_outlet[4];                /* indexed by enum direction4 */
   } tx;                                /* terrain extra */
+  struct {
+    struct Sprite
+      *main[EDGE_COUNT],
+      *city[EDGE_COUNT],
+      *worked[EDGE_COUNT],
+      *unavailable[EDGE_COUNT],
+      *selected[EDGE_COUNT],
+      *coastline[EDGE_COUNT],
+      *borders[EDGE_COUNT][2],
+      *player_borders[MAX_NUM_PLAYERS + MAX_NUM_BARBARIANS][EDGE_COUNT][2];
+  } grid;
+  struct {
+    struct Sprite *player[MAX_NUM_PLAYERS + MAX_NUM_BARBARIANS];
+  } colors;
 
   struct terrain_drawing_data *terrain[MAX_NUM_TERRAINS];
 };
Index: data/isotrident.tilespec
===================================================================
RCS file: /home/freeciv/CVS/freeciv/data/isotrident.tilespec,v
retrieving revision 1.30
diff -u -r1.30 isotrident.tilespec
--- data/isotrident.tilespec    16 Feb 2005 18:09:05 -0000      1.30
+++ data/isotrident.tilespec    24 Feb 2005 02:29:45 -0000
@@ -49,9 +49,11 @@
 ; low-color systems, and if there are any duplicate tags (lattermost
 ; tag is used).
 files = 
+  "misc/colors.spec",
   "isotrident/terrain1.spec",
   "isotrident/terrain2.spec",
   "isotrident/tiles.spec",
+  "isotrident/grid.spec",
   "misc/small.spec",
   "trident/units.spec",
   "isotrident/unitextras.spec",
Index: data/trident.tilespec
===================================================================
RCS file: /home/freeciv/CVS/freeciv/data/trident.tilespec,v
retrieving revision 1.30
diff -u -r1.30 trident.tilespec
--- data/trident.tilespec       11 Feb 2005 16:58:00 -0000      1.30
+++ data/trident.tilespec       24 Feb 2005 02:29:45 -0000
@@ -45,10 +45,12 @@
 ; low-color systems, and if there are any duplicate tags (lattermost
 ; tag is used).
 files = 
+  "misc/colors.spec",
   "trident/tiles.spec",
   "misc/small.spec",
   "trident/units.spec",
   "misc/flags.spec",
+  "trident/grid.spec",
   "trident/roads.spec",
   "misc/buildings.spec",
   "misc/space.spec",
Index: data/trident_shields.tilespec
===================================================================
RCS file: /home/freeciv/CVS/freeciv/data/trident_shields.tilespec,v
retrieving revision 1.21
diff -u -r1.21 trident_shields.tilespec
--- data/trident_shields.tilespec       13 Feb 2005 19:09:05 -0000      1.21
+++ data/trident_shields.tilespec       24 Feb 2005 02:29:45 -0000
@@ -48,11 +48,13 @@
 ; low-color systems, and if there are any duplicate tags (lattermost
 ; tag is used).
 files = 
+  "misc/colors.spec",
   "trident/tiles.spec",
   "misc/small.spec",
   "trident/units.spec",
   "trident/select.spec",
   "misc/flags.spec",
+  "trident/grid.spec",
   "trident/roads.spec",
   "misc/buildings.spec",
   "misc/space.spec",
Index: data/isotrident/Makefile.am
===================================================================
RCS file: /home/freeciv/CVS/freeciv/data/isotrident/Makefile.am,v
retrieving revision 1.8
diff -u -r1.8 Makefile.am
--- data/isotrident/Makefile.am 13 Feb 2005 19:09:05 -0000      1.8
+++ data/isotrident/Makefile.am 24 Feb 2005 02:29:45 -0000
@@ -8,6 +8,8 @@
        cities.spec     \
        fog.png         \
        fog.spec        \
+       grid.png        \
+       grid.spec       \
        morecities.png  \
        morecities.spec \
        nuke.spec       \
Index: data/misc/Makefile.am
===================================================================
RCS file: /home/freeciv/CVS/freeciv/data/misc/Makefile.am,v
retrieving revision 1.8
diff -u -r1.8 Makefile.am
--- data/misc/Makefile.am       3 Nov 2004 03:34:54 -0000       1.8
+++ data/misc/Makefile.am       24 Feb 2005 02:29:45 -0000
@@ -8,6 +8,8 @@
        buildings.spec  \
        chiefs.png      \
        chiefs.spec     \
+       colors.png      \
+       colors.spec     \
        flags.spec      \
        intro.png       \
        radar.png       \
Index: data/trident/Makefile.am
===================================================================
RCS file: /home/freeciv/CVS/freeciv/data/trident/Makefile.am,v
retrieving revision 1.10
diff -u -r1.10 Makefile.am
--- data/trident/Makefile.am    13 Feb 2005 19:09:05 -0000      1.10
+++ data/trident/Makefile.am    24 Feb 2005 02:29:45 -0000
@@ -11,6 +11,8 @@
        explosions.spec \
        fog.png         \
        fog.spec        \
+       grid.png        \
+       grid.spec       \
        roads.png       \
        roads.spec      \
        select.png      \

[Prev in Thread] Current Thread [Next in Thread]