OpenRCT2/src/openrct2/scenes/title/TitleScene.cpp

465 lines
13 KiB
C++
Raw Normal View History

/*****************************************************************************
* Copyright (c) 2014-2024 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "TitleScene.h"
#include "../../Context.h"
#include "../../Game.h"
#include "../../GameState.h"
#include "../../Input.h"
#include "../../OpenRCT2.h"
#include "../../Version.h"
#include "../../audio/audio.h"
#include "../../config/Config.h"
#include "../../core/Console.hpp"
#include "../../drawing/Drawing.h"
#include "../../interface/Screenshot.h"
#include "../../interface/Viewport.h"
#include "../../interface/Window.h"
#include "../../localisation/Localisation.h"
#include "../../network/NetworkBase.h"
#include "../../network/network.h"
#include "../../scenario/Scenario.h"
#include "../../scenario/ScenarioRepository.h"
#include "../../ui/UiContext.h"
#include "../../util/Util.h"
2018-06-22 23:15:14 +02:00
#include "TitleSequence.h"
#include "TitleSequenceManager.h"
#include "TitleSequencePlayer.h"
using namespace OpenRCT2;
2017-07-23 00:42:14 +02:00
// TODO Remove when no longer required.
bool gPreviewingTitleSequenceInGame;
2017-07-23 00:42:14 +02:00
ITitleSequencePlayer* TitleScene::GetSequencePlayer()
2017-07-23 00:42:14 +02:00
{
return _sequencePlayer;
}
size_t TitleScene::GetCurrentSequence()
2017-07-23 00:42:14 +02:00
{
return _currentSequence;
}
bool TitleScene::PreviewSequence(size_t value)
2017-07-23 00:42:14 +02:00
{
_currentSequence = value;
_previewingSequence = TryLoadSequence(true);
if (_previewingSequence)
Feature: Preview title sequences in-game Title sequences can now be played back in-game, allowing for much easier editing. Improved title sequence playback in general. Clicking play while on a different title sequence will play the new one. Clicking stop will make the title screen go back to the config title sequence. And the closing the title sequence window will also make the game go back to the config title sequence, and reload the sequence if it was modified. Changes made to title sequences in-game are now correctly loaded in the title screen. Starting a title sequence within the editor will now always reset it even if it's the current playing sequence. (Not for playing in the editor though). Get Location in title sequence command editor now has 100% accuracy compared to before where it would usually get some offset value. Added `get_map_coordinates_from_pos_window` which will allow getting the viewport coordinates of a specific window even if the input coordinates are under another window. This has use with getting 2D positions from the main window without the other windows getting in the way. Options window will now always specify the config title sequence in the dropdown and not the current title sequence. Made a global variable `gLoadKeepWindowsOpen`, in game.h to keep windows open when loading a park. When loading a title sequence park in-game. The sequence player will force-close all park-specific windows ahead of time. Skipping while testing title sequences no longer needs to reload the park if the current playback position is already before the target position and ahead of the load position. Added changelog entry.
2017-10-30 12:07:01 +01:00
{
if (!(gScreenFlags & SCREEN_FLAGS_TITLE_DEMO))
{
gPreviewingTitleSequenceInGame = true;
}
}
else
{
_currentSequence = TitleGetConfigSequence();
if (gScreenFlags & SCREEN_FLAGS_TITLE_DEMO)
{
TryLoadSequence();
}
Feature: Preview title sequences in-game Title sequences can now be played back in-game, allowing for much easier editing. Improved title sequence playback in general. Clicking play while on a different title sequence will play the new one. Clicking stop will make the title screen go back to the config title sequence. And the closing the title sequence window will also make the game go back to the config title sequence, and reload the sequence if it was modified. Changes made to title sequences in-game are now correctly loaded in the title screen. Starting a title sequence within the editor will now always reset it even if it's the current playing sequence. (Not for playing in the editor though). Get Location in title sequence command editor now has 100% accuracy compared to before where it would usually get some offset value. Added `get_map_coordinates_from_pos_window` which will allow getting the viewport coordinates of a specific window even if the input coordinates are under another window. This has use with getting 2D positions from the main window without the other windows getting in the way. Options window will now always specify the config title sequence in the dropdown and not the current title sequence. Made a global variable `gLoadKeepWindowsOpen`, in game.h to keep windows open when loading a park. When loading a title sequence park in-game. The sequence player will force-close all park-specific windows ahead of time. Skipping while testing title sequences no longer needs to reload the park if the current playback position is already before the target position and ahead of the load position. Added changelog entry.
2017-10-30 12:07:01 +01:00
}
return _previewingSequence;
}
void TitleScene::StopPreviewingSequence()
{
if (_previewingSequence)
{
WindowBase* mainWindow = WindowGetMain();
if (mainWindow != nullptr)
{
WindowUnfollowSprite(*mainWindow);
}
_previewingSequence = false;
_currentSequence = TitleGetConfigSequence();
gPreviewingTitleSequenceInGame = false;
}
}
bool TitleScene::IsPreviewingSequence()
{
return _previewingSequence;
2017-07-23 00:42:14 +02:00
}
bool TitleScene::ShouldHideVersionInfo()
2017-07-23 00:42:14 +02:00
{
return _hideVersionInfo;
}
void TitleScene::SetHideVersionInfo(bool value)
2017-07-23 00:42:14 +02:00
{
_hideVersionInfo = value;
}
void TitleScene::Load()
2017-07-23 00:42:14 +02:00
{
LOG_VERBOSE("TitleScene::Load()");
2017-07-23 00:42:14 +02:00
if (GameIsPaused())
2017-07-23 00:42:14 +02:00
{
PauseToggle();
2017-07-23 00:42:14 +02:00
}
gScreenFlags = SCREEN_FLAGS_TITLE_DEMO;
gScreenAge = 0;
gCurrentLoadedPath.clear();
2017-07-23 00:42:14 +02:00
2021-08-17 09:36:18 +02:00
#ifndef DISABLE_NETWORK
GetContext().GetNetwork().Close();
2021-08-17 09:36:18 +02:00
#endif
2024-03-26 15:08:17 +01:00
gameStateInitAll(GetGameState(), DEFAULT_MAP_SIZE);
2023-01-16 21:14:50 +01:00
ViewportInitAll();
2022-11-06 21:49:07 +01:00
ContextOpenWindow(WindowClass::MainWindow);
2017-07-23 00:42:14 +02:00
CreateWindows();
TitleInitialise();
OpenRCT2::Audio::PlayTitleMusic();
2017-07-23 00:42:14 +02:00
if (gOpenRCT2ShowChangelog)
{
gOpenRCT2ShowChangelog = false;
2022-11-06 21:49:07 +01:00
ContextOpenWindow(WindowClass::Changelog);
2017-07-23 00:42:14 +02:00
}
if (_sequencePlayer != nullptr)
{
// Force the title sequence to load / update so we
// don't see a blank screen for a split second.
_loadedTitleSequenceId = SIZE_MAX;
2017-07-23 00:42:14 +02:00
TryLoadSequence();
_sequencePlayer->Update();
}
LOG_VERBOSE("TitleScene::Load() finished");
2017-07-23 00:42:14 +02:00
}
void TitleScene::Tick()
{
2017-07-23 00:42:14 +02:00
gInUpdateCode = true;
2023-01-16 21:14:50 +01:00
ScreenshotCheck();
TitleHandleKeyboardInput();
2017-07-23 00:42:14 +02:00
if (GameIsNotPaused())
2017-07-23 00:42:14 +02:00
{
TryLoadSequence();
_sequencePlayer->Update();
int32_t numUpdates = 1;
2018-06-22 23:15:14 +02:00
if (gGameSpeed > 1)
{
2017-07-23 00:42:14 +02:00
numUpdates = 1 << (gGameSpeed - 1);
}
for (int32_t i = 0; i < numUpdates; i++)
2017-07-23 00:42:14 +02:00
{
2024-03-26 15:08:17 +01:00
gameStateUpdateLogic();
2017-07-23 00:42:14 +02:00
}
UpdatePaletteEffects();
// update_weather_animation();
2017-07-23 00:42:14 +02:00
}
InputSetFlag(INPUT_FLAG_VIEWPORT_SCROLLING, false);
2017-07-23 00:42:14 +02:00
2022-11-06 21:49:07 +01:00
ContextUpdateMapTooltip();
WindowDispatchUpdateAll();
2017-07-23 00:42:14 +02:00
2022-11-06 21:49:07 +01:00
ContextHandleInput();
2017-07-23 00:42:14 +02:00
gInUpdateCode = false;
}
void TitleScene::Stop()
{
Audio::StopAll();
}
void TitleScene::ChangePresetSequence(size_t preset)
2017-07-23 00:42:14 +02:00
{
size_t count = TitleSequenceManager::GetCount();
2017-10-31 17:57:16 +01:00
if (preset >= count)
2017-07-23 00:42:14 +02:00
{
return;
}
const utf8* configId = TitleSequenceManagerGetConfigID(preset);
gConfigInterface.CurrentTitleSequencePreset = configId;
if (!_previewingSequence)
_currentSequence = preset;
WindowInvalidateAll();
2017-07-23 00:42:14 +02:00
}
/**
2017-07-23 00:42:14 +02:00
* Creates the windows shown on the title screen; New game, load game,
* tutorial, toolbox and exit.
*/
void TitleScene::CreateWindows()
2017-07-23 00:42:14 +02:00
{
2022-11-06 21:49:07 +01:00
ContextOpenWindow(WindowClass::TitleMenu);
ContextOpenWindow(WindowClass::TitleExit);
ContextOpenWindow(WindowClass::TitleOptions);
ContextOpenWindow(WindowClass::TitleLogo);
WindowResizeGui(ContextGetWidth(), ContextGetHeight());
2017-07-23 00:42:14 +02:00
_hideVersionInfo = false;
}
void TitleScene::TitleInitialise()
{
if (_sequencePlayer == nullptr)
{
_sequencePlayer = GetContext().GetUiContext()->GetTitleSequencePlayer();
}
if (gConfigInterface.RandomTitleSequence)
2019-12-19 20:55:04 +01:00
{
const size_t total = TitleSequenceManager::GetCount();
if (total > 0)
2020-01-20 22:35:51 +01:00
{
bool RCT1Installed = false, RCT1AAInstalled = false, RCT1LLInstalled = false;
uint32_t RCT1Count = 0;
const size_t scenarioCount = ScenarioRepositoryGetCount();
for (size_t s = 0; s < scenarioCount; s++)
2020-01-20 22:35:51 +01:00
{
const ScenarioSource sourceGame = ScenarioRepositoryGetByIndex(s)->SourceGame;
switch (sourceGame)
{
case ScenarioSource::RCT1:
RCT1Count++;
break;
case ScenarioSource::RCT1_AA:
RCT1AAInstalled = true;
break;
case ScenarioSource::RCT1_LL:
RCT1LLInstalled = true;
break;
default:
break;
}
2019-12-19 20:55:04 +01:00
}
2020-01-20 22:35:51 +01:00
// Mega Park can show up in the scenario list even if RCT1 has been uninstalled, so it must be greater than 1
RCT1Installed = RCT1Count > 1;
2020-01-20 22:35:51 +01:00
int32_t random = 0;
bool safeSequence = false;
const std::string RCT1String = LanguageGetString(STR_TITLE_SEQUENCE_RCT1);
const std::string RCT1AAString = LanguageGetString(STR_TITLE_SEQUENCE_RCT1_AA);
const std::string RCT1LLString = LanguageGetString(STR_TITLE_SEQUENCE_RCT1_AA_LL);
2020-01-20 22:35:51 +01:00
// Ensure the random sequence chosen isn't from RCT1 or expansion if the player doesn't have it installed
while (!safeSequence)
2020-01-20 22:35:51 +01:00
{
random = UtilRand() % static_cast<int32_t>(total);
const utf8* scName = TitleSequenceManagerGetName(random);
if (scName == RCT1String)
{
safeSequence = RCT1Installed;
}
else if (scName == RCT1AAString)
{
safeSequence = RCT1AAInstalled;
}
else if (scName == RCT1LLString)
{
safeSequence = RCT1LLInstalled;
}
else
{
safeSequence = true;
}
2019-12-19 20:55:04 +01:00
}
ChangePresetSequence(random);
2019-12-19 20:55:04 +01:00
}
}
size_t seqId = TitleGetConfigSequence();
if (seqId == SIZE_MAX)
{
seqId = TitleSequenceManagerGetIndexForConfigID("*OPENRCT2");
if (seqId == SIZE_MAX)
{
seqId = 0;
}
}
ChangePresetSequence(static_cast<int32_t>(seqId));
}
bool TitleScene::TryLoadSequence(bool loadPreview)
{
if (_loadedTitleSequenceId != _currentSequence || loadPreview)
{
2021-01-28 01:50:30 +01:00
if (_sequencePlayer == nullptr)
{
_sequencePlayer = GetContext().GetUiContext()->GetTitleSequencePlayer();
2021-01-28 01:50:30 +01:00
}
size_t numSequences = TitleSequenceManager::GetCount();
if (numSequences > 0)
{
size_t targetSequence = _currentSequence;
do
{
if (_sequencePlayer->Begin(targetSequence) && _sequencePlayer->Update())
{
_loadedTitleSequenceId = targetSequence;
if (targetSequence != _currentSequence && !loadPreview)
{
// Forcefully change the preset to a preset that works.
const utf8* configId = TitleSequenceManagerGetConfigID(targetSequence);
gConfigInterface.CurrentTitleSequencePreset = configId;
}
2017-07-23 00:42:14 +02:00
_currentSequence = targetSequence;
GfxInvalidateScreen();
return true;
}
targetSequence = (targetSequence + 1) % numSequences;
2018-06-22 23:15:14 +02:00
} while (targetSequence != _currentSequence && !loadPreview);
}
Console::Error::WriteLine("Unable to play any title sequences.");
_sequencePlayer->Eject();
_currentSequence = SIZE_MAX;
_loadedTitleSequenceId = SIZE_MAX;
if (!loadPreview)
{
2024-03-26 15:08:17 +01:00
gameStateInitAll(GetGameState(), DEFAULT_MAP_SIZE);
GameNotifyMapChanged();
}
return false;
}
return true;
}
void TitleCreateWindows()
2018-02-01 18:49:14 +01:00
{
auto* context = OpenRCT2::GetContext();
auto* titleScene = static_cast<TitleScene*>(context->GetTitleScene());
if (titleScene != nullptr)
{
titleScene->CreateWindows();
}
2018-02-01 18:49:14 +01:00
}
void* TitleGetSequencePlayer()
2018-02-01 18:49:14 +01:00
{
auto* context = OpenRCT2::GetContext();
auto* titleScene = static_cast<TitleScene*>(context->GetTitleScene());
if (titleScene != nullptr)
{
return titleScene->GetSequencePlayer();
2017-07-23 00:42:14 +02:00
}
return nullptr;
2018-02-01 18:49:14 +01:00
}
void TitleSequenceChangePreset(size_t preset)
2018-02-01 18:49:14 +01:00
{
auto* context = OpenRCT2::GetContext();
auto* titleScene = static_cast<TitleScene*>(context->GetTitleScene());
if (titleScene != nullptr)
2017-07-23 00:42:14 +02:00
{
titleScene->ChangePresetSequence(preset);
2017-07-23 00:42:14 +02:00
}
2018-02-01 18:49:14 +01:00
}
bool TitleShouldHideVersionInfo()
2018-02-01 18:49:14 +01:00
{
auto* context = OpenRCT2::GetContext();
auto* titleScene = static_cast<TitleScene*>(context->GetTitleScene());
if (titleScene != nullptr)
2017-07-23 00:42:14 +02:00
{
return titleScene->ShouldHideVersionInfo();
2017-07-23 00:42:14 +02:00
}
return false;
2018-02-01 18:49:14 +01:00
}
void TitleSetHideVersionInfo(bool value)
2018-02-01 18:49:14 +01:00
{
auto* context = OpenRCT2::GetContext();
auto* titleScene = static_cast<TitleScene*>(context->GetTitleScene());
if (titleScene != nullptr)
2017-07-23 00:42:14 +02:00
{
titleScene->SetHideVersionInfo(value);
2017-07-23 00:42:14 +02:00
}
2018-02-01 18:49:14 +01:00
}
size_t TitleGetConfigSequence()
2018-02-01 18:49:14 +01:00
{
return TitleSequenceManagerGetIndexForConfigID(gConfigInterface.CurrentTitleSequencePreset.c_str());
2018-02-01 18:49:14 +01:00
}
Feature: Preview title sequences in-game Title sequences can now be played back in-game, allowing for much easier editing. Improved title sequence playback in general. Clicking play while on a different title sequence will play the new one. Clicking stop will make the title screen go back to the config title sequence. And the closing the title sequence window will also make the game go back to the config title sequence, and reload the sequence if it was modified. Changes made to title sequences in-game are now correctly loaded in the title screen. Starting a title sequence within the editor will now always reset it even if it's the current playing sequence. (Not for playing in the editor though). Get Location in title sequence command editor now has 100% accuracy compared to before where it would usually get some offset value. Added `get_map_coordinates_from_pos_window` which will allow getting the viewport coordinates of a specific window even if the input coordinates are under another window. This has use with getting 2D positions from the main window without the other windows getting in the way. Options window will now always specify the config title sequence in the dropdown and not the current title sequence. Made a global variable `gLoadKeepWindowsOpen`, in game.h to keep windows open when loading a park. When loading a title sequence park in-game. The sequence player will force-close all park-specific windows ahead of time. Skipping while testing title sequences no longer needs to reload the park if the current playback position is already before the target position and ahead of the load position. Added changelog entry.
2017-10-30 12:07:01 +01:00
size_t TitleGetCurrentSequence()
2018-02-01 18:49:14 +01:00
{
auto* context = OpenRCT2::GetContext();
auto* titleScene = static_cast<TitleScene*>(context->GetTitleScene());
if (titleScene != nullptr)
2017-07-23 00:42:14 +02:00
{
return titleScene->GetCurrentSequence();
2017-07-23 00:42:14 +02:00
}
return 0;
2018-02-01 18:49:14 +01:00
}
bool TitlePreviewSequence(size_t value)
2018-02-01 18:49:14 +01:00
{
auto* context = OpenRCT2::GetContext();
auto* titleScene = static_cast<TitleScene*>(context->GetTitleScene());
if (titleScene != nullptr)
{
return titleScene->PreviewSequence(value);
}
2018-02-01 18:49:14 +01:00
return false;
}
void TitleStopPreviewingSequence()
2018-02-01 18:49:14 +01:00
{
auto* context = OpenRCT2::GetContext();
auto* titleScene = static_cast<TitleScene*>(context->GetTitleScene());
if (titleScene != nullptr)
{
titleScene->StopPreviewingSequence();
}
2018-02-01 18:49:14 +01:00
}
bool TitleIsPreviewingSequence()
2018-02-01 18:49:14 +01:00
{
auto* context = OpenRCT2::GetContext();
auto* titleScene = static_cast<TitleScene*>(context->GetTitleScene());
if (titleScene != nullptr)
2017-07-23 00:42:14 +02:00
{
return titleScene->IsPreviewingSequence();
}
2018-02-01 18:49:14 +01:00
return false;
}
2023-04-03 22:18:42 +02:00
void DrawOpenRCT2(DrawPixelInfo& dpi, const ScreenCoordsXY& screenCoords)
2018-02-01 18:49:14 +01:00
{
2020-10-16 01:13:52 +02:00
thread_local std::string buffer;
buffer.clear();
buffer.assign("{OUTLINE}{WHITE}");
2018-02-01 18:49:14 +01:00
// Write name and version information
2020-10-16 01:13:52 +02:00
buffer += gVersionInfoFull;
GfxDrawString(dpi, screenCoords + ScreenCoordsXY(5, 5 - 13), buffer.c_str(), { COLOUR_BLACK });
int16_t width = static_cast<int16_t>(GfxGetStringWidth(buffer, FontStyle::Medium));
2018-02-01 18:49:14 +01:00
// Write platform information
buffer.assign("{OUTLINE}{WHITE}");
buffer.append(OPENRCT2_PLATFORM);
2020-10-16 01:13:52 +02:00
buffer.append(" (");
buffer.append(OPENRCT2_ARCHITECTURE);
buffer.append(")");
2023-04-03 22:18:42 +02:00
GfxDrawString(dpi, screenCoords + ScreenCoordsXY(5, 5), buffer.c_str(), { COLOUR_BLACK });
width = std::max(width, static_cast<int16_t>(GfxGetStringWidth(buffer, FontStyle::Medium)));
// Invalidate screen area
GfxSetDirtyBlocks({ screenCoords - ScreenCoordsXY(0, 13),
screenCoords + ScreenCoordsXY{ width + 5, 30 } }); // 30 is an arbitrary height to catch both strings
}