Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/world.cc @ 3441

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

orxonox/trunk: implemented attenuation

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