Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged branches/parenting back to the.
merged with command:
svn merge branches/parenting trunk -r 3247:HEAD
resolved all conflicts in favor of parenting.

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