Complete.Org: Mailing Lists: Archives: freeciv-dev: June 2004:
[Freeciv-Dev] (PR#8960) new function map_to_gui_vector
Home

[Freeciv-Dev] (PR#8960) new function map_to_gui_vector

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#8960) new function map_to_gui_vector
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 11 Jun 2004 11:43:24 -0700
Reply-to: rt@xxxxxxxxxxx

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

This patch adds a new function map_to_gui_vector.  It is used in 3 
places in place of what is now duplicated copies of the same logic.

As a side effect the city_to_canvas_pos is slightly more extensible, as 
it doesn't hard-code the origin of the citydlg but just centers the 
citydlg within the available window.

jason

Index: client/citydlg_common.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/citydlg_common.c,v
retrieving revision 1.35
diff -u -r1.35 citydlg_common.c
--- client/citydlg_common.c     2 Jun 2004 22:54:14 -0000       1.35
+++ client/citydlg_common.c     11 Jun 2004 17:16:12 -0000
@@ -61,22 +61,13 @@
 **************************************************************************/
 bool city_to_canvas_pos(int *canvas_x, int *canvas_y, int city_x, int city_y)
 {
-  if (is_isometric) {
-    /*
-     * The top-left corner is in the center of tile (-2, 2).  However,
-     * we're looking for the top-left corner of the tile, so we
-     * subtract off half a tile in each direction.  For a more
-     * rigorous example, see map_pos_to_canvas_pos().
-     */
-    int iso_x = (city_x - city_y) + (2 * CITY_MAP_RADIUS);
-    int iso_y = (city_x + city_y) - (0);
-
-    *canvas_x = (iso_x - 1) * NORMAL_TILE_WIDTH / 2;
-    *canvas_y = (iso_y - 1) * NORMAL_TILE_HEIGHT / 2;
-  } else {
-    *canvas_x = city_x * NORMAL_TILE_WIDTH;
-    *canvas_y = city_y * NORMAL_TILE_HEIGHT;
-  }
+  const int x0 = CITY_MAP_RADIUS, y0 = CITY_MAP_RADIUS;
+  const int width = get_citydlg_canvas_width();
+  const int height = get_citydlg_canvas_height();
+
+  map_to_gui_vector(canvas_x, canvas_y, city_x - x0, city_y - y0);
+  *canvas_x += (width - NORMAL_TILE_WIDTH) / 2;
+  *canvas_y += (height - NORMAL_TILE_HEIGHT) / 2;
 
   if (!is_valid_city_coords(city_x, city_y)) {
     assert(FALSE);
Index: client/mapview_common.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/mapview_common.c,v
retrieving revision 1.123
diff -u -r1.123 mapview_common.c
--- client/mapview_common.c     10 Jun 2004 01:04:52 -0000      1.123
+++ client/mapview_common.c     11 Jun 2004 17:16:13 -0000
@@ -122,12 +122,15 @@
 }
 
 /****************************************************************************
-  Translate from map to gui coordinate systems.
+  Translate from a cartesian system to the GUI system.  This function works
+  on vectors, meaning it can be passed a (dx,dy) pair and will return the
+  change in GUI coordinates corresponding to this vector.  It is thus more
+  general than map_to_gui_pos.
 
-  GUI coordinates are comparable to canvas coordinates but extend in all
-  directions.  gui(0,0) == map(0,0).
+  Note that a gui_to_map_vector function is not possible, since the
+  resulting map vector may differ based on the origin of the gui vector.
 ****************************************************************************/
-static void map_to_gui_pos(int *gui_x, int *gui_y, int map_x, int map_y)
+void map_to_gui_vector(int *gui_dx, int *gui_dy, int map_dx, int map_dy)
 {
   if (is_isometric) {
     /*
@@ -141,15 +144,28 @@
      * 789                4 8
      *                     7
      */
-    *gui_x = (map_x - map_y) * NORMAL_TILE_WIDTH / 2;
-    *gui_y = (map_x + map_y) * NORMAL_TILE_HEIGHT / 2;
+    *gui_dx = (map_dx - map_dy) * NORMAL_TILE_WIDTH / 2;
+    *gui_dy = (map_dx + map_dy) * NORMAL_TILE_HEIGHT / 2;
   } else {
-    *gui_x = map_x * NORMAL_TILE_HEIGHT;
-    *gui_y = map_y * NORMAL_TILE_WIDTH;
+    *gui_dx = map_dx * NORMAL_TILE_HEIGHT;
+    *gui_dy = map_dy * NORMAL_TILE_WIDTH;
   }
 }
 
 /****************************************************************************
+  Translate from map to gui coordinate systems.
+
+  GUI coordinates are comparable to canvas coordinates but extend in all
+  directions.  gui(0,0) == map(0,0).
+****************************************************************************/
+static void map_to_gui_pos(int *gui_x, int *gui_y, int map_x, int map_y)
+{
+  /* Since the GUI origin is the same as the map origin we can just do a
+   * vector conversion. */
+  map_to_gui_vector(gui_x, gui_y, map_x, map_y);
+}
+
+/****************************************************************************
   Translate from gui to map coordinate systems.  See map_to_gui_pos().
 
   Note that you lose some information in this conversion.  If you convert
@@ -1892,14 +1908,7 @@
 
     assert(smooth_move_unit_msec > 0);
 
-    /* See map_to_canvas_pos for an explanation. */
-    if (is_isometric) {
-      canvas_dx = (dx - dy) * NORMAL_TILE_WIDTH / 2;
-      canvas_dy = (dx + dy) * NORMAL_TILE_HEIGHT / 2;
-    } else {
-      canvas_dx = NORMAL_TILE_WIDTH * dx;
-      canvas_dy = NORMAL_TILE_HEIGHT * dy;
-    }
+    map_to_gui_vector(&canvas_dx, &canvas_dy, dx, dy);
 
     map_to_canvas_pos(&start_x, &start_y, map_x, map_y);
     if (is_isometric) {
Index: client/mapview_common.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/mapview_common.h,v
retrieving revision 1.66
diff -u -r1.66 mapview_common.h
--- client/mapview_common.h     10 Jun 2004 01:04:52 -0000      1.66
+++ client/mapview_common.h     11 Jun 2004 17:16:13 -0000
@@ -226,6 +226,7 @@
 void refresh_tile_mapcanvas(int x, int y, bool write_to_screen);
 enum color_std get_grid_color(int x1, int y1, int x2, int y2);
 
+void map_to_gui_vector(int *gui_dx, int *gui_dy, int map_dx, int map_dy);
 bool map_to_canvas_pos(int *canvas_x, int *canvas_y, int map_x, int map_y);
 bool canvas_to_map_pos(int *map_x, int *map_y, int canvas_x, int canvas_y);
 

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#8960) new function map_to_gui_vector, Jason Short <=