Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/parenting: fixed major flaw in BezierCurve-calculation. before all BezierCurves started in the Origin. Now they are realy bezier-curves.

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