Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/parenting: :TrackManager: forking works.

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