src/player_gui.cpp
changeset 9252 71843bf1bf33
parent 9251 bee6451d87a7
child 9273 35e0224ea8f1
--- a/src/player_gui.cpp	Fri May 16 07:34:48 2008 +0000
+++ b/src/player_gui.cpp	Fri May 16 13:21:43 2008 +0000
@@ -35,12 +35,6 @@
 #include "table/sprites.h"
 #include "table/strings.h"
 
-struct highscore_d {
-	uint32 background_img;
-	int8 rank;
-};
-assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(highscore_d));
-
 enum {
 	FIRST_GUI_CALL = INT_MAX,  ///< default value to specify thuis is the first call of the resizable gui
 };
@@ -1421,106 +1415,155 @@
 
 /********** HIGHSCORE and ENDGAME windows */
 
-/* Always draw a maximized window and within there the centered background */
-static void SetupHighScoreEndWindow(Window *w, uint *x, uint *y)
-{
-	uint i;
-	/* resize window to "full-screen" */
-	w->width = _screen.width;
-	w->height = _screen.height;
-	w->widget[0].right = w->width - 1;
-	w->widget[0].bottom = w->height - 1;
+extern StringID EndGameGetPerformanceTitleFromValue(uint value);
 
-	DrawWindowWidgets(w);
 
-	/* Center Highscore/Endscreen background */
-	*x = max(0, (_screen.width  / 2) - (640 / 2));
-	*y = max(0, (_screen.height / 2) - (480 / 2));
-	for (i = 0; i < 10; i++) // the image is split into 10 50px high parts
-		DrawSprite(WP(w, highscore_d).background_img + i, PAL_NONE, *x, *y + (i * 50));
-}
+struct EndGameHighScoreBaseWindow : Window
+{
+	uint32 background_img;
+	int8 rank;
 
-extern StringID EndGameGetPerformanceTitleFromValue(uint value);
+	EndGameHighScoreBaseWindow(const WindowDesc *desc) : Window(desc)
+	{
+	}
+
+	/* Always draw a maximized window and within there the centered background */
+	void SetupHighScoreEndWindow(uint *x, uint *y)
+	{
+		/* resize window to "full-screen" */
+		this->width = _screen.width;
+		this->height = _screen.height;
+		this->widget[0].right = this->width - 1;
+		this->widget[0].bottom = this->height - 1;
+
+		DrawWindowWidgets(this);
+
+		/* Center Highscore/Endscreen background */
+		*x = max(0, (_screen.width  / 2) - (640 / 2));
+		*y = max(0, (_screen.height / 2) - (480 / 2));
+		for (uint i = 0; i < 10; i++) { // the image is split into 10 50px high parts
+			DrawSprite(this->background_img + i, PAL_NONE, *x, *y + (i * 50));
+		}
+	}
+
+	virtual void OnClick(Point pt, int widget)
+	{
+		delete this;
+	}
+};
 
 /** End game window shown at the end of the game */
