Complete.Org: Mailing Lists: Archives: freeciv-dev: March 2003:
[Freeciv-Dev] (PR#3779) remove get_map_xy/get_canvas_xy
Home

[Freeciv-Dev] (PR#3779) remove get_map_xy/get_canvas_xy

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients:;
Subject: [Freeciv-Dev] (PR#3779) remove get_map_xy/get_canvas_xy
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 23 Mar 2003 21:01:39 -0800
Reply-to: rt@xxxxxxxxxxxxxx

The attached patch removes get_map_xy/get_canvas_xy wrappers, and
replaces them with the "properly" named and prototyped functions. 
get_map_xy/get_canvas_xy still exist as wrapper macros, but only in the
GUI code.

- get_map_xy and get_canvas_xy are removed from client common code.
- Renames map_pos_to_canvas_pos => map_to_canvas_pos
          canvas_pos_to_map_pos => canvas_to_map_pos
- Removes the extraneous variables from these functions' prototypes.
- Reorder the arguments of these functions to be consistent.
- Puts these functions into mapview_common.h.
- All client common code calls these functions now instead of get_***_xy.
- Adds wrapper macros to gui-xxx/mapview.h, to avoid having to rename
every instance of these functions in the GUI code.  GUI authors will
eventually want to remove these wrappers.

jason

Index: client/mapctrl_common.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/mapctrl_common.c,v
retrieving revision 1.6
diff -u -r1.6 mapctrl_common.c
--- client/mapctrl_common.c     2003/03/18 02:14:21     1.6
+++ client/mapctrl_common.c     2003/03/24 04:56:01
@@ -58,7 +58,7 @@
   canvas_y = mapview_canvas.height / 2;
   canvas_x += DIR_DX[gui_dir] * mapview_canvas.width / 2;
   canvas_y += DIR_DY[gui_dir] * mapview_canvas.height / 2;
-  get_map_xy(canvas_x, canvas_y, &map_x, &map_y);
+  canvas_to_map_pos(&map_x, &map_y, canvas_x, canvas_y);
   center_tile_mapcanvas(map_x, map_y);
 }
 
@@ -109,7 +109,7 @@
       && draw_goto_line) {
     int x, y, old_x, old_y;
 
-    get_map_xy(canvas_x, canvas_y, &x, &y);
+    canvas_to_map_pos(&x, &y, canvas_x, canvas_y);
 
     get_line_dest(&old_x, &old_y);
     if (!same_pos(old_x, old_y, x, y)) {
Index: client/mapview_common.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/mapview_common.c,v
retrieving revision 1.37
diff -u -r1.37 mapview_common.c
--- client/mapview_common.c     2003/03/18 02:08:21     1.37
+++ client/mapview_common.c     2003/03/24 04:56:01
@@ -75,7 +75,7 @@
         * for tiles 2-8 (actually we end up drawing 1 as well). */
        square_iterate(x - 1, y - 1, 1, city_x, city_y) {
          if ((pcity = map_get_city(city_x, city_y))) {
-           get_canvas_xy(city_x, city_y, &canvas_x, &canvas_y);
+           map_to_canvas_pos(&canvas_x, &canvas_y, city_x, city_y);
            show_city_desc(pcity, canvas_x, canvas_y);
          }
        } square_iterate_end;
@@ -91,7 +91,7 @@
 
          if (normalize_map_pos(&city_x, &city_y)
              && (pcity = map_get_city(city_x, city_y))) {
-           get_canvas_xy(city_x, city_y, &canvas_x, &canvas_y);
+           map_to_canvas_pos(&canvas_x, &canvas_y, city_x, city_y);
            show_city_desc(pcity, canvas_x, canvas_y);
          }
        }
@@ -152,12 +152,7 @@
   in canvas_x,canvas_y it returns whether the tile is inside the
   visible map.
 **************************************************************************/
-static bool map_pos_to_canvas_pos(int map_x, int map_y,
-                                 int *canvas_x, int *canvas_y,
-                                 int map_view_topleft_map_x,
-                                 int map_view_topleft_map_y,
-                                 int map_view_pixel_width,
-                                 int map_view_pixel_height)
+bool map_to_canvas_pos(int *canvas_x, int *canvas_y, int map_x, int map_y)
 {
   if (is_isometric) {
     /* For a simpler example of this math, see
@@ -171,7 +166,7 @@
      * all.
      */
     map_x %= map.xsize;
-    if (map_x < map_view_topleft_map_x) {
+    if (map_x < mapview_canvas.map_x0) {
       map_x += map.xsize;
     }
 
@@ -186,12 +181,10 @@
      * 789                4 8
      *                     7
      */
-    iso_x =
-       (map_x - map_y) - (map_view_topleft_map_x -
-                          map_view_topleft_map_y);
-    iso_y =
-       (map_x + map_y) - (map_view_topleft_map_x +
-                          map_view_topleft_map_y);
+    iso_x = (map_x - map_y)
+      - (mapview_canvas.map_x0 - mapview_canvas.map_y0);
+    iso_y = (map_x + map_y)
+      - (mapview_canvas.map_x0 + mapview_canvas.map_y0);
 
     /*
      * As the above picture shows, each isometric-coordinate unit
@@ -209,45 +202,35 @@
      * visible on the canvas.
      */
     return (*canvas_x > -NORMAL_TILE_WIDTH)
-       && *canvas_x < (map_view_pixel_width + NORMAL_TILE_WIDTH / 2)
+       && *canvas_x < (mapview_canvas.width + NORMAL_TILE_WIDTH / 2)
        && (*canvas_y > -NORMAL_TILE_HEIGHT)
-       && (*canvas_y < map_view_pixel_height);
+       && *canvas_y < mapview_canvas.height;
   } else {                     /* is_isometric */
-    /* map_view_map_width is the width in tiles/map positions */
-    int map_view_map_width =
-       (map_view_pixel_width + NORMAL_TILE_WIDTH - 1) / NORMAL_TILE_WIDTH;
-
-    if (map_view_topleft_map_x + map_view_map_width <= map.xsize) {
-      *canvas_x = map_x - map_view_topleft_map_x;
-    } else if (map_x >= map_view_topleft_map_x) {
-      *canvas_x = map_x - map_view_topleft_map_x;
-    }
-      else if (map_x <
-              map_adjust_x(map_view_topleft_map_x +
-                           map_view_map_width)) {*canvas_x =
-         map_x + map.xsize - map_view_topleft_map_x;
+    if (mapview_canvas.map_x0 + mapview_canvas.tile_width <= map.xsize) {
+      *canvas_x = map_x - mapview_canvas.map_x0;
+    } else if (map_x >= mapview_canvas.map_x0) {
+      *canvas_x = map_x - mapview_canvas.map_x0;
+    } else if (map_x < map_adjust_x(mapview_canvas.map_x0
+                                   + mapview_canvas.tile_width)) {
+      *canvas_x = map_x + map.xsize - mapview_canvas.map_x0;
     } else {
       *canvas_x = -1;
     }
 
-    *canvas_y = map_y - map_view_topleft_map_y;
+    *canvas_y = map_y - mapview_canvas.map_y0;
 
     *canvas_x *= NORMAL_TILE_WIDTH;
     *canvas_y *= NORMAL_TILE_HEIGHT;
 
-    return *canvas_x >= 0
-       && *canvas_x < map_view_pixel_width
-       && *canvas_y >= 0 && *canvas_y < map_view_pixel_height;
+    return *canvas_x >= 0 && *canvas_x < mapview_canvas.width
+        && *canvas_y >= 0 && *canvas_y < mapview_canvas.height;
   }
 }
 
 /**************************************************************************
   Finds the map coordinates corresponding to pixel coordinates.
 **************************************************************************/
-static void canvas_pos_to_map_pos(int canvas_x, int canvas_y,
-                                 int *map_x, int *map_y,
-                                 int map_view_topleft_map_x,
-                                 int map_view_topleft_map_y)
+void canvas_to_map_pos(int *map_x, int *map_y, int canvas_x, int canvas_y)
 {
   if (is_isometric) {
     /* The basic operation here is a simple pi/4 rotation; however, we
@@ -284,8 +267,8 @@
     *map_y = canvas_y / NORMAL_TILE_HEIGHT;
   }
 
-  *map_x += map_view_topleft_map_x;
-  *map_y += map_view_topleft_map_y;
+  *map_x += mapview_canvas.map_x0;
+  *map_y += mapview_canvas.map_y0;
 
   /*
    * If we are outside the map find the nearest tile, with distance as
@@ -295,34 +278,13 @@
 }
 
 /**************************************************************************
-  Finds the pixel coordinates of a tile.  Beside setting the results
-  in canvas_x, canvas_y it returns whether the tile is inside the
-  visible map.
-**************************************************************************/
-bool get_canvas_xy(int map_x, int map_y, int *canvas_x, int *canvas_y)
-{
-  return map_pos_to_canvas_pos(map_x, map_y, canvas_x, canvas_y,
-                              mapview_canvas.map_x0, mapview_canvas.map_y0,
-                              mapview_canvas.width, mapview_canvas.height);
-}
-
-/**************************************************************************
-  Finds the map coordinates corresponding to pixel coordinates.
-**************************************************************************/
-void get_map_xy(int canvas_x, int canvas_y, int *map_x, int *map_y)
-{
-  canvas_pos_to_map_pos(canvas_x, canvas_y, map_x, map_y,
-                       mapview_canvas.map_x0, mapview_canvas.map_y0);
-}
-
-/**************************************************************************
   Finds the current center tile of the mapcanvas.
 **************************************************************************/
 void get_center_tile_mapcanvas(int *map_x, int *map_y)
 {
   /* This sets the pointers map_x and map_y */
-  get_map_xy(mapview_canvas.width / 2, mapview_canvas.height / 2,
-            map_x, map_y);
+  canvas_to_map_pos(map_x, map_y,
+                   mapview_canvas.width / 2, mapview_canvas.height / 2);
 }
 
 /**************************************************************************
@@ -368,7 +330,7 @@
 {
   int dummy_x, dummy_y;                /* well, it needs two pointers... */
 
-  return get_canvas_xy(map_x, map_y, &dummy_x, &dummy_y);
+  return map_to_canvas_pos(&dummy_x, &dummy_y, map_x, map_y);
 }
 
 /**************************************************************************
@@ -389,7 +351,7 @@
 
     /* The border consists of the half-tile on the left and top of the
      * screen, and the 1.5-tiles on the right and bottom. */
-    return (get_canvas_xy(map_x, map_y, &canvas_x, &canvas_y)
+    return (map_to_canvas_pos(&canvas_x, &canvas_y, map_x, map_y)
            && canvas_x > NORMAL_TILE_WIDTH / 2
            && canvas_x < mapview_canvas.width - 3 * NORMAL_TILE_WIDTH / 2
            && canvas_y >= NORMAL_TILE_HEIGHT
@@ -516,7 +478,7 @@
 {
   int canvas_x, canvas_y;
 
-  if (get_canvas_xy(map_x, map_y, &canvas_x, &canvas_y)) {
+  if (map_to_canvas_pos(&canvas_x, &canvas_y, map_x, map_y)) {
     freelog(LOG_DEBUG, "putting (%d,%d) at (%d,%d)",
            map_x, map_y, canvas_x, canvas_y);
     put_one_tile(mapview_canvas.store, map_x, map_y,
@@ -533,7 +495,7 @@
 {
   int canvas_x, canvas_y;
 
-  if (get_canvas_xy(map_x, map_y, &canvas_x, &canvas_y)) {
+  if (map_to_canvas_pos(&canvas_x, &canvas_y, map_x, map_y)) {
     int height, width, height_unit;
     int offset_x, offset_y, offset_y_unit;
 
@@ -682,7 +644,7 @@
 
     /* Lastly draw our changes to the screen. */
     /* top left corner */
-    get_canvas_xy(x, y, &canvas_start_x, &canvas_start_y);
+    map_to_canvas_pos(&canvas_start_x, &canvas_start_y, x, y);
 
     /* top left corner in isometric view */
     canvas_start_x -= height * NORMAL_TILE_WIDTH / 2;
@@ -717,7 +679,7 @@
     /* Here we draw a rectangle that includes the updated tiles.  This
      * method can fail if the area wraps off one side of the screen and
      * back to the other. */
-    get_canvas_xy(x, y, &canvas_start_x, &canvas_start_y);
+    map_to_canvas_pos(&canvas_start_x, &canvas_start_y, x, y);
     dirty_rect(canvas_start_x, canvas_start_y,
               width * NORMAL_TILE_WIDTH,
               height * NORMAL_TILE_HEIGHT);
@@ -783,7 +745,7 @@
 
        if (normalize_map_pos(&x, &y)
            && (pcity = map_get_city(x, y))) {
-         get_canvas_xy(x, y, &canvas_x, &canvas_y);
+         map_to_canvas_pos(&canvas_x, &canvas_y, x, y);
          show_city_desc(pcity, canvas_x, canvas_y);
        }
       }
@@ -799,7 +761,7 @@
 
        if (normalize_map_pos(&x, &y)
            && (pcity = map_get_city(x, y))) {
-         get_canvas_xy(x, y, &canvas_x, &canvas_y);
+         map_to_canvas_pos(&canvas_x, &canvas_y, x, y);
          show_city_desc(pcity, canvas_x, canvas_y);
        }
       }
@@ -926,7 +888,7 @@
       steps = smooth_move_unit_steps;
     }
 
-    get_canvas_xy(map_x, map_y, &start_x, &start_y);
+    map_to_canvas_pos(&start_x, &start_y, map_x, map_y);
     if (is_isometric) {
       start_y -= NORMAL_TILE_HEIGHT / 2;
     }
Index: client/mapview_common.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/mapview_common.h,v
retrieving revision 1.26
diff -u -r1.26 mapview_common.h
--- client/mapview_common.h     2003/03/01 21:03:55     1.26
+++ client/mapview_common.h     2003/03/24 04:56:01
@@ -137,8 +137,8 @@
 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);
 
-bool get_canvas_xy(int map_x, int map_y, int *canvas_x, int *canvas_y);
-void get_map_xy(int canvas_x, int canvas_y, int *map_x, int *map_y);
+bool map_to_canvas_pos(int *canvas_x, int *canvas_y, int map_x, int map_y);
+void canvas_to_map_pos(int *map_x, int *map_y, int canvas_x, int canvas_y);
 
 void get_center_tile_mapcanvas(int *map_x, int *map_y);
 void center_tile_mapcanvas(int map_x, int map_y);
Index: client/gui-gtk/mapview.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/mapview.h,v
retrieving revision 1.19
diff -u -r1.19 mapview.h
--- client/gui-gtk/mapview.h    2003/02/27 00:31:10     1.19
+++ client/gui-gtk/mapview.h    2003/03/24 04:56:01
@@ -56,4 +56,10 @@
 #define map_canvas_store_twidth mapview_canvas.tile_width
 #define map_canvas_store_theight mapview_canvas.tile_height
 
+/* Use of these wrapper functions is deprecated. */
+#define get_map_xy(canvas_x, canvas_y, map_x, map_y) \
+  canvas_to_map_pos(map_x, map_y, canvas_x, canvas_y)
+#define get_canvas_xy(map_x, map_y, canvas_x, canvas_y) \
+  map_to_canvas_pos(canvas_x, canvas_y, map_x, map_y)
+
 #endif  /* FC__MAPVIEW_H */
Index: client/gui-gtk-2.0/mapview.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/mapview.h,v
retrieving revision 1.10
diff -u -r1.10 mapview.h
--- client/gui-gtk-2.0/mapview.h        2003/02/27 00:31:10     1.10
+++ client/gui-gtk-2.0/mapview.h        2003/03/24 04:56:01
@@ -58,4 +58,10 @@
 #define map_canvas_store_twidth mapview_canvas.tile_width
 #define map_canvas_store_theight mapview_canvas.tile_height
 
+/* Use of these wrapper functions is deprecated. */
+#define get_map_xy(canvas_x, canvas_y, map_x, map_y) \
+  canvas_to_map_pos(map_x, map_y, canvas_x, canvas_y)
+#define get_canvas_xy(map_x, map_y, canvas_x, canvas_y) \
+  map_to_canvas_pos(canvas_x, canvas_y, map_x, map_y)
+
 #endif  /* FC__MAPVIEW_H */
Index: client/gui-mui/mapview.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-mui/mapview.h,v
retrieving revision 1.5
diff -u -r1.5 mapview.h
--- client/gui-mui/mapview.h    2002/11/15 09:24:52     1.5
+++ client/gui-mui/mapview.h    2003/03/24 04:56:02
@@ -19,4 +19,10 @@
 
 void create_line_at_mouse_pos(void);
 
+/* Use of these wrapper functions is deprecated. */
+#define get_map_xy(canvas_x, canvas_y, map_x, map_y) \
+  canvas_to_map_pos(map_x, map_y, canvas_x, canvas_y)
+#define get_canvas_xy(map_x, map_y, canvas_x, canvas_y) \
+  map_to_canvas_pos(canvas_x, canvas_y, map_x, map_y)
+
 #endif  /* FC__MAPVIEW_H */
Index: client/gui-sdl/mapview.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-sdl/mapview.h,v
retrieving revision 1.8
diff -u -r1.8 mapview.h
--- client/gui-sdl/mapview.h    2003/03/13 13:27:55     1.8
+++ client/gui-sdl/mapview.h    2003/03/24 04:56:02
@@ -42,4 +42,10 @@
 void sdl_dirty_rect(SDL_Rect rect);
 void flush_all(void);
 
+/* Use of these wrapper functions is deprecated. */
+#define get_map_xy(canvas_x, canvas_y, map_x, map_y) \
+  canvas_to_map_pos(map_x, map_y, canvas_x, canvas_y)
+#define get_canvas_xy(map_x, map_y, canvas_x, canvas_y) \
+  map_to_canvas_pos(canvas_x, canvas_y, map_x, map_y)
+
 #endif /* FC__MAPVIEW_H */
Index: client/gui-win32/mapview.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-win32/mapview.h,v
retrieving revision 1.8
diff -u -r1.8 mapview.h
--- client/gui-win32/mapview.h  2003/02/27 00:31:10     1.8
+++ client/gui-win32/mapview.h  2003/03/24 04:56:02
@@ -38,4 +38,10 @@
 #define map_view_width mapview_canvas.tile_width
 #define map_view_height mapview_canvas.tile_height
 
+/* Use of these wrapper functions is deprecated. */
+#define get_map_xy(canvas_x, canvas_y, map_x, map_y) \
+  canvas_to_map_pos(map_x, map_y, canvas_x, canvas_y)
+#define get_canvas_xy(map_x, map_y, canvas_x, canvas_y) \
+  map_to_canvas_pos(canvas_x, canvas_y, map_x, map_y)
+
 #endif  /* FC__MAPVIEW_H */
Index: client/gui-xaw/mapview.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/mapview.h,v
retrieving revision 1.15
diff -u -r1.15 mapview.h
--- client/gui-xaw/mapview.h    2003/02/27 00:31:11     1.15
+++ client/gui-xaw/mapview.h    2003/03/24 04:56:02
@@ -56,4 +56,10 @@
 #define map_canvas_store_twidth mapview_canvas.tile_width
 #define map_canvas_store_theight mapview_canvas.tile_height
 
+/* Use of these wrapper functions is deprecated. */
+#define get_map_xy(canvas_x, canvas_y, map_x, map_y) \
+  canvas_to_map_pos(map_x, map_y, canvas_x, canvas_y)
+#define get_canvas_xy(map_x, map_y, canvas_x, canvas_y) \
+  map_to_canvas_pos(canvas_x, canvas_y, map_x, map_y)
+
 #endif  /* FC__MAPVIEW_H */

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#3779) remove get_map_xy/get_canvas_xy, Jason Short <=