Complete.Org: Mailing Lists: Archives: freeciv-dev: May 2004:
[Freeciv-Dev] (PR#8746) extended show_city_desc function
Home

[Freeciv-Dev] (PR#8746) extended show_city_desc function

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#8746) extended show_city_desc function
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 15 May 2004 10:34:14 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=8746 >

This patch extends the show_city_desc function in two ways:

- It takes a canvas parameter telling what canvas to draw on.  This is 
simple enough except under the win32 client, where I just added a FIXME. 
  This is a step toward having multiple backing stores (see PR#8703).

- It takes *width and *height parameters which it fills with the width 
and height of the city desc.  The caller uses these to determine what 
city texts must be redrawn when an area is updated.  This makes things 
more correct since it should now be impossible for city descriptions to 
be overwritten (by moving units, goto lines, etc).

jason

Index: client/mapview_common.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/mapview_common.c,v
retrieving revision 1.115
diff -u -r1.115 mapview_common.c
--- client/mapview_common.c     15 May 2004 16:36:55 -0000      1.115
+++ client/mapview_common.c     15 May 2004 17:14:31 -0000
@@ -1370,8 +1370,8 @@
 void show_city_descriptions(int canvas_x, int canvas_y,
                            int width, int height)
 {
-  const int dx = MAX(MAX_CITY_DESC_WIDTH - NORMAL_TILE_WIDTH, 0);
-  const int dy = MAX_CITY_DESC_HEIGHT;
+  static int max_desc_width = 0, max_desc_height = 0;
+  const int dx = max_desc_width - NORMAL_TILE_WIDTH, dy = max_desc_height;
 
   if (!draw_city_names && !draw_city_productions) {
     return;
@@ -1399,15 +1399,21 @@
    */
   gui_rect_iterate(mapview_canvas.gui_x0 + canvas_x - dx / 2,
                   mapview_canvas.gui_y0 + canvas_y - dy,
-                  width + dx, height + dy,
+                  width + dx, height + dy - NORMAL_TILE_HEIGHT,
                   map_x, map_y, draw) {
     int canvas_x, canvas_y;
     struct city *pcity;
 
     if (normalize_map_pos(&map_x, &map_y)
        && (pcity = map_get_city(map_x, map_y))) {
+      int width = 0, height = 0;
+
       (void) map_to_canvas_pos(&canvas_x, &canvas_y, map_x, map_y);
-      show_city_desc(pcity, canvas_x, canvas_y);
+      show_city_desc(mapview_canvas.store, canvas_x, canvas_y,
+                    pcity, &width, &height);
+
+      max_desc_width = MAX(width, max_desc_width);
+      max_desc_height = MAX(height, max_desc_height);
     }
   } gui_rect_iterate_end;
 }
Index: client/gui-gtk/mapview.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/mapview.c,v
retrieving revision 1.218
diff -u -r1.218 mapview.c
--- client/gui-gtk/mapview.c    26 Apr 2004 21:11:19 -0000      1.218
+++ client/gui-gtk/mapview.c    15 May 2004 17:14:31 -0000
@@ -536,7 +536,8 @@
 /**************************************************************************
 ...
 **************************************************************************/
-void show_city_desc(struct city *pcity, int canvas_x, int canvas_y)
+void show_city_desc(struct canvas *pcanvas, int canvas_x, int canvas_y,
+                   struct city *pcity, int *width, int *height)
 {
   static char buffer[512], buffer2[32];
   int w, w2, ascent;
@@ -545,6 +546,8 @@
   canvas_x += NORMAL_TILE_WIDTH / 2;
   canvas_y += NORMAL_TILE_HEIGHT;
 
+  *width = *height = 0;
+
   get_city_mapview_name_and_growth(pcity, buffer, sizeof(buffer),
                                   buffer2, sizeof(buffer2), &color);
 
@@ -555,32 +558,37 @@
   }
   w2 = gdk_string_width(prod_fontset, buffer2);
 
-  gtk_draw_shadowed_string(map_canvas_store, main_fontset,
+  gtk_draw_shadowed_string(pcanvas->pixmap, main_fontset,
                           toplevel->style->black_gc,
                           toplevel->style->white_gc,
                           canvas_x - (w + w2) / 2,
                           canvas_y + ascent,
                           buffer);
   gdk_gc_set_foreground(civ_gc, colors_standard[color]);
-  gtk_draw_shadowed_string(map_canvas_store, prod_fontset,
+  gtk_draw_shadowed_string(pcanvas->pixmap, prod_fontset,
                           toplevel->style->black_gc,
                           civ_gc,
                           canvas_x - (w + w2) / 2 + w,
                           canvas_y + ascent,
                           buffer2);
 
+  *width = w + w2;
+  *height = gdk_string_height(main_fontset, buffer) + 3;
+
   if (draw_city_productions && (pcity->owner==game.player_idx)) {
     if (draw_city_names) {
-      canvas_y += gdk_string_height(main_fontset, buffer);
+      canvas_y += *height;
     }
 
     get_city_mapview_production(pcity, buffer, sizeof(buffer));
 
     gdk_string_extents(prod_fontset, buffer, NULL, NULL, &w, &ascent, NULL);
-    gtk_draw_shadowed_string(map_canvas_store, prod_fontset,
+    gtk_draw_shadowed_string(pcanvas->pixmap, prod_fontset,
                             toplevel->style->black_gc,
                             toplevel->style->white_gc, canvas_x - w / 2,
-                            canvas_y + ascent + 3, buffer);
+                            canvas_y + ascent, buffer);
+
+    *height += gdk_string_height(prod_fontset, buffer);
   }
 }
 
Index: client/gui-gtk-2.0/mapview.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/mapview.c,v
retrieving revision 1.123
diff -u -r1.123 mapview.c
--- client/gui-gtk-2.0/mapview.c        26 Apr 2004 21:11:19 -0000      1.123
+++ client/gui-gtk-2.0/mapview.c        15 May 2004 17:14:31 -0000
@@ -510,7 +510,8 @@
 /**************************************************************************
 ...
 **************************************************************************/
-void show_city_desc(struct city *pcity, int canvas_x, int canvas_y)
+void show_city_desc(struct canvas *pcanvas, int canvas_x, int canvas_y,
+                   struct city *pcity, int *width, int *height)
 {
   static char buffer[512], buffer2[32];
   PangoRectangle rect, rect2;
@@ -522,6 +523,8 @@
     layout = pango_layout_new(gdk_pango_context_get());
   }
 
+  *width = *height = 0;
+
   canvas_x += NORMAL_TILE_WIDTH / 2;
   canvas_y += NORMAL_TILE_HEIGHT;
 
@@ -554,7 +557,7 @@
       rect2.width = 0;
     }
 
-    gtk_draw_shadowed_string(map_canvas_store,
+    gtk_draw_shadowed_string(pcanvas->v.pixmap,
                             toplevel->style->black_gc,
                             toplevel->style->white_gc,
                             canvas_x - (rect.width + rect2.width) / 2,
@@ -564,7 +567,7 @@
       pango_layout_set_font_description(layout, city_productions_font);
       pango_layout_set_text(layout, buffer2, -1);
       gdk_gc_set_foreground(civ_gc, colors_standard[color]);
-      gtk_draw_shadowed_string(map_canvas_store,
+      gtk_draw_shadowed_string(pcanvas->v.pixmap,
                               toplevel->style->black_gc,
                               civ_gc,
                               canvas_x - (rect.width + rect2.width) / 2
@@ -575,6 +578,9 @@
     }
 
     canvas_y += rect.height + 3;
+
+    *width = rect.width + rect2.width;
+    *height += rect.height + 3;
   }
 
   if (draw_city_productions && (pcity->owner==game.player_idx)) {
@@ -584,11 +590,14 @@
     pango_layout_set_text(layout, buffer, -1);
 
     pango_layout_get_pixel_extents(layout, &rect, NULL);
-    gtk_draw_shadowed_string(map_canvas_store,
+    gtk_draw_shadowed_string(pcanvas->v.pixmap,
                             toplevel->style->black_gc,
                             toplevel->style->white_gc,
                             canvas_x - rect.width / 2,
                             canvas_y + PANGO_ASCENT(rect), layout);
+
+    *width = MAX(*width, rect.width);
+    *height += rect.height;
   }
 }
 
Index: client/gui-stub/mapview.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-stub/mapview.c,v
retrieving revision 1.42
diff -u -r1.42 mapview.c
--- client/gui-stub/mapview.c   23 Apr 2004 22:58:06 -0000      1.42
+++ client/gui-stub/mapview.c   15 May 2004 17:14:31 -0000
@@ -121,10 +121,18 @@
 }
 
 /****************************************************************************
-  Draw a description for the given city.  (canvas_x, canvas_y) is the
-  canvas position of the city itself.
+  Draw a description for the given city.  This description may include the
+  name, turns-to-grow, production, and city turns-to-build (depending on
+  client options).
+
+  (canvas_x, canvas_y) gives the location on the given canvas at which to
+  draw the description.  This is the location of the city itself so the
+  text must be drawn underneath it.  pcity gives the city to be drawn,
+  while (*width, *height) should be set by show_ctiy_desc to contain the
+  width and height of the text block.
 ****************************************************************************/
-void show_city_desc(struct city *pcity, int canvas_x, int canvas_y)
+void show_city_desc(struct canvas *pcanvas, int canvas_x, int canvas_y,
+                   struct city *pcity, int *width, int *height)
 {
   /* PORTME */
 }
Index: client/gui-win32/mapview.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-win32/mapview.c,v
retrieving revision 1.115
diff -u -r1.115 mapview.c
--- client/gui-win32/mapview.c  26 Apr 2004 21:11:19 -0000      1.115
+++ client/gui-win32/mapview.c  15 May 2004 17:14:31 -0000
@@ -503,7 +503,8 @@
 /**************************************************************************
 
 **************************************************************************/
-void show_city_desc(struct city *pcity, int canvas_x, int canvas_y)
+void show_city_desc(struct canvas *pcanvas, int canvas_x, int canvas_y,
+                   struct city *pcity, int *width, int *height)
 {
   char buffer[500];
   int y_offset;
@@ -511,10 +512,13 @@
   HBITMAP old;
 
   /* TODO: hdc should be stored statically */
+  /* FIXME: we should draw to the given pcanvas. */
   hdc = CreateCompatibleDC(NULL);
   old = SelectObject(hdc, mapstorebitmap);
   SetBkMode(hdc,TRANSPARENT);
 
+  *width = *height = 0;
+
   y_offset = canvas_y + NORMAL_TILE_HEIGHT;
   if (draw_city_names && pcity->name) {
     RECT rc;
@@ -538,6 +542,10 @@
     SetTextColor(hdc, RGB(255, 255, 255));
     DrawText(hdc, pcity->name, strlen(pcity->name), &rc,
             DT_NOCLIP | DT_CENTER);
+
+    *width = rc.right - rc.left + 1;
+    *height = rc.bottom - rc.top + 2;
+
     y_offset = rc.bottom + 2;
   }
 
@@ -560,6 +568,9 @@
     rc.bottom--;
     SetTextColor(hdc, RGB(255, 255, 255));
     DrawText(hdc, buffer, strlen(buffer), &rc, DT_NOCLIP | DT_CENTER);
+
+    *width = MAX(*width, rc.right - rc.left + 1);
+    *height += rc.bottom - rc.top + 1;
   }
 
   SelectObject(hdc, old);
Index: client/gui-xaw/mapview.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/mapview.c,v
retrieving revision 1.171
diff -u -r1.171 mapview.c
--- client/gui-xaw/mapview.c    23 Apr 2004 22:58:06 -0000      1.171
+++ client/gui-xaw/mapview.c    15 May 2004 17:14:31 -0000
@@ -627,7 +627,8 @@
 /**************************************************************************
 Draw at x = left of string, y = top of string.
 **************************************************************************/
-static void draw_shadowed_string(XFontStruct * font, GC font_gc,
+static void draw_shadowed_string(struct canvas *pcanvas,
+                                XFontStruct * font, GC font_gc,
                                 enum color_std foreground,
                                 enum color_std shadow,
                                 int x, int y, const char *string)
@@ -637,16 +638,17 @@
   y += font->ascent;
 
   XSetForeground(display, font_gc, colors_standard[shadow]);
-  XDrawString(display, map_canvas_store, font_gc, x + 1, y + 1, string, len);
+  XDrawString(display, pcanvas->pixmap, font_gc, x + 1, y + 1, string, len);
 
   XSetForeground(display, font_gc, colors_standard[foreground]);
-  XDrawString(display, map_canvas_store, font_gc, x, y, string, len);
+  XDrawString(display, pcanvas->pixmap, font_gc, x, y, string, len);
 }
 
 /**************************************************************************
 ...
 **************************************************************************/
-void show_city_desc(struct city *pcity, int canvas_x, int canvas_y)
+void show_city_desc(struct canvas *pcanvas, int canvas_x, int canvas_y,
+                   struct city *pcity, int *width, int *height)
 {
   char buffer[512], buffer2[512];
   enum color_std color;
@@ -665,16 +667,19 @@
   }
   w2 = XTextWidth(main_font_struct, buffer2, strlen(buffer2));
 
-  draw_shadowed_string(main_font_struct, font_gc,
+  draw_shadowed_string(pcanvas, main_font_struct, font_gc,
                       COLOR_STD_WHITE, COLOR_STD_BLACK,
                       canvas_x - (w + w2) / 2,
                       canvas_y, buffer);
 
-  draw_shadowed_string(prod_font_struct, prod_font_gc, color,
+  draw_shadowed_string(pcanvas, prod_font_struct, prod_font_gc, color,
                       COLOR_STD_BLACK,
                       canvas_x - (w + w2) / 2 + w,
                       canvas_y, buffer2);
 
+  *width = w + w2;
+  *height = main_font_struct->ascent + main_font_struct->descent;
+
   if (draw_city_productions && (pcity->owner == game.player_idx)) {
     if (draw_city_names) {
       canvas_y += main_font_struct->ascent + main_font_struct->descent;
@@ -683,10 +688,13 @@
     get_city_mapview_production(pcity, buffer, sizeof(buffer));
     w = XTextWidth(prod_font_struct, buffer, strlen(buffer));
 
-    draw_shadowed_string(prod_font_struct, prod_font_gc,
+    draw_shadowed_string(pcanvas, prod_font_struct, prod_font_gc,
                         COLOR_STD_WHITE, COLOR_STD_BLACK,
                         canvas_x - w / 2,
                         canvas_y, buffer);
+
+    *width = MAX(*width, w);
+    *height += prod_font_struct->ascent + prod_font_struct->descent;
   }
 }
 
Index: client/include/mapview_g.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/include/mapview_g.h,v
retrieving revision 1.51
diff -u -r1.51 mapview_g.h
--- client/include/mapview_g.h  13 Mar 2004 19:07:30 -0000      1.51
+++ client/include/mapview_g.h  15 May 2004 17:14:31 -0000
@@ -33,7 +33,8 @@
 void canvas_free(struct canvas *store);
 struct canvas *get_overview_window(void);
 
-void show_city_desc(struct city *pcity, int canvas_x, int canvas_y);
+void show_city_desc(struct canvas *pcanvas, int canvas_x, int canvas_y,
+                   struct city *pcity, int *width, int *height);
 void prepare_show_city_descriptions(void);
 
 void put_one_tile_iso(struct canvas *pcanvas,

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#8746) extended show_city_desc function, Jason Short <=