Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3438 was 3438, checked in by bensch, 20 years ago

orxonox/trunk: light: comments, errormessages, light up again

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