Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/sound_engine/src/story_entities/world.cc @ 3898

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

orxonox/branches/sound_engine: sound works sloppy, test it

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