Game Development Reference
In-Depth Information
Listing 25-9 updates the Game::RunGame to use a packaged_task to load the user's save game data.
Listing 25-9. Using a packaged_task
bool LoadSaveGame()
{
return SerializationManager::GetSingleton().Load();
}
void Game::RunGame()
{
InitializeRooms();
std::packaged_task< bool() > loaderTask{ LoadSaveGame };
std::thread loaderThread{ std::ref{ loaderTask } };
auto loaderFuture = loaderTask.get_future();
while (loaderFuture.wait_for(std::chrono::seconds{ 0 }) != std::future_status::ready)
{
// Wait until the future is ready.
// In a full game you could update a spinning progress icon!
int x = 0;
}
bool userSaveLoaded = loaderFuture.get();
WelcomePlayer(userSaveLoaded);
while (!HasFinished())
{
GivePlayerOptions();
stringstream playerInputStream;
GetPlayerInput(playerInputStream);
EvaluateInput(playerInputStream);
bool playerWon = true;
for (auto& enemy : m_enemies)
{
playerWon &= enemy->IsAlive() == false;
}
if (playerWon)
{
SetPlayerWon();
}
}
 
Search WWH ::




Custom Search