4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
30 /** \file SG_IObject.h
40 // used for debugging: stage of the game engine main loop at which a Scenegraph modification is done
45 SG_STAGE_NETWORK_UPDATE,
47 SG_STAGE_PHYSICS1_UPDATE,
49 SG_STAGE_CONTROLLER_UPDATE,
51 SG_STAGE_ACTUATOR_UPDATE,
53 SG_STAGE_PHYSICS2_UPDATE,
61 extern SG_Stage gSG_Stage;
63 inline void SG_SetActiveStage(SG_Stage stage)
73 typedef std::vector<SG_Controller*> SGControllerList;
75 typedef void* (*SG_ReplicationNewCallback)(
81 typedef void* (*SG_DestructionNewCallback)(
87 typedef void (*SG_UpdateTransformCallback)(
93 typedef bool (*SG_ScheduleUpdateCallback)(
99 typedef bool (*SG_RescheduleUpdateCallback)(
100 SG_IObject* sgobject,
107 * SG_Callbacks hold 2 call backs to the outside world.
108 * The first is meant to be called when objects are replicated.
109 * And allows the outside world to syncronise external objects
110 * with replicated nodes and their children.
111 * The second is called when a node is detroyed and again
112 * is their for synconisation purposes
113 * These callbacks may both be NULL.
114 * The efficacy of this approach has not been proved some
115 * alternatives might be to perform all replication and destruction
117 * To define a class interface rather than a simple function
118 * call back so that replication information can be transmitted from
126 m_destructionfunc(NULL),
128 m_schedulefunc(NULL),
129 m_reschedulefunc(NULL)
134 SG_ReplicationNewCallback repfunc,
135 SG_DestructionNewCallback destructfunc,
136 SG_UpdateTransformCallback updatefunc,
137 SG_ScheduleUpdateCallback schedulefunc,
138 SG_RescheduleUpdateCallback reschedulefunc
140 m_replicafunc(repfunc),
141 m_destructionfunc(destructfunc),
142 m_updatefunc(updatefunc),
143 m_schedulefunc(schedulefunc),
144 m_reschedulefunc(reschedulefunc)
148 SG_ReplicationNewCallback m_replicafunc;
149 SG_DestructionNewCallback m_destructionfunc;
150 SG_UpdateTransformCallback m_updatefunc;
151 SG_ScheduleUpdateCallback m_schedulefunc;
152 SG_RescheduleUpdateCallback m_reschedulefunc;
156 base object that can be part of the scenegraph.
158 class SG_IObject : public SG_QList
162 void* m_SGclientObject;
163 void* m_SGclientInfo;
164 SG_Callbacks m_callbacks;
165 SGControllerList m_SGcontrollers;
168 virtual ~SG_IObject();
172 * Add a pointer to a controller allocated on the heap, to
173 * this node. This memory for this controller becomes the
174 * responsibility of this class. It will be deleted when
175 * this object is deleted.
184 * Clear the array of pointers to controllers associated with
185 * this node. This does not delete the controllers themselves!
186 * This should be used very carefully to avoid memory
191 RemoveAllControllers(
194 /// Needed for replication
197 * Return a reference to this node's controller list.
198 * Whilst we don't wish to expose full control of the container
199 * to the user we do allow them to call non_const methods
200 * on pointers in the container. C++ topic: how to do this in
204 SGControllerList& GetSGControllerList()
206 return m_SGcontrollers;
212 SG_Callbacks& GetCallBackFunctions()
218 * Get the client object associated with this
219 * node. This interface allows you to associate
220 * arbitray external objects with this node. They are
221 * passed to the callback functions when they are
222 * activated so you can syncronise these external objects
223 * upon replication and destruction
227 inline const void* GetSGClientObject() const
229 return m_SGclientObject;
232 inline void* GetSGClientObject()
234 return m_SGclientObject;
238 * Set the client object for this node. This is just a
239 * pointer to an object allocated that should exist for
240 * the duration of the lifetime of this object, or untill
241 * this function is called again.
244 void SetSGClientObject(void* clientObject)
246 m_SGclientObject = clientObject;
250 /* needed for scene switching */
251 inline const void* GetSGClientInfo() const
253 return m_SGclientInfo;
255 inline void* GetSGClientInfo()
257 return m_SGclientInfo;
259 void SetSGClientInfo(void* clientInfo)
261 m_SGclientInfo = clientInfo;
266 * Set the current simulation time for this node.
267 * The implementation of this function runs through
268 * the nodes list of controllers and calls their SetSimulatedTime methods
271 void SetControllerTime(double time);
281 ActivateReplicationCallback(
285 if (m_callbacks.m_replicafunc)
287 // Call client provided replication func
288 if (m_callbacks.m_replicafunc(replica,m_SGclientObject,m_SGclientInfo) == NULL)
296 ActivateDestructionCallback(
299 if (m_callbacks.m_destructionfunc)
301 // Call client provided destruction function on this!
302 m_callbacks.m_destructionfunc(this,m_SGclientObject,m_SGclientInfo);
306 // no callback but must still destroy the node to avoid memory leak
312 ActivateUpdateTransformCallback(
315 if (m_callbacks.m_updatefunc)
317 // Call client provided update func.
318 m_callbacks.m_updatefunc(this, m_SGclientObject, m_SGclientInfo);
323 ActivateScheduleUpdateCallback(
326 // HACK, this check assumes that the scheduled nodes are put on a DList (see SG_Node.h)
327 // The early check on Empty() allows up to avoid calling the callback function
328 // when the node is already scheduled for update.
329 if (Empty() && m_callbacks.m_schedulefunc)
331 // Call client provided update func.
332 return m_callbacks.m_schedulefunc(this, m_SGclientObject, m_SGclientInfo);
338 ActivateRecheduleUpdateCallback(
341 if (m_callbacks.m_reschedulefunc)
343 // Call client provided update func.
344 m_callbacks.m_reschedulefunc(this, m_SGclientObject, m_SGclientInfo);
352 SG_Callbacks& callbacks
356 const SG_IObject &other
360 #ifdef WITH_CXX_GUARDEDALLOC
362 void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:SG_IObject"); }
363 void operator delete( void *mem ) { MEM_freeN(mem); }
367 #endif //__SG_IOBJECT