changeset 14471:f3cfcaa79037 draft

(svn r19040) -Codechange: Introduce inverse function of RemapCoords.
author alberth <alberth@openttd.org>
date Sat, 06 Feb 2010 13:14:40 +0000
parents b95ae6b0e474
children 22e7dc22c3bd
files src/landscape.h src/smallmap_gui.cpp
diffstat 2 files changed, 33 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/src/landscape.h
+++ b/src/landscape.h
@@ -70,6 +70,20 @@
 	return RemapCoords(x, y, GetSlopeZ(x, y));
 }
 
+/**
+ * Map 2D viewport or smallmap coordinate to 3D world or tile coordinate.
+ * Function assumes <tt>z == 0</tt>. For other values of \p z, add \p z to \a y before the call.
+ * @param x X coordinate of the 2D coordinate.
+ * @param y Y coordinate of the 2D coordinate.
+ * @return X and Y components of equivalent world or tile coordinate.
+ * @note Inverse of #RemapCoords function. Smaller values may get rounded.
+ */
+static inline Point InverseRemapCoords(int x, int y)
+{
+	Point pt = {(y * 2 - x) >> 2, (y * 2 + x) >> 2};
+	return pt;
+}
+
 uint ApplyFoundationToSlope(Foundation f, Slope *s);
 void DrawFoundation(TileInfo *ti, Foundation f);
 bool HasFoundationNW(TileIndex tile, Slope slope_here, uint z_here);
--- a/src/smallmap_gui.cpp
+++ b/src/smallmap_gui.cpp
@@ -699,15 +699,12 @@
 		/* Find main viewport. */
 		const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
 
-		int tx = ((vp->virtual_top << 1) - vp->virtual_left) >> 6;
-		int ty = ((vp->virtual_top << 1) + vp->virtual_left) >> 6;
-		Point tl = this->RemapTile(tx, ty);
+		Point tile = InverseRemapCoords(vp->virtual_left, vp->virtual_top);
+		Point tl = this->RemapTile(tile.x >> 4, tile.y >> 4);
+		tl.x -= this->subscroll;
 
-		tx = (((vp->virtual_top + vp->virtual_height) << 1) - (vp->virtual_left + vp->virtual_width)) >> 6;
-		ty = (((vp->virtual_top + vp->virtual_height) << 1) + (vp->virtual_left + vp->virtual_width)) >> 6;
-		Point br = this->RemapTile(tx, ty);
-
-		tl.x -= this->subscroll;
+		tile = InverseRemapCoords(vp->virtual_left + vp->virtual_width, vp->virtual_top + vp->virtual_height);
+		Point br = this->RemapTile(tile.x >> 4, tile.y >> 4);
 		br.x -= this->subscroll;
 
 		SmallMapWindow::DrawVertMapIndicator(tl.x, tl.y, br.y);
@@ -1110,24 +1107,24 @@
 	void SetNewScroll(int sx, int sy, int sub)
 	{
 		const NWidgetBase *wi = this->GetWidget<NWidgetBase>(SM_WIDGET_MAP);
-		int hx = wi->current_x / 2;
-		int hy = wi->current_y / 2;
-		int hvx = (hx * -4 + hy * 8) * this->zoom;
-		int hvy = (hx *  4 + hy * 8) * this->zoom;
-		if (sx < -hvx) {
-			sx = -hvx;
+		Point hv = InverseRemapCoords(wi->current_x * TILE_SIZE / 2, wi->current_y * TILE_SIZE / 2);
+		hv.x *= this->zoom;
+		hv.y *= this->zoom;
+
+		if (sx < -hv.x) {
+			sx = -hv.x;
 			sub = 0;
 		}
-		if (sx > (int)MapMaxX() * TILE_SIZE - hvx) {
-			sx = MapMaxX() * TILE_SIZE - hvx;
+		if (sx > (int)MapMaxX() * TILE_SIZE - hv.x) {
+			sx = MapMaxX() * TILE_SIZE - hv.x;
 			sub = 0;
 		}
-		if (sy < -hvy) {
-			sy = -hvy;
+		if (sy < -hv.y) {
+			sy = -hv.y;
 			sub = 0;
 		}
-		if (sy > (int)MapMaxY() * TILE_SIZE - hvy) {
-			sy = MapMaxY() * TILE_SIZE - hvy;
+		if (sy > (int)MapMaxY() * TILE_SIZE - hv.y) {
+			sy = MapMaxY() * TILE_SIZE - hv.y;
 			sub = 0;
 		}
 
@@ -1151,18 +1148,13 @@
 	void SmallMapCenterOnCurrentPos()
 	{
 		const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
-
-		int x = vp->virtual_left + vp->virtual_width  / 2;
-		int y = vp->virtual_top  + vp->virtual_height / 2;
-
-		int tx = (y * 2 - x) >> 2;
-		int ty = (y * 2 + x) >> 2;
+		Point pt = InverseRemapCoords(vp->virtual_left + vp->virtual_width  / 2, vp->virtual_top  + vp->virtual_height / 2);
 
 		int sub;
 		const NWidgetBase *wid = this->GetWidget<NWidgetBase>(SM_WIDGET_MAP);
 		Point tile = this->PixelToTile(wid->current_x / 2, wid->current_y / 2, &sub);
 
-		this->SetNewScroll(tx - tile.x * TILE_SIZE, ty - tile.y * TILE_SIZE, sub);
+		this->SetNewScroll(pt.x - tile.x * TILE_SIZE, pt.y - tile.y * TILE_SIZE, sub);
 		this->SetDirty();
 	}
 };