Game Development Reference
In-Depth Information
void RunGame();
virtual void HandleEvent(const Event* pEvent);
// From QuitObserver
virtual void OnQuit();
bool HasFinished() const
{
m_finishedQueryLock.lock();
bool hasFinished = m_playerQuit || m_playerWon;
m_finishedQueryLock.unlock();
return hasFinished;
}
};
The Game class shows how you can construct classes in C++. There is a parent class from which Game
derives. This class provides an interface that includes virtual methods. The Game class overrides these
virtual methods with specific instances of its own. A perfect example of this is the HandleEvent method.
Game also shows how you can customize STL templates for your own uses. There is an array of
Room::Pointer instances as well as a vector of EnemyBase::Pointer instances. These types of
pointers are created using type aliases. Type aliases in C++ allow you to create your own named
types and are generally a good idea. If you ever need to change the type of an object at a later date
you can get away with just changing the type alias. If you hadn't used an alias you would be required
to manually change every location where the type had been used.
There is also a mutex present in the Game class. This mutex is a clue to the fact that C++ allows you
to write programs that can execute on multiple CPU cores at once. A mutex is a mutual exclusion
object that allows you to ensure that only a single thread is accessing a single variable at one time.
Listing 27-2 contains the final source code for the Game::RunGame method. This method consists of
code that shows how you can iterate over collections and use futures.
Listing 27-2. The Game::RunGame Method
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!
int32_t x = 0;
}
bool userSaveLoaded = loaderFuture.get();
WelcomePlayer(userSaveLoaded);
 
Search WWH ::




Custom Search