Remove ZeroedMemoryAllocator as a base class of Window
[openttd/fttd.git] / src / highscore_gui.cpp
bloba32eecdea025c6d9b2f51767cae086178af1d59f
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file highscore_gui.cpp Definition of the HighScore and EndGame windows */
12 #include "stdafx.h"
13 #include "highscore.h"
14 #include "table/strings.h"
15 #include "gfx_func.h"
16 #include "table/sprites.h"
17 #include "window_gui.h"
18 #include "window_func.h"
19 #include "network/network.h"
20 #include "command_func.h"
21 #include "company_func.h"
22 #include "company_base.h"
23 #include "strings_func.h"
24 #include "hotkeys.h"
26 #include "widgets/highscore_widget.h"
28 struct EndGameHighScoreBaseWindow : Window {
29 uint32 background_img;
30 int8 rank;
32 EndGameHighScoreBaseWindow (const WindowDesc *desc)
33 : Window (desc), background_img (0), rank (0)
35 this->InitNested();
36 CLRBITS(this->flags, WF_WHITE_BORDER);
37 ResizeWindow(this, _screen.width - this->width, _screen.height - this->height);
40 /* Always draw a maximized window and within it the centered background */
41 void SetupHighScoreEndWindow()
43 /* Resize window to "full-screen". */
44 if (this->width != _screen.width || this->height != _screen.height) ResizeWindow(this, _screen.width - this->width, _screen.height - this->height);
46 this->DrawWidgets();
48 /* Standard background slices are 50 pixels high, but it's designed
49 * for 480 pixels total. 96% of 500 is 480. */
50 Dimension dim = GetSpriteSize(this->background_img);
51 Point pt = this->GetTopLeft(dim.width, dim.height * 96 / 10);
52 /* Center Highscore/Endscreen background */
53 for (uint i = 0; i < 10; i++) { // the image is split into 10 50px high parts
54 DrawSprite(this->background_img + i, PAL_NONE, pt.x, pt.y + (i * dim.height));
58 /** Return the coordinate of the screen such that a window of 640x480 is centered at the screen. */
59 Point GetTopLeft(int x, int y)
61 Point pt = {max(0, (_screen.width / 2) - (x / 2)), max(0, (_screen.height / 2) - (y / 2))};
62 return pt;
65 virtual void OnClick(Point pt, int widget, int click_count)
67 this->Delete();
70 virtual EventState OnKeyPress(WChar key, uint16 keycode)
72 /* All keys are 'handled' by this window but we want to make
73 * sure that 'quit' still works correctly. Not handling the
74 * quit key is enough so the main toolbar can handle it. */
75 if (IsQuitKey(keycode)) return ES_NOT_HANDLED;
77 switch (keycode) {
78 /* Keys for telling we want to go on */
79 case WKC_RETURN:
80 case WKC_ESC:
81 case WKC_SPACE:
82 this->Delete();
83 return ES_HANDLED;
85 default:
86 /* We want to handle all keys; we don't want windows in
87 * the background to open. Especially the ones that do
88 * locate themselves based on the status-/toolbars. */
89 return ES_HANDLED;
94 /** End game window shown at the end of the game */
95 struct EndGameWindow : EndGameHighScoreBaseWindow {
96 EndGameWindow (const WindowDesc *desc) : EndGameHighScoreBaseWindow(desc)
98 /* Pause in single-player to have a look at the highscore at your own leisure */
99 if (!_networking) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
101 this->background_img = SPR_TYCOON_IMG1_BEGIN;
103 if (_local_company != COMPANY_SPECTATOR) {
104 const Company *c = Company::Get(_local_company);
105 if (c->old_economy[0].performance_history == SCORE_MAX) {
106 this->background_img = SPR_TYCOON_IMG2_BEGIN;
110 /* In a network game show the endscores of the custom difficulty 'network' which is
111 * a TOP5 of that game, and not an all-time TOP5. */
112 if (_networking) {
113 this->window_number = SP_MULTIPLAYER;
114 this->rank = SaveHighScoreValueNetwork();
115 } else {
116 /* in single player _local company is always valid */
117 const Company *c = Company::Get(_local_company);
118 this->window_number = SP_CUSTOM;
119 this->rank = SaveHighScoreValue(c);
122 MarkWholeScreenDirty();
125 void OnDelete (void) FINAL_OVERRIDE
127 if (!_networking) DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE); // unpause
128 ShowHighscoreTable(this->window_number, this->rank);
131 virtual void OnPaint()
133 this->SetupHighScoreEndWindow();
134 Point pt = this->GetTopLeft(640, 480);
136 const Company *c = Company::GetIfValid(_local_company);
137 if (c == NULL) return;
139 /* We need to get performance from last year because the image is shown
140 * at the start of the new year when these things have already been copied */
141 if (this->background_img == SPR_TYCOON_IMG2_BEGIN) { // Tycoon of the century \o/
142 SetDParam(0, c->index);
143 SetDParam(1, c->index);
144 SetDParam(2, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history));
145 DrawStringMultiLine(pt.x + 15, pt.x + 640 - 25, pt.y + 90, pt.y + 160, STR_HIGHSCORE_PRESIDENT_OF_COMPANY_ACHIEVES_STATUS, TC_FROMSTRING, SA_CENTER);
146 } else {
147 SetDParam(0, c->index);
148 SetDParam(1, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history));
149 DrawStringMultiLine(pt.x + 36, pt.x + 640, pt.y + 140, pt.y + 206, STR_HIGHSCORE_COMPANY_ACHIEVES_STATUS, TC_FROMSTRING, SA_CENTER);
154 struct HighScoreWindow : EndGameHighScoreBaseWindow {
155 bool game_paused_by_player; ///< True if the game was paused by the player when the highscore window was opened.
157 HighScoreWindow (const WindowDesc *desc, int difficulty, int8 ranking)
158 : EndGameHighScoreBaseWindow (desc),
159 game_paused_by_player (_pause_mode == PM_PAUSED_NORMAL)
161 /* pause game to show the chart */
162 if (!_networking && !this->game_paused_by_player) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
164 /* Close all always on-top windows to get a clean screen */
165 if (_game_mode != GM_MENU) HideVitalWindows();
167 MarkWholeScreenDirty();
168 this->window_number = difficulty; // show highscore chart for difficulty...
169 this->background_img = SPR_HIGHSCORE_CHART_BEGIN; // which background to show
170 this->rank = ranking;
173 void OnDelete (void) FINAL_OVERRIDE
175 if (_game_mode != GM_MENU) ShowVitalWindows();
177 if (!_networking && !this->game_paused_by_player) DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE); // unpause
180 virtual void OnPaint()
182 const HighScore *hs = _highscore_table[this->window_number];
184 this->SetupHighScoreEndWindow();
185 Point pt = this->GetTopLeft(640, 480);
187 SetDParam(0, ORIGINAL_END_YEAR);
188 DrawStringMultiLine(pt.x + 70, pt.x + 570, pt.y, pt.y + 140, !_networking ? STR_HIGHSCORE_TOP_COMPANIES_WHO_REACHED : STR_HIGHSCORE_TOP_COMPANIES_NETWORK_GAME, TC_FROMSTRING, SA_CENTER);
190 /* Draw Highscore peepz */
191 for (uint8 i = 0; i < lengthof(_highscore_table[0]); i++) {
192 SetDParam(0, i + 1);
193 DrawString(pt.x + 40, pt.x + 600, pt.y + 140 + (i * 55), STR_HIGHSCORE_POSITION);
195 if (hs[i].company[0] != '\0') {
196 TextColour colour = (this->rank == i) ? TC_RED : TC_BLACK; // draw new highscore in red
198 SetDParamStr(0, hs[i].company);
199 DrawString(pt.x + 71, pt.x + 569, pt.y + 140 + (i * 55), STR_JUST_BIG_RAW_STRING, colour);
200 SetDParam(0, hs[i].title);
201 SetDParam(1, hs[i].score);
202 DrawString(pt.x + 71, pt.x + 569, pt.y + 140 + FONT_HEIGHT_LARGE + (i * 55), STR_HIGHSCORE_STATS, colour);
208 static const NWidgetPart _nested_highscore_widgets[] = {
209 NWidget(WWT_PANEL, COLOUR_BROWN, WID_H_BACKGROUND), SetMinimalSize(641, 481), SetResize(1, 1), EndContainer(),
212 static const WindowDesc _highscore_desc(
213 WDP_MANUAL, 0, 0,
214 WC_HIGHSCORE, WC_NONE,
216 _nested_highscore_widgets, lengthof(_nested_highscore_widgets)
219 static const WindowDesc _endgame_desc(
220 WDP_MANUAL, 0, 0,
221 WC_ENDSCREEN, WC_NONE,
223 _nested_highscore_widgets, lengthof(_nested_highscore_widgets)
227 * Show the highscore table for a given difficulty. When called from
228 * endgame ranking is set to the top5 element that was newly added
229 * and is thus highlighted
231 void ShowHighscoreTable(int difficulty, int8 ranking)
233 DeleteWindowByClass(WC_HIGHSCORE);
234 new HighScoreWindow(&_highscore_desc, difficulty, ranking);
238 * Show the endgame victory screen in 2050. Update the new highscore
239 * if it was high enough
241 void ShowEndGameChart()
243 /* Dedicated server doesn't need the highscore window and neither does -v null. */
244 if (_network_dedicated || (!_networking && !Company::IsValidID(_local_company))) return;
246 HideVitalWindows();
247 DeleteWindowByClass(WC_ENDSCREEN);
248 new EndGameWindow(&_endgame_desc);