Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: simple animation is moving, but not realy what it should

File size: 24.9 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            WorldEntity* a = new Environment();
445            this->spawn(a, new Vector(200.0, -35.0, 5.0), new Quaternion());
446
447            WorldEntity* b = new Environment();
448            this->localPlayer->addChild(b);
449            b->setRelCoor(new Vector(10.0, 0.0, 0.0));
450            this->spawn(b);
451           
452            this->simpleAnimation = SimpleAnimation::getInstance();
453
454            /*
455              frame->position = point;
456              frame->orientation = orientation;
457              frame->time = time;
458              frame->mode = DEFAULT_ANIMATION_MODE;
459            */
460
461            KeyFrame* f1 = new KeyFrame;
462            f1->position = new Vector(10.0, 1.0, 1.0);
463            f1->direction = new Quaternion();
464            f1->time = 1.0;
465            f1->mode = LINEAR;
466
467
468            KeyFrame* f2 = new KeyFrame;
469            f2->position = new Vector(10.0, 1.0, -1.0);
470            f2->direction = new Quaternion();
471            f2->time = 1.0;
472            f2->mode = LINEAR;
473
474            this->simpleAnimation->animatorBegin();
475            this->simpleAnimation->selectObject(b);
476            this->simpleAnimation->addKeyFrame(f1);
477            this->simpleAnimation->addKeyFrame(f2);
478            this->simpleAnimation->animatorEnd();
479
480            this->simpleAnimation->start();
481
482            /*
483            Vector* es = new Vector (10, 5, 0);
484            Quaternion* qs = new Quaternion ();
485            WorldEntity* pr = new Primitive(P_CYLINDER);
486            pr->setName("primitive");
487            this->spawn(pr, this->localPlayer, es, qs, PNODE_MOVEMENT);
488            */
489
490            /*monitor progress*/
491            this->glmis->step();
492
493            //      trackManager->setBindSlave(env);
494            PNode* tn = trackManager->getTrackNode();
495            tn->addChild(this->localPlayer);
496
497            //localCamera->setParent(TrackNode::getInstance());
498            tn->addChild(this->localCamera);
499            //      localCamera->lookAt(tn);
500            this->localPlayer->setMode(PNODE_ALL);
501            //Vector* cameraOffset = new Vector (0, 5, -10);
502            trackManager->condition(2, LEFTRIGHT, this->localPlayer);
503            this->glmis->step();
504
505            break;
506          }
507        default:
508          printf("World::load() - no world with ID %i found", this->debugWorldNr );
509        }
510    }
511  else if(this->worldName != NULL)
512    {
513
514    }
515
516  // initialize debug coord system
517  objectList = glGenLists(1);
518  glNewList (objectList, GL_COMPILE);
519 
520  trackManager->drawGraph(.01);
521  trackManager->debug(2);
522  glEndList();
523
524  terrain = new Terrain("../data/worlds/newGround.obj");
525  terrain->setRelCoor(new Vector(0,-10,0));
526  this->spawn(terrain);
527
528}
529
530
531/**
532   \brief initializes a new World shortly before start
533
534   this is the function, that will be loaded shortly before the world is
535   started
536*/
537ErrorMessage World::init()
538{
539  this->bPause = false;
540  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
541  cn->addToWorld(this);
542  cn->enable(true);
543}
544
545
546/**
547   \brief starts the World
548*/
549ErrorMessage World::start()
550{
551  PRINTF(3)("World::start() - starting current World: nr %i\n", this->debugWorldNr);
552  this->bQuitOrxonox = false;
553  this->bQuitCurrentGame = false;
554  this->mainLoop();
555}
556
557/**
558   \brief stops the world.
559
560   This happens, when the player decides to end the Level.
561*/
562ErrorMessage World::stop()
563{
564  PRINTF(3)("World::stop() - got stop signal\n");
565  this->bQuitCurrentGame = true;
566}
567
568/**
569   \brief pauses the Game
570*/
571ErrorMessage World::pause()
572{
573  this->isPaused = true;
574}
575
576/**
577   \brief ends the pause Phase
578*/
579ErrorMessage World::resume()
580{
581  this->isPaused = false;
582}
583
584/**
585   \brief destroys the World
586*/
587ErrorMessage World::destroy()
588{
589
590}
591
592/**
593   \brief shows the loading screen
594*/
595void World::displayLoadScreen ()
596{
597  PRINTF(3)("World::displayLoadScreen - start\n"); 
598 
599  //GLMenuImageScreen*
600  this->glmis = GLMenuImageScreen::getInstance();
601  this->glmis->init();
602  this->glmis->setMaximum(8);
603  this->glmis->draw();
604 
605  PRINTF(3)("World::displayLoadScreen - end\n"); 
606}
607
608/**
609   \brief removes the loadscreen, and changes over to the game
610
611   \todo take out the delay
612*/
613void World::releaseLoadScreen ()
614{
615  PRINTF(3)("World::releaseLoadScreen - start\n"); 
616  this->glmis->setValue(this->glmis->getMaximum());
617  //SDL_Delay(500);
618  PRINTF(3)("World::releaseLoadScreen - end\n"); 
619}
620
621
622/**
623   \brief gets the list of entities from the world
624   \returns entity list
625*/
626tList<WorldEntity>* World::getEntities()
627{
628  return this->entities;
629}
630
631
632/**
633   \brief this returns the current game time
634   \returns elapsed game time
635*/
636double World::getGameTime()
637{
638  return this->gameTime;
639}
640
641
642/**
643    \brief checks for collisions
644   
645    This method runs through all WorldEntities known to the world and checks for collisions
646    between them. In case of collisions the collide() method of the corresponding entities
647    is called.
648*/
649void World::collide ()
650{
651  /*
652  List *a, *b;
653  WorldEntity *aobj, *bobj;
654   
655  a = entities;
656 
657  while( a != NULL)
658    {
659      aobj = a->nextElement();
660      if( aobj->bCollide && aobj->collisioncluster != NULL)
661        {
662          b = a->nextElement();
663          while( b != NULL )
664            {
665              bobj = b->nextElement();
666              if( bobj->bCollide && bobj->collisioncluster != NULL )
667                {
668                  unsigned long ahitflg, bhitflg;
669                  if( check_collision ( &aobj->place, aobj->collisioncluster,
670                                        &ahitflg, &bobj->place, bobj->collisioncluster,
671                                        &bhitflg) );
672                  {
673                    aobj->collide (bobj, ahitflg, bhitflg);
674                    bobj->collide (aobj, bhitflg, ahitflg);
675                  }
676                }
677              b = b->nextElement();
678            }
679        }
680      a = a->enumerate();
681    }
682  */
683}
684
685/**
686    \brief runs through all entities calling their draw() methods
687*/
688void World::draw ()
689{
690  /* draw entities */
691  WorldEntity* entity;
692  glLoadIdentity();
693
694  //entity = this->entities->enumerate();
695  tIterator<WorldEntity>* iterator = this->entities->getIterator();
696  entity = iterator->nextElement();
697  while( entity != NULL ) 
698    { 
699      if( entity->bDraw ) entity->draw();
700      //entity = this->entities->nextElement();
701      entity = iterator->nextElement();
702    }
703  delete iterator;
704 
705  glCallList (objectList);
706  //! \todo skysphere is a WorldEntity and should be inside of the world-entity-list.
707  skySphere->draw();
708
709  testFont->printText(0, 0, 1, "orxonox_" PACKAGE_VERSION);
710
711  lightMan->draw(); // must be at the end of the drawing procedure, otherwise Light cannot be handled as PNodes //
712}
713
714
715/**
716   \brief function to put your own debug stuff into it. it can display informations about
717   the current class/procedure
718*/
719void World::debug()
720{
721  PRINTF(2)("debug() - starting debug\n");
722  PNode* p1 = NullParent::getInstance ();
723  PNode* p2 = new PNode (new Vector(2, 2, 2), p1);
724  PNode* p3 = new PNode (new Vector(4, 4, 4), p1);
725  PNode* p4 = new PNode (new Vector(6, 6, 6), p2);
726
727  p1->debug ();
728  p2->debug ();
729  p3->debug ();
730  p4->debug ();
731
732  p1->shiftCoor (new Vector(-1, -1, -1));
733
734  printf("World::debug() - shift\n");
735  p1->debug ();
736  p2->debug ();
737  p3->debug ();
738  p4->debug ();
739 
740  p1->update (0);
741
742  printf ("World::debug() - update\n");
743  p1->debug ();
744  p2->debug ();
745  p3->debug ();
746  p4->debug ();
747
748  p2->shiftCoor (new Vector(-1, -1, -1));
749  p1->update (0);
750
751  p1->debug ();
752  p2->debug ();
753  p3->debug ();
754  p4->debug ();
755
756  p2->setAbsCoor (new Vector(1,2,3));
757
758
759 p1->update (0);
760
761  p1->debug ();
762  p2->debug ();
763  p3->debug ();
764  p4->debug ();
765
766  delete p1;
767 
768 
769  /*
770  WorldEntity* entity;
771  printf("counting all entities\n");
772  printf("World::debug() - enumerate()\n");
773  entity = entities->enumerate(); 
774  while( entity != NULL )
775    {
776      if( entity->bDraw ) printf("got an entity\n");
777      entity = entities->nextElement();
778    }
779  */
780}
781
782
783/**
784  \brief main loop of the world: executing all world relevant function
785
786  in this loop we synchronize (if networked), handle input events, give the heart-beat to
787  all other member-entities of the world (tick to player, enemies etc.), checking for
788  collisions drawing everything to the screen.
789*/
790void World::mainLoop()
791{
792  this->lastFrame = SDL_GetTicks ();
793  PRINTF(3)("World::mainLoop() - Entering main loop\n");
794  while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* \todo implement pause */
795    {
796      PRINTF(3)("World::mainloop() - number of entities: %i\n", this->entities->getSize());
797      // Network
798      this->synchronize ();
799      // Process input
800      this->handleInput ();
801      if( this->bQuitCurrentGame || this->bQuitOrxonox)
802          break;
803      // Process time
804      this->tick ();
805      // Update the state
806      this->update ();     
807      // Process collision
808      this->collide ();
809      // Draw
810      this->display ();
811
812      //      for( int i = 0; i < 5000000; i++) {}
813      /* \todo this is to slow down the program for openGl Software emulator computers, reimplement*/
814    }
815  PRINTF(3)("World::mainLoop() - Exiting the main loop\n");
816}
817
818
819/**
820   \brief synchronize local data with remote data
821*/
822void World::synchronize ()
823{
824  // Get remote input
825  // Update synchronizables
826}
827
828
829/**
830   \brief run all input processing
831
832   the command node is the central input event dispatcher. the node uses the even-queue from
833   sdl and has its own event-passing-queue.
834*/
835void World::handleInput ()
836{
837  // localinput
838  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
839  cn->process();
840  // remoteinput
841}
842
843
844/**
845   \brief advance the timeline
846
847   this calculates the time used to process one frame (with all input handling, drawing, etc)
848   the time is mesured in ms and passed to all world-entities and other classes that need
849   a heart-beat.
850*/
851void World::tick ()
852{
853  Uint32 currentFrame = SDL_GetTicks();
854  if(!this->bPause)
855    {
856      this->dt = currentFrame - this->lastFrame;
857     
858      if( this->dt > 0)
859        {
860          float fps = 1000/dt;
861          PRINTF(3)("fps = %f\n", fps);
862        }
863      else
864        {
865          /* the frame-rate is limited to 100 frames per second, all other things are for
866             nothing.
867          */
868          PRINTF(2)("fps = 1000 - frame rate is adjusted\n");
869          SDL_Delay(10);
870          this->dt = 10;
871        }
872      //this->timeSlice (dt);
873     
874      /* function to let all entities tick (iterate through list) */
875      float seconds = this->dt / 1000.0;     
876      this->gameTime += seconds;
877      //entity = entities->enumerate();
878      tIterator<WorldEntity>* iterator = this->entities->getIterator();
879      WorldEntity* entity = iterator->nextElement();
880      while( entity != NULL) 
881        { 
882          entity->tick (seconds);
883          entity = iterator->nextElement();
884        }
885      delete iterator;
886      //skySphere->updatePosition(localCamera->absCoordinate);
887     
888      /* update tick the rest */
889      this->trackManager->tick(this->dt);
890      this->localCamera->tick(this->dt);
891      this->garbageCollector->tick(seconds);
892      this->simpleAnimation->tick(seconds);
893    }
894  this->lastFrame = currentFrame;
895}
896
897
898/**
899   \brief this function gives the world a consistant state
900
901   after ticking (updating the world state) this will give a constistant
902   state to the whole system.
903*/
904void World::update()
905{
906  this->garbageCollector->update();
907  this->nullParent->update (dt);
908}
909
910
911/**
912   \brief render the current frame
913   
914   clear all buffers and draw the world
915*/
916void World::display ()
917{
918  // clear buffer
919  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
920  // set camera
921  this->localCamera->apply ();
922  // draw world
923  this->draw();
924  // draw HUD
925  /* \todo draw HUD */
926  // flip buffers
927  SDL_GL_SwapBuffers();
928  //SDL_Surface* screen = Orxonox::getInstance()->getScreen ();
929  //SDL_Flip (screen);
930}
931
932
933/**
934   \brief add and spawn a new entity to this world
935   \param entity to be added
936*/
937void World::spawn(WorldEntity* entity)
938{
939  this->entities->add (entity);
940  entity->postSpawn ();
941}
942
943
944/**
945   \brief add and spawn a new entity to this world
946   \param entity to be added
947   \param absCoor At what coordinates to add this entity.
948   \param absDir In which direction should it look.
949*/
950void World::spawn(WorldEntity* entity, Vector* absCoor, Quaternion* absDir)
951{
952  this->entities->add (entity);
953
954  entity->setAbsCoor (absCoor);
955  entity->setAbsDir (absDir);
956
957  entity->postSpawn ();
958}
959
960
961/**
962   \brief add and spawn a new entity to this world
963   \param entity to be added
964   \param entity to be added to (PNode)
965   \param At what relative  coordinates to add this entity.
966   \param In which relative direction should it look.
967*/
968void World::spawn(WorldEntity* entity, PNode* parentNode, 
969                  Vector* relCoor, Quaternion* relDir, 
970                  int parentingMode)
971{
972  this->nullParent = NullParent::getInstance();
973  if( parentNode != NULL)
974    {
975      parentNode->addChild (entity);
976     
977      entity->setRelCoor (relCoor);
978      entity->setRelDir (relDir);
979      entity->setMode(parentingMode);
980     
981      this->entities->add (entity);
982     
983      entity->postSpawn ();
984    }
985}
986
987
988
989/**
990  \brief commands that the world must catch
991  \returns false if not used by the world
992*/
993bool World::command(Command* cmd)
994{
995  if( !strcmp( cmd->cmd, "view0")) this->localCamera->setViewMode(VIEW_NORMAL);
996  else if( !strcmp( cmd->cmd, "view1")) this->localCamera->setViewMode(VIEW_BEHIND);
997  else if( !strcmp( cmd->cmd, "view2")) this->localCamera->setViewMode(VIEW_FRONT);
998  else if( !strcmp( cmd->cmd, "view3")) this->localCamera->setViewMode(VIEW_LEFT);
999  else if( !strcmp( cmd->cmd, "view4")) this->localCamera->setViewMode(VIEW_RIGHT);
1000  else if( !strcmp( cmd->cmd, "view5")) this->localCamera->setViewMode(VIEW_TOP);
1001 
1002  return false;
1003}
1004
Note: See TracBrowser for help on using the repository browser.