Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3804 was 3803, checked in by bensch, 21 years ago

orxonox/trunk: SkyBox even better,

world-entity implements a draw function of its own

some other minor stuff

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