00001
00002
00003
00004
00005
00006
00011 #include <iterator>
00012
00013
00014
00015
00016 #include "Planet.h"
00017 #include "Sun.h"
00018
00019 #include "ObjectManager.h"
00020 #include "Configuration.h"
00021 #include "TextureManager.h"
00022 #include "Window.h"
00023
00024 #include "Message.h"
00025 #include "random.h"
00026
00027 #define ObjectCOUNT 2
00028
00029 char ObjectNames[ObjectCOUNT][10] = {"planet","sun"} ;
00030
00031 ObjectManager ourObjectManager;
00032
00038 Object * ObjectManager::createFreeObject(const string & objectType) {
00039 Object * obj;
00040
00041 if(objectType == "planet") {
00042 obj = new Planet();
00043 } else if(objectType == "sun") {
00044 obj = new Sun();
00045 } else {
00046 Message::msg(Message::ERROR, "Warning: Object type ",objectType,
00047 " is not available!");
00048 obj = new Planet();
00049 }
00050 return obj;
00051 }
00052
00057 void ObjectManager::addFreeObjectToObjectList(Object * obj) {
00058 if(obj->getDrawPriority() == 0)
00059 objects.push_back(obj);
00060 else
00061 objects.push_front(obj);
00062 return;
00063 }
00064
00070 Object * ObjectManager::createObject(const string & objectType) {
00071 Object * obj=createFreeObject(objectType);
00072 addFreeObjectToObjectList(obj);
00073 return obj;
00074 }
00075
00079 Object * ObjectManager::getFarthestAwayObject() {
00080 double farthestDistanz_=0;
00081 Object * farthestObject_=0;
00082
00083
00084 for (objectList::iterator i=objects.begin() ; i != objects.end();i ++ ) {
00085 double aktDist_= (*i)->pos.length();
00086 if(aktDist_ >=farthestDistanz_) {
00087 farthestDistanz_=aktDist_;
00088 farthestObject_ = *i;
00089 }
00090 }
00091 return farthestObject_;
00092 }
00093
00104 void ObjectManager::createRandomObject(string type, int mMass,int mPos,
00105 int mR, int mV) {
00106
00107 Object * neu=createObject(type);
00108
00109
00110 neu->v.x = potenzRand(0,mV);
00111 neu->v.y = potenzRand(0,mV);
00112 neu->v.z = potenzRand(0,mV);
00113 neu->pos.x = potenzRand(1,mPos,true);
00114 neu->pos.y = potenzRand(1,mPos,true);
00115 neu->pos.z = potenzRand(1,mPos,true);
00116 neu->mass = potenzRand(1,mMass);
00117 neu->radius = potenzRand(1,mR);
00118 }
00119
00120
00121
00127 void ObjectManager::nextTimeStep() {
00128 oldPositionTimeSteps++;
00129
00130
00131 oldPositions.push_back(OldPosition(Vector(),0,true));
00132
00133
00134 for (objectList::iterator it=objects.begin() ; it != objects.end();it ++) {
00135 oldPositions.push_back(OldPosition((*it)->pos,(*it)->radius));
00136 }
00137
00138
00139 if(oldPositionTimeSteps > ourConfiguration.drawOldPositionsTimeSteps) {
00140 int deleteStepCount =
00141 oldPositionTimeSteps-ourConfiguration.drawOldPositionsTimeSteps;
00142
00143 for(int i=0; i<deleteStepCount; i++) {
00144
00145 oldPositions.pop_front();
00146
00147
00148 while(! oldPositions.front().isFirstInThisStep && ! oldPositions.empty())
00149
00150
00151 oldPositions.pop_front();
00152 }
00153
00154 oldPositionTimeSteps-=deleteStepCount;
00155 }
00156 }
00157
00161 void ObjectManager::drawObjects() {
00162
00163 for (objectList::iterator it=objects.begin() ; it != objects.end();it ++)
00164 (*it)->draw();
00165
00166
00167
00168 if(ourConfiguration.drawOldPositions) {
00169 double alphaBlending=1.0;
00170 glEnable(GL_BLEND);
00171 glDisable(GL_DEPTH_TEST);
00172 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00173
00174 GLfloat emission[] = { 1.0, 1.0, 1.0, 1.0 };
00175 glMaterialfv(GL_FRONT, GL_EMISSION, emission);
00176
00177 GLUquadricObj * planetQuadric = gluNewQuadric();
00178
00179
00180 int aktStep = 0;
00181
00182 for(list<OldPosition>::iterator it= oldPositions.begin(); it !=
00183 oldPositions.end(); it++) {
00184
00185 if(it->isFirstInThisStep) {
00186 aktStep++;
00187 alphaBlending = sqrt(aktStep / (double) oldPositionTimeSteps);
00188 glColor4f(1.0, 1.0, 1.0, alphaBlending);
00189 continue;
00190 }
00191
00192 glPushMatrix();
00193 Vector adaptedpos = Object::getDistanceModified(it->pos);
00194 glTranslatef(adaptedpos.x,adaptedpos.y,adaptedpos.z);
00195
00196 gluSphere( planetQuadric,Object::getRadiusModified(it->radius),
00197 ourConfiguration.sphereDetaillevel,
00198 ourConfiguration.sphereDetaillevel );
00199
00200 glPopMatrix();
00201 }
00202 gluDeleteQuadric(planetQuadric);
00203 glColor4f(1.0, 1.0, 1.0, 1.0);
00204 glDisable(GL_BLEND);
00205 glEnable(GL_DEPTH_TEST);
00206
00207 }
00208
00209 }
00210
00215 void ObjectManager::deleteObject(objectList::iterator & it) {
00216 objectList::iterator it2 = it;
00217 it++;
00218
00219
00220
00221 if(*it2 == ourConfiguration.clickedObject)
00222 Window::selectObject(0);
00223
00224
00225 delete (*it2);
00226
00227 objects.erase(it2);
00228 }
00229
00234 void ObjectManager::deleteObject(Object * obj) {
00235 for(objectList::iterator it= objects.begin() ; it != objects.end(); it++) {
00236 if(*it == obj) {
00237
00238 deleteObject(it);
00239 return;
00240 }
00241 }
00242 }
00243
00247 void ObjectManager::deleteAllObjects() {
00248
00249 ourTextureManager.freeTextures();
00250
00251 oldPositions.clear();
00252
00253 oldPositionTimeSteps = 0;
00254
00255 for (objectList::iterator it=objects.begin() ; it != objects.end();it ++)
00256 delete(*it);
00257
00258 objects.clear();
00259 }
00260
00264 ObjectManager::ObjectManager() {
00265 oldPositionTimeSteps = 0;
00266 }
00267
00268
00272 ObjectManager::~ObjectManager() {
00273 deleteAllObjects();
00274 }
00275
00280 void ObjectManager::load(istream & FILE) {
00281 string s;
00282
00283
00284 while (! FILE.fail() ) {
00285 FILE >> s;
00286 if(s == "!!BEGINOBJECTS!!")
00287 break;
00288 }
00289 if(s != "!!BEGINOBJECTS!!")
00290 Message::msg(Message::ERROR, "BEGINOBJECTS MARK NOT FOUND!");
00291
00292
00293 deleteAllObjects();
00294
00295 int count=0;
00296 while(! FILE.fail() ) {
00297 FILE >> s;
00298 if(s == "1000")
00299 break;
00300 Object * obj;
00301 if(s == "SELECT") {
00302 FILE >> s;
00303
00304 obj = createObject(s);
00305 ourConfiguration.clickedObject = obj;
00306 } else {
00307 obj = createObject(s);
00308 }
00309
00310 count++;
00311
00312 obj->load(FILE);
00313
00314 }
00315 FILE >> s;
00316 }
00317
00322 void ObjectManager::save(ostream & FILE) {
00323 FILE << "!!BEGINOBJECTS!!" << endl;
00324 for(objectList::iterator it= objects.begin() ; it != objects.end(); it++) {
00325 if(*it == ourConfiguration.clickedObject)
00326
00327 FILE << "SELECT ";
00328 (*it)->save(FILE);
00329 }
00330
00331 FILE << "1000 !!ENDOBJECTS!!"<< endl;
00332 }
00333
00337 void ObjectManager::viewDetailInformationForAllObjects() {
00338 cout << endl;
00339 for (objectList::iterator i=objects.begin() ; i != objects.end();i ++) {
00340 cout << " Adresse:" << *i << " pos:" << (*i)->pos << " v:" << (*i)->v
00341 << " r:" << (*i)->radius << " mass:" << (*i)->mass << endl;
00342 }
00343
00344 cout << "OBJEKTE: " << objects.size() << endl;
00345 }
00346
00350 int ObjectManager::getTypeCount() {
00351 return ObjectCOUNT;
00352 }
00353
00359 int ObjectManager::TypeToInt(const string & objType) {
00360 for(int i=0; i < ObjectCOUNT; i++)
00361 if(objType == ObjectNames[i])
00362 return i;
00363 cerr << "Unknown Object detected (ObjectManager): " << objType << endl;
00364 return 0;
00365 }
00366
00372 char * ObjectManager::NumToType(const int typenum) {
00373 if(typenum < ObjectCOUNT)
00374 return ObjectNames[typenum];
00375
00376 cerr << "Unknown Object detected (ObjectManager): " << typenum << endl;
00377 return ObjectNames[0];
00378 }