changeset 11700:5cb273d3151b draft

(svn r16086) -Codechange: magic numbers substitutes by enums and some type safety
author rubidium <rubidium@openttd.org>
date Sat, 18 Apr 2009 21:10:36 +0000
parents 31c1b52310df
children 362680cbb585
files src/tile_type.h src/tilehighlight_type.h src/viewport.cpp
diffstat 3 files changed, 56 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/src/tile_type.h
+++ b/src/tile_type.h
@@ -8,9 +8,10 @@
 #include "core/enum_type.hpp"
 
 enum {
-	TILE_SIZE   = 16,   ///< Tiles are 16x16 "units" in size
-	TILE_PIXELS = 32,   ///< a tile is 32x32 pixels
-	TILE_HEIGHT =  8,   ///< The standard height-difference between tiles on two levels is 8 (z-diff 8)
+	TILE_SIZE      = 16,            ///< Tiles are 16x16 "units" in size
+	TILE_UNIT_MASK = TILE_SIZE - 1, ///< for masking in/out the inner-tile units.
+	TILE_PIXELS    = 32,            ///< a tile is 32x32 pixels
+	TILE_HEIGHT    =  8,            ///< The standard height-difference between tiles on two levels is 8 (z-diff 8)
 
 	MAX_TILE_HEIGHT     = 15,                    ///< Maximum allowed tile height
 	MAX_SNOWLINE_HEIGHT = (MAX_TILE_HEIGHT - 2), ///< Maximum allowed snowline height
--- a/src/tilehighlight_type.h
+++ b/src/tilehighlight_type.h
@@ -39,6 +39,7 @@
 	HT_DIR_HL = 3,    ///< horizontal lower
 	HT_DIR_VL = 4,    ///< vertical left
 	HT_DIR_VR = 5,    ///< vertical right
+	HT_DIR_END,       ///< end marker
 	HT_DIR_MASK = 0x7 ///< masks the drag-direction
 };
 DECLARE_ENUM_AS_BIT_SET(HighLightStyle);
@@ -59,9 +60,9 @@
 	byte dirty;
 	byte sizelimit;
 
-	byte drawstyle;      // lower bits 0-3 are reserved for detailed highlight information information
-	byte new_drawstyle;  // only used in UpdateTileSelection() to as a buffer to compare if there was a change between old and new
-	byte next_drawstyle; // queued, but not yet drawn style
+	HighLightStyle drawstyle;      // lower bits 0-3 are reserved for detailed highlight information information
+	HighLightStyle new_drawstyle;  // only used in UpdateTileSelection() to as a buffer to compare if there was a change between old and new
+	HighLightStyle next_drawstyle; // queued, but not yet drawn style
 
 	ViewportHighlightMode place_mode;
 	bool make_square_red;
