Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/story_entities/world.cc @ 3787

Last change on this file since 3787 was 3763, checked in by bensch, 20 years ago

orxonox/trunk: skySphere now added to WorldEntity-List

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