Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: some more classes now destroy themselves via virtual-destructors and call to predecessing destroy-function
also made
#include "stdincl.h" out of unnecessary h-files, so we got faster compile time.

File size: 21.1 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
[2190]17#include "world.h"
18#include "world_entity.h"
[3365]19#include "track_manager.h"
[2036]20#include "player.h"
[2190]21#include "command_node.h"
22#include "camera.h"
[2816]23#include "environment.h"
[3521]24#include "primitive.h"
[3365]25#include "p_node.h"
26#include "null_parent.h"
27#include "helper_parent.h"
28#include "glmenu_imagescreen.h"
[3419]29#include "skysphere.h"
[3436]30#include "light.h"
[3475]31#include "fontset.h"
[3535]32#include "track_node.h"
[2036]33
[1856]34using namespace std;
[1853]35
36
[1858]37/**
[2551]38    \brief create a new World
39   
40    This creates a new empty world!
[1858]41*/
[2636]42World::World (char* name)
[1855]43{
[3526]44  this->init(name, -1);
[3529]45  printf("World::World - generating Nullparent\n");
46  NullParent* np = NullParent::getInstance();
[1855]47}
48
[3449]49/**
50   \brief creates a new World...
51   \param worldID with this ID
52*/
[2636]53World::World (int worldID)
54{
[3526]55  this->init(NULL, worldID);
[2636]56}
57
[1858]58/**
[2551]59    \brief remove the World from memory
[3365]60   
61    delete everything explicitly, that isn't contained in the parenting tree!
62    things contained in the tree are deleted automaticaly
[1858]63*/
[2190]64World::~World ()
[1872]65{
[3220]66  printf("World::~World() - deleting current world\n");
[3226]67  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
[3220]68  cn->unbind(this->localPlayer);
69  cn->reset();
[3459]70
[3220]71  this->localCamera->destroy();
[3529]72  this->nullParent->destroy();
73  delete this->nullParent;
[3502]74  //delete this->skySphere;
[3365]75
76  //delete this->trackManager;
77
78  /*
[3220]79  WorldEntity* entity = entities->enumerate(); 
80  while( entity != NULL )
81    {
82      entity->destroy();
83      entity = entities->nextElement();
84    }
85  this->entities->destroy();
[3365]86  */
[3220]87
[3365]88  /* FIX the parent list has to be cleared - not possible if we got the old list also*/
89
90
91  //delete this->entities;
92  //delete this->localCamera;
[3220]93  /* this->localPlayer hasn't to be deleted explicitly, it is
94     contained in entities*/
[1872]95}
[1858]96
[3526]97/**
98   \brief initializes a new World
99*/
100void World::init(char* name, int worldID)
101{
102  this->setClassName ("World");
[2636]103
[3526]104  this->worldName = name;
105  this->debugWorldNr = worldID;
106  this->entities = new tList<WorldEntity>();
107
108  // Enable default GL stuff
109  glEnable(GL_DEPTH_TEST);
110
111}
112
113
[3449]114/**
115   \brief loads the World by initializing all resources, and set their default values.
116*/
[3459]117ErrorMessage World::load()
[2636]118{
[3365]119  //  BezierCurve* tmpCurve = new BezierCurve();
[2636]120  if(this->debugWorldNr != -1)
121    {
[3455]122      // initializing Font
[3456]123      testFont = new FontSet();
124      testFont->buildFont("../data/pictures/font.tga");
[3455]125
126      // initializing the TrackManager
[3365]127      trackManager = TrackManager::getInstance();
128      trackManager->addPoint(Vector(0,-5,0));
129      trackManager->addPoint(Vector(10,0,5));
130      trackManager->addPoint(Vector(20,0,-5));
131      trackManager->addPoint(Vector(30,0,5));
132      trackManager->addPoint(Vector(40,0,5));
[3522]133      trackManager->setDuration(4);
134      int fork11, fork12, fork13;
135      trackManager->fork(3, &fork11, &fork12, &fork13);
[3365]136      trackManager->workOn(fork11);
[3522]137      trackManager->addPoint(Vector(70, 0, -10));
138      trackManager->addPoint(Vector(100, 0, -15));
139      trackManager->addPoint(Vector(300, 0, -15));
140      trackManager->setDuration(10);
[3365]141      trackManager->workOn(fork12);
[3522]142      trackManager->addPoint(Vector(70,  0, 0));
143      trackManager->addPoint(Vector(100, 0, 0));
144      trackManager->addPoint(Vector(120,10, 0));
145      trackManager->addPoint(Vector(150,10, 0));
146      trackManager->addPoint(Vector(180,15, 0));
147      trackManager->addPoint(Vector(200,10, 0));
[3433]148      trackManager->setDuration(7);
[3365]149      trackManager->workOn(fork13);
[3522]150      trackManager->addPoint(Vector(70,  0, 10));
151      trackManager->addPoint(Vector(100, 0, 30));
152      trackManager->addPoint(Vector(120,-10, 30));
153      trackManager->addPoint(Vector(150,-10, 30));
154      trackManager->setDuration(10);
155      trackManager->join(2, fork12, fork13);
156
157      trackManager->workOn(5);
158      trackManager->addPoint(Vector(250, 20, 10));
159      trackManager->addPoint(Vector(290, 20, 10));
160      trackManager->setDuration(5);
161      trackManager->setSavePoint();
162      trackManager->addPoint(Vector(350, 20, 10));
163      trackManager->addPoint(Vector(360, 20, 10));
164      trackManager->addPoint(Vector(370, 20, 10));
165      trackManager->setDuration(5);
166
167      trackManager->join(2, 6, fork11);
168
[3433]169      trackManager->finalize();
[3522]170
[3433]171     
[3368]172      /*monitor progress*/
173      this->glmis->step();
174
[3365]175      /*
[3433]176        tmpCurve->addNode(Vector(10,  -1,  -1));
177        tmpCurve->addNode(Vector(10,  -2,   2));
178        tmpCurve->addNode(Vector(10,   3,   3));
179        tmpCurve->addNode(Vector(10,   4,  -4), 0);
180        tmpCurve->addNode(Vector(10,  -1,  -1));
181        tmpCurve->addNode(Vector(10,  -2,   2));
182        tmpCurve->addNode(Vector(10,   3,   3));
183        tmpCurve->addNode(Vector(10,   4,  -4), 0);
184        tmpCurve->debug();
[3365]185      */
[2636]186      switch(this->debugWorldNr)
187        {
[3225]188          /*
189            this loads the hard-coded debug world. this only for simplicity and will be
190            removed by a reald world-loader, which interprets a world-file.
191            if you want to add an own debug world, just add a case DEBUG_WORLD_[nr] and
192            make whatever you want...
193           */
[2636]194        case DEBUG_WORLD_0:
195          {
[3365]196            this->nullParent = NullParent::getInstance ();
197            this->nullParent->setName ("NullParent");
198
[3194]199            // !\todo old track-system has to be removed
200
[3365]201            //create helper for player
202            HelperParent* hp = new HelperParent ();
203            /* the player has to be added to this helper */
204
[2636]205            // create a player
[3365]206            WorldEntity* myPlayer = new Player ();
207            myPlayer->setName ("player");
208            this->spawn (myPlayer);
[3368]209            this->localPlayer = myPlayer;
210            /*monitor progress*/
211            this->glmis->step();           
212
[2636]213            // bind input
[3365]214            Orxonox *orx = Orxonox::getInstance ();
[3226]215            orx->getLocalInput()->bind (myPlayer);
[2636]216           
217            // bind camera
218            this->localCamera = new Camera(this);
[3365]219            this->localCamera->setName ("camera");
[3461]220            this->localCamera->bind (myPlayer);
[3507]221            /*monitor progress*/
222            this->glmis->step();           
[2816]223
[3419]224            // Create SkySphere
225            skySphere = new Skysphere("../data/pictures/sky-replace.jpg");
[3527]226            this->localCamera->addChild(this->skySphere);
227            this->skySphere->setMode(MOVEMENT);
[3419]228
[3368]229            /*monitor progress*/
230            this->glmis->step();
231
[3521]232           
233            WorldEntity* env = new Environment();
234            env->setName ("env");
235            this->spawn(env);
236
[3539]237            /*
[3365]238            Vector* es = new Vector (50, 2, 0);
239            Quaternion* qs = new Quaternion ();
[3521]240            WorldEntity* pr = new Primitive(PSPHERE);
241            pr->setName("primitive");
242            this->spawn(pr, this->localPlayer, es, qs, ROTATION);
[3539]243            */
[3521]244
[3539]245
[3368]246            /*monitor progress*/
247            this->glmis->step();
[2816]248
[3535]249            //      trackManager->setBindSlave(env);
[3539]250            TrackNode* tn = TrackNode::getInstance();
251            tn->addChild(myPlayer);
252
253            //Vector v(1.0, 0.0, 0.0);
254            //Quaternion* q = new Quaternion(90.0, v);
255            //this->relDirection = this->relDirection * q;
256           
257            //tn->setAbsDir(q);
258
[3537]259            //localCamera->setParent(TrackNode::getInstance());
[3543]260            TrackNode::getInstance()->addChild (this->localCamera);
[3538]261            //Vector* cameraOffset = new Vector (0, 5, -10);
262            Vector* cameraOffset = new Vector (-10, 5, 0);
[3537]263            this->localCamera->setRelCoor (cameraOffset);
[3522]264            trackManager->condition(1, NEAREST, myPlayer);
265
[2636]266            break;
267          }
268        case DEBUG_WORLD_1:
269          {
[3365]270            this->nullParent = NullParent::getInstance ();
271            this->nullParent->setName ("NullParent");
272
[2636]273            // create a player
[2644]274            WorldEntity* myPlayer = new Player();
[3365]275            myPlayer->setName ("player");
[2644]276            this->spawn(myPlayer);
[3194]277            this->localPlayer = myPlayer;           
[2636]278           
279            // bind input
280            Orxonox *orx = Orxonox::getInstance();
[3226]281            orx->getLocalInput()->bind (myPlayer);
[2636]282           
283            // bind camera
[3365]284            this->localCamera = new Camera (this);
285            this->localCamera->setName ("camera");
[3461]286            this->localCamera->bind (myPlayer); 
[3365]287            this->localPlayer->addChild (this->localCamera);
[3429]288
289            // Create SkySphere
290            skySphere = new Skysphere("../data/pictures/sky-replace.jpg");
[3502]291            this->localPlayer->addChild(this->skySphere);
[3429]292
[2636]293            break;
[3429]294
295
[2636]296          }
297        default:
298          printf("World::load() - no world with ID %i found", this->debugWorldNr );
299        }
300    }
301  else if(this->worldName != NULL)
302    {
303
304    }
[2731]305
306  // initialize debug coord system
307  objectList = glGenLists(1);
308  glNewList (objectList, GL_COMPILE);
[3526]309
[2792]310  glColor3f(1.0,0,0);
[3200]311
312  int sizeX = 100;
[3365]313  int sizeZ = 80;
[3200]314  float length = 1000;
315  float width = 200;
316  float widthX = float (length /sizeX);
[3365]317  float widthZ = float (width /sizeZ);
[3199]318 
[3365]319  float height [sizeX][sizeZ];
320  Vector normal_vectors[sizeX][sizeZ];
[3199]321 
[3200]322 
323  for ( int i = 0; i<sizeX-1; i+=1)
[3365]324    for (int j = 0; j<sizeZ-1;j+=1)
[3200]325      //height[i][j] = rand()/20046 + (j-25)*(j-25)/30;
[3199]326#ifdef __WIN32__
[3200]327      height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5;
[3199]328#else
[3200]329      height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5;
[3199]330#endif
[3200]331
[3365]332  //Die Huegel ein wenig glaetten
[3199]333  for (int h=1; h<2;h++)
[3200]334    for (int i=1;i<sizeX-2 ;i+=1 )
[3365]335      for(int j=1;j<sizeZ-2;j+=1)
[3199]336        height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4;
337 
338  //Berechnung von normalen Vektoren
[3200]339  for(int i=1;i<sizeX-2;i+=1)
[3365]340    for(int j=1;j<sizeZ-2 ;j+=1)
[2792]341      {
[3365]342        Vector v1 = Vector (widthX*(1),      height[i][j],      widthZ*(j) );
343        Vector v2 = Vector (widthX*(i-1),    height[i-1][j],    widthZ*(j));
344        Vector v3 = Vector (widthX*(i),      height[i][j+1],    widthZ*(j+1));
345        Vector v4 = Vector (widthX*(i+1),    height[i+1][j],    widthZ*(j));
346        Vector v5 = Vector (widthX*(i),      height[i][j-1],    widthZ*(j-1));
[3199]347       
[3200]348        Vector c1 = v2 - v1;
349        Vector c2 = v3 - v1;
350        Vector c3=  v4 - v1;
351        Vector c4 = v5 - v1;
352        Vector zero = Vector (0,0,0);
[3365]353        normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4);
[3199]354        normal_vectors[i][j].normalize();
[3200]355      }
356
[3526]357  glBegin(GL_QUADS);
[3200]358  int snowheight=3;
359  for ( int i = 0; i<sizeX; i+=1)
[3365]360    for (int j = 0; j<sizeZ;j+=1)
[3200]361      {   
[3365]362        Vector v1 = Vector (widthX*(i),      height[i][j]-20,       widthZ*(j)  -width/2);
363        Vector v2 = Vector (widthX*(i+1),    height[i+1][j]-20,     widthZ*(j)  -width/2);
364        Vector v3 = Vector (widthX*(i+1),    height[i+1][j+1]-20,   widthZ*(j+1)-width/2);
365        Vector v4 = Vector (widthX*(i),      height[i][j+1]-20,     widthZ*(j+1)-width/2);
[3200]366        float a[3];
367        if(height[i][j]<snowheight){
368          a[0]=0;
369          a[1]=1.0-height[i][j]/10-.3;
370          a[2]=0;
371          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
[3199]372        }
[3200]373        else{
[3199]374            a[0]=1.0;
375            a[1]=1.0;
376            a[2]=1.0;
377            glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
[2817]378           
[3199]379        }
[3200]380        glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z);
381        glVertex3f(v1.x, v1.y, v1.z);
382        if(height[i+1][j]<snowheight){
383          a[0]=0;
384          a[1] =1.0-height[i+1][j]/10-.3;
385          a[2]=0;
386          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
387        }
388        else{
389          a[0]=1.0;
390          a[1]=1.0;
391          a[2]=1.0;
392          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
393         
394        }
395        glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z);
396        glVertex3f(v2.x, v2.y, v2.z);
397        if(height[i+1][j+1]<snowheight){
398          a[0]=0;
399          a[1] =1.0-height[i+1][j+1]/10-.3;
400          a[2]=0;
401          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
402        }
403        else{
404          a[0]=1.0;
405          a[1]=1.0;
406          a[2]=1.0;
407          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
408         
409         
410        }
411        glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z);
412        glVertex3f(v3.x, v3.y, v3.z);
413        if(height[i][j+1]<snowheight){
414          a[0]=0;
415          a[1] =1.0-height[i+1][j+1]/10-.3;
416          a[2]=0;
417          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
418        }
419        else{
420          a[0]=1.0;
421          a[1]=1.0;
422          a[2]=1.0;
423          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
424        }
425        glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z);
426        glVertex3f(v4.x, v4.y, v4.z);
427       
428      }
[3199]429  glEnd();
[3365]430
431  trackManager->drawGraph(.01);
432  trackManager->debug(2);
[2731]433  glEndList();
[3526]434
435
[3455]436  // LIGHT initialisation
437  light = Light::getInstance();
438  light->addLight(0);
439  light->setAttenuation(QUADRATIC, 1.0);
440  light->setAttenuation(CONSTANT, 2.0);
441  light->setAttenuation(QUADRATIC, 1.0);
[3458]442  light->setPosition(10.0, 10.0, 50.0);
[3455]443  light->setDiffuseColor(1,1,1);
444  //  light->addLight(1);
445  //  light->setPosition(20, 10, -20);
446  //  light->setDiffuseColor(0,0,0);
447  light->debug();
448
449
[2636]450}
451
[3459]452/**
453   \brief initializes a new World
454*/
455ErrorMessage World::init()
456{
457  this->bPause = false;
458  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
459  cn->addToWorld(this);
460  cn->enable(true);
[2636]461
[3459]462  //glMap1f (GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &ctrlpoints[0][0]);
463  //glEnable (GL_MAP1_VERTEX_3);
464 
465  //theNurb = gluNewNurbsRenderer ();
466  //gluNurbsProperty (theNurb, GLU_NURBS_MODE, GLU_NURBS_TESSELLATOR);
467  //gluNurbsProperty (theNurb, GLU_NURBS_VERTEX, vertexCallback );
468
469}
470
471
472/**
473   \brief starts the World
474*/
475ErrorMessage World::start()
476{
477  printf("World::start() - starting current World: nr %i\n", this->debugWorldNr);
478  this->bQuitOrxonox = false;
479  this->bQuitCurrentGame = false;
480  this->mainLoop();
481}
482
483/**
484   \brief stops the world.
485
486   This happens, when the player decides to end the Level.
487*/
488ErrorMessage World::stop()
489{
490  printf("World::stop() - got stop signal\n");
491  this->bQuitCurrentGame = true;
492}
493
494/**
495   \brief pauses the Game
496*/
497ErrorMessage World::pause()
498{
499  this->isPaused = true;
500}
501
502/**
503   \brief ends the pause Phase
504*/
505ErrorMessage World::resume()
506{
507  this->isPaused = false;
508}
509
510/**
511   \brief destroys the World
512*/
513ErrorMessage World::destroy()
514{
515  delete trackManager;
516}
517
518/**
519   \brief shows the loading screen
520*/
521void World::displayLoadScreen ()
522{
523  printf ("World::displayLoadScreen - start\n"); 
524 
525  //GLMenuImageScreen*
526  this->glmis = GLMenuImageScreen::getInstance();
527  this->glmis->init();
528  this->glmis->setMaximum(10);
529  this->glmis->draw();
530 
531  printf ("World::displayLoadScreen - end\n"); 
532}
533
534/**
535   \brief removes the loadscreen, and changes over to the game
536
537   \todo take out the delay
538*/
539void World::releaseLoadScreen ()
540{
541  printf ("World::releaseLoadScreen - start\n"); 
542  this->glmis->setValue(this->glmis->getMaximum());
543  SDL_Delay(500);
544  printf ("World::releaseLoadScreen - end\n"); 
545}
546
547
[2636]548/**
[2551]549    \brief checks for collisions
550   
551    This method runs through all WorldEntities known to the world and checks for collisions
552    between them. In case of collisions the collide() method of the corresponding entities
553    is called.
[1858]554*/
[2190]555void World::collide ()
[1858]556{
[2816]557  /*
558  List *a, *b;
[2551]559  WorldEntity *aobj, *bobj;
[2816]560   
561  a = entities;
[2551]562 
563  while( a != NULL)
564    {
[2816]565      aobj = a->nextElement();
[2551]566      if( aobj->bCollide && aobj->collisioncluster != NULL)
[2190]567        {
[2816]568          b = a->nextElement();
[2551]569          while( b != NULL )
570            {
[2816]571              bobj = b->nextElement();
[2551]572              if( bobj->bCollide && bobj->collisioncluster != NULL )
[2190]573                {
[2551]574                  unsigned long ahitflg, bhitflg;
575                  if( check_collision ( &aobj->place, aobj->collisioncluster,
576                                        &ahitflg, &bobj->place, bobj->collisioncluster,
577                                        &bhitflg) );
578                  {
579                    aobj->collide (bobj, ahitflg, bhitflg);
580                    bobj->collide (aobj, bhitflg, ahitflg);
581                  }
[2190]582                }
[2816]583              b = b->nextElement();
[2551]584            }
[2190]585        }
[2816]586      a = a->enumerate();
[2551]587    }
[2816]588  */
[1858]589}
590
591/**
[2551]592    \brief runs through all entities calling their draw() methods
[1931]593*/
[2190]594void World::draw ()
[2077]595{
[3462]596  /* draw entities */
[2551]597  WorldEntity* entity;
[3526]598  glLoadIdentity();
599
[2822]600  entity = this->entities->enumerate();
[2816]601  while( entity != NULL ) 
[2551]602    { 
[2822]603      if( entity->bDraw ) entity->draw();
604      entity = this->entities->nextElement();
[3365]605    } 
[2551]606 
[2731]607  glCallList (objectList);
[3419]608  //! \todo skysphere is a WorldEntity and should be inside of the world-entity-list.
609  skySphere->draw();
610
[3456]611  testFont->printText(0, 0, 1, "orxonox_" PACKAGE_VERSION);
[3455]612
[1931]613}
614
[2816]615
[2190]616/**
[3225]617   \brief function to put your own debug stuff into it. it can display informations about
618   the current class/procedure
619*/
[2640]620void World::debug()
621{
[3365]622  printf ("World::debug() - starting debug\n");
623  PNode* p1 = NullParent::getInstance ();
624  PNode* p2 = new PNode (new Vector(2, 2, 2), p1);
625  PNode* p3 = new PNode (new Vector(4, 4, 4), p1);
626  PNode* p4 = new PNode (new Vector(6, 6, 6), p2);
627
628  p1->debug ();
629  p2->debug ();
630  p3->debug ();
631  p4->debug ();
632
633  p1->shiftCoor (new Vector(-1, -1, -1));
634
635  printf("World::debug() - shift\n");
636  p1->debug ();
637  p2->debug ();
638  p3->debug ();
639  p4->debug ();
640 
641  p1->update (1);
642
643  printf ("World::debug() - update\n");
644  p1->debug ();
645  p2->debug ();
646  p3->debug ();
647  p4->debug ();
648
649  p2->shiftCoor (new Vector(-1, -1, -1));
650  p1->update (2);
651
652  p1->debug ();
653  p2->debug ();
654  p3->debug ();
655  p4->debug ();
656
657  p2->setAbsCoor (new Vector(1,2,3));
658
659
660 p1->update (2);
661
662  p1->debug ();
663  p2->debug ();
664  p3->debug ();
665  p4->debug ();
666
667  p1->destroy ();
668 
669 
670  /*
[2640]671  WorldEntity* entity;
672  printf("counting all entities\n");
[2816]673  printf("World::debug() - enumerate()\n");
674  entity = entities->enumerate(); 
675  while( entity != NULL )
[2640]676    {
677      if( entity->bDraw ) printf("got an entity\n");
[2816]678      entity = entities->nextElement();
[2640]679    }
[3365]680  */
[2640]681}
[2636]682
[2640]683
[3449]684/**
[3225]685  \brief main loop of the world: executing all world relevant function
686
687  in this loop we synchronize (if networked), handle input events, give the heart-beat to
688  all other member-entities of the world (tick to player, enemies etc.), checking for
689  collisions drawing everything to the screen.
690*/
[2636]691void World::mainLoop()
692{
[3365]693  this->lastFrame = SDL_GetTicks ();
[3220]694  printf("World::mainLoop() - Entering main loop\n");
[3215]695  while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* \todo implement pause */
[2551]696    {
[2636]697      // Network
[3365]698      this->synchronize ();
[2636]699      // Process input
[3365]700      this->handleInput ();
[3215]701      if( this->bQuitCurrentGame || this->bQuitOrxonox)
702        {
703          printf("World::mainLoop() - leaving loop earlier...\n");
704          break;
705        }
[2636]706      // Process time
[3365]707      this->timeSlice ();
[2636]708      // Process collision
[3459]709      this->collide ();
[2636]710      // Draw
[3365]711      this->display ();
[2816]712 
[3365]713      for( int i = 0; i < 5000000; i++) {}
714      /* \todo this is to slow down the program for openGl Software emulator computers, reimplement*/
[2551]715    }
[3215]716  printf("World::mainLoop() - Exiting the main loop\n");
[1899]717}
718
[3459]719
[2190]720/**
[2636]721   \brief synchronize local data with remote data
[1855]722*/
[2636]723void World::synchronize ()
[1855]724{
[2636]725  // Get remote input
726  // Update synchronizables
[1855]727}
[2636]728
[3459]729
[2636]730/**
731   \brief run all input processing
[3225]732
733   the command node is the central input event dispatcher. the node uses the even-queue from
734   sdl and has its own event-passing-queue.
[2636]735*/
[3225]736void World::handleInput ()
[2636]737{
738  // localinput
[3225]739  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
[3216]740  cn->process();
[2636]741  // remoteinput
742}
743
[3459]744
[2636]745/**
746   \brief advance the timeline
[3225]747
748   this calculates the time used to process one frame (with all input handling, drawing, etc)
749   the time is mesured in ms and passed to all world-entities and other classes that need
750   a heart-beat.
[2636]751*/
[3225]752void World::timeSlice ()
[2636]753{
754  Uint32 currentFrame = SDL_GetTicks();
755  if(!this->bPause)
756    {
757      Uint32 dt = currentFrame - this->lastFrame;
[2816]758     
[2636]759      if(dt > 0)
760        {
761          float fps = 1000/dt;
762          printf("fps = %f\n", fps);
763        }
764      else
765        {
[3225]766          /* the frame-rate is limited to 100 frames per second, all other things are for
767             nothing.
768          */
[3194]769          printf("fps = 1000 - frame rate is adjusted\n");
770          SDL_Delay(10);
771          dt = 10;
[2636]772        }
[3459]773      //this->timeSlice (dt);
774     
775      /* function to let all entities tick (iterate through list) */
776      WorldEntity* entity;
777      float seconds = dt / 1000.0;     
778      this->nullParent->update (seconds);
779      entity = entities->enumerate(); 
780      while( entity != NULL) 
781        { 
782          entity->tick (seconds);
783          entity = entities->nextElement();
784        }
[3502]785      //skySphere->updatePosition(localCamera->absCoordinate);
[3459]786     
787      /* update tick the rest */
[3225]788      this->localCamera->timeSlice(dt);
[3433]789      this->trackManager->tick(dt);
[2636]790    }
791  this->lastFrame = currentFrame;
792}
793
[3216]794
[2636]795/**
[3225]796   \brief render the current frame
797   
798   clear all buffers and draw the world
[2636]799*/
800void World::display ()
801{
802  // clear buffer
803  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
804  // set camera
805  this->localCamera->apply ();
806  // draw world
807  this->draw();
808  // draw HUD
[3365]809  /* \todo draw HUD */
[2636]810  // flip buffers
811  SDL_GL_SwapBuffers();
[3365]812  //SDL_Surface* screen = Orxonox::getInstance()->getScreen ();
813  //SDL_Flip (screen);
[2636]814}
815
[2644]816
[3225]817/**
818   \brief add and spawn a new entity to this world
819   \param entity to be added
820*/
[2644]821void World::spawn(WorldEntity* entity)
822{
[3529]823  this->nullParent->addChild (entity);
[3365]824  this->entities->add (entity);
[3233]825  entity->postSpawn ();
[2816]826}
827
828
[3225]829/**
830   \brief add and spawn a new entity to this world
831   \param entity to be added
[3449]832   \param absCoor At what coordinates to add this entity.
833   \param absDir In which direction should it look.
[3225]834*/
[3365]835void World::spawn(WorldEntity* entity, Vector* absCoor, Quaternion* absDir)
[2816]836{
[3529]837  this->nullParent->addChild (entity);
838  this->entities->add (entity);
839
[3365]840  entity->setAbsCoor (absCoor);
841  entity->setAbsDir (absDir);
842
[3233]843  entity->postSpawn ();
[2644]844}
[2816]845
846
[3521]847/**
848   \brief add and spawn a new entity to this world
849   \param entity to be added
850   \param entity to be added to (PNode)
851   \param At what relative  coordinates to add this entity.
852   \param In which relative direction should it look.
853*/
854void World::spawn(WorldEntity* entity, PNode* parentNode, 
855                  Vector* relCoor, Quaternion* relDir, 
856                  parentingMode mode)
857{
[3216]858
[3529]859  if( parentNode != NULL)
[3521]860    {
861      parentNode->addChild (entity);
862     
863      entity->setRelCoor (relCoor);
864      entity->setRelDir (relDir);
865     
866      this->entities->add (entity);
867     
868      entity->postSpawn ();
869    }
870}
871
872
873
[3449]874/**
[3225]875  \brief commands that the world must catch
876  \returns false if not used by the world
877*/
[3216]878bool World::command(Command* cmd)
879{
880  return false;
881}
[3365]882
Note: See TracBrowser for help on using the repository browser.