Game Development Reference
In-Depth Information
The bold lines of code in Listing 21-3 contain the first uses of new and delete that you have seen
in this topic. You don't need to worry about the details of what these keywords do at the moment,
as we'll be covering dynamic memory allocation in detail in Chapter 24. You can however place a
breakpoint in the Singleton constructor and destructor to see those in action.
We'll add more code to the EventManager class before it will prove to be useful, so we cover that now.
Implementing the EventManager Class
The EventManager is going to store Event objects in an unordered_map . The keys to this map will be
hash values calculated from strings that will be used to register events, attach listeners to events,
and eventually send the events. This means that you will need an Event class, an EventHandler
class, and a method to convert strings into unique hashes. Once these classes are all in place and
operational we'll replace the current method used to detect when the player has chosen to quit the
game with the new event system.
The EventHandler Interface
Listing 21-4 shows the code for the EventHandler interface.
Listing 21-4. The EventHandler Interface
class Event;
class EventHandler
{
public:
virtual void HandleEvent(const Event* pEvent) = 0;
};
The EventHandler interface is very basic. It simply provides a base class from which other classes
can derive to give themselves a common interface that can be used by the rest of the event system.
The Event class is one place where this is important, as it stores a vector of EventHandler pointers.
The Event Class
The Event class will be used to represent an individual event within the EventManager . This event
class is responsible for forwarding the event to all attached listener objects. To achieve this, the
Event class will store EventHandler pointers in a vector. You can see this in Listing 21-5.
Listing 21-5. The Event Class
using EventID = int;
class EventHandler;
class Event
{
 
Search WWH ::




Custom Search