Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/parenting: :Curve: bezierCurve now can have 0,1,2,3 points. instead of only more than 3

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