Game Development Reference
In-Depth Information
public:
explicit Serializable(unsigned int id)
: m_id{ id }
{
SerializationManager::GetSingleton().RegisterSerializable(this);
}
Serializable::~Serializable()
{
SerializationManager* pSerializationManager =
SerializationManager::GetSingletonPtr();
if (pSerializationManager)
{
pSerializationManager->RemoveSerializable(this);
}
}
virtual void OnSave(std::ofstream& file) = 0;
virtual void OnLoad(std::ifstream& file) = 0;
unsigned int GetId() const { return m_id; }
};
The Serializable class is intended to be inherited by the classes you would like to be able to save
between game sessions and is therefore implemented as an interface. This is achieved by making
the OnSave and OnLoad method purely virtual.
Each Serializable also stores an ID in the m_id variable. The constructor and destructor
automatically adds and removes the object from the SerializationManager object that it accesses
via the Singleton pattern.
Saving and Loading Text Adventure
The first step toward being able to save and load the game is to create the SerializationManager .
Listing 24-7 shows the updated main function.
Listing 24-7. The Updated main Function
int _tmain(int argc, _TCHAR* argv[])
{
new SerializationManager();
Game game;
game.RunGame();
delete SerializationManager::GetSingletonPtr();
return 0;
}
 
Search WWH ::




Custom Search