OpenTTD Source: src/highscore_gui.cpp Source File

OpenTTD Source 12.2

highscore_gui.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10 #include "stdafx.h"
11 #include "highscore.h"
12 #include "table/strings.h"
13 #include "gfx_func.h"
14 #include "table/sprites.h"
15 #include "window_gui.h"
16 #include "window_func.h"
17 #include "network/network.h"
18 #include "command_func.h"
19 #include "company_func.h"
20 #include "company_base.h"
21 #include "strings_func.h"
22 #include "hotkeys.h"
23 #include "zoom_func.h"
24
25 #include "widgets/highscore_widget.h"
26
27 #include "safeguards.h"
28
29 struct EndGameHighScoreBaseWindow : Window {
30 uint32 background_img;
31 int8 rank;
32
33 EndGameHighScoreBaseWindow(WindowDesc *desc) : Window(desc)
34 {
35 this->InitNested();
36 CLRBITS(this->flags, WF_WHITE_BORDER);
37 ResizeWindow(this, _screen.width - this->width, _screen.height - this->height);
38 }
39
40 /* Always draw a maximized window and within it the centered background */
41 void SetupHighScoreEndWindow()
42 {
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);
45
46 this->DrawWidgets();
47
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));
55 }
56 }
57
59 Point GetTopLeft(int x, int y)
60 {
61 Point pt = {std::max(0, (_screen.width / 2) - (x / 2)), std::max(0, (_screen.height / 2) - (y / 2))};
62 return pt;
63 }
64
65 void OnClick(Point pt, int widget, int click_count) override
66 {
67 this->Close();
68 }
69
70 EventState OnKeyPress(WChar key, uint16 keycode) override
71 {
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;
76
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->Close();
83 return ES_HANDLED;
84
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;
90 }
91 }
92 };
93
95 struct EndGameWindow : EndGameHighScoreBaseWindow {
96 EndGameWindow(WindowDesc *desc) : EndGameHighScoreBaseWindow(desc)
97 {
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);
100
101 this->background_img = SPR_TYCOON_IMG1_BEGIN;
102
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;
107 }
108 }
109
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 singleplayer mode _local company is always valid */
117 const Company *c = Company::Get(_local_company);
118 this->window_number = SP_CUSTOM;
119 this->rank = SaveHighScoreValue(c);
120 }
121
122 MarkWholeScreenDirty();
123 }
124
125 void Close() override
126 {
127 if (!_networking) DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE); // unpause
128 ShowHighscoreTable(this->window_number, this->rank);
129 this->EndGameHighScoreBaseWindow::Close();
130 }
131
132 void OnPaint() override
133 {
134 this->SetupHighScoreEndWindow();
135 Point pt = this->GetTopLeft(ScaleGUITrad(640), ScaleGUITrad(480));
136
137 const Company *c = Company::GetIfValid(_local_company);
138 if (c == nullptr) return;
139
140 /* We need to get performance from last year because the image is shown
141 * at the start of the new year when these things have already been copied */
142 if (this->background_img == SPR_TYCOON_IMG2_BEGIN) { // Tycoon of the century \o/
143 SetDParam(0, c->index);
144 SetDParam(1, c->index);
145 SetDParam(2, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history));
146 DrawStringMultiLine(pt.x + ScaleGUITrad(15), pt.x + ScaleGUITrad(640) - ScaleGUITrad(25), pt.y + ScaleGUITrad(90), pt.y + ScaleGUITrad(160), STR_HIGHSCORE_PRESIDENT_OF_COMPANY_ACHIEVES_STATUS, TC_FROMSTRING, SA_CENTER);
147 } else {
148 SetDParam(0, c->index);
149 SetDParam(1, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history));
150 DrawStringMultiLine(pt.x + ScaleGUITrad(36), pt.x + ScaleGUITrad(640), pt.y + ScaleGUITrad(140), pt.y + ScaleGUITrad(206), STR_HIGHSCORE_COMPANY_ACHIEVES_STATUS, TC_FROMSTRING, SA_CENTER);
151 }
152 }
153 };
154
155 struct HighScoreWindow : EndGameHighScoreBaseWindow {
156 bool game_paused_by_player;
157
158 HighScoreWindow(WindowDesc *desc, int difficulty, int8 ranking) : EndGameHighScoreBaseWindow(desc)
159 {
160 /* pause game to show the chart */
161 this->game_paused_by_player = _pause_mode == PM_PAUSED_NORMAL;
162 if (!_networking && !this->game_paused_by_player) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
163
164 /* Close all always on-top windows to get a clean screen */
165 if (_game_mode != GM_MENU) HideVitalWindows();
166
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;
171 }
172
173 void Close() override
174 {
175 if (_game_mode != GM_MENU) ShowVitalWindows();
176
177 if (!_networking && !this->game_paused_by_player) DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE); // unpause
178
179 this->EndGameHighScoreBaseWindow::Close();
180 }
181
182 void OnPaint() override
183 {
184 const HighScore *hs = _highscore_table[this->window_number];
185
186 this->SetupHighScoreEndWindow();
187 Point pt = this->GetTopLeft(ScaleGUITrad(640), ScaleGUITrad(480));
188
189 SetDParam(0, _settings_game.game_creation.ending_year);
190 DrawStringMultiLine(pt.x + ScaleGUITrad(70), pt.x + ScaleGUITrad(570), pt.y, pt.y + ScaleGUITrad(140), !_networking ? STR_HIGHSCORE_TOP_COMPANIES_WHO_REACHED : STR_HIGHSCORE_TOP_COMPANIES_NETWORK_GAME, TC_FROMSTRING, SA_CENTER);
191
192 /* Draw Highscore peepz */
193 for (uint8 i = 0; i < lengthof(_highscore_table[0]); i++) {
194 SetDParam(0, i + 1);
195 DrawString(pt.x + ScaleGUITrad(40), pt.x + ScaleGUITrad(600), pt.y + ScaleGUITrad(140 + i * 55), STR_HIGHSCORE_POSITION);
196
197 if (hs[i].company[0] != '\0') {
198 TextColour colour = (this->rank == i) ? TC_RED : TC_BLACK; // draw new highscore in red
199
200 SetDParamStr(0, hs[i].company);
201 DrawString(pt.x + ScaleGUITrad(71), pt.x + ScaleGUITrad(569), pt.y + ScaleGUITrad(140 + i * 55), STR_JUST_BIG_RAW_STRING, colour);
202 SetDParam(0, hs[i].title);
203 SetDParam(1, hs[i].score);
204 DrawString(pt.x + ScaleGUITrad(71), pt.x + ScaleGUITrad(569), pt.y + ScaleGUITrad(140) + FONT_HEIGHT_LARGE + ScaleGUITrad(i * 55), STR_HIGHSCORE_STATS, colour);
205 }
206 }
207 }
208 };
209
210 static const NWidgetPart _nested_highscore_widgets[] = {
211 NWidget(WWT_PANEL, COLOUR_BROWN, WID_H_BACKGROUND), SetResize(1, 1), EndContainer(),
212 };
213
214 static WindowDesc _highscore_desc(
215 WDP_MANUAL, nullptr, 0, 0,
216 WC_HIGHSCORE, WC_NONE,
217 0,
218 _nested_highscore_widgets, lengthof(_nested_highscore_widgets)
219 );
220
221 static WindowDesc _endgame_desc(
222 WDP_MANUAL, nullptr, 0, 0,
223 WC_ENDSCREEN, WC_NONE,
224 0,
225 _nested_highscore_widgets, lengthof(_nested_highscore_widgets)
226 );
227
233 void ShowHighscoreTable(int difficulty, int8 ranking)
234 {
235 CloseWindowByClass(WC_HIGHSCORE);
236 new HighScoreWindow(&_highscore_desc, difficulty, ranking);
237 }
238
243 void ShowEndGameChart()
244 {
245 /* Dedicated server doesn't need the highscore window and neither does -v null. */
246 if (_network_dedicated || (!_networking && !Company::IsValidID(_local_company))) return;
247
248 HideVitalWindows();
249 CloseWindowByClass(WC_ENDSCREEN);
250 new EndGameWindow(&_endgame_desc);
251 }
ES_HANDLED
@ ES_HANDLED
The passed event is handled.
Definition: window_type.h:718
HideVitalWindows
void HideVitalWindows()
Close all always on-top windows to get an empty screen.
Definition: window.cpp:3340
Pool::PoolItem<&_company_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:337
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:35
HighScoreWindow::game_paused_by_player
bool game_paused_by_player
True if the game was paused by the player when the highscore window was opened.
Definition: highscore_gui.cpp:156
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
command_func.h
Pool::PoolItem<&_company_pool >::GetIfValid
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:348
company_base.h
HighScore
Definition: highscore.h:17
SaveHighScoreValue
int8 SaveHighScoreValue(const Company *c)
Save the highscore for the company.
Definition: highscore.cpp:52
HighScoreWindow::Close
void Close() override
Hide the window and all its child windows, and mark them for a later deletion.
Definition: highscore_gui.cpp:173
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:235
highscore_widget.h
SP_MULTIPLAYER
@ SP_MULTIPLAYER
Special "multiplayer" highscore. Not saved, always specific to the current game.
Definition: settings_type.h:48
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:250
SetResize
static NWidgetPart SetResize(int16 dx, int16 dy)
Widget part function for setting the resize step.
Definition: widget_type.h:993
zoom_func.h
DrawString
int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition: gfx.cpp:643
WID_H_BACKGROUND
@ WID_H_BACKGROUND
Background of the window.
Definition: highscore_widget.h:15
Window::Window
Window(WindowDesc *desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition: window.cpp:1799
SetDParam
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:196
NWidgetPart
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:971
EndGameHighScoreBaseWindow::OnClick
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition: highscore_gui.cpp:65
HighScoreWindow
Definition: highscore_gui.cpp:155
DrawStringMultiLine
int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly over multiple lines.
Definition: gfx.cpp:788
GameSettings::game_creation
GameCreationSettings game_creation
settings used during the creation of a game (map)
Definition: settings_type.h:577
WC_HIGHSCORE
@ WC_HIGHSCORE
Highscore; Window numbers:
Definition: window_type.h:641
gfx_func.h
_highscore_table
HighScore _highscore_table[SP_HIGHSCORE_END][5]
various difficulty-settings; top 5
Definition: highscore.cpp:22
WindowDesc
High level window description.
Definition: window_gui.h:168
window_gui.h
ShowVitalWindows
void ShowVitalWindows()
Show the vital in-game windows.
Definition: main_gui.cpp:559
CompanyEconomyEntry::performance_history
int32 performance_history
Company score (scale 0-1000)
Definition: company_base.h:26
DoCommandP
bool DoCommandP(const CommandContainer *container, bool my_cmd)
Shortcut for the long DoCommandP when having a container with the data.
Definition: command.cpp:541
Window::InitNested
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition: window.cpp:1789
CMD_PAUSE
@ CMD_PAUSE
pause the game
Definition: command_type.h:256
WF_WHITE_BORDER
@ WF_WHITE_BORDER
Window white border counter bit mask.
Definition: window_gui.h:242
highscore.h
ES_NOT_HANDLED
@ ES_NOT_HANDLED
The passed event is not handled.
Definition: window_type.h:719
SP_CUSTOM
@ SP_CUSTOM
No profile, special "custom" highscore.
Definition: settings_type.h:45
_pause_mode
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:48
EndGameHighScoreBaseWindow
Definition: highscore_gui.cpp:29
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:53
_local_company
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
safeguards.h
Window::flags
WindowFlags flags
Window flags.
Definition: window_gui.h:305
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:57
FONT_HEIGHT_LARGE
#define FONT_HEIGHT_LARGE
Height of characters in the large (FS_LARGE) font.
Definition: gfx_func.h:170
DrawSprite
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
Draw a sprite, not in a viewport.
Definition: gfx.cpp:1042
sprites.h
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
_network_dedicated
bool _network_dedicated
are we a dedicated server?
Definition: network.cpp:60
stdafx.h
Window::window_number
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:307
EndGameWindow::OnPaint
void OnPaint() override
The window must be repainted.
Definition: highscore_gui.cpp:132
SCORE_MAX
@ SCORE_MAX
The max score that can be in the performance history.
Definition: economy_type.h:59
WC_NONE
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:37
GameCreationSettings::ending_year
Year ending_year
scoring end date
Definition: settings_type.h:307
EndGameHighScoreBaseWindow::OnKeyPress
EventState OnKeyPress(WChar key, uint16 keycode) override
A key has been pressed.
Definition: highscore_gui.cpp:70
ShowEndGameChart
void ShowEndGameChart()
Show the endgame victory screen in 2050.
Definition: highscore_gui.cpp:243
GetSpriteSize
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
Definition: gfx.cpp:977
EndGameWindow::Close
void Close() override
Hide the window and all its child windows, and mark them for a later deletion.
Definition: highscore_gui.cpp:125
EndContainer
static NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
Definition: widget_type.h:1092
strings_func.h
EndGameHighScoreBaseWindow::GetTopLeft
Point GetTopLeft(int x, int y)
Return the coordinate of the screen such that a window of 640x480 is centered at the screen.
Definition: highscore_gui.cpp:59
IsQuitKey
bool IsQuitKey(uint16 keycode)
Does the given keycode match one of the keycodes bound to 'quit game'?
Definition: main_gui.cpp:519
ScaleGUITrad
static int ScaleGUITrad(int value)
Scale traditional pixel dimensions to GUI zoom level.
Definition: zoom_func.h:76
NWidget
static NWidgetPart NWidget(WidgetType tp, Colours col, int16 idx=-1)
Widget part function for starting a new 'real' widget.
Definition: widget_type.h:1207
COMPANY_SPECTATOR
@ COMPANY_SPECTATOR
The client is spectating.
Definition: company_type.h:35
CloseWindowByClass
void CloseWindowByClass(WindowClass cls)
Close all windows of a given class.
Definition: window.cpp:1188
HighScoreWindow::OnPaint
void OnPaint() override
The window must be repainted.
Definition: highscore_gui.cpp:182
WWT_PANEL
@ WWT_PANEL
Simple depressed panel.
Definition: widget_type.h:48
PM_PAUSED_NORMAL
@ PM_PAUSED_NORMAL
A game normally paused.
Definition: openttd.h:62
EventState
EventState
State of handling an event.
Definition: window_type.h:717
EndGameWindow
End game window shown at the end of the game.
Definition: highscore_gui.cpp:95
WC_ENDSCREEN
@ WC_ENDSCREEN
Endscreen; Window numbers:
Definition: window_type.h:647
company_func.h
network.h
window_func.h
SA_CENTER
@ SA_CENTER
Center both horizontally and vertically.
Definition: gfx_type.h:338
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:378
Window::width
int width
width of the window (number of pixels to the right in x direction)
Definition: window_gui.h:314
MarkWholeScreenDirty
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1751
WDP_MANUAL
@ WDP_MANUAL
Manually align the window (so no automatic location finding)
Definition: window_gui.h:155
SaveHighScoreValueNetwork
int8 SaveHighScoreValueNetwork()
Save the highscores in a network game when it has ended.
Definition: highscore.cpp:88
Window
Data structure for an opened window.
Definition: window_gui.h:279
Pool::PoolItem<&_company_pool >::IsValidID
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:326
Window::DrawWidgets
void DrawWidgets() const
Paint all widgets of a window.
Definition: widget.cpp:636
ShowHighscoreTable
void ShowHighscoreTable(int difficulty, int8 ranking)
Show the highscore table for a given difficulty.
Definition: highscore_gui.cpp:233
Company
Definition: company_base.h:115
CompanyProperties::old_economy
CompanyEconomyEntry old_economy[MAX_HISTORY_QUARTERS]
Economic data of the company of the last MAX_HISTORY_QUARTERS quarters.
Definition: company_base.h:98
CLRBITS
#define CLRBITS(x, y)
Clears several bits in a variable.
Definition: bitmath_func.hpp:166
SetDParamStr
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:296
hotkeys.h
ResizeWindow
void ResizeWindow(Window *w, int delta_x, int delta_y, bool clamp_to_screen)
Resize the window.
Definition: window.cpp:2069
Window::Close
virtual void Close()
Hide the window and all its child windows, and mark them for a later deletion.
Definition: window.cpp:1092