Complete.Org: Mailing Lists: Archives: freeciv-dev: November 2001:
[Freeciv-Dev] get_canvas_xy unification (PR#1054)
Home

[Freeciv-Dev] get_canvas_xy unification (PR#1054)

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv-dev@xxxxxxxxxxx
Cc: bugs@xxxxxxxxxxxxxxxxxxx
Subject: [Freeciv-Dev] get_canvas_xy unification (PR#1054)
From: jdorje@xxxxxxxxxxxxxxxxxxxxx
Date: Sun, 4 Nov 2001 19:14:03 -0800 (PST)

The attached patch makes a new common function, base_get_canvas_xy,
which is called by get_canvas_xy for each GUI platform.

There are several advantages to this: it reduces overall code,
consolidates places where changes will have to be made in the GUI, and
provides isometric-view capabilities to this function for all GUI's.

get_canvas_xy is kept around because the variables needed by
base_get_canvas_xy are not common.  But they should be.  I would propose
a naming system that is a cross between the win32 and gtk current
systems: map_view_x0/map_view_y0 for the top-left window corner,
map_win_width/map_win_height for the canvas width and height.  Currently
map_canvas_store_twidth/map_canvas_store_theight are used for the canvas
width in tiles; these are misnamed but I'm not sure what would be better
(map_win_twidth/map_win_theight, perhaps?).  This patch is a big step
toward an eventual total unification.

Platforms tested: gtk
Untested: xaw (I'll test shortly), win32, mui
Unaffected: beos

jason
? rc
? diff
? old
? topology
Index: client/mapview_common.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/mapview_common.c,v
retrieving revision 1.1
diff -u -r1.1 mapview_common.c
--- client/mapview_common.c     2001/10/30 12:11:43     1.1
+++ client/mapview_common.c     2001/11/05 03:04:10
@@ -21,6 +21,7 @@
 #include "mapview_g.h"
 
 #include "mapview_common.h"
+#include "tilespec.h"
 
 /**************************************************************************
  Refreshes a single tile on the map canvas.
@@ -77,5 +78,67 @@
     return COLOR_STD_RED;
   } else {
     return COLOR_STD_WHITE;
+  }
+}
+
+int base_get_canvas_xy(int map_x, int map_y,
+                      int *canvas_x, int *canvas_y,
+                      int map_view_x0, int map_view_y0,
+                      int canvas_width, int canvas_height)
+{
+  if (is_isometric) {
+    /* canvas_x,canvas_y is the top corner of the actual tile, not the pixmap.
+       This function also handles non-adjusted tile coords (like -1, -2) as if
+       they were adjusted.  You might want to first take a look at the simpler
+       city_get_canvas() for basic understanding. */
+    int diff_xy;
+    int diff_x, diff_y;
+
+    map_x %= map.xsize;
+    if (map_x < map_view_x0) map_x += map.xsize;
+    diff_xy = (map_x + map_y) - (map_view_x0 + map_view_y0);
+    /* one diff_xy value defines a line parallel with the top of the isometric
+       view. */
+    *canvas_y = diff_xy/2 * NORMAL_TILE_HEIGHT + (diff_xy%2) * 
(NORMAL_TILE_HEIGHT/2);
+
+    /* changing both x and y with the same amount doesn't change the isometric
+       x value. (draw a grid to see it!) */
+    map_x -= diff_xy/2;
+    map_y -= diff_xy/2;
+    diff_x = map_x - map_view_x0;
+    diff_y = map_view_y0 - map_y;
+
+    *canvas_x = (diff_x - 1) * NORMAL_TILE_WIDTH
+      + (diff_x == diff_y ? NORMAL_TILE_WIDTH : NORMAL_TILE_WIDTH/2)
+      /* tiles starting above the visible area */
+      + (diff_y > diff_x ? NORMAL_TILE_WIDTH : 0);
+
+    /* We now have the corner of the sprite. For drawing we move it. */
+    *canvas_x -= NORMAL_TILE_WIDTH/2;
+
+    return (*canvas_x > -NORMAL_TILE_WIDTH)
+      && *canvas_x < (canvas_width + NORMAL_TILE_WIDTH/2)
+      && (*canvas_y > -NORMAL_TILE_HEIGHT)
+      && (*canvas_y < canvas_height);
+  } else { /* is_isometric */
+    /* twidth is the width in tiles */
+    int twidth = (canvas_width + NORMAL_TILE_WIDTH - 1) / NORMAL_TILE_WIDTH;
+    if (map_view_x0+twidth <= map.xsize)
+      *canvas_x = map_x-map_view_x0;
+    else if(map_x >= map_view_x0)
+      *canvas_x = map_x-map_view_x0;
+    else if(map_x < map_adjust_x(map_view_x0+twidth))
+      *canvas_x = map_x+map.xsize-map_view_x0;
+    else *canvas_x = -1;
+
+    *canvas_y = map_y - map_view_y0;
+
+    *canvas_x *= NORMAL_TILE_WIDTH;
+    *canvas_y *= NORMAL_TILE_HEIGHT;
+
+    return *canvas_x >= 0
+       && *canvas_x < canvas_width
+       && *canvas_y >= 0
+       && *canvas_y < canvas_height;
   }
 }
Index: client/mapview_common.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/mapview_common.h,v
retrieving revision 1.1
diff -u -r1.1 mapview_common.h
--- client/mapview_common.h     2001/10/30 12:11:43     1.1
+++ client/mapview_common.h     2001/11/05 03:04:10
@@ -18,5 +18,9 @@
 
 void refresh_tile_mapcanvas(int x, int y, int write_to_screen);
 enum color_std get_grid_color(int x1, int y1, int x2, int y2);
+int base_get_canvas_xy(int map_x, int map_y,
+                      int *canvas_x, int *canvas_y,
+                      int map_view_x0, int map_view_y0,
+                      int twidth, int theight);
 
 #endif /* FC__MAPVIEW_COMMON_H */
Index: client/gui-gtk/mapview.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/mapview.c,v
retrieving revision 1.107
diff -u -r1.107 mapview.c
--- client/gui-gtk/mapview.c    2001/10/30 12:11:44     1.107
+++ client/gui-gtk/mapview.c    2001/11/05 03:04:11
@@ -255,62 +255,9 @@
 **************************************************************************/
 static int get_canvas_xy(int map_x, int map_y, int *canvas_x, int *canvas_y)
 {
-  if (is_isometric) {
-    /* canvas_x,canvas_y is the top corner of the actual tile, not the pixmap.
-       This function also handels non-adjusted tile coords (like -1, -2) as if
-       they were adjusted.
-       You might want to first take a look at the simpler city_get_xy() for 
basic
-       understanding. */
-    int diff_xy;
-    int diff_x, diff_y;
-    int width, height;
-    gdk_window_get_size(map_canvas->window, &width, &height);
-
-    map_x %= map.xsize;
-    if (map_x < map_view_x0) map_x += map.xsize;
-    diff_xy = (map_x + map_y) - (map_view_x0 + map_view_y0);
-    /* one diff_xy value defines a line parallel with the top of the isometric
-       view. */
-    *canvas_y = diff_xy/2 * NORMAL_TILE_HEIGHT + (diff_xy%2) * 
(NORMAL_TILE_HEIGHT/2);
-
-    /* changing both x and y with the same amount doesn't change the isometric
-       x value. (draw a grid to see it!) */
-    map_x -= diff_xy/2;
-    map_y -= diff_xy/2;
-    diff_x = map_x - map_view_x0;
-    diff_y = map_view_y0 - map_y;
-
-    *canvas_x = (diff_x - 1) * NORMAL_TILE_WIDTH
-      + (diff_x == diff_y ? NORMAL_TILE_WIDTH : NORMAL_TILE_WIDTH/2)
-      /* tiles starting above the visible area */
-      + (diff_y > diff_x ? NORMAL_TILE_WIDTH : 0);
-
-    /* We now have the corner of the sprite. For drawing we move it. */
-    *canvas_x -= NORMAL_TILE_WIDTH/2;
-
-    return (*canvas_x > -NORMAL_TILE_WIDTH)
-      && *canvas_x < (width + NORMAL_TILE_WIDTH/2)
-      && (*canvas_y > -NORMAL_TILE_HEIGHT)
-      && (*canvas_y < height);
-  } else { /* is_isometric */
-    if (map_view_x0+map_canvas_store_twidth <= map.xsize)
-      *canvas_x = map_x-map_view_x0;
-    else if(map_x >= map_view_x0)
-      *canvas_x = map_x-map_view_x0;
-    else if(map_x < map_adjust_x(map_view_x0+map_canvas_store_twidth))
-      *canvas_x = map_x+map.xsize-map_view_x0;
-    else *canvas_x = -1;
-
-    *canvas_y = map_y - map_view_y0;
-
-    *canvas_x *= NORMAL_TILE_WIDTH;
-    *canvas_y *= NORMAL_TILE_HEIGHT;
-
-    return *canvas_x >= 0
-       && *canvas_x < map_canvas_store_twidth * NORMAL_TILE_WIDTH
-       && *canvas_y >= 0
-       && *canvas_y < map_canvas_store_theight * NORMAL_TILE_HEIGHT;
-  }
+  int width, height;
+  gdk_window_get_size(map_canvas->window, &width, &height);
+  return base_get_canvas_xy(map_x, map_y, canvas_x, canvas_y, map_view_x0, 
map_view_y0, width, height);
 }
 
 /**************************************************************************
Index: client/gui-mui/mapview.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-mui/mapview.c,v
retrieving revision 1.34
diff -u -r1.34 mapview.c
--- client/gui-mui/mapview.c    2001/10/30 12:11:45     1.34
+++ client/gui-mui/mapview.c    2001/11/05 03:04:12
@@ -379,69 +379,11 @@
 **************************************************************************/
 int get_canvas_xy(int map_x, int map_y, int *canvas_x, int *canvas_y)
 {
-  int map_canvas_store_twidth = xget(main_map_area, MUIA_Map_HorizVisible);
-  int map_canvas_store_theight = xget(main_map_area, MUIA_Map_VertVisible);
   int map_view_x0 = xget(main_map_area, MUIA_Map_HorizFirst);
   int map_view_y0 = xget(main_map_area, MUIA_Map_VertFirst);
-
-  if (is_isometric) {
-    /* canvas_x,canvas_y is the top corner of the actual tile, not the pixmap.
-       This function also handels non-adjusted tile coords (like -1, -2) as if
-       they were adjusted.
-       You might want to first take a look at the simpler city_get_xy() for 
basic
-       understanding. */
-    int diff_xy;
-    int diff_x, diff_y;
-    int width, height;
-
-    width = _mwidth(main_map_area); /* !! */
-    height = _mheight(main_map_area); /* !! */
-
-    map_x %= map.xsize;
-    if (map_x < map_view_x0) map_x += map.xsize;
-    diff_xy = (map_x + map_y) - (map_view_x0 + map_view_y0);
-    /* one diff_xy value defines a line parallel with the top of the isometric
-       view. */
-    *canvas_y = diff_xy/2 * NORMAL_TILE_HEIGHT + (diff_xy%2) * 
(NORMAL_TILE_HEIGHT/2);
-
-    /* changing both x and y with the same amount doesn't change the isometric
-       x value. (draw a grid to see it!) */
-    map_x -= diff_xy/2;
-    map_y -= diff_xy/2;
-    diff_x = map_x - map_view_x0;
-    diff_y = map_view_y0 - map_y;
-
-    *canvas_x = (diff_x - 1) * NORMAL_TILE_WIDTH
-      + (diff_x == diff_y ? NORMAL_TILE_WIDTH : NORMAL_TILE_WIDTH/2)
-      /* tiles starting above the visible area */
-      + (diff_y > diff_x ? NORMAL_TILE_WIDTH : 0);
-
-    /* We now have the corner of the sprite. For drawing we move it. */
-    *canvas_x -= NORMAL_TILE_WIDTH/2;
-
-    return (*canvas_x > -NORMAL_TILE_WIDTH)
-      && *canvas_x < (width + NORMAL_TILE_WIDTH/2)
-      && (*canvas_y > -NORMAL_TILE_HEIGHT)
-      && (*canvas_y < height);
-  } else { /* is_isometric */
-    if (map_view_x0+map_canvas_store_twidth <= map.xsize)
-      *canvas_x = map_x-map_view_x0;
-    else if(map_x >= map_view_x0)
-      *canvas_x = map_x-map_view_x0;
-    else if(map_x < map_adjust_x(map_view_x0+map_canvas_store_twidth))
-      *canvas_x = map_x+map.xsize-map_view_x0;
-    else *canvas_x = -1;
-
-    *canvas_y = map_y - map_view_y0;
-
-    *canvas_x *= NORMAL_TILE_WIDTH;
-    *canvas_y *= NORMAL_TILE_HEIGHT;
-
-    return *canvas_x >= 0
-       && *canvas_x < map_canvas_store_twidth * NORMAL_TILE_WIDTH
-       && *canvas_y >= 0
-       && *canvas_y < map_canvas_store_theight * NORMAL_TILE_HEIGHT;
-  }
+  int width = _mwidth(main_map_area); /* !! */
+  int height = _mheight(main_map_area); /* !! */
+  return base_get_canvas_xy(map_x, map_y, canvas_x, canvas_y, map_view_x0, 
map_view_y0, width, height);
 }
 
 /**************************************************************************
Index: client/gui-win32/mapview.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-win32/mapview.c,v
retrieving revision 1.11
diff -u -r1.11 mapview.c
--- client/gui-win32/mapview.c  2001/10/30 18:17:25     1.11
+++ client/gui-win32/mapview.c  2001/11/05 03:04:13
@@ -1240,59 +1240,7 @@
 **************************************************************************/
 static int get_canvas_xy(int map_x, int map_y, int *canvas_x, int *canvas_y)
 {
-  if (is_isometric) {
-    /* canvas_x,canvas_y is the top corner of the actual tile, not the pixmap.
-       This function also handels non-adjusted tile coords (like -1, -2) as if
-       they were adjusted.
-       You might want to first take a look at the simpler city_get_xy() for 
basic
-       understanding. */
-    int diff_xy;
-    int diff_x, diff_y;
-    int width, height;
-    width=map_win_width;
-    height=map_win_height;
-    map_x %= map.xsize;
-    if (map_x < map_view_x) map_x += map.xsize;
-    diff_xy = (map_x + map_y) - (map_view_x + map_view_y);
-    /* one diff_xy value defines a line parallel with the top of the isometric
-       view. */
-    *canvas_y = diff_xy/2 * NORMAL_TILE_HEIGHT + (diff_xy%2) * 
(NORMAL_TILE_HEIGHT/2);
-    /* changing both x and y with the same amount doesn't change the isometric
-       x value. (draw a grid to see it!) */
-    map_x -= diff_xy/2;
-    map_y -= diff_xy/2;
-    diff_x = map_x - map_view_x;
-    diff_y = map_view_y - map_y;
-    
-    *canvas_x = (diff_x - 1) * NORMAL_TILE_WIDTH
-      + (diff_x == diff_y ? NORMAL_TILE_WIDTH : NORMAL_TILE_WIDTH/2)
-      /* tiles starting above the visible area */
-      + (diff_y > diff_x ? NORMAL_TILE_WIDTH : 0);
-    
-    /* We now have the corner of the sprite. For drawing we move it. */
-    *canvas_x -= NORMAL_TILE_WIDTH/2;
-    
-    return (*canvas_x > (-NORMAL_TILE_WIDTH))
-      && *canvas_x < (width+NORMAL_TILE_WIDTH/2)
-      && (*canvas_y > (-NORMAL_TILE_HEIGHT))
-      && (*canvas_y < (height)); 
-  } else { /* is_isometric */
-    if (map_view_x+map_view_width<=map.xsize)
-      *canvas_x=map_x-map_view_x;
-    else if (map_x >= map_view_x)
-      *canvas_x=map_x-map_view_x;
-    else if (map_x < map_adjust_x(map_view_x+map_view_width))
-      *canvas_x=map_x+map.xsize-map_view_x;
-    else *canvas_x=-1;
-    *canvas_y=map_y-map_view_y;
-    *canvas_x*=NORMAL_TILE_WIDTH;
-    *canvas_y*=NORMAL_TILE_HEIGHT;
-    
-    return *canvas_x >= 0
-      && *canvas_x <(map_view_width*NORMAL_TILE_WIDTH)
-      && *canvas_y >=0
-      && *canvas_y <(map_view_height*NORMAL_TILE_HEIGHT);
-  }
+  return base_get_canvas_xy(map_x, map_y, canvas_x, canvas_y, map_view_x, 
map_view_y, map_win_width, map_win_height);
 }
 
 
Index: client/gui-xaw/mapview.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/mapview.c,v
retrieving revision 1.87
diff -u -r1.87 mapview.c
--- client/gui-xaw/mapview.c    2001/10/30 12:11:46     1.87
+++ client/gui-xaw/mapview.c    2001/11/05 03:04:14
@@ -88,23 +88,10 @@
 **************************************************************************/
 static int get_canvas_xy(int map_x, int map_y, int *canvas_x, int *canvas_y)
 {
-  if (map_view_x0+map_canvas_store_twidth <= map.xsize)
-    *canvas_x = map_x-map_view_x0;
-  else if(map_x >= map_view_x0)
-    *canvas_x = map_x-map_view_x0;
-  else if(map_x < map_adjust_x(map_view_x0+map_canvas_store_twidth))
-    *canvas_x = map_x+map.xsize-map_view_x0;
-  else *canvas_x = -1;
+  int width, height;
+  XtVaGetValues(map_canvas, XtNwidth, &width, XtNheight, &height, NULL);
 
-  *canvas_y = map_y - map_view_y0;
-
-  *canvas_x *= NORMAL_TILE_WIDTH;
-  *canvas_y *= NORMAL_TILE_HEIGHT;
-
-  return *canvas_x >= 0
-      && *canvas_x < map_canvas_store_twidth * NORMAL_TILE_WIDTH
-      && *canvas_y >= 0
-      && *canvas_y < map_canvas_store_theight * NORMAL_TILE_HEIGHT;
+  return base_get_canvas_xy(map_x, map_y, canvas_x, canvas_y, map_view_x0, 
map_view_y0, width, height);
 }
 
 /**************************************************************************

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