Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/particleEngine/src/story_entities/world.cc @ 3950

Last change on this file since 3950 was 3950, checked in by bensch, 19 years ago

orxonox/branches/particleEngine: timing is ok now when emitting small numbers/sec

File size: 27.2 KB
Line 
1
2/*
3   orxonox - the future of 3D-vertical-scrollers
4
5   Copyright (C) 2004 orx
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2, or (at your option)
10   any later version.
11
12   ### File Specific:
13   main-programmer: Patrick Boenzli
14   co-programmer: Christian Meyer
15*/
16
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD
18
19#include "world.h"
20
21#include "orxonox.h"
22
23#include "p_node.h"
24#include "null_parent.h"
25#include "helper_parent.h"
26#include "track_node.h"
27#include "world_entity.h"
28#include "player.h"
29#include "camera.h"
30#include "environment.h"
31#include "skysphere.h"
32#include "skybox.h"
33#include "satellite.h"
34#include "terrain.h"
35#include "light.h"
36#include "text_engine.h"
37
38#include "track_manager.h"
39#include "garbage_collector.h"
40#include "animation_player.h"
41
42#include "particle_engine.h"
43#include "particle_system.h"
44#include "particle_emitter.h"
45
46#include "command_node.h"
47#include "glmenu_imagescreen.h"
48#include "list.h"
49
50
51
52using namespace std;
53
54
55WorldInterface* WorldInterface::singletonRef = 0;
56
57
58/**
59   \brief private constructor because of singleton
60*/
61WorldInterface::WorldInterface()
62{
63  this->worldIsInitialized = false;
64  this->worldReference = NULL;
65}
66
67/**
68   \brief public deconstructor
69*/
70WorldInterface::~WorldInterface()
71{
72  this->singletonRef = NULL;
73  this->worldIsInitialized = false;
74  this->worldReference = NULL;
75}
76
77/**
78   \brief gets the singleton instance
79   \returns singleton instance
80*/
81WorldInterface* WorldInterface::getInstance()
82{
83  if( singletonRef == NULL)
84    singletonRef = new WorldInterface();
85  return singletonRef;
86}
87
88
89/**
90   \brief initializes the interface
91   \param reference to the world
92
93   if the worldinterface is not initilizes, there wont be any
94   useable interface
95*/
96void WorldInterface::init(World* world)
97{
98  this->worldReference = world;
99  if( world != NULL)
100    {
101      this->worldIsInitialized = true;
102      PRINTF(3)("WorldInterface up and running\n");
103    }
104}
105
106
107/**
108   \brief gets the entity list from the world
109   \return entity list
110*/
111tList<WorldEntity>* WorldInterface::getEntityList()
112{
113  if( this->worldIsInitialized)
114    return this->worldReference->getEntities();
115  PRINT(1)("Someone tried to use the WorldInterface before it has been initizlized! this can result in SEGFAULTs!\n");
116  return NULL;
117}
118
119
120
121/**
122    \brief create a new World
123   
124    This creates a new empty world!
125*/
126World::World (char* name)
127{
128  this->init(name, -1);
129  //NullParent* np = NullParent::getInstance();
130}
131
132/**
133   \brief creates a new World...
134   \param worldID with this ID
135*/
136World::World (int worldID)
137{
138  this->init(NULL, worldID);
139}
140
141/**
142    \brief remove the World from memory
143   
144    delete everything explicitly, that isn't contained in the parenting tree!
145    things contained in the tree are deleted automaticaly
146*/
147World::~World ()
148{
149  PRINTF(3)("World::~World() - deleting current world\n");
150  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
151  cn->unbind(this->localPlayer);
152  cn->reset();
153
154  ResourceManager::getInstance()->debug();
155  ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL);
156  ResourceManager::getInstance()->debug();
157
158  delete WorldInterface::getInstance();
159
160  delete this->nullParent;
161  delete this->entities;
162  delete this->lightMan;
163  delete this->trackManager;
164  TextEngine::getInstance()->flush();
165
166  AnimationPlayer::getInstance()->debug();
167  delete AnimationPlayer::getInstance(); // this should be at the end of the unloading sequence.
168 
169  ParticleEngine::getInstance()->debug();
170  delete ParticleEngine::getInstance();
171
172  //delete garbagecollecor
173  //delete animator
174
175
176}
177
178/**
179   \brief initializes the world.
180
181   set all stuff here that is world generic and does not use to much memory
182   because the real init() function StoryEntity::init() will be called
183   shortly before start of the game. 
184   since all worlds are initiated/referenced before they will be started.
185   NO LEVEL LOADING HERE - NEVER!
186*/
187void World::init(char* name, int worldID)
188{
189  this->setClassName ("World");
190
191  this->worldName = name;
192  this->debugWorldNr = worldID;
193  this->entities = new tList<WorldEntity>();
194  AnimationPlayer::getInstance(); // initializes the animationPlayer
195
196  ParticleEngine::getInstance();
197}
198
199
200/**
201   \brief this is executed before load
202
203   since the load function sometimes needs data, that has been init before
204   the load and after the proceeding storyentity has finished
205*/
206ErrorMessage World::preLoad()
207{
208  /* init the world interface */
209  WorldInterface* wi = WorldInterface::getInstance();
210  wi->init(this);
211  this->garbageCollector = GarbageCollector::getInstance();
212}
213
214
215/**
216   \brief loads the World by initializing all resources, and set their default values.
217*/
218ErrorMessage World::load()
219{
220  //  BezierCurve* tmpCurve = new BezierCurve();
221  if(this->debugWorldNr != -1)
222    {
223      // initializing Font
224      this->glmis->step();
225      // initializing the TrackManager
226      trackManager = TrackManager::getInstance();
227      //trackManager->addPoint(Vector(0,0,0));
228      trackManager->addPoint(Vector(150, -35, 5));
229      trackManager->addPoint(Vector(200,-35, 5));
230      trackManager->addPoint(Vector(250, -35, 5));
231      trackManager->addPoint(Vector(320,-33,-.55));
232      trackManager->setDuration(2);
233      trackManager->setSavePoint();
234
235      trackManager->addPoint(Vector(410, 0, 0));
236      trackManager->addPoint(Vector(510, 20, -10));
237      trackManager->addPoint(Vector(550, 20, -10));
238      trackManager->addPoint(Vector(570, 20, -10));
239      trackManager->setDuration(5);
240     
241      int fork11, fork12;
242      trackManager->fork(2, &fork11, &fork12);
243      trackManager->workOn(fork11);
244      trackManager->addPoint(Vector(640, 25, -30));
245      trackManager->addPoint(Vector(700, 40, -120));
246      trackManager->addPoint(Vector(800, 50, -150));
247      trackManager->addPoint(Vector(900, 60, -100));
248      trackManager->addPoint(Vector(900, 60, -70));
249      trackManager->addPoint(Vector(990, 65, -15));
250      trackManager->addPoint(Vector(1050, 65, -10));
251      trackManager->addPoint(Vector(1100, 65, -20));
252      trackManager->setDuration(10);
253
254      trackManager->workOn(fork12);
255      trackManager->addPoint(Vector(640, 25, 20));
256      trackManager->addPoint(Vector(670, 50, 120));
257      trackManager->addPoint(Vector(700, 70, 80));
258      trackManager->addPoint(Vector(800, 70, 65));
259      trackManager->addPoint(Vector(850, 65, 65));
260      trackManager->addPoint(Vector(920, 35, 40));
261      trackManager->addPoint(Vector(945, 40, 40));
262      trackManager->addPoint(Vector(970, 24, 40));
263      trackManager->addPoint(Vector(1000, 40, -7));
264      trackManager->setDuration(10);
265     
266
267      trackManager->join(2, fork11, fork12);
268
269      trackManager->workOn(5);
270      trackManager->addPoint(Vector(1200, 60, -50));
271      trackManager->addPoint(Vector(1300, 50, -50));
272      trackManager->addPoint(Vector(1400, 40, -50));
273      trackManager->addPoint(Vector(1500, 40, -60));
274      trackManager->addPoint(Vector(1600, 35, -55));
275      trackManager->addPoint(Vector(1700, 45, -40));
276      trackManager->addPoint(Vector(1750, 60, -40));
277      trackManager->addPoint(Vector(1770, 80, -40));
278      trackManager->addPoint(Vector(1800, 100, -40));
279      trackManager->setDuration(10);
280
281      trackManager->finalize();
282
283     
284      /*monitor progress*/
285      this->glmis->step();
286
287      // LIGHT initialisation
288      lightMan = LightManager::getInstance();
289      lightMan->setAmbientColor(.1,.1,.1);
290      lightMan->addLight();
291      //      lightMan->setAttenuation(1.0, .01, 0.0);
292      //      lightMan->setDiffuseColor(1,1,1);
293      //  lightMan->addLight(1);
294      //  lightMan->setPosition(20, 10, -20);
295      //  lightMan->setDiffuseColor(0,0,0);
296      lightMan->debug();
297
298      switch(this->debugWorldNr)
299        {
300          /*
301            this loads the hard-coded debug world. this only for simplicity and will be
302            removed by a reald world-loader, which interprets a world-file.
303            if you want to add an own debug world, just add a case DEBUG_WORLD_[nr] and
304            make whatever you want...
305           */
306        case DEBUG_WORLD_0:
307          {
308            lightMan->setPosition(-5.0, 10.0, -40.0);
309            this->nullParent = NullParent::getInstance ();
310            this->nullParent->setName ("NullParent");
311
312            // !\todo old track-system has to be removed
313
314            //create helper for player
315            //HelperParent* hp = new HelperParent ();
316            /* the player has to be added to this helper */
317
318            // create a player
319            this->localPlayer = new Player ();
320            this->localPlayer->setName ("player");
321            this->spawn (this->localPlayer);
322            /*monitor progress*/
323            //this->glmis->step();
324            this->glmis->step();
325
326            // bind input
327            Orxonox *orx = Orxonox::getInstance ();
328            orx->getLocalInput()->bind (this->localPlayer);
329           
330            // bind camera
331            this->localCamera = new Camera();
332            this->localCamera->setName ("camera");
333           
334            /*monitor progress*/
335            this->glmis->step();
336
337            // Create SkySphere
338            //      this->skySphere = new Skysphere("../data/pictures/sky-replace.jpg");
339            //      this->skySphere->setName("SkySphere");
340            //      this->localCamera->addChild(this->skySphere);
341            //      this->spawn(this->skySphere);
342            skyBox = new SkyBox();
343            skyBox->setTexture("pictures/sky/skybox", "jpg");
344            skyBox->setParent(localCamera);
345            this->spawn(skyBox);
346
347            /*monitor progress*/
348            this->glmis->step();
349
350           
351            WorldEntity* env = new Environment();
352            env->setName ("env");
353            this->spawn(env);
354
355           
356            /*
357            Vector* es = new Vector (10, 5, 0);
358            Quaternion* qs = new Quaternion ();
359            WorldEntity* pr = new Primitive(P_CYLINDER);
360            pr->setName("primitive");
361            this->spawn(pr, this->localPlayer, es, qs, PNODE_MOVEMENT);
362            */
363
364            /*monitor progress*/
365            this->glmis->step();
366
367            //      trackManager->setBindSlave(env);
368            PNode* tn = trackManager->getTrackNode();
369            tn->addChild(this->localPlayer);
370            this->localCamera->lookAt(tn);
371
372            //localCamera->setParent(TrackNode::getInstance());
373            tn->addChild(this->localCamera);
374            //      localCamera->lookAt(tn);
375            this->localPlayer->setMode(PNODE_ALL);
376            //Vector* cameraOffset = new Vector (0, 5, -10);
377            trackManager->condition(2, LEFTRIGHT, this->localPlayer);
378            this->glmis->step();
379           
380            ParticleEmitter* testEmitter,* testEmitter2;
381            ParticleSystem* testSystem;
382            testEmitter = new ParticleEmitter(Vector(-1,0,0), .4, 60.0, 0);
383            testEmitter->setParent(localPlayer);
384            testEmitter->setRelCoor(Vector(-3, 0, 0));
385            testEmitter->setName("ship emitter");
386            /*
387            testEmitter2 = new ParticleEmitter(Vector(1,0,0), .1, 1.0, 1);
388            testEmitter2->setParent(tn);
389            testEmitter2->setName("crap emitter");
390            */
391            testSystem = new ParticleSystem(10000);
392            testSystem->setLifeSpan(1, .2);
393            testSystem->setRadius(3.0, 0.0, .5);
394            testSystem->setConserve(.8);
395            testSystem->setInheritSpeed(1);
396            testSystem->setName("system");
397
398            ParticleEngine::getInstance()->addConnection(testEmitter, testSystem);
399            //      ParticleEngine::getInstance()->addConnection(testEmitter2, testSystem);
400            ParticleEngine::getInstance()->debug();
401
402            break;
403          }
404        case DEBUG_WORLD_1:
405          {
406            lightMan->setPosition(.0, .0, .0);
407            lightMan->setAttenuation(1.0, .01, 0.0);
408            lightMan->setSpecularColor(1,0,0);
409            this->nullParent = NullParent::getInstance ();
410            this->nullParent->setName ("NullParent");
411
412            // create a player
413            WorldEntity* myPlayer = new Player();
414            myPlayer->setName ("player");
415            this->spawn(myPlayer);
416            this->localPlayer = myPlayer;           
417           
418            // bind input
419            Orxonox *orx = Orxonox::getInstance();
420            orx->getLocalInput()->bind (myPlayer);
421           
422            // bind camera
423            this->localCamera = new Camera ();
424            this->localCamera->setName ("camera");
425            this->localCamera->lookAt(LightManager::getInstance()->getLight(0));
426            this->localCamera->setParent(this->localPlayer);
427
428            // Create SkySphere
429            skySphere = new Skysphere("../data/pictures/sky-replace.jpg");
430            this->localPlayer->addChild(this->skySphere);
431            this->spawn(this->skySphere);
432            Vector* es = new Vector (20, 0, 0);
433            Quaternion* qs = new Quaternion ();
434
435            lightMan->getLight(0)->setParent(trackManager->getTrackNode());
436            break;
437          }
438        case DEBUG_WORLD_2:
439          {
440            lightMan->setAmbientColor(.1,.1,.1);
441            lightMan->addLight();
442            lightMan->setPosition(-5.0, 10.0, -40.0);
443            this->nullParent = NullParent::getInstance ();
444            this->nullParent->setName ("NullParent");
445
446            // !\todo old track-system has to be removed
447
448            //create helper for player
449            //HelperParent* hp = new HelperParent ();
450            /* the player has to be added to this helper */
451
452            // create a player
453            this->localPlayer = new Player ();
454            this->localPlayer->setName ("player");
455            this->spawn (this->localPlayer);
456            /*monitor progress*/
457            //this->glmis->step();         
458            this->glmis->step();
459
460            // bind input
461            Orxonox *orx = Orxonox::getInstance ();
462            orx->getLocalInput()->bind (this->localPlayer);
463           
464            // bind camera
465            this->localCamera = new Camera();
466            this->localCamera->setName ("camera");
467            this->localCamera->lookAt(this->localPlayer);
468            this->localCamera->setParent(this->localPlayer);
469           
470            /*monitor progress*/
471            this->glmis->step();
472
473            // Create SkySphere
474            this->skySphere = new Skysphere("../data/pictures/sky-replace.jpg");
475            this->skySphere->setName("SkySphere");
476            this->spawn(this->skySphere);
477            this->localCamera->addChild(this->skySphere);
478            this->skySphere->setMode(PNODE_MOVEMENT);
479            /*monitor progress*/
480            this->glmis->step();
481
482
483            WorldEntity* baseNode = new Satellite(Vector(1,0,1), 1.2);
484            this->localPlayer->addChild(baseNode);
485            baseNode->setRelCoor(Vector(10.0, 2.0, 1.0));
486            this->spawn(baseNode);
487
488            WorldEntity* secondNode = new Satellite(Vector(0,0,1), 2.0);
489            baseNode->addChild(secondNode);
490            secondNode->setRelCoor(Vector(0.0, 0.0, 3.0));
491            this->spawn(secondNode);
492
493
494            WorldEntity* thirdNode = new Satellite(Vector(0,0,1), 1.0);
495            secondNode->addChild(thirdNode);
496            thirdNode->setRelCoor(Vector(2.0, 0.0, 0.0));
497            this->spawn(thirdNode);
498
499           
500           
501
502            WorldEntity* b = new Environment();
503            this->localPlayer->addChild(b);
504            b->setRelCoor(Vector(10.0, 1.0, 1.0));
505            this->spawn(b);
506
507           
508            WorldEntity* c = new Environment();
509            this->localPlayer->addChild(c);
510            c->setRelCoor(Vector(10.0, 2.0, -1.0));
511            this->spawn(c);
512           
513            /*     
514                  KeyFrame* f1 = new KeyFrame;
515                  f1->position = new Vector(-1.1, 0.0, 2.6);
516                  f1->direction = new Quaternion();
517                  f1->time = 1.0;
518                  f1->mode = NEG_EXP;
519                 
520                 
521                  KeyFrame* f2 = new KeyFrame;
522                  f2->position = new Vector(-2.1, 0.0, 2.6);
523                  f2->direction = new Quaternion();
524                  f2->time = 0.1;
525                  f2->mode = NEG_EXP;
526                 
527                  KeyFrame* f3 = new KeyFrame;
528                  f3->position = new Vector(10.0, 2.0, -1.0);
529                  f3->direction = new Quaternion();
530                  f3->time = 0.2;
531                  f3->mode = NEG_EXP;
532                 
533                  KeyFrame* f4 = new KeyFrame;
534                  f4->position = new Vector(10.0, 5.0, -1.0);
535                  f4->direction = new Quaternion();
536                  f4->time = 1.0;
537                  f4->mode = NEG_EXP;
538                 
539                 
540                 
541                  this->simpleAnimation->animatorBegin();
542                  this->simpleAnimation->selectObject(b);
543                  this->simpleAnimation->setAnimationMode(SINGLE);
544                  this->simpleAnimation->addKeyFrame(f1);
545                  this->simpleAnimation->addKeyFrame(f2);
546                  this->simpleAnimation->start();
547                  this->simpleAnimation->selectObject(c);
548                  this->simpleAnimation->addKeyFrame(f3);
549                  this->simpleAnimation->addKeyFrame(f4);
550                  this->simpleAnimation->start();
551                  this->simpleAnimation->animatorEnd();
552            */
553
554            /*
555            Vector* es = new Vector (10, 5, 0);
556            Quaternion* qs = new Quaternion ();
557            WorldEntity* pr = new Primitive(P_CYLINDER);
558            pr->setName("primitive");
559            this->spawn(pr, this->localPlayer, es, qs, PNODE_MOVEMENT);
560            */
561
562            /*monitor progress*/
563            this->glmis->step();
564
565            //      trackManager->setBindSlave(env);
566            PNode* tn = trackManager->getTrackNode();
567            tn->addChild(this->localPlayer);
568
569            //localCamera->setParent(TrackNode::getInstance());
570            tn->addChild(this->localCamera);
571            //      localCamera->lookAt(tn);
572            this->localPlayer->setMode(PNODE_ALL);
573            //Vector* cameraOffset = new Vector (0, 5, -10);
574            trackManager->condition(2, LEFTRIGHT, this->localPlayer);
575            this->glmis->step();
576
577            break;
578          }
579        default:
580          printf("World::load() - no world with ID %i found", this->debugWorldNr );
581        }
582    }
583  else if(this->worldName != NULL)
584    {
585
586    }
587
588  // initialize debug coord system
589  objectList = glGenLists(1);
590  glNewList (objectList, GL_COMPILE);
591 
592  //  trackManager->drawGraph(.01);
593  trackManager->debug(2);
594  glEndList();
595
596  terrain = new Terrain("../data/worlds/newGround.obj");
597  terrain->setRelCoor(Vector(0,-10,0));
598  this->spawn(terrain);
599
600}
601
602
603/**
604   \brief initializes a new World shortly before start
605
606   this is the function, that will be loaded shortly before the world is
607   started
608*/
609ErrorMessage World::init()
610{
611  this->bPause = false;
612  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
613  cn->addToWorld(this);
614  cn->enable(true);
615}
616
617
618/**
619   \brief starts the World
620*/
621ErrorMessage World::start()
622{
623  PRINTF(3)("World::start() - starting current World: nr %i\n", this->debugWorldNr);
624  this->bQuitOrxonox = false;
625  this->bQuitCurrentGame = false;
626  this->mainLoop();
627}
628
629/**
630   \brief stops the world.
631
632   This happens, when the player decides to end the Level.
633*/
634ErrorMessage World::stop()
635{
636  PRINTF(3)("World::stop() - got stop signal\n");
637  this->bQuitCurrentGame = true;
638}
639
640/**
641   \brief pauses the Game
642*/
643ErrorMessage World::pause()
644{
645  this->isPaused = true;
646}
647
648/**
649   \brief ends the pause Phase
650*/
651ErrorMessage World::resume()
652{
653  this->isPaused = false;
654}
655
656/**
657   \brief destroys the World
658*/
659ErrorMessage World::destroy()
660{
661
662}
663
664/**
665   \brief shows the loading screen
666*/
667void World::displayLoadScreen ()
668{
669  PRINTF(3)("World::displayLoadScreen - start\n"); 
670 
671  //GLMenuImageScreen*
672  this->glmis = GLMenuImageScreen::getInstance();
673  this->glmis->init();
674  this->glmis->setMaximum(8);
675  this->glmis->draw();
676 
677  PRINTF(3)("World::displayLoadScreen - end\n"); 
678}
679
680/**
681   \brief removes the loadscreen, and changes over to the game
682
683   \todo take out the delay
684*/
685void World::releaseLoadScreen ()
686{
687  PRINTF(3)("World::releaseLoadScreen - start\n"); 
688  this->glmis->setValue(this->glmis->getMaximum());
689  //SDL_Delay(500);
690  PRINTF(3)("World::releaseLoadScreen - end\n"); 
691}
692
693
694/**
695   \brief gets the list of entities from the world
696   \returns entity list
697*/
698tList<WorldEntity>* World::getEntities()
699{
700  return this->entities;
701}
702
703
704/**
705   \brief this returns the current game time
706   \returns elapsed game time
707*/
708double World::getGameTime()
709{
710  return this->gameTime;
711}
712
713
714/**
715    \brief checks for collisions
716   
717    This method runs through all WorldEntities known to the world and checks for collisions
718    between them. In case of collisions the collide() method of the corresponding entities
719    is called.
720*/
721void World::collide ()
722{
723  /*
724  List *a, *b;
725  WorldEntity *aobj, *bobj;
726   
727  a = entities;
728 
729  while( a != NULL)
730    {
731      aobj = a->nextElement();
732      if( aobj->bCollide && aobj->collisioncluster != NULL)
733        {
734          b = a->nextElement();
735          while( b != NULL )
736            {
737              bobj = b->nextElement();
738              if( bobj->bCollide && bobj->collisioncluster != NULL )
739                {
740                  unsigned long ahitflg, bhitflg;
741                  if( check_collision ( &aobj->place, aobj->collisioncluster,
742                                        &ahitflg, &bobj->place, bobj->collisioncluster,
743                                        &bhitflg) );
744                  {
745                    aobj->collide (bobj, ahitflg, bhitflg);
746                    bobj->collide (aobj, bhitflg, ahitflg);
747                  }
748                }
749              b = b->nextElement();
750            }
751        }
752      a = a->enumerate();
753    }
754  */
755}
756
757/**
758    \brief runs through all entities calling their draw() methods
759*/
760void World::draw ()
761{
762  /* draw entities */
763  WorldEntity* entity;
764  glLoadIdentity();
765
766  //entity = this->entities->enumerate();
767  tIterator<WorldEntity>* iterator = this->entities->getIterator();
768  entity = iterator->nextElement();
769  while( entity != NULL ) 
770    { 
771      if( entity->bDraw ) entity->draw();
772      //entity = this->entities->nextElement();
773      entity = iterator->nextElement();
774    }
775  delete iterator;
776 
777  glCallList (objectList);
778
779  TextEngine::getInstance()->draw();
780
781  ParticleEngine::getInstance()->draw();
782
783  lightMan->draw(); // must be at the end of the drawing procedure, otherwise Light cannot be handled as PNodes //
784}
785
786
787/**
788   \brief function to put your own debug stuff into it. it can display informations about
789   the current class/procedure
790*/
791void World::debug()
792{
793  PRINTF(2)("debug() - starting debug\n");
794  PNode* p1 = NullParent::getInstance ();
795  PNode* p2 = new PNode (Vector(2, 2, 2), p1);
796  PNode* p3 = new PNode (Vector(4, 4, 4), p1);
797  PNode* p4 = new PNode (Vector(6, 6, 6), p2);
798
799  p1->debug ();
800  p2->debug ();
801  p3->debug ();
802  p4->debug ();
803
804  p1->shiftCoor (Vector(-1, -1, -1));
805
806  printf("World::debug() - shift\n");
807  p1->debug ();
808  p2->debug ();
809  p3->debug ();
810  p4->debug ();
811 
812  p1->update (0);
813
814  printf ("World::debug() - update\n");
815  p1->debug ();
816  p2->debug ();
817  p3->debug ();
818  p4->debug ();
819
820  p2->shiftCoor (Vector(-1, -1, -1));
821  p1->update (0);
822
823  p1->debug ();
824  p2->debug ();
825  p3->debug ();
826  p4->debug ();
827
828  p2->setAbsCoor (Vector(1,2,3));
829
830
831 p1->update (0);
832
833  p1->debug ();
834  p2->debug ();
835  p3->debug ();
836  p4->debug ();
837
838  delete p1;
839 
840 
841  /*
842  WorldEntity* entity;
843  printf("counting all entities\n");
844  printf("World::debug() - enumerate()\n");
845  entity = entities->enumerate(); 
846  while( entity != NULL )
847    {
848      if( entity->bDraw ) printf("got an entity\n");
849      entity = entities->nextElement();
850    }
851  */
852}
853
854
855/**
856  \brief main loop of the world: executing all world relevant function
857
858  in this loop we synchronize (if networked), handle input events, give the heart-beat to
859  all other member-entities of the world (tick to player, enemies etc.), checking for
860  collisions drawing everything to the screen.
861*/
862void World::mainLoop()
863{
864  this->lastFrame = SDL_GetTicks ();
865  PRINTF(3)("World::mainLoop() - Entering main loop\n");
866  while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* \todo implement pause */
867    {
868      PRINTF(3)("World::mainloop() - number of entities: %i\n", this->entities->getSize());
869      // Network
870      this->synchronize ();
871      // Process input
872      this->handleInput ();
873      if( this->bQuitCurrentGame || this->bQuitOrxonox)
874          break;
875      // Process time
876      this->tick ();
877      // Update the state
878      this->update ();     
879      // Process collision
880      this->collide ();
881      // Draw
882      this->display ();
883
884      //      for( int i = 0; i < 5000000; i++) {}
885      /* \todo this is to slow down the program for openGl Software emulator computers, reimplement*/
886    }
887  PRINTF(3)("World::mainLoop() - Exiting the main loop\n");
888}
889
890
891/**
892   \brief synchronize local data with remote data
893*/
894void World::synchronize ()
895{
896  // Get remote input
897  // Update synchronizables
898}
899
900
901/**
902   \brief run all input processing
903
904   the command node is the central input event dispatcher. the node uses the even-queue from
905   sdl and has its own event-passing-queue.
906*/
907void World::handleInput ()
908{
909  // localinput
910  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
911  cn->process();
912  // remoteinput
913}
914
915
916/**
917   \brief advance the timeline
918
919   this calculates the time used to process one frame (with all input handling, drawing, etc)
920   the time is mesured in ms and passed to all world-entities and other classes that need
921   a heart-beat.
922*/
923void World::tick ()
924{
925  Uint32 currentFrame = SDL_GetTicks();
926  if(!this->bPause)
927    {
928      this->dt = currentFrame - this->lastFrame;
929     
930      if( this->dt > 0)
931        {
932          float fps = 1000/dt;
933
934          // temporary, only for showing how fast the text-engine is
935          char tmpChar[20];
936          sprintf(tmpChar, "fps: %4.0f", fps);
937        }
938      else
939        {
940          /* the frame-rate is limited to 100 frames per second, all other things are for
941             nothing.
942          */
943          PRINTF(2)("fps = 1000 - frame rate is adjusted\n");
944          SDL_Delay(10);
945          this->dt = 10;
946        }
947      //this->timeSlice (dt);
948     
949      /* function to let all entities tick (iterate through list) */
950      float seconds = this->dt / 1000.0;     
951      this->gameTime += seconds;
952      //entity = entities->enumerate();
953      tIterator<WorldEntity>* iterator = this->entities->getIterator();
954      WorldEntity* entity = iterator->nextElement();
955      while( entity != NULL) 
956        { 
957          entity->tick (seconds);
958          entity = iterator->nextElement();
959        }
960      delete iterator;
961      //skySphere->updatePosition(localCamera->absCoordinate);
962     
963      /* update tick the rest */
964      this->trackManager->tick(this->dt);
965      this->localCamera->tick(this->dt);
966      this->garbageCollector->tick(seconds);
967
968      AnimationPlayer::getInstance()->tick(seconds);
969
970      ParticleEngine::getInstance()->tick(seconds);
971    }
972  this->lastFrame = currentFrame;
973}
974
975
976/**
977   \brief this function gives the world a consistant state
978
979   after ticking (updating the world state) this will give a constistant
980   state to the whole system.
981*/
982void World::update()
983{
984  this->garbageCollector->update();
985  this->nullParent->update (dt);
986}
987
988
989/**
990   \brief render the current frame
991   
992   clear all buffers and draw the world
993*/
994void World::display ()
995{
996  // clear buffer
997  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
998  // set camera
999  this->localCamera->apply ();
1000  // draw world
1001  this->draw();
1002  // draw HUD
1003  /* \todo draw HUD */
1004  // flip buffers
1005  SDL_GL_SwapBuffers();
1006  //SDL_Surface* screen = Orxonox::getInstance()->getScreen ();
1007  //SDL_Flip (screen);
1008}
1009
1010
1011/**
1012   \brief add and spawn a new entity to this world
1013   \param entity to be added
1014*/
1015void World::spawn(WorldEntity* entity)
1016{
1017  this->entities->add (entity);
1018  entity->postSpawn ();
1019}
1020
1021
1022/**
1023   \brief add and spawn a new entity to this world
1024   \param entity to be added
1025   \param absCoor At what coordinates to add this entity.
1026   \param absDir In which direction should it look.
1027*/
1028void World::spawn(WorldEntity* entity, Vector* absCoor, Quaternion* absDir)
1029{
1030  this->entities->add (entity);
1031
1032  entity->setAbsCoor (*absCoor);
1033  entity->setAbsDir (*absDir);
1034
1035  entity->postSpawn ();
1036}
1037
1038
1039/**
1040   \brief add and spawn a new entity to this world
1041   \param entity to be added
1042   \param entity to be added to (PNode)
1043   \param At what relative  coordinates to add this entity.
1044   \param In which relative direction should it look.
1045*/
1046void World::spawn(WorldEntity* entity, PNode* parentNode, 
1047                  Vector* relCoor, Quaternion* relDir, 
1048                  int parentingMode)
1049{
1050  this->nullParent = NullParent::getInstance();
1051  if( parentNode != NULL)
1052    {
1053      parentNode->addChild (entity);
1054     
1055      entity->setRelCoor (*relCoor);
1056      entity->setRelDir (*relDir);
1057      entity->setMode(parentingMode);
1058     
1059      this->entities->add (entity);
1060     
1061      entity->postSpawn ();
1062    }
1063}
1064
1065
1066
1067/**
1068  \brief commands that the world must catch
1069  \returns false if not used by the world
1070*/
1071bool World::command(Command* cmd)
1072{
1073  if( !strcmp( cmd->cmd, "view0")) this->localCamera->setViewMode(VIEW_NORMAL);
1074  else if( !strcmp( cmd->cmd, "view1")) this->localCamera->setViewMode(VIEW_BEHIND);
1075  else if( !strcmp( cmd->cmd, "view2")) this->localCamera->setViewMode(VIEW_FRONT);
1076  else if( !strcmp( cmd->cmd, "view3")) this->localCamera->setViewMode(VIEW_LEFT);
1077  else if( !strcmp( cmd->cmd, "view4")) this->localCamera->setViewMode(VIEW_RIGHT);
1078  else if( !strcmp( cmd->cmd, "view5")) this->localCamera->setViewMode(VIEW_TOP);
1079
1080  return false;
1081}
1082
Note: See TracBrowser for help on using the repository browser.