Blender  V2.59
SG_IObject.h
Go to the documentation of this file.
00001 /*
00002  * $Id: SG_IObject.h 35082 2011-02-22 19:30:37Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00021  * All rights reserved.
00022  *
00023  * The Original Code is: all of this file.
00024  *
00025  * Contributor(s): none yet.
00026  *
00027  * ***** END GPL LICENSE BLOCK *****
00028  */
00029 
00034 #ifndef __SG_IOBJECT
00035 #define __SG_IOBJECT
00036 
00037 #include "SG_QList.h"
00038 #include <vector>
00039 
00040 // used for debugging: stage of the game engine main loop at which a Scenegraph modification is done
00041 enum SG_Stage
00042 {
00043         SG_STAGE_UNKNOWN = 0,
00044         SG_STAGE_NETWORK,
00045         SG_STAGE_NETWORK_UPDATE,
00046         SG_STAGE_PHYSICS1,
00047         SG_STAGE_PHYSICS1_UPDATE,
00048         SG_STAGE_CONTROLLER,
00049         SG_STAGE_CONTROLLER_UPDATE,
00050         SG_STAGE_ACTUATOR,
00051         SG_STAGE_ACTUATOR_UPDATE,
00052         SG_STAGE_PHYSICS2,
00053         SG_STAGE_PHYSICS2_UPDATE,
00054         SG_STAGE_SCENE,
00055         SG_STAGE_RENDER,
00056         SG_STAGE_CONVERTER,
00057         SG_STAGE_CULLING,
00058         SG_STAGE_MAX
00059 };
00060 
00061 extern SG_Stage gSG_Stage;
00062 
00063 inline void SG_SetActiveStage(SG_Stage stage)
00064 {
00065         gSG_Stage = stage;
00066 }
00067         
00068 
00069 
00070 class SG_Controller;
00071 class SG_IObject;
00072 
00073 typedef std::vector<SG_Controller*> SGControllerList;
00074 
00075 typedef void* (*SG_ReplicationNewCallback)(
00076         SG_IObject* sgobject,
00077         void*   clientobj,
00078         void*   clientinfo
00079 );
00080 
00081 typedef void* (*SG_DestructionNewCallback)(
00082         SG_IObject* sgobject,
00083         void*   clientobj,
00084         void*   clientinfo
00085 );
00086 
00087 typedef void  (*SG_UpdateTransformCallback)(
00088         SG_IObject* sgobject,
00089         void*   clientobj,
00090         void*   clientinfo
00091 );
00092 
00093 typedef bool  (*SG_ScheduleUpdateCallback)(
00094         SG_IObject* sgobject,
00095         void*   clientobj,
00096         void*   clientinfo
00097 );
00098 
00099 typedef bool  (*SG_RescheduleUpdateCallback)(
00100         SG_IObject* sgobject,
00101         void*   clientobj,
00102         void*   clientinfo
00103 );
00104 
00105 
00121 struct  SG_Callbacks
00122 {
00123         SG_Callbacks(
00124         ):
00125                 m_replicafunc(NULL),
00126                 m_destructionfunc(NULL),
00127                 m_updatefunc(NULL),
00128                 m_schedulefunc(NULL),
00129                 m_reschedulefunc(NULL)
00130         {
00131         };
00132                 
00133         SG_Callbacks(
00134                 SG_ReplicationNewCallback repfunc,
00135                 SG_DestructionNewCallback destructfunc,
00136                 SG_UpdateTransformCallback updatefunc,
00137                 SG_ScheduleUpdateCallback schedulefunc,
00138                 SG_RescheduleUpdateCallback reschedulefunc
00139         ): 
00140                 m_replicafunc(repfunc),
00141                 m_destructionfunc(destructfunc),
00142                 m_updatefunc(updatefunc),
00143                 m_schedulefunc(schedulefunc),
00144                 m_reschedulefunc(reschedulefunc)
00145         {
00146         };
00147 
00148         SG_ReplicationNewCallback       m_replicafunc;
00149         SG_DestructionNewCallback       m_destructionfunc;
00150         SG_UpdateTransformCallback      m_updatefunc;
00151         SG_ScheduleUpdateCallback       m_schedulefunc;
00152         SG_RescheduleUpdateCallback m_reschedulefunc;
00153 };
00154 
00158 class SG_IObject : public SG_QList
00159 {
00160 private :
00161 
00162         void*   m_SGclientObject;
00163         void*   m_SGclientInfo;
00164         SG_Callbacks m_callbacks;
00165         SGControllerList        m_SGcontrollers;
00166 
00167 public:
00168         virtual ~SG_IObject();
00169 
00170 
00178                 void                            
00179         AddSGController(
00180                 SG_Controller* cont
00181         );
00182 
00190                 void                            
00191         RemoveAllControllers(
00192         ); 
00193 
00195 
00204         SGControllerList& GetSGControllerList()
00205         { 
00206                 return m_SGcontrollers; 
00207         }
00208 
00212         SG_Callbacks& GetCallBackFunctions()
00213         {
00214                 return m_callbacks;
00215         }
00216         
00227         inline const void* GetSGClientObject() const    
00228         {
00229                 return m_SGclientObject;
00230         }
00231 
00232         inline void* GetSGClientObject()
00233         { 
00234                 return m_SGclientObject;
00235         }
00236 
00244         void SetSGClientObject(void* clientObject)
00245         {
00246                 m_SGclientObject = clientObject;
00247         }
00248 
00249 
00250         /* needed for scene switching */
00251         inline const void* GetSGClientInfo() const
00252         {
00253                 return m_SGclientInfo;
00254         }
00255         inline void* GetSGClientInfo()
00256         {
00257                 return m_SGclientInfo;
00258         }
00259         void SetSGClientInfo(void* clientInfo)
00260         {
00261                 m_SGclientInfo = clientInfo;
00262         }
00263 
00264 
00271         void SetControllerTime(double time);
00272         
00273         virtual 
00274                 void            
00275         Destruct(
00276         ) = 0;
00277 
00278 protected :
00279 
00280                 bool
00281         ActivateReplicationCallback(
00282                 SG_IObject *replica
00283         )
00284         {
00285                 if (m_callbacks.m_replicafunc)
00286                 {
00287                         // Call client provided replication func
00288                         if (m_callbacks.m_replicafunc(replica,m_SGclientObject,m_SGclientInfo) == NULL)
00289                                 return false;
00290                 }
00291                 return true;
00292         }
00293 
00294 
00295                 void
00296         ActivateDestructionCallback(
00297         )
00298         {
00299                 if (m_callbacks.m_destructionfunc)
00300                 {
00301                         // Call client provided destruction function on this!
00302                         m_callbacks.m_destructionfunc(this,m_SGclientObject,m_SGclientInfo);
00303                 }
00304                 else
00305                 {
00306                         // no callback but must still destroy the node to avoid memory leak
00307                         delete this;
00308                 }
00309         }
00310         
00311                 void
00312         ActivateUpdateTransformCallback(
00313         )
00314         {
00315                 if (m_callbacks.m_updatefunc)
00316                 {
00317                         // Call client provided update func.
00318                         m_callbacks.m_updatefunc(this, m_SGclientObject, m_SGclientInfo);
00319                 }
00320         }
00321 
00322                 bool
00323         ActivateScheduleUpdateCallback(
00324         )
00325         {
00326                 // HACK, this check assumes that the scheduled nodes are put on a DList (see SG_Node.h)
00327                 // The early check on Empty() allows up to avoid calling the callback function
00328                 // when the node is already scheduled for update.
00329                 if (Empty() && m_callbacks.m_schedulefunc)
00330                 {
00331                         // Call client provided update func.
00332                         return m_callbacks.m_schedulefunc(this, m_SGclientObject, m_SGclientInfo);
00333                 }
00334                 return false;
00335         }
00336 
00337                 void
00338         ActivateRecheduleUpdateCallback(
00339         )
00340         {
00341                 if (m_callbacks.m_reschedulefunc)
00342                 {
00343                         // Call client provided update func.
00344                         m_callbacks.m_reschedulefunc(this, m_SGclientObject, m_SGclientInfo);
00345                 }
00346         }
00347 
00348 
00349         SG_IObject(
00350                 void* clientobj,
00351                 void* clientinfo,
00352                 SG_Callbacks& callbacks
00353         );
00354 
00355         SG_IObject(
00356                 const SG_IObject &other
00357         );
00358 
00359 
00360 #ifdef WITH_CXX_GUARDEDALLOC
00361 public:
00362         void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:SG_IObject"); }
00363         void operator delete( void *mem ) { MEM_freeN(mem); }
00364 #endif
00365 };
00366 
00367 #endif //__SG_IOBJECT
00368