Skip to content

Commit

Permalink
Merge pull request #13011 from ZehMatt/refactor/titlesequencemanager
Browse files Browse the repository at this point in the history
Refactor code around TitleSequence
  • Loading branch information
ZehMatt committed Sep 29, 2020
2 parents a850d76 + 005e1e0 commit 754f6b3
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 394 deletions.
60 changes: 29 additions & 31 deletions src/openrct2-ui/title/TitleSequencePlayer.cpp
Expand Up @@ -47,7 +47,7 @@ class TitleSequencePlayer final : public ITitleSequencePlayer
private:
GameState& _gameState;

TitleSequence* _sequence = nullptr;
std::unique_ptr<TitleSequence> _sequence;
int32_t _position = 0;
int32_t _waitCounter = 0;

Expand All @@ -73,7 +73,6 @@ class TitleSequencePlayer final : public ITitleSequencePlayer

void Eject() override
{
FreeTitleSequence(_sequence);
_sequence = nullptr;
}

Expand All @@ -93,7 +92,7 @@ class TitleSequencePlayer final : public ITitleSequencePlayer
}

Eject();
_sequence = sequence;
_sequence = std::move(sequence);

Reset();
return true;
Expand All @@ -111,7 +110,7 @@ class TitleSequencePlayer final : public ITitleSequencePlayer
}

