changeset 8095:538d8ce486cd draft

(svn r11656) -Codechange: add ZOOM_LVL_BEGIN and postfix operators so ZoomLevel can be used in some iterations
author smatz <smatz@openttd.org>
date Mon, 17 Dec 2007 22:04:07 +0000
parents 09992312a239
children 10b0b806a2b4
files src/blitter/8bpp_optimized.cpp src/main_gui.cpp src/openttd.cpp src/sound.cpp src/zoom.hpp
diffstat 5 files changed, 23 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/blitter/8bpp_optimized.cpp
+++ b/src/blitter/8bpp_optimized.cpp
@@ -17,7 +17,7 @@
 	uint offset = 0;
 
 	/* Find the offset of this zoom-level */
-	offset = ((const uint8 *)bp->sprite)[(int)zoom * 2] | ((const byte *)bp->sprite)[(int)zoom * 2 + 1] << 8;
+	offset = ((const uint8 *)bp->sprite)[(int)(zoom - ZOOM_LVL_BEGIN) * 2] | ((const byte *)bp->sprite)[(int)(zoom - ZOOM_LVL_BEGIN) * 2 + 1] << 8;
 
 	/* Find where to start reading in the source sprite */
 	src = (const uint8 *)bp->sprite + offset;
@@ -110,9 +110,9 @@
 	uint index = 0;
 
 	/* Make memory for all zoom-levels */