-static void EndGameWndProc(Window *w, WindowEvent *e)
-{
-	switch (e->event) {
-		case WE_PAINT: {
-			const Player *p;
-			uint x, y;
-
-			SetupHighScoreEndWindow(w, &x, &y);
-
-			if (!IsValidPlayer(_local_player)) break;
+struct EndGameWindow : EndGameHighScoreBaseWindow {
+	EndGameWindow(const WindowDesc *desc) : EndGameHighScoreBaseWindow(desc)
+	{
+		/* Pause in single-player to have a look at the highscore at your own leisure */
+		if (!_networking) DoCommandP(0, 1, 0, NULL, CMD_PAUSE);
 
-			p = GetPlayer(_local_player);
-			/* We need to get performance from last year because the image is shown
-			 * at the start of the new year when these things have already been copied */
-			if (WP(w, highscore_d).background_img == SPR_TYCOON_IMG2_BEGIN) { // Tycoon of the century \o/
-				SetDParam(0, p->index);
-				SetDParam(1, p->index);
-				SetDParam(2, EndGameGetPerformanceTitleFromValue(p->old_economy[0].performance_history));
-				DrawStringMultiCenter(x + (640 / 2), y + 107, STR_021C_OF_ACHIEVES_STATUS, 640);
-			} else {
-				SetDParam(0, p->index);
-				SetDParam(1, EndGameGetPerformanceTitleFromValue(p->old_economy[0].performance_history));
-				DrawStringMultiCenter(x + (640 / 2), y + 157, STR_021B_ACHIEVES_STATUS, 640);
+		this->background_img = SPR_TYCOON_IMG1_BEGIN;
+
+		if (_local_player != PLAYER_SPECTATOR) {
+			const Player *p = GetPlayer(_local_player);
+			if (p->old_economy[0].performance_history == SCORE_MAX) {
+				this->background_img = SPR_TYCOON_IMG2_BEGIN;
 			}
-		} break;
-
-		case WE_CLICK: /* Close the window (and show the highscore window) */
-			delete w;
-			break;
+		}
 
-		case WE_DESTROY: /* Show the highscore window when this one is closed */
-			if (!_networking) DoCommandP(0, 0, 0, NULL, CMD_PAUSE); // unpause
-			ShowHighscoreTable(w->window_number, WP(w, highscore_d).rank);
-			break;
+		/* In a network game show the endscores of the custom difficulty 'network' which is the last one
+		 * as well as generate a TOP5 of that game, and not an all-time top5. */
+		if (_networking) {
+			this->window_number = lengthof(_highscore_table) - 1;
+			this->rank = SaveHighScoreValueNetwork();
+		} else {
+			/* in single player _local player is always valid */
+			const Player *p = GetPlayer(_local_player);
+			this->window_number = _opt.diff_level;
+			this->rank = SaveHighScoreValue(p);
+		}
+
+		MarkWholeScreenDirty();
 	}
-}
 
-static void HighScoreWndProc(Window *w, WindowEvent *e)
+	~EndGameWindow()
+	{
+		if (!_networking) DoCommandP(0, 0, 0, NULL, CMD_PAUSE); // unpause
+		ShowHighscoreTable(this->window_number, this->rank);
+	}
+
+	virtual void OnPaint()
+	{
+		const Player *p;
+		uint x, y;
+
+		this->SetupHighScoreEndWindow(&x, &y);
+
+		if (!IsValidPlayer(_local_player)) return;
+
+		p = GetPlayer(_local_player);
+		/* We need to get performance from last year because the image is shown
+		 * at the start of the new year when these things have already been copied */
+		if (this->background_img == SPR_TYCOON_IMG2_BEGIN) { // Tycoon of the century \o/
+			SetDParam(0, p->index);
+			SetDParam(1, p->index);
+			SetDParam(2, EndGameGetPerformanceTitleFromValue(p->old_economy[0].performance_history));
+			DrawStringMultiCenter(x + (640 / 2), y + 107, STR_021C_OF_ACHIEVES_STATUS, 640);
+		} else {
+			SetDParam(0, p->index);
+			SetDParam(1, EndGameGetPerformanceTitleFromValue(p->old_economy[0].performance_history));
+			DrawStringMultiCenter(x + (640 / 2), y + 157, STR_021B_ACHIEVES_STATUS, 640);
+		}
+	}
+};
+
+struct HighScoreWindow : EndGameHighScoreBaseWindow
 {
-	switch (e->event) {
-		case WE_PAINT: {
-			const HighScore *hs = _highscore_table[w->window_number];
-			uint x, y;
-			uint8 i;
-
-			SetupHighScoreEndWindow(w, &x, &y);
-
-			SetDParam(0, _patches.ending_year);
-			SetDParam(1, w->window_number + STR_6801_EASY);
-			DrawStringMultiCenter(x + (640 / 2), y + 62, !_networking ? STR_0211_TOP_COMPANIES_WHO_REACHED : STR_TOP_COMPANIES_NETWORK_GAME, 500);
-
-			/* Draw Highscore peepz */
-			for (i = 0; i < lengthof(_highscore_table[0]); i++) {
-				SetDParam(0, i + 1);
-				DrawString(x + 40, y + 140 + (i * 55), STR_0212, TC_BLACK);
+	HighScoreWindow(const WindowDesc *desc, int difficulty, int8 ranking) : EndGameHighScoreBaseWindow(desc)
+	{
+		/* pause game to show the chart */
+		if (!_networking) DoCommandP(0, 1, 0, NULL, CMD_PAUSE);
 
-				if (hs[i].company[0] != '\0') {
-					TextColour colour = (WP(w, highscore_d).rank == (int8)i) ? TC_RED : TC_BLACK; // draw new highscore in red
-
-					DoDrawString(hs[i].company, x + 71, y + 140 + (i * 55), colour);
-					SetDParam(0, hs[i].title);
-					SetDParam(1, hs[i].score);
-					DrawString(x + 71, y + 160 + (i * 55), STR_HIGHSCORE_STATS, colour);
-				}
-			}
-		} break;
+		/* Close all always on-top windows to get a clean screen */
+		if (_game_mode != GM_MENU) HideVitalWindows();
 
-		case WE_CLICK: /* Onclick to close window, and in destroy event handle the rest */
-			delete w;
-			break;
+		MarkWholeScreenDirty();
+		this->window_number = difficulty; // show highscore chart for difficulty...
+		this->background_img = SPR_HIGHSCORE_CHART_BEGIN; // which background to show
+		this->rank = ranking;
+	}
 
-		case WE_DESTROY: /* Get back all the hidden windows */
-			if (_game_mode != GM_MENU) ShowVitalWindows();
+	~HighScoreWindow()
+	{
+		if (_game_mode != GM_MENU) ShowVitalWindows();
 
-			if (!_networking) DoCommandP(0, 0, 0, NULL, CMD_PAUSE); // unpause
-			break;
+		if (!_networking) DoCommandP(0, 0, 0, NULL, CMD_PAUSE); // unpause
+	}
+
+	virtual void OnPaint()
+	{
+		const HighScore *hs = _highscore_table[this->window_number];
+		uint x, y;
+
+		this->SetupHighScoreEndWindow(&x, &y);
+
+		SetDParam(0, _patches.ending_year);
+		SetDParam(1, this->window_number + STR_6801_EASY);
+		DrawStringMultiCenter(x + (640 / 2), y + 62, !_networking ? STR_0211_TOP_COMPANIES_WHO_REACHED : STR_TOP_COMPANIES_NETWORK_GAME, 500);
+
+		/* Draw Highscore peepz */
+		for (uint8 i = 0; i < lengthof(_highscore_table[0]); i++) {
+			SetDParam(0, i + 1);
+			DrawString(x + 40, y + 140 + (i * 55), STR_0212, TC_BLACK);
+
+			if (hs[i].company[0] != '\0') {
+				TextColour colour = (this->rank == i) ? TC_RED : TC_BLACK; // draw new highscore in red
+
+				DoDrawString(hs[i].company, x + 71, y + 140 + (i * 55), colour);
+				SetDParam(0, hs[i].title);
+				SetDParam(1, hs[i].score);
+				DrawString(x + 71, y + 160 + (i * 55), STR_HIGHSCORE_STATS, colour);
+			}
 		}
-}
+	}
+};
 
 static const Widget _highscore_widgets[] = {
 {      WWT_PANEL, RESIZE_NONE, 16, 0, 640, 0, 480, 0x0, STR_NULL},
@@ -1532,7 +1575,7 @@
 	WC_HIGHSCORE, WC_NONE,
 	0,
 	_highscore_widgets,
-	HighScoreWndProc
+	NULL
 };
 
 static const WindowDesc _endgame_desc = {
@@ -1540,7 +1583,7 @@
 	WC_ENDSCREEN, WC_NONE,
 	0,
 	_highscore_widgets,
-	EndGameWndProc
+	NULL
 };
 
 /** Show the highscore table for a given difficulty. When called from
@@ -1548,61 +1591,18 @@
  * and is thus highlighted */
 void ShowHighscoreTable(int difficulty, int8 ranking)
 {
-	Window *w;
-
-	/* pause game to show the chart */
-	if (!_networking) DoCommandP(0, 1, 0, NULL, CMD_PAUSE);
-
-	/* Close all always on-top windows to get a clean screen */
-	if (_game_mode != GM_MENU) HideVitalWindows();
-
 	DeleteWindowByClass(WC_HIGHSCORE);
-	w = new Window(&_highscore_desc);
-
-	if (w != NULL) {
-		MarkWholeScreenDirty();
-		w->window_number = difficulty; // show highscore chart for difficulty...
-		WP(w, highscore_d).background_img = SPR_HIGHSCORE_CHART_BEGIN; // which background to show
-		WP(w, highscore_d).rank = ranking;
-	}
+	new HighScoreWindow(&_highscore_desc, difficulty, ranking);
 }
 
 /** Show the endgame victory screen in 2050. Update the new highscore
  * if it was high enough */
 void ShowEndGameChart()
 {
-	Window *w;
-
 	/* Dedicated server doesn't need the highscore window */
 	if (_network_dedicated) return;
-	/* Pause in single-player to have a look at the highscore at your own leisure */
-	if (!_networking) DoCommandP(0, 1, 0, NULL, CMD_PAUSE);
 
 	HideVitalWindows();
 	DeleteWindowByClass(WC_ENDSCREEN);
-	w = new Window(&_endgame_desc);
-
-	if (w != NULL) {
-		MarkWholeScreenDirty();
-
-		WP(w, highscore_d).background_img = SPR_TYCOON_IMG1_BEGIN;
-
-		if (_local_player != PLAYER_SPECTATOR) {
-			const Player *p = GetPlayer(_local_player);
-			if (p->old_economy[0].performance_history == SCORE_MAX)
-				WP(w, highscore_d).background_img = SPR_TYCOON_IMG2_BEGIN;
-		}
-
-		/* In a network game show the endscores of the custom difficulty 'network' which is the last one
-		 * as well as generate a TOP5 of that game, and not an all-time top5. */
-		if (_networking) {
-			w->window_number = lengthof(_highscore_table) - 1;
-			WP(w, highscore_d).rank = SaveHighScoreValueNetwork();
-		} else {
-			/* in single player _local player is always valid */
-			const Player *p = GetPlayer(_local_player);
-			w->window_number = _opt.diff_level;
-			WP(w, highscore_d).rank = SaveHighScoreValue(p);
-		}
-	}
+	new EndGameWindow(&_endgame_desc);
 }