Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3734 was 3734, checked in by patrick, 19 years ago

orxonox/trunk: SimpleAnimation: the NEG_EXP function now gets scaled in a very smooth manner.

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