-	memory += (int)ZOOM_LVL_END * sizeof(uint16);
-	for (int i = 0; i < (int)ZOOM_LVL_END; i++) {
-		memory += UnScaleByZoom(sprite->height, (ZoomLevel)i) * UnScaleByZoom(sprite->width, (ZoomLevel)i);
+	memory += (int)(ZOOM_LVL_END - ZOOM_LVL_BEGIN) * sizeof(uint16);
+	for (ZoomLevel i = ZOOM_LVL_BEGIN; i < ZOOM_LVL_END; i++) {
+		memory += UnScaleByZoom(sprite->height, i) * UnScaleByZoom(sprite->width, i);
 		index += 2;
 	}
 
@@ -121,7 +121,7 @@
 	temp_dst = MallocT<byte>(memory);
 
 	/* Make the sprites per zoom-level */
-	for (int i = 0; i < (int)ZOOM_LVL_END; i++) {
+	for (ZoomLevel i = ZOOM_LVL_BEGIN; i < ZOOM_LVL_END; i++) {
 		/* Store the scaled image */
 		const SpriteLoader::CommonPixel *src;
 
@@ -131,19 +131,19 @@
 
 		byte *dst = &temp_dst[index];
 
-		for (int y = 0; y < UnScaleByZoom(sprite->height, (ZoomLevel)i); y++) {
+		for (int y = 0; y < UnScaleByZoom(sprite->height, i); y++) {
 			uint trans = 0;
 			uint pixels = 0;
 			uint last_color = 0;
 			uint count_index = 0;
 			uint rx = 0;
-			src = &sprite->data[ScaleByZoom(y, (ZoomLevel)i) * sprite->width];
+			src = &sprite->data[ScaleByZoom(y, i) * sprite->width];
 
-			for (int x = 0; x < UnScaleByZoom(sprite->width, (ZoomLevel)i); x++) {
+			for (int x = 0; x < UnScaleByZoom(sprite->width, i); x++) {
 				uint color = 0;
 
 				/* Get the color keeping in mind the zoom-level */
-				for (int j = 0; j < ScaleByZoom(1, (ZoomLevel)i); j++) {
+				for (int j = 0; j < ScaleByZoom(1, i); j++) {
 					if (src->m != 0) color = src->m;
 					src++;
 					rx++;
--- a/src/main_gui.cpp
+++ b/src/main_gui.cpp
@@ -872,7 +872,7 @@
 	switch (how) {
 		case ZOOM_IN:
 			if (vp->zoom == ZOOM_LVL_MIN) return false;
-			vp->zoom = (ZoomLevel)((byte)vp->zoom - 1);
+			vp->zoom = (ZoomLevel)((int)vp->zoom - 1);
 			vp->virtual_width >>= 1;
 			vp->virtual_height >>= 1;
 
@@ -883,7 +883,7 @@
 			break;
 		case ZOOM_OUT:
 			if (vp->zoom == ZOOM_LVL_MAX) return false;
-			vp->zoom = (ZoomLevel)((byte)vp->zoom + 1);
+			vp->zoom = (ZoomLevel)((int)vp->zoom + 1);
 
 			WP(w, vp_d).scrollpos_x -= vp->virtual_width >> 1;
 			WP(w, vp_d).scrollpos_y -= vp->virtual_height >> 1;
--- a/src/openttd.cpp
+++ b/src/openttd.cpp
@@ -1291,7 +1291,7 @@
 	WP(w, vp_d).dest_scrollpos_y = _saved_scrollpos_y;
 
 	ViewPort *vp = w->viewport;
-	vp->zoom = (ZoomLevel)min(_saved_scrollpos_zoom, ZOOM_LVL_MAX);
+	vp->zoom = min(_saved_scrollpos_zoom, ZOOM_LVL_MAX);
 	vp->virtual_width = ScaleByZoom(vp->width, vp->zoom);
 	vp->virtual_height = ScaleByZoom(vp->height, vp->zoom);
 
--- a/src/sound.cpp
+++ b/src/sound.cpp
@@ -157,7 +157,7 @@
 
 
 static const byte _vol_factor_by_zoom[] = {255, 190, 134, 87};
-assert_compile(lengthof(_vol_factor_by_zoom) == ZOOM_LVL_END);
+assert_compile(lengthof(_vol_factor_by_zoom) == ZOOM_LVL_END - ZOOM_LVL_BEGIN);
 
 static const byte _sound_base_vol[] = {
 	128,  90, 128, 128, 128, 128, 128, 128,
@@ -216,7 +216,7 @@
 			StartSound(
 				sound,
 				left / max(1, vp->virtual_width / ((PANNING_LEVELS << 1) + 1)) - PANNING_LEVELS,
-				(GetSound(sound)->volume * msf.effect_vol * _vol_factor_by_zoom[vp->zoom]) >> 15
+				(GetSound(sound)->volume * msf.effect_vol * _vol_factor_by_zoom[vp->zoom - ZOOM_LVL_BEGIN]) >> 15
 			);
 			return;
 		}
--- a/src/zoom.hpp
+++ b/src/zoom.hpp
@@ -5,8 +5,11 @@
 #ifndef ZOOM_HPP
 #define ZOOM_HPP
 
+#include "helpers.hpp"
+
 enum ZoomLevel {
 	/* Our possible zoom-levels */
+	ZOOM_LVL_BEGIN  = 0,
 	ZOOM_LVL_NORMAL = 0,
 	ZOOM_LVL_OUT_2X,
 	ZOOM_LVL_OUT_4X,
@@ -32,6 +35,8 @@
 
 extern ZoomLevel _saved_scrollpos_zoom;
 
+DECLARE_POSTFIX_INCREMENT(ZoomLevel)
+
 /**
  * Scale by zoom level, usually shift left (when zoom > ZOOM_LVL_NORMAL)
  * When shifting right, value is rounded up
@@ -42,7 +47,7 @@
 static inline int ScaleByZoom(int value, ZoomLevel zoom)
 {
 	if (zoom == ZOOM_LVL_NORMAL) return value;
-	int izoom = (int)zoom - (int)ZOOM_LVL_NORMAL;
+	int izoom = zoom - ZOOM_LVL_NORMAL;
 	return (zoom > ZOOM_LVL_NORMAL) ? value << izoom : (value + (1 << -izoom) - 1) >> -izoom;
 }
 
@@ -56,7 +61,7 @@
 static inline int UnScaleByZoom(int value, ZoomLevel zoom)
 {
 	if (zoom == ZOOM_LVL_NORMAL) return value;
-	int izoom = (int)zoom - (int)ZOOM_LVL_NORMAL;
+	int izoom = zoom - ZOOM_LVL_NORMAL;
 	return (zoom > ZOOM_LVL_NORMAL) ? (value + (1 << izoom) - 1) >> izoom : value << -izoom;
 }
 
@@ -69,7 +74,7 @@
 static inline int ScaleByZoomLower(int value, ZoomLevel zoom)
 {
 	if (zoom == ZOOM_LVL_NORMAL) return value;
-	int izoom = (int)zoom - (int)ZOOM_LVL_NORMAL;
+	int izoom = zoom - ZOOM_LVL_NORMAL;
 	return (zoom > ZOOM_LVL_NORMAL) ? value << izoom : value >> -izoom;
 }
 
@@ -82,7 +87,7 @@
 static inline int UnScaleByZoomLower(int value, ZoomLevel zoom)
 {
 	if (zoom == ZOOM_LVL_NORMAL) return value;
-	int izoom = (int)zoom - (int)ZOOM_LVL_NORMAL;
+	int izoom = zoom - ZOOM_LVL_NORMAL;
 	return (zoom > ZOOM_LVL_NORMAL) ? value >> izoom : value << -izoom;
 }