1 /*****************************************************************************
2  * Copyright (c) 2014-2020 OpenRCT2 developers
3  *
4  * For a complete list of all authors, please refer to contributors.md
5  * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
6  *
7  * OpenRCT2 is licensed under the GNU General Public License version 3.
8  *****************************************************************************/
9 
10 #include "TitleScreen.h"
11 
12 #include "../Context.h"
13 #include "../Game.h"
14 #include "../GameState.h"
15 #include "../Input.h"
16 #include "../OpenRCT2.h"
17 #include "../Version.h"
18 #include "../audio/audio.h"
19 #include "../config/Config.h"
20 #include "../core/Console.hpp"
21 #include "../drawing/Drawing.h"
22 #include "../interface/Screenshot.h"
23 #include "../interface/Viewport.h"
24 #include "../interface/Window.h"
25 #include "../localisation/Localisation.h"
26 #include "../network/NetworkBase.h"
27 #include "../network/network.h"
28 #include "../scenario/Scenario.h"
29 #include "../scenario/ScenarioRepository.h"
30 #include "../ui/UiContext.h"
31 #include "../util/Util.h"
32 #include "TitleSequence.h"
33 #include "TitleSequenceManager.h"
34 #include "TitleSequencePlayer.h"
35 
36 using namespace OpenRCT2;
37 
38 // TODO Remove when no longer required.
39 bool gPreviewingTitleSequenceInGame;
40 static TitleScreen* _singleton = nullptr;
41 
TitleScreen(GameState & gameState)42 TitleScreen::TitleScreen(GameState& gameState)
43     : _gameState(gameState)
44 {
45     _singleton = this;
46 }
47 
~TitleScreen()48 TitleScreen::~TitleScreen()
49 {
50     _singleton = nullptr;
51 }
52 
GetSequencePlayer()53 ITitleSequencePlayer* TitleScreen::GetSequencePlayer()
54 {
55     return _sequencePlayer;
56 }
57 
GetCurrentSequence()58 size_t TitleScreen::GetCurrentSequence()
59 {
60     return _currentSequence;
61 }
62 
PreviewSequence(size_t value)63 bool TitleScreen::PreviewSequence(size_t value)
64 {
65     _currentSequence = value;
66     _previewingSequence = TryLoadSequence(true);
67     if (_previewingSequence)
68     {
69         if (!(gScreenFlags & SCREEN_FLAGS_TITLE_DEMO))
70         {
71             gPreviewingTitleSequenceInGame = true;
72         }
73     }
74     else
75     {
76         _currentSequence = title_get_config_sequence();
77         if (gScreenFlags & SCREEN_FLAGS_TITLE_DEMO)
78         {
79             TryLoadSequence();
80         }
81     }
82     return _previewingSequence;
83 }
84 
StopPreviewingSequence()85 void TitleScreen::StopPreviewingSequence()
86 {
87     if (_previewingSequence)
88     {
89         rct_window* mainWindow = window_get_main();
90         if (mainWindow != nullptr)
91         {
92             window_unfollow_sprite(mainWindow);
93         }
94         _previewingSequence = false;
95         _currentSequence = title_get_config_sequence();
96         gPreviewingTitleSequenceInGame = false;
97     }
98 }
99 
IsPreviewingSequence()100 bool TitleScreen::IsPreviewingSequence()
101 {
102     return _previewingSequence;
103 }
104 
ShouldHideVersionInfo()105 bool TitleScreen::ShouldHideVersionInfo()
106 {
107     return _hideVersionInfo;
108 }
109 
SetHideVersionInfo(bool value)110 void TitleScreen::SetHideVersionInfo(bool value)
111 {
112     _hideVersionInfo = value;
113 }
114 
Load()115 void TitleScreen::Load()
116 {
117     log_verbose("TitleScreen::Load()");
118 
119     if (game_is_paused())
120     {
121         pause_toggle();
122     }
123 
124     gScreenFlags = SCREEN_FLAGS_TITLE_DEMO;
125     gScreenAge = 0;
126     gCurrentLoadedPath = "";
127 
128 #ifndef DISABLE_NETWORK
129     GetContext()->GetNetwork().Close();
130 #endif
131     OpenRCT2::Audio::StopAll();
132     GetContext()->GetGameState()->InitAll(150);
133     viewport_init_all();
134     context_open_window(WC_MAIN_WINDOW);
135     CreateWindows();
136     TitleInitialise();
137     OpenRCT2::Audio::PlayTitleMusic();
138 
139     if (gOpenRCT2ShowChangelog)
140     {
141         gOpenRCT2ShowChangelog = false;
142         context_open_window(WC_CHANGELOG);
143     }
144 
145     if (_sequencePlayer != nullptr)
146     {
147         _sequencePlayer->Begin(_currentSequence);
148 
149         // Force the title sequence to load / update so we
150         // don't see a blank screen for a split second.
151         TryLoadSequence();
152         _sequencePlayer->Update();
153     }
154 
155     log_verbose("TitleScreen::Load() finished");
156 }
157 
Update()158 void TitleScreen::Update()
159 {
160     gInUpdateCode = true;
161 
162     screenshot_check();
163     title_handle_keyboard_input();
164 
165     if (game_is_not_paused())
166     {
167         TryLoadSequence();
168         _sequencePlayer->Update();
169 
170         int32_t numUpdates = 1;
171         if (gGameSpeed > 1)
172         {
173             numUpdates = 1 << (gGameSpeed - 1);
174         }
175         for (int32_t i = 0; i < numUpdates; i++)
176         {
177             _gameState.UpdateLogic();
178         }
179         update_palette_effects();
180         // update_weather_animation();
181     }
182 
183     input_set_flag(INPUT_FLAG_VIEWPORT_SCROLLING, false);
184 
185     context_update_map_tooltip();
186     window_dispatch_update_all();
187 
188     gSavedAge++;
189 
190     context_handle_input();
191 
192     gInUpdateCode = false;
193 }
194 
ChangePresetSequence(size_t preset)195 void TitleScreen::ChangePresetSequence(size_t preset)
196 {
197     size_t count = TitleSequenceManager::GetCount();
198     if (preset >= count)
199     {
200         return;
201     }
202 
203     const utf8* configId = title_sequence_manager_get_config_id(preset);
204     SafeFree(gConfigInterface.current_title_sequence_preset);
205     gConfigInterface.current_title_sequence_preset = _strdup(configId);
206 
207     if (!_previewingSequence)
208         _currentSequence = preset;
209     window_invalidate_all();
210 }
211 
212 /**
213  * Creates the windows shown on the title screen; New game, load game,
214  * tutorial, toolbox and exit.
215  */
CreateWindows()216 void TitleScreen::CreateWindows()
217 {
218     context_open_window(WC_TITLE_MENU);
219     context_open_window(WC_TITLE_EXIT);
220     context_open_window(WC_TITLE_OPTIONS);
221     context_open_window(WC_TITLE_LOGO);
222     window_resize_gui(context_get_width(), context_get_height());
223     _hideVersionInfo = false;
224 }
225 
TitleInitialise()226 void TitleScreen::TitleInitialise()
227 {
228     if (_sequencePlayer == nullptr)
229     {
230         _sequencePlayer = GetContext()->GetUiContext()->GetTitleSequencePlayer();
231     }
232     if (gConfigInterface.random_title_sequence)
233     {
234         bool RCT1Installed = false, RCT1AAInstalled = false, RCT1LLInstalled = false;
235         int RCT1Count = 0;
236         size_t scenarioCount = scenario_repository_get_count();
237 
238         for (size_t s = 0; s < scenarioCount; s++)
239         {
240             if (scenario_repository_get_by_index(s)->source_game == ScenarioSource::RCT1)
241             {
242                 RCT1Count++;
243             }
244             if (scenario_repository_get_by_index(s)->source_game == ScenarioSource::RCT1_AA)
245             {
246                 RCT1AAInstalled = true;
247             }
248             if (scenario_repository_get_by_index(s)->source_game == ScenarioSource::RCT1_LL)
249             {
250                 RCT1LLInstalled = true;
251             }
252         }
253 
254         // Mega Park can show up in the scenario list even if RCT1 has been uninstalled, so it must be greater than 1
255         if (RCT1Count > 1)
256         {
257             RCT1Installed = true;
258         }
259 
260         int32_t random = 0;
261         bool safeSequence = false;
262         std::string RCT1String = format_string(STR_TITLE_SEQUENCE_RCT1, nullptr);
263         std::string RCT1AAString = format_string(STR_TITLE_SEQUENCE_RCT1_AA, nullptr);
264         std::string RCT1LLString = format_string(STR_TITLE_SEQUENCE_RCT1_AA_LL, nullptr);
265 
266         // Ensure the random sequence chosen isn't from RCT1 or expansion if the player doesn't have it installed
267         while (!safeSequence)
268         {
269             size_t total = TitleSequenceManager::GetCount();
270             random = util_rand() % static_cast<int32_t>(total);
271             const utf8* scName = title_sequence_manager_get_name(random);
272             safeSequence = true;
273             if (scName == RCT1String)
274             {
275                 safeSequence = RCT1Installed;
276             }
277             if (scName == RCT1AAString)
278             {
279                 safeSequence = RCT1AAInstalled;
280             }
281             if (scName == RCT1LLString)
282             {
283                 safeSequence = RCT1LLInstalled;
284             }
285         }
286         ChangePresetSequence(random);
287     }
288     size_t seqId = title_get_config_sequence();
289     if (seqId == SIZE_MAX)
290     {
291         seqId = title_sequence_manager_get_index_for_config_id("*OPENRCT2");
292         if (seqId == SIZE_MAX)
293         {
294             seqId = 0;
295         }
296     }
297     ChangePresetSequence(static_cast<int32_t>(seqId));
298 }
299 
TryLoadSequence(bool loadPreview)300 bool TitleScreen::TryLoadSequence(bool loadPreview)
301 {
302     if (_loadedTitleSequenceId != _currentSequence || loadPreview)
303     {
304         if (_sequencePlayer == nullptr)
305         {
306             _sequencePlayer = GetContext()->GetUiContext()->GetTitleSequencePlayer();
307         }
308 
309         size_t numSequences = TitleSequenceManager::GetCount();
310         if (numSequences > 0)
311         {
312             size_t targetSequence = _currentSequence;
313             do
314             {
315                 if (_sequencePlayer->Begin(targetSequence) && _sequencePlayer->Update())
316                 {
317                     _loadedTitleSequenceId = targetSequence;
318                     if (targetSequence != _currentSequence && !loadPreview)
319                     {
320                         // Forcefully change the preset to a preset that works.
321                         const utf8* configId = title_sequence_manager_get_config_id(targetSequence);
322                         SafeFree(gConfigInterface.current_title_sequence_preset);
323                         gConfigInterface.current_title_sequence_preset = _strdup(configId);
324                     }
325                     _currentSequence = targetSequence;
326                     gfx_invalidate_screen();
327                     return true;
328                 }
329                 targetSequence = (targetSequence + 1) % numSequences;
330             } while (targetSequence != _currentSequence && !loadPreview);
331         }
332         Console::Error::WriteLine("Unable to play any title sequences.");
333         _sequencePlayer->Eject();
334         _currentSequence = SIZE_MAX;
335         _loadedTitleSequenceId = SIZE_MAX;
336         if (!loadPreview)
337         {
338             GetContext()->GetGameState()->InitAll(150);
339         }
340         return false;
341     }
342     return true;
343 }
344 
title_load()345 void title_load()
346 {
347     if (_singleton != nullptr)
348     {
349         _singleton->Load();
350     }
351 }
352 
title_create_windows()353 void title_create_windows()
354 {
355     if (_singleton != nullptr)
356     {
357         _singleton->CreateWindows();
358     }
359 }
360 
title_get_sequence_player()361 void* title_get_sequence_player()
362 {
363     void* result = nullptr;
364     if (_singleton != nullptr)
365     {
366         result = _singleton->GetSequencePlayer();
367     }
368     return result;
369 }
370 
title_sequence_change_preset(size_t preset)371 void title_sequence_change_preset(size_t preset)
372 {
373     if (_singleton != nullptr)
374     {
375         _singleton->ChangePresetSequence(preset);
376     }
377 }
378 
title_should_hide_version_info()379 bool title_should_hide_version_info()
380 {
381     bool result = false;
382     if (_singleton != nullptr)
383     {
384         result = _singleton->ShouldHideVersionInfo();
385     }
386     return result;
387 }
388 
title_set_hide_version_info(bool value)389 void title_set_hide_version_info(bool value)
390 {
391     if (_singleton != nullptr)
392     {
393         _singleton->SetHideVersionInfo(value);
394     }
395 }
396 
title_get_config_sequence()397 size_t title_get_config_sequence()
398 {
399     return title_sequence_manager_get_index_for_config_id(gConfigInterface.current_title_sequence_preset);
400 }
401 
title_get_current_sequence()402 size_t title_get_current_sequence()
403 {
404     size_t result = 0;
405     if (_singleton != nullptr)
406     {
407         result = _singleton->GetCurrentSequence();
408     }
409     return result;
410 }
411 
title_preview_sequence(size_t value)412 bool title_preview_sequence(size_t value)
413 {
414     if (_singleton != nullptr)
415     {
416         return _singleton->PreviewSequence(value);
417     }
418     return false;
419 }
420 
title_stop_previewing_sequence()421 void title_stop_previewing_sequence()
422 {
423     if (_singleton != nullptr)
424     {
425         _singleton->StopPreviewingSequence();
426     }
427 }
428 
title_is_previewing_sequence()429 bool title_is_previewing_sequence()
430 {
431     if (_singleton != nullptr)
432     {
433         return _singleton->IsPreviewingSequence();
434     }
435     return false;
436 }
437 
DrawOpenRCT2(rct_drawpixelinfo * dpi,const ScreenCoordsXY & screenCoords)438 void DrawOpenRCT2(rct_drawpixelinfo* dpi, const ScreenCoordsXY& screenCoords)
439 {
440     thread_local std::string buffer;
441     buffer.clear();
442     buffer.assign("{OUTLINE}{WHITE}");
443 
444     // Write name and version information
445     buffer += gVersionInfoFull;
446     gfx_draw_string(dpi, screenCoords + ScreenCoordsXY(5, 5 - 13), buffer.c_str(), { COLOUR_BLACK });
447 
448     // Invalidate screen area
449     int16_t width = static_cast<int16_t>(gfx_get_string_width(buffer, FontSpriteBase::MEDIUM));
450     gfx_set_dirty_blocks(
451         { screenCoords, screenCoords + ScreenCoordsXY{ width, 30 } }); // 30 is an arbitrary height to catch both strings
452 
453     // Write platform information
454     buffer.assign("{OUTLINE}{WHITE}");
455     buffer.append(OPENRCT2_PLATFORM);
456     buffer.append(" (");
457     buffer.append(OPENRCT2_ARCHITECTURE);
458     buffer.append(")");
459     gfx_draw_string(dpi, screenCoords + ScreenCoordsXY(5, 5), buffer.c_str(), { COLOUR_BLACK });
460 }
461