Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/parenting: TrackManager: implemented Debug-output:

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