Game Development Reference
In-Depth Information
can destroy other processes, or even worse cause a recursive call to indirectly cause
itself to be destroyed. Think of the likely code for a hand grenade exploding. The
VOnUpdate() would likely query the game object lists for every object in a certain
range, and then cause all those objects to be destroyed in a nice fireball. The grenade
object would be included in the list of objects in range, wouldn
t it?
The solution to this problem involves some kind of reference counting system or
maybe a smart pointer. The shared_ptr template class in Chapter 3,
'
Coding Tid-
bits and Style That Saved Me,
solves this problem well, and it will be used in the
next section.
A Simple Cooperative Multitasker
A good process class should contain some additional data members and methods to
make it interesting and flexible. There are as many ways to create this class as there
are programmers, but this should give you a good start. There are two classes in this
nugget of code:
n class Process: A base class for processes. You
'
ll inherit from this class and
redefine the VOnUpdate() method.
n class ProcessManager: This is a container and manager for running all
your cooperative processes.
Here
'
s the definition for Process :
// some smart pointer typedef
'
s
class Process;
typedef shared_ptr<Process> StrongProcessPtr;
typedef weak_ptr<Process> WeakProcessPtr;
class Process
{
friend class ProcessManager;
public:
enum State
{
// Processes that are neither dead nor alive
UNINITIALIZED = 0, // created but not running
REMOVED, // removed from the process list but not destroyed; this can
// happen when a process that is already running is parented
// to another process.
// Living processes
RUNNING, // initialized and running
 
Search WWH ::




Custom Search