00001 /* 00002 SagaEngine library 00003 Copyright (c) 2002-2006 Skalden Studio AS 00004 00005 This software is provided 'as-is', without any express or implied 00006 warranty. In no event will the authors be held liable for any 00007 damages arising from the use of this software. 00008 00009 Permission is granted to distribute the library under the terms of the 00010 Q Public License version 1.0. Be sure to read and understand the license 00011 before using the library. It should be included here, or you may read it 00012 at http://www.trolltech.com/products/qt/licenses/licensing/qpl 00013 00014 The original version of this library can be located at: 00015 http://www.sagaengine.com/ 00016 00017 Rune Myrland 00018 rune@skalden.com 00019 */ 00020 00021 #include "ThingMOManager.hpp" 00022 #include "ThingMO.hpp" 00023 #include "ThingMultiMO.hpp" 00024 #include "ThingMOInfo.hpp" 00025 #include "ThingMOInfoList.hpp" 00026 #include "ThingMOFactory.hpp" 00027 #include "ThingMovableObjectFactory.hpp" 00028 #include "ThingEntityFactory.hpp" 00029 #include "ThingDebugFactory.hpp" 00030 #include "ThingParticleFactory.hpp" 00031 #include "ThingMultiMOFactory.hpp" 00032 #include "ThingLightFactory.hpp" 00033 #include "ThingBillboardFactory.hpp" 00034 #include "ThingStaticGeometryFactory.hpp" 00035 #include "O3dThingComponent.hpp" 00036 00037 using namespace se_core; 00038 00039 namespace se_ogre { 00040 00041 ThingMOManager 00042 ::ThingMOManager() : infoCount_(0), factoryCount_(0) { 00043 infoList_ = new ThingMOInfoList*[MAX_INFOS]; 00044 factories_ = new const ThingMOFactory*[MAX_FACTORIES]; 00045 00046 static const ThingMovableObjectFactory tmofMovableObject("default"); 00047 static const ThingEntityFactory tmofEntity; 00048 static const ThingDebugFactory tmofDebug; 00049 static const ThingLightFactory tmofLight; 00050 static const ThingBillboardFactory tmofBillboard; 00051 static const ThingStaticGeometryFactory tmofStaticGeometry; 00052 static const ThingParticleFactory tmofParticle; 00053 static const ThingMultiMOFactory tmofMultiMO; 00054 } 00055 00056 00057 ThingMOManager 00058 ::~ThingMOManager() { 00059 reset(); 00060 for(int i = 0; i < infoCount_; ++i) { 00061 delete infoList_[i]; 00062 } 00063 delete[] infoList_; 00064 delete[] factories_; 00065 } 00066 00067 /*23283940 Elin Bjelkerud 00068 void ThingMOManager 00069 ::addInfo(ThingMOInfo* info) { 00070 // Insert - keep sorted 00071 int i = infoCount_; 00072 while(i > 0 && info->thingType_.compare(info_[i - 1]->thingType_) < 0) { 00073 info_[ i ] = info_[i - 1]; 00074 --i; 00075 } 00076 info_[i] = info; 00077 ++infoCount_; 00078 LogDetail("Added meshinfo for: " << info->thingType_); 00079 } 00080 */ 00081 00082 00083 void ThingMOManager 00084 ::addInfoList(ThingMOInfoList* infoList) { 00085 Assert(infoCount_ < MAX_INFOS); 00086 // Insert - keep sorted 00087 int i = infoCount_; 00088 while(i > 0 && infoList->thingType_.compare(infoList_[i - 1]->thingType_) < 0) { 00089 infoList_[ i ] = infoList_[i - 1]; 00090 --i; 00091 } 00092 infoList_[i] = infoList; 00093 ++infoCount_; 00094 //LogDetail("Added meshinfo for: " << infoList->thingType_); 00095 } 00096 00097 00098 void ThingMOManager 00099 ::reset() { 00100 while(infoCount_ > 0) { 00101 --infoCount_; 00102 delete infoList_[ infoCount_ ]; 00103 } 00104 infoCount_ = 0; 00105 } 00106 00107 int ThingMOManager 00108 ::infoIndex(const char* thingType) const { 00109 int min = 0; 00110 int max = infoCount_ - 1; 00111 00112 // Binary search 00113 while(max >= min) { 00114 int middle = (min + max) >> 1; 00115 int cmp = infoList_[ middle ]->thingType_.compare( thingType ); 00116 if(cmp < 0) { 00117 min = middle + 1; 00118 } 00119 else if(cmp > 0) { 00120 max = middle - 1; 00121 } 00122 else { 00123 return middle; 00124 } 00125 } 00126 00127 return -1; 00128 } 00129 00130 00131 const ThingMOInfo* ThingMOManager 00132 ::info(int index) const { 00133 return infoList_[index]->infos_[0]; 00134 } 00135 00136 00137 const ThingMOInfo* ThingMOManager 00138 ::info(const char* thingType) const { 00139 int index = infoIndex(thingType); 00140 if(index < 0) { 00141 LogDetail("Couldn't find model for " << thingType); 00142 return 0; 00143 } 00144 //LogDetail("Model count for " << thingType << " is " << infoList_[index]->infoCount_); 00145 return infoList_[index]->infos_[0]; 00146 } 00147 00148 00149 const ThingMOInfoList* ThingMOManager 00150 ::infoList(const char* thingType) const { 00151 int index = infoIndex(thingType); 00152 if(index < 0) 00153 return 0; 00154 return infoList_[index]; 00155 } 00156 00157 00158 void ThingMOManager 00159 ::addFactory(const ThingMOFactory* factory) { 00160 Assert(factoryCount_ < MAX_FACTORIES); 00161 // Insert - keep sorted 00162 int i = factoryCount_; 00163 while(i > 0 && factory->type().compare(factories_[i - 1]->type()) < 0) { 00164 factories_[ i ] = factories_[i - 1]; 00165 --i; 00166 } 00167 factories_[i] = factory; 00168 ++factoryCount_; 00169 //LogDetail("Added factory for movable object type: " << factory->type()); 00170 } 00171 00172 00173 int ThingMOManager 00174 ::factoryIndex(const char* moType) const { 00175 Assert(factories_[0]->type().get() != (const char*)(0x1)); 00176 int min = 0; 00177 int max = factoryCount_ - 1; 00178 00179 // Binary search 00180 while(max >= min) { 00181 int middle = (min + max) >> 1; 00182 int cmp = factories_[ middle ]->type().compare( moType ); 00183 if(cmp < 0) { 00184 min = middle + 1; 00185 } 00186 else if(cmp > 0) { 00187 max = middle - 1; 00188 } 00189 else { 00190 return middle; 00191 } 00192 } 00193 00194 return -1; 00195 } 00196 00197 00198 const ThingMOFactory* ThingMOManager 00199 ::factory(const char* moType) const { 00200 int index = factoryIndex(moType); 00201 if(index < 0) 00202 return 0; 00203 return factories_[index]; 00204 } 00205 00206 00207 void ThingMOManager 00208 ::create(O3dThingComponent* parent) { 00209 const ThingMOInfoList* inf = infoList(parent->owner()->name()); 00210 for(int i = 0; i < inf->infoCount_; ++i) { 00211 const char* factoryType = inf->infos_[i]->movableObjectType_.get(); 00212 const ThingMOFactory* f = factory(factoryType); 00213 if(!f) { 00214 LogWarning("Movable object factory type " << factoryType << " does not exist for " << parent->owner()->name()); 00215 continue; 00216 } 00217 ThingMO* child = f->create(*PosComponent::get(*parent), *inf->infos_[i]); 00218 Assert(child); 00219 parent->add(*child); 00220 } 00221 } 00222 00223 00224 void ThingMOManager 00225 ::release(ThingMO* tmo) { 00226 tmo->factory_.release(tmo); 00227 } 00228 00229 00230 void ThingMOManager 00231 ::release(O3dThingComponent* tc) { 00232 delete tc; 00233 } 00234 00235 00236 } 00237
Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:09 2007 by Doxygen version 1.3.9.1.