// Check that position is valid
if (_position >= static_cast<int32_t>(_sequence->NumCommands))
if (_position >= static_cast<int32_t>(_sequence->Commands.size()))
{
_position = 0;
return false;
Expand All @@ -123,8 +122,8 @@ class TitleSequencePlayer final : public ITitleSequencePlayer
_waitCounter--;
if (_waitCounter == 0)
{
const TitleCommand* command = &_sequence->Commands[_position];
if (command->Type == TITLE_SCRIPT_WAIT)
const auto& command = _sequence->Commands[_position];
if (command.Type == TITLE_SCRIPT_WAIT)
{
IncrementPosition();
}
Expand All @@ -134,14 +133,14 @@ class TitleSequencePlayer final : public ITitleSequencePlayer
{
while (true)
{
const TitleCommand* command = &_sequence->Commands[_position];
const auto& command = _sequence->Commands[_position];
if (ExecuteCommand(command))
{
if (command->Type == TITLE_SCRIPT_WAIT)
if (command.Type == TITLE_SCRIPT_WAIT)
{
break;
}
if (command->Type != TITLE_SCRIPT_RESTART)
if (command.Type != TITLE_SCRIPT_RESTART)
{
IncrementPosition();
}
Expand All @@ -156,7 +155,7 @@ class TitleSequencePlayer final : public ITitleSequencePlayer
{
if (!SkipToNextLoadCommand() || _position == entryPosition)
{
Console::Error::WriteLine("Unable to load any parks from %s.", _sequence->Name);
Console::Error::WriteLine("Unable to load any parks from %s.", _sequence->Name.c_str());
return false;
}
}
Expand All @@ -173,7 +172,7 @@ class TitleSequencePlayer final : public ITitleSequencePlayer

void Seek(int32_t targetPosition) override
{
if (targetPosition < 0 || targetPosition >= static_cast<int32_t>(_sequence->NumCommands))
if (targetPosition < 0 || targetPosition >= static_cast<int32_t>(_sequence->Commands.size()))
{
throw std::runtime_error("Invalid position.");
}
Expand All @@ -189,7 +188,7 @@ class TitleSequencePlayer final : public ITitleSequencePlayer
// Set position to the last LOAD command before target position
for (int32_t i = targetPosition; i >= 0; i--)
{
const TitleCommand* command = &_sequence->Commands[i];
const TitleCommand& command = _sequence->Commands[i];
if ((_position == i && _position != targetPosition) || TitleSequenceIsLoadCommand(command))
{
// Break if we have a new load command or if we're already in the range of the correct load command
Expand Down Expand Up @@ -222,7 +221,7 @@ class TitleSequencePlayer final : public ITitleSequencePlayer
void IncrementPosition()
{
_position++;
if (_position >= static_cast<int32_t>(_sequence->NumCommands))
if (_position >= static_cast<int32_t>(_sequence->Commands.size()))
{
_position = 0;
}
Expand All @@ -236,59 +235,58 @@ class TitleSequencePlayer final : public ITitleSequencePlayer
{
IncrementPosition();
command = &_sequence->Commands[_position];
} while (!TitleSequenceIsLoadCommand(command) && _position != entryPosition);
} while (!TitleSequenceIsLoadCommand(*command) && _position != entryPosition);
return _position != entryPosition;
}

bool ExecuteCommand(const TitleCommand* command)
bool ExecuteCommand(const TitleCommand& command)
{
switch (command->Type)
switch (command.Type)
{
case TITLE_SCRIPT_END:
_waitCounter = 1;
break;
case TITLE_SCRIPT_WAIT:
// The waitCounter is measured in 25-ms game ticks. Previously it was seconds * 40 ticks/second, now it is ms /
// 25 ms/tick
_waitCounter = std::max<int32_t>(1, command->Milliseconds / static_cast<uint32_t>(GAME_UPDATE_TIME_MS));
_waitCounter = std::max<int32_t>(1, command.Milliseconds / static_cast<uint32_t>(GAME_UPDATE_TIME_MS));
break;
case TITLE_SCRIPT_LOCATION:
{
auto loc = TileCoordsXY(command->X, command->Y).ToCoordsXY().ToTileCentre();
auto loc = TileCoordsXY(command.X, command.Y).ToCoordsXY().ToTileCentre();
SetViewLocation(loc);
break;
}
case TITLE_SCRIPT_ROTATE:
RotateView(command->Rotations);
RotateView(command.Rotations);
break;
case TITLE_SCRIPT_ZOOM:
SetViewZoom(command->Zoom);
SetViewZoom(command.Zoom);
break;
case TITLE_SCRIPT_SPEED:
gGameSpeed = std::clamp<uint8_t>(command->Speed, 1, 4);
gGameSpeed = std::clamp<uint8_t>(command.Speed, 1, 4);
break;
case TITLE_SCRIPT_FOLLOW:
FollowSprite(command->SpriteIndex);
FollowSprite(command.SpriteIndex);
break;
case TITLE_SCRIPT_RESTART:
Reset();
break;
case TITLE_SCRIPT_LOAD:
{
bool loadSuccess = false;
uint8_t saveIndex = command->SaveIndex;
TitleSequenceParkHandle* parkHandle = TitleSequenceGetParkHandle(_sequence, saveIndex);
uint8_t saveIndex = command.SaveIndex;
auto parkHandle = TitleSequenceGetParkHandle(*_sequence, saveIndex);
if (parkHandle != nullptr)
{
loadSuccess = LoadParkFromStream(static_cast<OpenRCT2::IStream*>(parkHandle->Stream), parkHandle->HintPath);
TitleSequenceCloseParkHandle(parkHandle);
loadSuccess = LoadParkFromStream(parkHandle->Stream.get(), parkHandle->HintPath);
}
if (!loadSuccess)
{
if (_sequence->NumSaves > saveIndex)
if (_sequence->Saves.size() > saveIndex)
{
const utf8* path = _sequence->Saves[saveIndex];
Console::Error::WriteLine("Failed to load: \"%s\" for the title sequence.", path);
const auto& path = _sequence->Saves[saveIndex];
Console::Error::WriteLine("Failed to load: \"%s\" for the title sequence.", path.c_str());
}
return false;
}
Expand All @@ -297,14 +295,14 @@ class TitleSequencePlayer final : public ITitleSequencePlayer
case TITLE_SCRIPT_LOADSC:
{
bool loadSuccess = false;
auto scenario = GetScenarioRepository()->GetByInternalName(command->Scenario);
auto scenario = GetScenarioRepository()->GetByInternalName(command.Scenario);
if (scenario != nullptr)
{
loadSuccess = LoadParkFromFile(scenario->path);
}
if (!loadSuccess)
{
Console::Error::WriteLine("Failed to load: \"%s\" for the title sequence.", command->Scenario);
Console::Error::WriteLine("Failed to load: \"%s\" for the title sequence.", command.Scenario);
return false;
}
break;
Expand Down
21 changes: 7 additions & 14 deletions src/openrct2-ui/windows/TitleCommandEditor.cpp
Expand Up @@ -238,7 +238,7 @@ void window_title_command_editor_open(TitleSequence* sequence, int32_t index, bo
switch (command.Type)
{
case TITLE_SCRIPT_LOAD:
if (command.SaveIndex >= _sequence->NumSaves)
if (command.SaveIndex >= _sequence->Saves.size())
command.SaveIndex = SAVE_INDEX_INVALID;
break;
case TITLE_SCRIPT_LOCATION:
Expand Down Expand Up @@ -329,20 +329,13 @@ static void window_title_command_editor_mouseup(rct_window* w, rct_widgetindex w
if (_window_title_command_editor_insert)
{
size_t insertIndex = _window_title_command_editor_index;
_sequence->NumCommands++;
_sequence->Commands = Memory::ReallocateArray(_sequence->Commands, _sequence->NumCommands);
for (size_t i = _sequence->NumCommands - 1; i > insertIndex; i--)
{
_sequence->Commands[i] = _sequence->Commands[i - 1];
}
_sequence->Commands[insertIndex] = command;
_sequence->Commands.insert(_sequence->Commands.begin() + insertIndex, command);
}
else
{
_sequence->Commands[_window_title_command_editor_index] = command;
TitleSequenceSave(_sequence);
}
TitleSequenceSave(_sequence);
TitleSequenceSave(*_sequence);

rct_window* title_editor_w = window_find_by_class(WC_TITLE_EDITOR);
if (title_editor_w != nullptr)
Expand Down Expand Up @@ -393,11 +386,11 @@ static void window_title_command_editor_mousedown(rct_window* w, rct_widgetindex
}
else if (command.Type == TITLE_SCRIPT_LOAD)
{
int32_t numItems = static_cast<int32_t>(_sequence->NumSaves);
int32_t numItems = static_cast<int32_t>(_sequence->Saves.size());
for (int32_t i = 0; i < numItems; i++)
{
gDropdownItemsFormat[i] = STR_OPTIONS_DROPDOWN_ITEM;
gDropdownItemsArgs[i] = reinterpret_cast<uintptr_t>(_sequence->Saves[i]);
gDropdownItemsArgs[i] = reinterpret_cast<uintptr_t>(_sequence->Saves[i].c_str());
}

window_dropdown_show_text_custom_width(
Expand Down Expand Up @@ -467,7 +460,7 @@ static void window_title_command_editor_dropdown(rct_window* w, rct_widgetindex
break;
case TITLE_SCRIPT_LOAD:
command.SaveIndex = 0;
if (command.SaveIndex >= _sequence->NumSaves)
if (command.SaveIndex >= _sequence->Saves.size())
{
command.SaveIndex = 0xFF;
}
Expand Down Expand Up @@ -769,7 +762,7 @@ static void window_title_command_editor_paint(rct_window* w, rct_drawpixelinfo*
else
{
auto ft = Formatter::Common();
ft.Add<utf8*>(_sequence->Saves[command.SaveIndex]);
ft.Add<utf8*>(_sequence->Saves[command.SaveIndex].c_str());
DrawTextEllipsised(
dpi, { w->windowPos.x + w->widgets[WIDX_INPUT].left + 1, w->windowPos.y + w->widgets[WIDX_INPUT].top },
w->widgets[WIDX_INPUT_DROPDOWN].left - w->widgets[WIDX_INPUT].left - 4, STR_STRING, ft, w->colours[1]);
Expand Down

0 comments on commit 754f6b3

Please sign in to comment.