Complete.Org: Mailing Lists: Archives: freeciv-dev: July 2003:
[Freeciv-Dev] (PR#4578) put_unit() in mapview_common
Home

[Freeciv-Dev] (PR#4578) put_unit() in mapview_common

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#4578) put_unit() in mapview_common
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 15 Jul 2003 23:42:14 -0700
Reply-to: rt@xxxxxxxxxxxxxx

This patch adds two new functions to mapview_common: put_unit and 
put_unit_full.  These draw a unit onto a canvas_store.  GUIs can be 
changed to use these functions instead of their own implementation of 
unit drawing.

gui-gtk and gui-xaw use these functions.  Other clients do not yet.

This patch is a minor cleanup and a step toward PR#4576 and PR#3572.

jason

? rc
Index: client/mapview_common.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/mapview_common.c,v
retrieving revision 1.46
diff -u -r1.46 mapview_common.c
--- client/mapview_common.c     2003/07/11 20:11:42     1.46
+++ client/mapview_common.c     2003/07/16 06:21:51
@@ -390,6 +390,47 @@
   }
 }
 
+/**************************************************************************
+  Draw the given unit onto the canvas store at the given location.
+
+  unit_offset_x, unit_offset_y, unit_width, unit_height are used
+  in iso-view to draw only part of the tile.  Non-iso view should use
+  put_unit_full instead.
+**************************************************************************/
+void put_unit(struct unit *punit, struct canvas_store *pcanvas_store,
+             int canvas_x, int canvas_y,
+             int unit_offset_x, int unit_offset_y,
+             int unit_width, int unit_height)
+{
+  struct Sprite *sprites[40];
+  int solid_bg, i;
+  int count = fill_unit_sprite_array(sprites, punit, &solid_bg);
+
+  if (!is_isometric && solid_bg) {
+    gui_put_rectangle(pcanvas_store, player_color(unit_owner(punit)),
+                     canvas_x, canvas_y, UNIT_TILE_WIDTH, UNIT_TILE_HEIGHT);
+  }
+
+  for (i = 0; i < count; i++) {
+    if (sprites[i]) {
+      /* units are never fogged */
+      gui_put_sprite(pcanvas_store,
+                    canvas_x, canvas_y, sprites[i],
+                    unit_offset_x, unit_offset_y,
+                    unit_width, unit_height);
+    }
+  }
+}
+
+/**************************************************************************
+  Draw the given unit onto the canvas store at the given location.
+**************************************************************************/
+void put_unit_full(struct unit *punit, struct canvas_store *pcanvas_store,
+                  int canvas_x, int canvas_y)
+{
+  put_unit(punit, pcanvas_store, canvas_x, canvas_y,
+          0, 0, UNIT_TILE_WIDTH, UNIT_TILE_HEIGHT);
+}
 
 /**************************************************************************
   Draw the given map tile at the given canvas position in non-isometric
Index: client/mapview_common.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/mapview_common.h,v
retrieving revision 1.30
diff -u -r1.30 mapview_common.h
--- client/mapview_common.h     2003/07/11 20:11:42     1.30
+++ client/mapview_common.h     2003/07/16 06:21:51
@@ -146,6 +146,13 @@
 bool tile_visible_mapcanvas(int map_x, int map_y);
 bool tile_visible_and_not_on_border_mapcanvas(int map_x, int map_y);
 
+void put_unit(struct unit *punit, struct canvas_store *pcanvas_store,
+             int canvas_x, int canvas_y,
+             int unit_offset_x, int unit_offset_y,
+             int unit_width, int unit_height);
+void put_unit_full(struct unit *punit, struct canvas_store *pcanvas_store,
+                  int canvas_x, int canvas_y);
+
 void put_one_tile(struct canvas_store *pcanvas_store, int map_x, int map_y,
                  int canvas_x, int canvas_y, bool citymode);
 
Index: client/gui-gtk/graphics.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/graphics.h,v
retrieving revision 1.12
diff -u -r1.12 graphics.h
--- client/gui-gtk/graphics.h   2003/02/20 23:14:33     1.12
+++ client/gui-gtk/graphics.h   2003/07/16 06:21:51
@@ -26,11 +26,6 @@
   int       height;
 };
 
-struct canvas_store
-{
-  GdkPixmap *pixmap;
-};
-
 typedef struct Sprite SPRITE;
 
 void   create_overlay_unit     (GtkWidget *pixcomm, int i);
Index: client/gui-gtk/gui_main.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/gui_main.c,v
retrieving revision 1.136
diff -u -r1.136 gui_main.c
--- client/gui-gtk/gui_main.c   2003/07/02 16:40:18     1.136
+++ client/gui-gtk/gui_main.c   2003/07/16 06:21:51
@@ -935,6 +935,7 @@
                                  -1);
   mapview_canvas.store = fc_malloc(sizeof(*mapview_canvas.store));
   mapview_canvas.store->pixmap = map_canvas_store;
+  mapview_canvas.store->pixcomm = NULL;
 
   overview_canvas_store = gdk_pixmap_new(root_window,
                                          overview_canvas_store_width,
Index: client/gui-gtk/gui_main.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/gui_main.h,v
retrieving revision 1.10
diff -u -r1.10 gui_main.h
--- client/gui-gtk/gui_main.h   2003/02/26 19:00:36     1.10
+++ client/gui-gtk/gui_main.h   2003/07/16 06:21:51
@@ -15,7 +15,14 @@
 
 #include <gtk/gtk.h>
 
+#include "gtkpixcomm.h"
 #include "gui_main_g.h"
+
+struct canvas_store
+{
+  GdkPixmap *pixmap;   /* if NULL, the pixcomm is drawn to instead. */
+  GtkPixcomm *pixcomm;
+};
 
 extern GdkFont *        main_fontset;
 extern GdkFont *        prod_fontset;
