changeset 5805:b7c3a94ff74e draft

(svn r8367) -Fix: [Graphs] Don't try to use the same value to mean something is invalid for both an int64 and a uint.
author maedhros <maedhros@openttd.org>
date Tue, 23 Jan 2007 11:20:59 +0000
parents 07c4b670fcda
children 4f5e2ea123e8
files src/graph_gui.cpp
diffstat 1 files changed, 24 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/graph_gui.cpp
+++ b/src/graph_gui.cpp
@@ -37,6 +37,10 @@
 	GRAPH_NUM_LINES_Y = 9,
 };
 
+/* Apparently these don't play well with enums. */
+static const int64 INVALID_DATAPOINT     = LLONG_MAX; // Value used for a datapoint that shouldn't be drawn.
+static const uint  INVALID_DATAPOINT_POS = UINT_MAX;  // Used to determine if the previous point was drawn.
+
 typedef struct GraphDrawer {
 	uint sel; // bitmask of the players *excluded* (e.g. 11111111 means that no players are shown)
 	byte num_dataset;
@@ -61,11 +65,9 @@
 	int64 cost[GRAPH_MAX_DATASETS][24]; // last 2 years
 } GraphDrawer;
 
-static const int64 INVALID_VALUE = 0x80000000;
-
 static void DrawGraph(const GraphDrawer *gw)
 {
-	uint x,y,old_x,old_y;
+	uint x, y;  // Reused whenever x and y coordinates are needed.
 	int right;
 	int64 highest_value;
 	int adj_height;
@@ -133,7 +135,7 @@
 			for (int j = 0; j < gw->num_on_x_axis; j++) {
 				int64 datapoint = gw->cost[i][j];
 
-				if (datapoint != INVALID_VALUE) {
+				if (datapoint != INVALID_DATAPOINT) {
 					/* For now, if the graph has negative values the scaling is
 					 * symmetrical about the x axis, so take the absolute value
 					 * of each data point. */
@@ -211,26 +213,31 @@
 			/* Centre the dot between the grid lines. */
 			x = gw->left + GRAPH_X_POSITION_BEGINNING + (GRAPH_X_POSITION_SEPARATION / 2);
 
-			byte color = gw->colors[i];
-			old_y = old_x = INVALID_VALUE;
+			byte color  = gw->colors[i];
+			uint prev_x = INVALID_DATAPOINT_POS;
+			uint prev_y = INVALID_DATAPOINT_POS;
 
 			for (int j = 0; j < gw->num_on_x_axis; j++) {
 				int64 datapoint = gw->cost[i][j];
 
-				if (datapoint != INVALID_VALUE) {
+				if (datapoint != INVALID_DATAPOINT) {
 					/* XXX: This can overflow if adj_height * datapoint is too
 					 * big to fit in an int64. */
 					y = gw->top + adj_height - (adj_height * datapoint) / highest_value;
 
+					/* Draw the point. */
 					GfxFillRect(x-1, y-1, x+1, y+1, color);
-					if (old_x != INVALID_VALUE)
-						GfxDrawLine(old_x, old_y, x, y, color);
+
+					/* Draw the line connected to the previous point. */
+					if (prev_x != INVALID_DATAPOINT_POS) GfxDrawLine(prev_x, prev_y, x, y, color);
 
-					old_x = x;
-					old_y = y;
+					prev_x = x;
+					prev_y = y;
 				} else {
-					old_x = INVALID_VALUE;
+					prev_x = INVALID_DATAPOINT_POS;
+					prev_y = INVALID_DATAPOINT_POS;
 				}
+
 				x += GRAPH_X_POSITION_SEPARATION;
 			}
 		}
@@ -378,7 +385,7 @@
 			if (p->is_active) {
 				gd.colors[numd] = _colour_gradient[p->player_color][6];
 				for (j = gd.num_on_x_axis, i = 0; --j >= 0;) {
-					gd.cost[numd][i] = (j >= p->num_valid_stat_ent) ? INVALID_VALUE : (p->old_economy[j].income + p->old_economy[j].expenses);
+					gd.cost[numd][i] = (j >= p->num_valid_stat_ent) ? INVALID_DATAPOINT : (p->old_economy[j].income + p->old_economy[j].expenses);
 					i++;
 				}
 			}
@@ -448,7 +455,7 @@
 			if (p->is_active) {
 				gd.colors[numd] = _colour_gradient[p->player_color][6];
 				for (j = gd.num_on_x_axis, i = 0; --j >= 0;) {
-					gd.cost[numd][i] = (j >= p->num_valid_stat_ent) ? INVALID_VALUE : p->old_economy[j].income;
+					gd.cost[numd][i] = (j >= p->num_valid_stat_ent) ? INVALID_DATAPOINT : p->old_economy[j].income;
 					i++;
 				}
 			}
@@ -518,7 +525,7 @@
 			if (p->is_active) {
 				gd.colors[numd] = _colour_gradient[p->player_color][6];
 				for (j = gd.num_on_x_axis, i = 0; --j >= 0;) {
-					gd.cost[numd][i] = (j >= p->num_valid_stat_ent) ? INVALID_VALUE : p->old_economy[j].delivered_cargo;
+					gd.cost[numd][i] = (j >= p->num_valid_stat_ent) ? INVALID_DATAPOINT : p->old_economy[j].delivered_cargo;
 					i++;
 				}
 			}
@@ -588,7 +595,7 @@
 			if (p->is_active) {
 				gd.colors[numd] = _colour_gradient[p->player_color][6];
 				for (j = gd.num_on_x_axis, i = 0; --j >= 0;) {
-					gd.cost[numd][i] = (j >= p->num_valid_stat_ent) ? INVALID_VALUE : p->old_economy[j].performance_history;
+					gd.cost[numd][i] = (j >= p->num_valid_stat_ent) ? INVALID_DATAPOINT : p->old_economy[j].performance_history;
 					i++;
 				}
 			}
@@ -661,7 +668,7 @@
 			if (p->is_active) {
 				gd.colors[numd] = _colour_gradient[p->player_color][6];
 				for (j = gd.num_on_x_axis, i = 0; --j >= 0;) {
-					gd.cost[numd][i] = (j >= p->num_valid_stat_ent) ? INVALID_VALUE : p->old_economy[j].company_value;
+					gd.cost[numd][i] = (j >= p->num_valid_stat_ent) ? INVALID_DATAPOINT : p->old_economy[j].company_value;
 					i++;
 				}
 			}