Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/parenting: :TrackManager: Joining works too. (grafically)

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