00001 #ifndef SGSCENE_H
00002 #define SGSCENE_H
00003
00004 #include "SGFrame.h"
00005 #include "SGBehaviorEngine.h"
00006 #include "SGRenderEngine.h"
00007
00008 namespace Spr {;
00009
00010
00011 class SGScene;
00012
00013 class SGObjectNamesLess{
00014 public:
00015 bool operator () (SGObject* o1, SGObject* o2) const;
00016 };
00017
00018
00019
00020 class SGObjectNames:public std::set<UTRef<SGObject>, SGObjectNamesLess >{
00021 public:
00022 typedef std::map<UTString, UTString> TNameMap;
00023 class SGObjectKey:public SGObject{
00024 public:
00025 SGObjectKey();
00026 ~SGObjectKey();
00027 DEF_UTTYPEINFODEF(SGObjectKey);
00028 };
00029 TNameMap nameMap;
00030 static SGObjectKey key;
00031 void Print(std::ostream& os) const;
00032
00033
00034 typedef std::pair<iterator, iterator> range_type;
00035 SGObject* Find(UTString name, UTString ns="", UTString cn="") const {
00036 key.name = name;
00037 key.nameSpace = ns;
00038 UTString className = cn;
00039 key.typeInfo.className = className.c_str();
00040 const_iterator lb = lower_bound(&key);
00041 if (lb==end()) return NULL;
00042 SGObjectNamesLess less;
00043 if (less(*lb, &key)) return NULL;
00044 const_iterator it = lb;
00045 ++it;
00046 if (it == end()) return *lb;
00047 if(less(&key, *it)) return *lb;
00048 return NULL;
00049 }
00050 range_type Range(UTString name, UTString ns="", UTString cn=""){
00051 key.name = name;
00052 key.nameSpace = ns;
00053 UTString className = cn;
00054 key.typeInfo.className = className.c_str();
00055 iterator lb = lower_bound(&key);
00056 if (lb==end()) return range_type(end(), end());
00057 SGObjectNamesLess less;
00058 iterator it = lb;
00059 while(it != end() && !less(*it, &key)){
00060 ++it;
00061 }
00062 return range_type(lb, it);
00063 }
00064
00065
00066
00067
00068 bool Add(SGObject* obj);
00069
00070 bool Del(SGObject* key){
00071 iterator it = find(key);
00072 if (it==end()) return false;
00073 erase(it);
00074 return true;
00075 }
00076
00077 bool Del(UTString name){
00078 SGObject key;
00079 key.name = name;
00080 key.AddRef();
00081 iterator it = find(&key);
00082 key.DelRef();
00083 if (it==end()) return false;
00084 erase(it);
00085 return true;
00086 }
00087 };
00088 inline std::ostream& operator << (std::ostream& os, const SGObjectNames& ns){
00089 ns.Print(os); return os;
00090 }
00091
00092
00093
00094
00095
00096
00097
00098
00099 class SPR_DLL SGScene:public SGObject{
00100 SGOBJECTDEF(SGScene);
00101 protected:
00102
00103 SGObjectNames names;
00104
00105 SGBehaviorEngines behaviors;
00106
00107 SGRenderEngines renderers;
00108
00109 UTRef<SGFrame> world;
00110
00111 double timeStep;
00112
00113 unsigned int count;
00114
00115 double velocityLossPerStep;
00116 public:
00117
00118 SGScene();
00119
00120 ~SGScene(){Clear();}
00121
00122 SGBehaviorEngines& GetBehaviors(){ return behaviors; }
00123 const SGBehaviorEngines& GetBehaviors() const { return behaviors; }
00124
00125 SGEngines& GetRenderers(){
00126 return renderers;
00127 }
00128
00129
00130 SGFrame* GetWorld(){ return world; }
00131
00132 SGObject* FindObject(UTString name, UTString ns=""){ return names.Find(name, ns); }
00133
00134 template <class T> void FindObject(UTRef<T>& t, UTString name, UTString ns=""){
00135 T* p;
00136 FindObject(p, name, ns);
00137 t = p;
00138 }
00139 template <class T> void FindObject(T*& t, UTString name, UTString ns=""){
00140 SGObject* p = names.Find(name, ns, GETCLASSNAMES(T));
00141 t = DCAST(T, p);
00142 }
00143 typedef SGObjectNames::iterator SetIt;
00144 typedef std::pair<SetIt, SetIt> SetRange;
00145 SetRange RangeObject(UTString n){ return names.Range(n); }
00146
00147 SGObjectNames::TNameMap& GetNameMap(){ return names.nameMap; }
00148
00149
00150 double GetTimeStep()const{return timeStep;}
00151
00152 void SetTimeStep(double dt);
00153
00154 double GetVelocityLossPerStep(){ return velocityLossPerStep; }
00155
00156 double GetVelocityLoss();
00157
00158 void SetVelocityLoss(double dt);
00159
00160 unsigned GetCount()const{return count;}
00161
00162 void SetCount(unsigned c){count = c;}
00163
00164 void Step();
00165
00166 void ClearForce();
00167
00168 void GenerateForce();
00169
00170 void Integrate();
00171
00172 void Render();
00173
00174 void Loaded(SGScene* scene);
00175
00176 void Clear();
00177
00178 void ClearName();
00179
00180 void Print(std::ostream& os) const { names.Print(os); }
00181
00182 virtual size_t NChildObjects();
00183
00184 virtual SGObject* ChildObject(size_t i);
00185
00186 virtual bool AddChildObject(SGObject* o, SGScene* s);
00187
00188 virtual bool DelChildObject(SGObject* o, SGScene* s);
00189
00190 virtual const UTTypeInfo** ChildCandidates();
00191
00192
00193 virtual void LoadState(const SGBehaviorStates& states);
00194
00195 virtual void SaveState(SGBehaviorStates& states) const;
00196 protected:
00197
00198 friend class SGObject;
00199 };
00200
00201 }
00202 #endif