--- a/src/viewport.cpp
+++ b/src/viewport.cpp
@@ -902,15 +902,15 @@
 			DrawSelectionSprite(_cur_dpi->zoom <= ZOOM_LVL_DETAIL ? SPR_DOT : SPR_DOT_SMALL, PAL_NONE, ti, z, foundation_part);
 		} else if (_thd.drawstyle & HT_RAIL /* && _thd.place_mode == VHM_RAIL*/) {
 			/* autorail highlight piece under cursor */
-			uint type = _thd.drawstyle & 0xF;
-			assert(type <= 5);
+			HighLightStyle type = _thd.drawstyle & HT_DIR_MASK;
+			assert(type < HT_DIR_END);
 			DrawAutorailSelection(ti, _autorail_type[type][0]);
 		} else if (IsPartOfAutoLine(ti->x, ti->y)) {
 			/* autorail highlighting long line */
-			int dir = _thd.drawstyle & ~0xF0;
+			HighLightStyle dir = _thd.drawstyle & HT_DIR_MASK;
 			uint side;
 
-			if (dir < 2) {
+			if (dir == HT_DIR_X || dir == HT_DIR_Y) {
 				side = 0;
 			} else {
 				TileIndex start = TileVirtXY(_thd.selstart.x, _thd.selstart.y);
@@ -942,8 +942,8 @@
 	_cur_ti = &ti;
 
 	/* Transform into tile coordinates and round to closest full tile */
-	x = ((_vd.dpi.top >> 1) - (_vd.dpi.left >> 2)) & ~0xF;
-	y = ((_vd.dpi.top >> 1) + (_vd.dpi.left >> 2) - 0x10) & ~0xF;
+	x = ((_vd.dpi.top >> 1) - (_vd.dpi.left >> 2)) & ~TILE_UNIT_MASK;
+	y = ((_vd.dpi.top >> 1) + (_vd.dpi.left >> 2) - TILE_SIZE) & ~TILE_UNIT_MASK;
 
 	/* determine size of area */
 	{
@@ -2065,8 +2065,8 @@
 		pt.y += 8;
 	}
 
-	_tile_fract_coords.x = pt.x & 0xF;
-	_tile_fract_coords.y = pt.y & 0xF;
+	_tile_fract_coords.x = pt.x & TILE_UNIT_MASK;
+	_tile_fract_coords.y = pt.y & TILE_UNIT_MASK;
 
 	w = GetCallbackWnd();
 	if (w != NULL) w->OnPlaceObject(pt, TileVirtXY(pt.x, pt.y));
@@ -2132,7 +2132,7 @@
 /** returns the best autorail highlight type from map coordinates */
 static HighLightStyle GetAutorailHT(int x, int y)
 {
-	return HT_RAIL | _autorail_piece[x & 0xF][y & 0xF];
+	return HT_RAIL | _autorail_piece[x & TILE_UNIT_MASK][y & TILE_UNIT_MASK];
 }
 
 /**
@@ -2147,16 +2147,16 @@
 	int x1;
 	int y1;
 
-	_thd.new_drawstyle = 0;
+	_thd.new_drawstyle = HT_NONE;
 
 	if (_thd.place_mode == VHM_SPECIAL) {
 		x1 = _thd.selend.x;
 		y1 = _thd.selend.y;
 		if (x1 != -1) {
-			int x2 = _thd.selstart.x & ~0xF;
-			int y2 = _thd.selstart.y & ~0xF;
-			x1 &= ~0xF;
-			y1 &= ~0xF;
+			int x2 = _thd.selstart.x & ~TILE_UNIT_MASK;
+			int y2 = _thd.selstart.y & ~TILE_UNIT_MASK;
+			x1 &= ~TILE_UNIT_MASK;
+			y1 &= ~TILE_UNIT_MASK;
 
 			if (x1 >= x2) Swap(x1, x2);
 			if (y1 >= y2) Swap(y1, y2);
@@ -2177,8 +2177,8 @@
 					break;
 				case VHM_POINT:
 					_thd.new_drawstyle = HT_POINT;
-					x1 += 8;
-					y1 += 8;
+					x1 += TILE_SIZE / 2;
+					y1 += TILE_SIZE / 2;
 					break;
 				case VHM_RAIL:
 					_thd.new_drawstyle = GetAutorailHT(pt.x, pt.y); // draw one highlighted tile
@@ -2187,8 +2187,8 @@
 					NOT_REACHED();
 					break;
 			}
-			_thd.new_pos.x = x1 & ~0xF;
-			_thd.new_pos.y = y1 & ~0xF;
+			_thd.new_pos.x = x1 & ~TILE_UNIT_MASK;
+			_thd.new_pos.y = y1 & ~TILE_UNIT_MASK;
 		}
 	}
 
@@ -2290,30 +2290,30 @@
 static HighLightStyle Check2x1AutoRail(int mode)
 {
 	int fxpy = _tile_fract_coords.x + _tile_fract_coords.y;
-	int sxpy = (_thd.selend.x & 0xF) + (_thd.selend.y & 0xF);
+	int sxpy = (_thd.selend.x & TILE_UNIT_MASK) + (_thd.selend.y & TILE_UNIT_MASK);
 	int fxmy = _tile_fract_coords.x - _tile_fract_coords.y;
-	int sxmy = (_thd.selend.x & 0xF) - (_thd.selend.y & 0xF);
+	int sxmy = (_thd.selend.x & TILE_UNIT_MASK) - (_thd.selend.y & TILE_UNIT_MASK);
 
 	switch (mode) {
 		default: NOT_REACHED();
 		case 0: // end piece is lower right
-			if (fxpy >= 20 && sxpy <= 12) { /*SwapSelection(); DoRailroadTrack(0); */return HT_DIR_HL; }
-			if (fxmy < -3 && sxmy > 3) {/* DoRailroadTrack(0); */return HT_DIR_VR; }
+			if (fxpy >= 20 && sxpy <= 12) return HT_DIR_HL;
+			if (fxmy < -3 && sxmy > 3) return HT_DIR_VR;
 			return HT_DIR_Y;
 
 		case 1:
-			if (fxmy > 3 && sxmy < -3) { /*SwapSelection(); DoRailroadTrack(0); */return HT_DIR_VL; }
-			if (fxpy <= 12 && sxpy >= 20) { /*DoRailroadTrack(0); */return HT_DIR_HU; }
+			if (fxmy > 3 && sxmy < -3) return HT_DIR_VL;
+			if (fxpy <= 12 && sxpy >= 20) return HT_DIR_HU;
 			return HT_DIR_Y;
 
 		case 2:
-			if (fxmy > 3 && sxmy < -3) { /*DoRailroadTrack(3);*/ return HT_DIR_VL; }
-			if (fxpy >= 20 && sxpy <= 12) { /*SwapSelection(); DoRailroadTrack(0); */return HT_DIR_HL; }
+			if (fxmy > 3 && sxmy < -3) return HT_DIR_VL;
+			if (fxpy >= 20 && sxpy <= 12) return HT_DIR_HL;
 			return HT_DIR_X;
 
 		case 3:
-			if (fxmy < -3 && sxmy > 3) { /*SwapSelection(); DoRailroadTrack(3);*/ return HT_DIR_VR; }
-			if (fxpy <= 12 && sxpy >= 20) { /*DoRailroadTrack(0); */return HT_DIR_HU; }
+			if (fxmy < -3 && sxmy > 3) return HT_DIR_VR;
+			if (fxpy <= 12 && sxpy >= 20) return HT_DIR_HU;
 			return HT_DIR_X;
 	}
 }
