Codechange: [Win32] simplify when/where GdiFlush() is called
[openttd-github.git] / src / bootstrap_gui.cpp
blob7ec391b47da120ee8927118b4aa0e7aac88bc963
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 */
8 /** @file bootstrap_gui.cpp Barely used user interface for bootstrapping OpenTTD, i.e. downloading the required content. */
10 #include "stdafx.h"
11 #include "base_media_base.h"
12 #include "blitter/factory.hpp"
14 #if defined(WITH_FREETYPE) || defined(WITH_UNISCRIBE) || defined(WITH_COCOA)
16 #include "core/geometry_func.hpp"
17 #include "fontcache.h"
18 #include "gfx_func.h"
19 #include "network/network.h"
20 #include "network/network_content_gui.h"
21 #include "openttd.h"
22 #include "strings_func.h"
23 #include "video/video_driver.hpp"
24 #include "window_func.h"
26 #include "widgets/bootstrap_widget.h"
28 #include "table/strings.h"
30 #include "safeguards.h"
32 /** Widgets for the background window to prevent smearing. */
33 static const struct NWidgetPart _background_widgets[] = {
34 NWidget(WWT_PANEL, COLOUR_DARK_BLUE, WID_BB_BACKGROUND), SetResize(1, 1),
37 /**
38 * Window description for the background window to prevent smearing.
40 static WindowDesc _background_desc(
41 WDP_MANUAL, nullptr, 0, 0,
42 WC_BOOTSTRAP, WC_NONE,
44 _background_widgets, lengthof(_background_widgets)
47 /** The background for the game. */
48 class BootstrapBackground : public Window {
49 public:
50 BootstrapBackground() : Window(&_background_desc)
52 this->InitNested(0);
53 CLRBITS(this->flags, WF_WHITE_BORDER);
54 ResizeWindow(this, _screen.width, _screen.height);
57 void DrawWidget(const Rect &r, int widget) const override
59 GfxFillRect(r.left, r.top, r.right, r.bottom, 4, FILLRECT_OPAQUE);
60 GfxFillRect(r.left, r.top, r.right, r.bottom, 0, FILLRECT_CHECKER);
64 /** Nested widgets for the download window. */
65 static const NWidgetPart _nested_boostrap_download_status_window_widgets[] = {
66 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CONTENT_DOWNLOAD_TITLE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
67 NWidget(WWT_PANEL, COLOUR_GREY, WID_NCDS_BACKGROUND),
68 NWidget(NWID_SPACER), SetMinimalSize(350, 0), SetMinimalTextLines(3, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 30),
69 EndContainer(),
72 /** Window description for the download window */
73 static WindowDesc _bootstrap_download_status_window_desc(
74 WDP_CENTER, nullptr, 0, 0,
75 WC_NETWORK_STATUS_WINDOW, WC_NONE,
76 WDF_MODAL,
77 _nested_boostrap_download_status_window_widgets, lengthof(_nested_boostrap_download_status_window_widgets)
81 /** Window for showing the download status of content */
82 struct BootstrapContentDownloadStatusWindow : public BaseNetworkContentDownloadStatusWindow {
83 public:
84 /** Simple call the constructor of the superclass. */
85 BootstrapContentDownloadStatusWindow() : BaseNetworkContentDownloadStatusWindow(&_bootstrap_download_status_window_desc)
89 void OnDownloadComplete(ContentID cid) override
91 /* We have completed downloading. We can trigger finding the right set now. */
92 BaseGraphics::FindSets();
94 /* And continue going into the menu. */
95 _game_mode = GM_MENU;
97 /* _exit_game is used to break out of the outer video driver's MainLoop. */
98 _exit_game = true;
99 delete this;
103 /** The widgets for the query. It has no close box as that sprite does not exist yet. */
104 static const NWidgetPart _bootstrap_query_widgets[] = {
105 NWidget(NWID_HORIZONTAL),
106 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_MISSING_GRAPHICS_SET_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
107 EndContainer(),
108 NWidget(WWT_PANEL, COLOUR_GREY),
109 NWidget(WWT_PANEL, COLOUR_GREY, WID_BAFD_QUESTION), EndContainer(),
110 NWidget(NWID_HORIZONTAL),
111 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_BAFD_YES), SetDataTip(STR_MISSING_GRAPHICS_YES_DOWNLOAD, STR_NULL),
112 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_BAFD_NO), SetDataTip(STR_MISSING_GRAPHICS_NO_QUIT, STR_NULL),
113 EndContainer(),
114 EndContainer(),
117 /** The window description for the query. */
118 static WindowDesc _bootstrap_query_desc(
119 WDP_CENTER, nullptr, 0, 0,
120 WC_CONFIRM_POPUP_QUERY, WC_NONE,
122 _bootstrap_query_widgets, lengthof(_bootstrap_query_widgets)
125 /** The window for the query. It can't use the generic query window as that uses sprites that don't exist yet. */
126 class BootstrapAskForDownloadWindow : public Window, ContentCallback {
127 Dimension button_size; ///< The dimension of the button
129 public:
130 /** Start listening to the content client events. */
131 BootstrapAskForDownloadWindow() : Window(&_bootstrap_query_desc)
133 this->InitNested(WN_CONFIRM_POPUP_QUERY_BOOTSTRAP);
134 _network_content_client.AddCallback(this);
137 /** Stop listening to the content client events. */
138 ~BootstrapAskForDownloadWindow()
140 _network_content_client.RemoveCallback(this);
143 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
145 /* We cache the button size. This is safe as no reinit can happen here. */
146 if (this->button_size.width == 0) {
147 this->button_size = maxdim(GetStringBoundingBox(STR_MISSING_GRAPHICS_YES_DOWNLOAD), GetStringBoundingBox(STR_MISSING_GRAPHICS_NO_QUIT));
148 this->button_size.width += WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
149 this->button_size.height += WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM;
152 switch (widget) {
153 case WID_BAFD_QUESTION:
154 /* The question is twice as wide as the buttons, and determine the height based on the width. */
155 size->width = this->button_size.width * 2;
156 size->height = GetStringHeight(STR_MISSING_GRAPHICS_SET_MESSAGE, size->width - WD_FRAMETEXT_LEFT - WD_FRAMETEXT_RIGHT) + WD_FRAMETEXT_BOTTOM + WD_FRAMETEXT_TOP;
157 break;
159 case WID_BAFD_YES:
160 case WID_BAFD_NO:
161 *size = this->button_size;
162 break;
166 void DrawWidget(const Rect &r, int widget) const override
168 if (widget != 0) return;
170 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top + WD_FRAMETEXT_TOP, r.bottom - WD_FRAMETEXT_BOTTOM, STR_MISSING_GRAPHICS_SET_MESSAGE, TC_FROMSTRING, SA_CENTER);
173 void OnClick(Point pt, int widget, int click_count) override
175 switch (widget) {
176 case WID_BAFD_YES:
177 /* We got permission to connect! Yay! */
178 _network_content_client.Connect();
179 break;
181 case WID_BAFD_NO:
182 _exit_game = true;
183 break;
185 default:
186 break;
190 void OnConnect(bool success) override
192 /* Once connected, request the metadata. */
193 _network_content_client.RequestContentList(CONTENT_TYPE_BASE_GRAPHICS);
196 void OnReceiveContentInfo(const ContentInfo *ci) override
198 /* And once the meta data is received, start downloading it. */
199 _network_content_client.Select(ci->id);
200 new BootstrapContentDownloadStatusWindow();
201 delete this;
205 #endif /* defined(WITH_FREETYPE) */
208 * Handle all procedures for bootstrapping OpenTTD without a base graphics set.
209 * This requires all kinds of trickery that is needed to avoid the use of
210 * sprites from the base graphics set which are pretty interwoven.
211 * @return True if a base set exists, otherwise false.
213 bool HandleBootstrap()
215 if (BaseGraphics::GetUsedSet() != nullptr) return true;
217 /* No user interface, bail out with an error. */
218 if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 0) goto failure;
220 /* If there is no network or no freetype, then there is nothing we can do. Go straight to failure. */
221 #if (defined(_WIN32) && defined(WITH_UNISCRIBE)) || (defined(WITH_FREETYPE) && (defined(WITH_FONTCONFIG) || defined(__APPLE__))) || defined(WITH_COCOA)
222 if (!_network_available) goto failure;
224 /* First tell the game we're bootstrapping. */
225 _game_mode = GM_BOOTSTRAP;
227 /* Initialise the freetype font code. */
228 InitializeUnicodeGlyphMap();
229 /* Next "force" finding a suitable freetype font as the local font is missing. */
230 CheckForMissingGlyphs(false);
232 /* Initialise the palette. The biggest step is 'faking' some recolour sprites.
233 * This way the mauve and gray colours work and we can show the user interface. */
234 GfxInitPalettes();
235 static const int offsets[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80, 0, 0, 0, 0x04, 0x08 };
236 for (uint i = 0; i != 16; i++) {
237 for (int j = 0; j < 8; j++) {
238 _colour_gradient[i][j] = offsets[i] + j;
242 /* Finally ask the question. */
243 new BootstrapBackground();
244 new BootstrapAskForDownloadWindow();
246 /* Process the user events. */
247 VideoDriver::GetInstance()->MainLoop();
249 /* _exit_game is used to get out of the video driver's main loop.
250 * In case GM_BOOTSTRAP is still set we did not exit it via the
251 * "download complete" event, so it was a manual exit. Obey it. */
252 _exit_game = _game_mode == GM_BOOTSTRAP;
253 if (_exit_game) return false;
255 /* Try to probe the graphics. Should work this time. */
256 if (!BaseGraphics::SetSet({})) goto failure;
258 /* Finally we can continue heading for the menu. */
259 _game_mode = GM_MENU;
260 return true;
261 #endif
263 /* Failure to get enough working to get a graphics set. */
264 failure:
265 usererror("Failed to find a graphics set. Please acquire a graphics set for OpenTTD. See section 1.4 of README.md.");
266 return false;