Index: client/gui-gtk/mapview.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/mapview.c,v
retrieving revision 1.171
diff -u -r1.171 mapview.c
--- client/gui-gtk/mapview.c    2003/07/11 20:11:42     1.171
+++ client/gui-gtk/mapview.c    2003/07/16 06:21:51
@@ -56,14 +56,8 @@
 static void put_overlay_tile_gpixmap(GtkPixcomm *p,
                                     int canvas_x, int canvas_y,
                                     struct Sprite *ssprite);
-static void put_unit_pixmap(struct unit *punit, GdkPixmap *pm,
-                           int canvas_x, int canvas_y);
 static void put_line(GdkDrawable *pm, int x, int y, int dir);
 
-static void put_unit_pixmap_draw(struct unit *punit, GdkPixmap *pm,
-                                int canvas_x, int canvas_y,
-                                int offset_x, int offset_y_unit,
-                                int width, int height_unit);
 static void pixmap_put_overlay_tile_draw(GdkDrawable *pixmap,
                                         int canvas_x, int canvas_y,
                                         struct Sprite *ssprite,
@@ -146,7 +140,7 @@
 
       put_one_tile(&store, losing_unit->x, losing_unit->y,
                   0, 0, FALSE);
-      put_unit_pixmap(losing_unit, single_tile_pixmap, 0, 0);
+      put_unit_full(losing_unit, &store, 0, 0);
       pixmap_put_overlay_tile(single_tile_pixmap, 0, 0,
                              sprites.explode.unit[i]);
 
@@ -357,6 +351,8 @@
                               int old_canvas_x, int old_canvas_y,
                               int new_canvas_x, int new_canvas_y)
 {
+  struct canvas_store store = {single_tile_pixmap};
+
   /* Clear old sprite. */
   gdk_draw_pixmap(map_canvas->window, civ_gc, map_canvas_store, old_canvas_x,
                  old_canvas_y, old_canvas_x, old_canvas_y, UNIT_TILE_WIDTH,
@@ -365,7 +361,7 @@
   /* Draw the new sprite. */
   gdk_draw_pixmap(single_tile_pixmap, civ_gc, map_canvas_store, new_canvas_x,
                  new_canvas_y, 0, 0, UNIT_TILE_WIDTH, UNIT_TILE_HEIGHT);
-  put_unit_pixmap(punit, single_tile_pixmap, 0, 0);
+  put_unit_full(punit, &store, 0, 0);
 
   /* Write to screen. */
   gdk_draw_pixmap(map_canvas->window, civ_gc, single_tile_pixmap, 0, 0,
@@ -633,73 +629,6 @@
 }
 
 /**************************************************************************
-FIXME: Find a better way to put flags and such on top.
-**************************************************************************/
-static void put_unit_pixmap(struct unit *punit, GdkPixmap *pm,
-                           int canvas_x, int canvas_y)
-{
-  int solid_bg;
-
-  if (is_isometric) {
-    struct Sprite *sprites[40];
-    int count = fill_unit_sprite_array(sprites, punit, &solid_bg);
-    int i;
-
-    assert(!solid_bg);
-    for (i=0; i<count; i++) {
-      if (sprites[i]) {
-       pixmap_put_overlay_tile(pm, canvas_x, canvas_y, sprites[i]);
-      }
-    }
-  } else { /* is_isometric */
-    struct Sprite *sprites[40];
-    int count = fill_unit_sprite_array(sprites, punit, &solid_bg);
-
-    if (count) {
-      int i = 0;
-
-      if (solid_bg) {
-       gdk_gc_set_foreground(fill_bg_gc,
-                             colors_standard[player_color(unit_owner(punit))]);
-       gdk_draw_rectangle(pm, fill_bg_gc, TRUE,
-                          canvas_x, canvas_y,
-                          UNIT_TILE_WIDTH, UNIT_TILE_HEIGHT);
-      } else {
-       pixmap_put_overlay_tile(pm, canvas_x, canvas_y, sprites[0]);
-       i++;
-      }
-
-      for (; i<count; i++) {
-       if (sprites[i])
-         pixmap_put_overlay_tile(pm, canvas_x, canvas_y, sprites[i]);
-      }
-    }
-  }
-}
-
-/**************************************************************************
-Only used for isometric view.
-**************************************************************************/
-static void put_unit_pixmap_draw(struct unit *punit, GdkPixmap *pm,
-                                int canvas_x, int canvas_y,
-                                int offset_x, int offset_y_unit,
-                                int width, int height_unit)
-{
-  struct Sprite *sprites[40];
-  int dummy;
-  int count = fill_unit_sprite_array(sprites, punit, &dummy);
-  int i;
-
-  for (i=0; i<count; i++) {
-    if (sprites[i]) {
-      pixmap_put_overlay_tile_draw(pm, canvas_x, canvas_y, sprites[i],
-                                  offset_x, offset_y_unit,
-                                  width, height_unit, 0);
-    }
-  }
-}
-
-/**************************************************************************
 Only used for isometric view.
 **************************************************************************/
 void put_one_tile_full(GdkDrawable *pm, int x, int y,
@@ -911,23 +840,9 @@
 **************************************************************************/
 void put_unit_gpixmap(struct unit *punit, GtkPixcomm *p)
 {
-  struct Sprite *sprites[40];
-  int solid_bg;
-  int count = fill_unit_sprite_array(sprites, punit, &solid_bg);
-
-  if (count) {
-    int i;
+  struct canvas_store canvas_store = {NULL, p};
 
-    if (solid_bg) {
-      gtk_pixcomm_fill(p, colors_standard[player_color(unit_owner(punit))],
-                      FALSE);
-    }
-
-    for (i=0;i<count;i++) {
-      if (sprites[i])
-        put_overlay_tile_gpixmap(p, 0, 0, sprites[i]);
-    }
-  }
+  put_unit_full(punit, &canvas_store, 0, 0);
 
   gtk_pixcomm_changed(GTK_PIXCOMM(p));
 }
@@ -1093,8 +1008,13 @@
                    struct Sprite *sprite,
                    int offset_x, int offset_y, int width, int height)
 {
-  pixmap_put_sprite(pcanvas_store->pixmap, canvas_x, canvas_y,
-                   sprite, offset_x, offset_y, width, height);
+  if (pcanvas_store->pixmap) {
+    pixmap_put_sprite(pcanvas_store->pixmap, canvas_x, canvas_y,
+                     sprite, offset_x, offset_y, width, height);
+  } else {
+    gtk_pixcomm_copyto(pcanvas_store->pixcomm, sprite,
+                      canvas_x, canvas_y, FALSE);
+  }
 }
 
 /**************************************************************************
@@ -1127,9 +1047,13 @@
                       enum color_std color,
                       int canvas_x, int canvas_y, int width, int height)
 {
-  gdk_gc_set_foreground(fill_bg_gc, colors_standard[color]);
-  gdk_draw_rectangle(pcanvas_store->pixmap, fill_bg_gc, TRUE,
-                    canvas_x, canvas_y, width, height);
+  if (pcanvas_store->pixmap) {
+    gdk_gc_set_foreground(fill_bg_gc, colors_standard[color]);
+    gdk_draw_rectangle(pcanvas_store->pixmap, fill_bg_gc, TRUE,
+                      canvas_x, canvas_y, width, height);
+  } else {
+    gtk_pixcomm_fill(pcanvas_store->pixcomm, colors_standard[color], FALSE);
+  }
 }
 
 /**************************************************************************
@@ -1547,6 +1471,7 @@
   int count, i = 0;
   int fog;
   int solid_bg;
+  struct canvas_store canvas_store = {pm};
 
   if (!width || !(height || height_unit))
     return;
@@ -1747,10 +1672,10 @@
 
   /*** Unit ***/
   if (punit && (draw_units || (punit == pfocus && draw_focus_unit))) {
-    put_unit_pixmap_draw(punit, pm,
-                        canvas_x, canvas_y - NORMAL_TILE_HEIGHT/2,
-                        offset_x, offset_y_unit,
-                        width, height_unit);
+    put_unit(punit, &canvas_store,
+            canvas_x, canvas_y - NORMAL_TILE_HEIGHT/2,
+            offset_x, offset_y_unit,
+            width, height_unit);
     if (!pcity && unit_list_size(&map_get_tile(x, y)->units) > 1)
       pixmap_put_overlay_tile_draw(pm,
                                   canvas_x, canvas_y-NORMAL_TILE_HEIGHT/2,
Index: client/gui-xaw/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/citydlg.c,v
retrieving revision 1.93
diff -u -r1.93 citydlg.c
--- client/gui-xaw/citydlg.c    2003/02/27 00:31:10     1.93
+++ client/gui-xaw/citydlg.c    2003/07/16 06:21:51
@@ -1703,16 +1703,17 @@
   for(i=0;
       i<pdialog->num_units_shown && ITERATOR_PTR(myiter);
       ITERATOR_NEXT(myiter), i++) {
+    struct canvas_store store;
+
     punit=(struct unit*)ITERATOR_PTR(myiter);
     pixcomm=pdialog->support_unit_pixcomms[i];
+    store.pixmap = XawPixcommPixmap(pixcomm);
 
     if(!adj_base && unitid && punit->id!=unitid)
       continue;
 
     XawPixcommClear(pixcomm); /* STG */
-    put_unit_pixmap(punit,
-                   XawPixcommPixmap(pixcomm), 
-                   0, 0);
+    put_unit_full(punit, &store, 0, 0);
     put_unit_pixmap_city_overlays(punit,
                                  XawPixcommPixmap(pixcomm));
 
@@ -1764,16 +1765,17 @@
   for(i=0;
       i<pdialog->num_units_shown && ITERATOR_PTR(myiter);
       ITERATOR_NEXT(myiter), i++) {
+    struct canvas_store store;
+
     punit=(struct unit*)ITERATOR_PTR(myiter);
     pixcomm=pdialog->present_unit_pixcomms[i];
+    store.pixmap = XawPixcommPixmap(pixcomm);
 
     if(!adj_base && unitid && punit->id!=unitid)
       continue;
 
     XawPixcommClear(pixcomm); /* STG */
-    put_unit_pixmap(punit,
-                   XawPixcommPixmap(pixcomm),
-                   0, 0);
+    put_unit_full(punit, &store, 0, 0);
 
     xaw_expose_now(pixcomm);
 
Index: client/gui-xaw/dialogs.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/dialogs.c,v
retrieving revision 1.85
diff -u -r1.85 dialogs.c
--- client/gui-xaw/dialogs.c    2003/07/10 03:50:56     1.85
+++ client/gui-xaw/dialogs.c    2003/07/16 06:21:51
@@ -1608,6 +1608,7 @@
     struct unit *punit=unit_list_get(&ptile->units, i);
     struct unit_type *punittemp=unit_type(punit);
     struct city *pcity;
+    struct canvas_store store;
     
     if(!(i%r))  {
       nargs=0;
@@ -1633,8 +1634,8 @@
 
     XFillRectangle(display, unit_select_pixmaps[i], fill_bg_gc,
                   0, 0, NORMAL_TILE_WIDTH, NORMAL_TILE_HEIGHT);
-
-    put_unit_pixmap(punit, unit_select_pixmaps[i], 0, 0);
+    store.pixmap = unit_select_pixmaps[i];
+    put_unit_full(punit, &store, 0, 0);
 
     nargs=0;
     XtSetArg(args[nargs], XtNbitmap, (XtArgVal)unit_select_pixmaps[i]);nargs++;
Index: client/gui-xaw/gui_main.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/gui_main.c,v
retrieving revision 1.80
diff -u -r1.80 gui_main.c
--- client/gui-xaw/gui_main.c   2003/05/03 20:20:16     1.80
+++ client/gui-xaw/gui_main.c   2003/07/16 06:21:51
@@ -837,7 +837,9 @@
   
   XawPixcommClear(w);
   if (punit) {
-    put_unit_pixmap(punit, XawPixcommPixmap(w), 0, 0);
+    struct canvas_store store = {XawPixcommPixmap(w)};
+
+    put_unit_full(punit, &store, 0, 0);
     xaw_expose_now(w);
   }
 }
Index: client/gui-xaw/mapview.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/mapview.c,v
retrieving revision 1.136
diff -u -r1.136 mapview.c
--- client/gui-xaw/mapview.c    2003/07/11 20:11:42     1.136
+++ client/gui-xaw/mapview.c    2003/07/16 06:21:51
@@ -103,7 +103,7 @@
     anim_timer = renew_timer_start(anim_timer, TIMER_USER, TIMER_ACTIVE);
 
     put_one_tile(&store, 0, 0, losing_unit->x, losing_unit->y, FALSE);
-    put_unit_pixmap(losing_unit, single_tile_pixmap, 0, 0);
+    put_unit_full(losing_unit, &store, 0, 0);
     pixmap_put_overlay_tile(single_tile_pixmap, 0, 0, sprites.explode.unit[i]);
 
     XCopyArea(display, single_tile_pixmap, XtWindow(map_canvas), civ_gc,
@@ -339,6 +339,8 @@
                               int old_canvas_x, int old_canvas_y,
                               int new_canvas_x, int new_canvas_y)
 {
+  struct canvas_store store = {single_tile_pixmap};
+
   /* Clear old sprite. */
   XCopyArea(display, map_canvas_store, XtWindow(map_canvas), civ_gc,
            old_canvas_x, old_canvas_y, UNIT_TILE_WIDTH, UNIT_TILE_HEIGHT,
@@ -348,7 +350,7 @@
   XCopyArea(display, map_canvas_store, single_tile_pixmap, civ_gc,
            new_canvas_x, new_canvas_y, UNIT_TILE_WIDTH, UNIT_TILE_HEIGHT, 0,
            0);
-  put_unit_pixmap(punit, single_tile_pixmap, 0, 0);
+  put_unit_full(punit, &store, 0, 0);
 
   /* Write to screen. */
   XCopyArea(display, single_tile_pixmap, XtWindow(map_canvas), civ_gc, 0, 0,
@@ -877,39 +879,6 @@
   pixmap_put_overlay_tile(pm, canvas_x, canvas_y, 
sprites.city.tile_shieldnum[shield]);
   pixmap_put_overlay_tile(pm, canvas_x, canvas_y, 
sprites.city.tile_tradenum[trade]);
 }
-
-
-/**************************************************************************
-...
-**************************************************************************/
-void put_unit_pixmap(struct unit *punit, Pixmap pm,
-                    int canvas_x, int canvas_y)
-{
-  struct Sprite *sprites[40];
-  int solid_bg;
-  int count = fill_unit_sprite_array(sprites, punit, &solid_bg);
-
-  if (count) {
-    int i = 0;
-
-    if (solid_bg) {
-      XSetForeground(display, fill_bg_gc,
-                    colors_standard[player_color(unit_owner(punit))]);
-      XFillRectangle(display, pm, fill_bg_gc, 
-                    canvas_x, canvas_y,
-                    NORMAL_TILE_WIDTH, NORMAL_TILE_HEIGHT);
-    } else {
-      pixmap_put_overlay_tile(pm, canvas_x, canvas_y, sprites[0]);
-      i++;
-    }
-
-    for (; i<count; i++) {
-      if (sprites[i])
-       pixmap_put_overlay_tile(pm, canvas_x, canvas_y, sprites[i]);
-    }
-  }
-}
-
 
 /**************************************************************************
   FIXME: 
Index: client/gui-xaw/mapview.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/mapview.h,v
retrieving revision 1.17
diff -u -r1.17 mapview.h
--- client/gui-xaw/mapview.h    2003/04/03 04:13:49     1.17
+++ client/gui-xaw/mapview.h    2003/07/16 06:21:51
@@ -28,9 +28,6 @@
 Pixmap get_citizen_pixmap(enum citizen_type type, int cnum,
                          struct city *pcity);
 
-void put_unit_pixmap(struct unit *punit, Pixmap pm,
-                    int canvas_x, int canvas_y);
-
 void put_unit_pixmap_city_overlays(struct unit *punit, Pixmap pm);
 
 void put_city_tile_output(Pixmap pm, int canvas_x, int canvas_y, 

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#4578) put_unit() in mapview_common, Jason Short <=