@@ -2440,12 +2440,11 @@
 static void CalcRaildirsDrawstyle(TileHighlightData *thd, int x, int y, int method)
 {
 	HighLightStyle b;
-	uint w, h;
-
-	int dx = thd->selstart.x - (thd->selend.x & ~0xF);
-	int dy = thd->selstart.y - (thd->selend.y & ~0xF);
-	w = abs(dx) + 16;
-	h = abs(dy) + 16;
+
+	int dx = thd->selstart.x - (thd->selend.x & ~TILE_UNIT_MASK);
+	int dy = thd->selstart.y - (thd->selend.y & ~TILE_UNIT_MASK);
+	uint w = abs(dx) + TILE_SIZE;
+	uint h = abs(dy) + TILE_SIZE;
 
 	if (TileVirtXY(thd->selstart.x, thd->selstart.y) == TileVirtXY(x, y)) { // check if we're only within one tile
 		if (method == VPM_RAILDIRS) {
@@ -2453,19 +2452,19 @@
 		} else { // rect for autosignals on one tile
 			b = HT_RECT;
 		}
-	} else if (h == 16) { // Is this in X direction?
-		if (dx == 16) { // 2x1 special handling
+	} else if (h == TILE_SIZE) { // Is this in X direction?
+		if (dx == TILE_SIZE) { // 2x1 special handling
 			b = (Check2x1AutoRail(3)) | HT_LINE;
-		} else if (dx == -16) {
+		} else if (dx == -TILE_SIZE) {
 			b = (Check2x1AutoRail(2)) | HT_LINE;
 		} else {
 			b = HT_LINE | HT_DIR_X;
 		}
 		y = thd->selstart.y;
-	} else if (w == 16) { // Or Y direction?
-		if (dy == 16) { // 2x1 special handling
+	} else if (w == TILE_SIZE) { // Or Y direction?
+		if (dy == TILE_SIZE) { // 2x1 special handling
 			b = (Check2x1AutoRail(1)) | HT_LINE;
-		} else if (dy == -16) { // 2x1 other direction
+		} else if (dy == -TILE_SIZE) { // 2x1 other direction
 			b = (Check2x1AutoRail(0)) | HT_LINE;
 		} else {
 			b = HT_LINE | HT_DIR_Y;
@@ -2479,27 +2478,26 @@
 		x = thd->selstart.x;
 	} else { // complicated direction
 		int d = w - h;
-		thd->selend.x = thd->selend.x & ~0xF;
-		thd->selend.y = thd->selend.y & ~0xF;
+		thd->selend.x = thd->selend.x & ~TILE_UNIT_MASK;
+		thd->selend.y = thd->selend.y & ~TILE_UNIT_MASK;
 
 		/* four cases. */
 		if (x > thd->selstart.x) {
 			if (y > thd->selstart.y) {
 				/* south */
 				if (d == 0) {
-					b = (x & 0xF) > (y & 0xF) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
+					b = (x & TILE_UNIT_MASK) > (y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
 				} else if (d >= 0) {
 					x = thd->selstart.x + h;
 					b = HT_LINE | HT_DIR_VL;
-					// return px == py || px == py + 16;
 				} else {
 					y = thd->selstart.y + w;
 					b = HT_LINE | HT_DIR_VR;
-				} // return px == py || px == py - 16;
+				}
 			} else {
 				/* west */
 				if (d == 0) {
-					b = (x & 0xF) + (y & 0xF) >= 0x10 ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU;
+					b = (x & TILE_UNIT_MASK) + (y & TILE_UNIT_MASK) >= TILE_SIZE ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU;
 				} else if (d >= 0) {
 					x = thd->selstart.x + h;
 					b = HT_LINE | HT_DIR_HL;
@@ -2512,27 +2510,25 @@
 			if (y > thd->selstart.y) {
 				/* east */
 				if (d == 0) {
-					b = (x & 0xF) + (y & 0xF) >= 0x10 ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU;
+					b = (x & TILE_UNIT_MASK) + (y & TILE_UNIT_MASK) >= TILE_SIZE ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU;
 				} else if (d >= 0) {
 					x = thd->selstart.x - h;
 					b = HT_LINE | HT_DIR_HU;
-					// return px == -py || px == -py - 16;
 				} else {
 					y = thd->selstart.y + w;
 					b = HT_LINE | HT_DIR_HL;
-				} // return px == -py || px == -py + 16;
+				}
 			} else {
 				/* north */
 				if (d == 0) {
-					b = (x & 0xF) > (y & 0xF) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
+					b = (x & TILE_UNIT_MASK) > (y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
 				} else if (d >= 0) {
 					x = thd->selstart.x - h;
 					b = HT_LINE | HT_DIR_VR;
-					// return px == py || px == py - 16;
 				} else {
 					y = thd->selstart.y - w;
 					b = HT_LINE | HT_DIR_VL;
-				} // return px == py || px == py + 16;
+				}
 			}
 		}
 	}