Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: camera looks into the right direction, as it shoul be.

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