Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/parenting/src/world.cc @ 3361

Last change on this file since 3361 was 3361, checked in by patrick, 21 years ago

orxonox/branches/parenting: did a little HUD on the screen. fancy but not realy serious…:) was just testing with my loadscreen

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  //glmis->draw();
619
620  // draw debug coord system
621  glCallList (objectList);
622
623}
624
625/**
626    \brief updates Placements and notifies entities when they left the
627    world
628   
629    This runs trough all WorldEntities and maps Locations to Placements
630    if they are bound, checks whether they left the level boundaries
631    and calls appropriate functions.
632*/
633void World::update ()
634{
635  /*
636  //List<WorldEntity> *l;
637  WorldEntity* entity;
638  Location* loc;
639  Placement* plc;
640  Uint32 t;
641 
642  //  l = entities->enumerate();
643  entity = this->entities->enumerate();
644  while( entity != NULL )
645    {
646
647     
648      if( !entity->isFree() )
649        {
650          loc = entity->getLocation();
651          plc = entity->getPlacement();
652          t = loc->part;
653         
654          if( t >= tracklen )
655            {
656              printf("An entity is out of the game area\n");
657              entity->leftWorld ();
658            }
659          else
660            {
661              while( track[t].mapCoords( loc, plc) )
662                {
663                  track[t].postLeave (entity);
664                  if( loc->part >= tracklen )
665                    {
666                      printf("An entity has left the game area\n");
667                      entity->leftWorld ();
668                      break;
669                    }
670                  track[loc->part].postEnter (entity);
671                }
672            }
673        }
674      else
675        {
676        }
677     
678      entity = entities->nextElement();
679    }
680  */ 
681}
682
683/**
684    \brief relays the passed time since the last frame to entities and Track parts
685    \param deltaT: the time passed since the last frame in milliseconds
686*/
687void World::timeSlice (Uint32 deltaT)
688{
689  //List<WorldEntity> *l;
690  WorldEntity* entity;
691  float seconds = deltaT / 1000.0;
692 
693  this->nullParent->update (seconds);
694  //this->nullParent->processTick (seconds);
695
696  entity = entities->enumerate(); 
697  while( entity != NULL) 
698    { 
699      entity->tick (seconds);
700      entity = entities->nextElement();
701    }
702
703  //for( int i = 0; i < tracklen; i++) track[i].tick (seconds);
704}
705
706/**
707   \brief removes level data from memory
708*/
709void World::unload()
710{
711  if( pathnodes) delete []pathnodes;
712  if( track) delete []pathnodes;
713}
714
715
716void World::setTrackLen(Uint32 len)
717{
718  this->tracklen = len;
719}
720
721int World::getTrackLen()
722{
723  return this->tracklen;
724}
725
726
727
728/**
729   \brief function to put your own debug stuff into it. it can display informations about
730   the current class/procedure
731*/
732void World::debug()
733{
734  printf ("World::debug() - starting debug\n");
735  PNode* p1 = NullParent::getInstance ();
736  PNode* p2 = new PNode (new Vector(2, 2, 2), p1);
737  PNode* p3 = new PNode (new Vector(4, 4, 4), p1);
738  PNode* p4 = new PNode (new Vector(6, 6, 6), p2);
739
740  p1->debug ();
741  p2->debug ();
742  p3->debug ();
743  p4->debug ();
744
745  p1->shiftCoor (new Vector(-1, -1, -1));
746
747  printf("World::debug() - shift\n");
748  p1->debug ();
749  p2->debug ();
750  p3->debug ();
751  p4->debug ();
752 
753  p1->update (1);
754
755  printf ("World::debug() - update\n");
756  p1->debug ();
757  p2->debug ();
758  p3->debug ();
759  p4->debug ();
760
761  p2->shiftCoor (new Vector(-1, -1, -1));
762  p1->update (2);
763
764  p1->debug ();
765  p2->debug ();
766  p3->debug ();
767  p4->debug ();
768
769  p2->setAbsCoor (new Vector(1,2,3));
770
771
772 p1->update (2);
773
774  p1->debug ();
775  p2->debug ();
776  p3->debug ();
777  p4->debug ();
778
779  p1->destroy ();
780 
781 
782  /*
783  WorldEntity* entity;
784  printf("counting all entities\n");
785  printf("World::debug() - enumerate()\n");
786  entity = entities->enumerate(); 
787  while( entity != NULL )
788    {
789      if( entity->bDraw ) printf("got an entity\n");
790      entity = entities->nextElement();
791    }
792  */
793}
794
795
796/*
797  \brief main loop of the world: executing all world relevant function
798
799  in this loop we synchronize (if networked), handle input events, give the heart-beat to
800  all other member-entities of the world (tick to player, enemies etc.), checking for
801  collisions drawing everything to the screen.
802*/
803void World::mainLoop()
804{
805  this->lastFrame = SDL_GetTicks ();
806  printf("World::mainLoop() - Entering main loop\n");
807  while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* \todo implement pause */
808    {
809      // Network
810      this->synchronize ();
811      // Process input
812      this->handleInput ();
813      if( this->bQuitCurrentGame || this->bQuitOrxonox)
814        {
815          printf("World::mainLoop() - leaving loop earlier...\n");
816          break;
817        }
818      // Process time
819      this->timeSlice ();
820      // Process collision
821      this->collision ();
822      // Draw
823      this->display ();
824 
825      for( int i = 0; i < 5000000; i++) {}
826      /* \todo this is to slow down the program for openGl Software emulator computers, reimplement*/
827    }
828  printf("World::mainLoop() - Exiting the main loop\n");
829}
830
831/**
832   \brief synchronize local data with remote data
833*/
834void World::synchronize ()
835{
836  // Get remote input
837  // Update synchronizables
838}
839
840/**
841   \brief run all input processing
842
843   the command node is the central input event dispatcher. the node uses the even-queue from
844   sdl and has its own event-passing-queue.
845*/
846void World::handleInput ()
847{
848  // localinput
849  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
850  cn->process();
851  // remoteinput
852}
853
854/**
855   \brief advance the timeline
856
857   this calculates the time used to process one frame (with all input handling, drawing, etc)
858   the time is mesured in ms and passed to all world-entities and other classes that need
859   a heart-beat.
860*/
861void World::timeSlice ()
862{
863  Uint32 currentFrame = SDL_GetTicks();
864  if(!this->bPause)
865    {
866      Uint32 dt = currentFrame - this->lastFrame;
867     
868      if(dt > 0)
869        {
870          float fps = 1000/dt;
871          printf("fps = %f\n", fps);
872        }
873      else
874        {
875          /* the frame-rate is limited to 100 frames per second, all other things are for
876             nothing.
877          */
878          printf("fps = 1000 - frame rate is adjusted\n");
879          SDL_Delay(10);
880          dt = 10;
881        }
882      this->timeSlice (dt);
883      this->update ();
884      this->localCamera->timeSlice(dt);
885    }
886  this->lastFrame = currentFrame;
887}
888
889
890/**
891   \brief compute collision detection
892*/
893void World::collision ()
894{
895  this->collide ();
896}
897
898
899/**
900   \brief render the current frame
901   
902   clear all buffers and draw the world
903*/
904void World::display ()
905{
906  // clear buffer
907  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
908  // set camera
909  this->localCamera->apply ();
910  // draw world
911  this->draw();
912  // draw HUD
913  /* \todo draw HUD */
914  // flip buffers
915  SDL_GL_SwapBuffers();
916  //SDL_Surface* screen = Orxonox::getInstance()->getScreen ();
917  //SDL_Flip (screen);
918}
919
920/**
921   \brief give back active camera
922   
923   this passes back the actualy active camera
924   \todo ability to define more than one camera or camera-places
925*/
926Camera* World::getCamera()
927{
928  return this->localCamera;
929}
930
931
932/**
933   \brief add and spawn a new entity to this world
934   \param entity to be added
935*/
936void World::spawn(WorldEntity* entity)
937{
938  if( this->nullParent != NULL && entity->parent == NULL)
939    this->nullParent->addChild (entity);
940
941  this->entities->add (entity);
942
943  entity->postSpawn ();
944}
945
946
947/**
948   \brief add and spawn a new entity to this world
949   \param entity to be added
950   \param location where to add
951*/
952void World::spawn(WorldEntity* entity, Vector* absCoor, Quaternion* absDir)
953{
954  entity->setAbsCoor (absCoor);
955  entity->setAbsDir (absDir);
956 
957  if( this->nullParent != NULL && entity->parent == NULL)
958    this->nullParent->addChild (entity);
959
960  this->entities->add (entity);
961
962  entity->postSpawn ();
963}
964
965
966
967/*
968  \brief commands that the world must catch
969  \returns false if not used by the world
970*/
971bool World::command(Command* cmd)
972{
973  return false;
974}
975
976
977
978
979void World::swap (unsigned char &a, unsigned char &b)
980{
981  unsigned char temp;
982  temp = a;
983  a    = b;
984  b    = temp;
985}
Note: See TracBrowser for help on using the repository browser.