Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3353 was 3353, checked in by patrick, 19 years ago

orxonox/branches/parenting: commented out the old nurbs opengl test lines, so that everyone can enjoy our own bezier-lines…:)

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  /*
563  glBegin(GL_LINE_STRIP);
564  glColor3f(1.0, 5.0, 1.0);
565  for( int i = 0; i <= 30; i++)
566    {
567      glEvalCoord1f ((GLfloat) i/30.0);
568    }
569  glEnd();
570  */
571
572  trackManager->drawGraph(.01);
573  trackManager->debug(2);
574  /*
575  glBegin(GL_LINES);
576  float i;
577  for(i = 0.0; i<1; i+=.01)
578    {
579      printf("%f, %f, %f\n",tmpCurve->calcPos(i).x, tmpCurve->calcPos(i).y, tmpCurve->calcPos(i).z);
580      glVertex3f(tmpCurve->calcPos(i).x, tmpCurve->calcPos(i).y, tmpCurve->calcPos(i).z);
581    }
582  */
583  glEnd();
584  glEndList();
585}
586
587
588/**
589    \brief checks for collisions
590   
591    This method runs through all WorldEntities known to the world and checks for collisions
592    between them. In case of collisions the collide() method of the corresponding entities
593    is called.
594*/
595void World::collide ()
596{
597  /*
598  List *a, *b;
599  WorldEntity *aobj, *bobj;
600   
601  a = entities;
602 
603  while( a != NULL)
604    {
605      aobj = a->nextElement();
606      if( aobj->bCollide && aobj->collisioncluster != NULL)
607        {
608          b = a->nextElement();
609          while( b != NULL )
610            {
611              bobj = b->nextElement();
612              if( bobj->bCollide && bobj->collisioncluster != NULL )
613                {
614                  unsigned long ahitflg, bhitflg;
615                  if( check_collision ( &aobj->place, aobj->collisioncluster,
616                                        &ahitflg, &bobj->place, bobj->collisioncluster,
617                                        &bhitflg) );
618                  {
619                    aobj->collide (bobj, ahitflg, bhitflg);
620                    bobj->collide (aobj, bhitflg, ahitflg);
621                  }
622                }
623              b = b->nextElement();
624            }
625        }
626      a = a->enumerate();
627    }
628  */
629}
630
631/**
632    \brief runs through all entities calling their draw() methods
633*/
634void World::draw ()
635{
636  // draw entities
637  WorldEntity* entity;
638  entity = this->entities->enumerate();
639  while( entity != NULL ) 
640    { 
641      if( entity->bDraw ) entity->draw();
642      entity = this->entities->nextElement();
643    } 
644 
645  // draw debug coord system
646  glCallList (objectList);
647
648}
649
650/**
651    \brief updates Placements and notifies entities when they left the
652    world
653   
654    This runs trough all WorldEntities and maps Locations to Placements
655    if they are bound, checks whether they left the level boundaries
656    and calls appropriate functions.
657*/
658void World::update ()
659{
660  /*
661  //List<WorldEntity> *l;
662  WorldEntity* entity;
663  Location* loc;
664  Placement* plc;
665  Uint32 t;
666 
667  //  l = entities->enumerate();
668  entity = this->entities->enumerate();
669  while( entity != NULL )
670    {
671
672     
673      if( !entity->isFree() )
674        {
675          loc = entity->getLocation();
676          plc = entity->getPlacement();
677          t = loc->part;
678         
679          if( t >= tracklen )
680            {
681              printf("An entity is out of the game area\n");
682              entity->leftWorld ();
683            }
684          else
685            {
686              while( track[t].mapCoords( loc, plc) )
687                {
688                  track[t].postLeave (entity);
689                  if( loc->part >= tracklen )
690                    {
691                      printf("An entity has left the game area\n");
692                      entity->leftWorld ();
693                      break;
694                    }
695                  track[loc->part].postEnter (entity);
696                }
697            }
698        }
699      else
700        {
701        }
702     
703      entity = entities->nextElement();
704    }
705  */ 
706}
707
708/**
709    \brief relays the passed time since the last frame to entities and Track parts
710    \param deltaT: the time passed since the last frame in milliseconds
711*/
712void World::timeSlice (Uint32 deltaT)
713{
714  //List<WorldEntity> *l;
715  WorldEntity* entity;
716  float seconds = deltaT / 1000.0;
717 
718  this->nullParent->update (seconds);
719  //this->nullParent->processTick (seconds);
720
721  entity = entities->enumerate(); 
722  while( entity != NULL) 
723    { 
724      entity->tick (seconds);
725      entity = entities->nextElement();
726    }
727
728  //for( int i = 0; i < tracklen; i++) track[i].tick (seconds);
729}
730
731/**
732   \brief removes level data from memory
733*/
734void World::unload()
735{
736  if( pathnodes) delete []pathnodes;
737  if( track) delete []pathnodes;
738}
739
740
741void World::setTrackLen(Uint32 len)
742{
743  this->tracklen = len;
744}
745
746int World::getTrackLen()
747{
748  return this->tracklen;
749}
750
751
752
753/**
754   \brief function to put your own debug stuff into it. it can display informations about
755   the current class/procedure
756*/
757void World::debug()
758{
759  printf ("World::debug() - starting debug\n");
760  PNode* p1 = NullParent::getInstance ();
761  PNode* p2 = new PNode (new Vector(2, 2, 2), p1);
762  PNode* p3 = new PNode (new Vector(4, 4, 4), p1);
763  PNode* p4 = new PNode (new Vector(6, 6, 6), p2);
764
765  p1->debug ();
766  p2->debug ();
767  p3->debug ();
768  p4->debug ();
769
770  p1->shiftCoor (new Vector(-1, -1, -1));
771
772  printf("World::debug() - shift\n");
773  p1->debug ();
774  p2->debug ();
775  p3->debug ();
776  p4->debug ();
777 
778  p1->update (1);
779
780  printf ("World::debug() - update\n");
781  p1->debug ();
782  p2->debug ();
783  p3->debug ();
784  p4->debug ();
785
786  p2->shiftCoor (new Vector(-1, -1, -1));
787  p1->update (2);
788
789  p1->debug ();
790  p2->debug ();
791  p3->debug ();
792  p4->debug ();
793
794  p2->setAbsCoor (new Vector(1,2,3));
795
796
797 p1->update (2);
798
799  p1->debug ();
800  p2->debug ();
801  p3->debug ();
802  p4->debug ();
803
804  p1->destroy ();
805 
806 
807  /*
808  WorldEntity* entity;
809  printf("counting all entities\n");
810  printf("World::debug() - enumerate()\n");
811  entity = entities->enumerate(); 
812  while( entity != NULL )
813    {
814      if( entity->bDraw ) printf("got an entity\n");
815      entity = entities->nextElement();
816    }
817  */
818}
819
820
821/*
822  \brief main loop of the world: executing all world relevant function
823
824  in this loop we synchronize (if networked), handle input events, give the heart-beat to
825  all other member-entities of the world (tick to player, enemies etc.), checking for
826  collisions drawing everything to the screen.
827*/
828void World::mainLoop()
829{
830  this->lastFrame = SDL_GetTicks ();
831  printf("World::mainLoop() - Entering main loop\n");
832  while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* \todo implement pause */
833    {
834      // Network
835      this->synchronize ();
836      // Process input
837      this->handleInput ();
838      if( this->bQuitCurrentGame || this->bQuitOrxonox)
839        {
840          printf("World::mainLoop() - leaving loop earlier...\n");
841          break;
842        }
843      // Process time
844      this->timeSlice ();
845      // Process collision
846      this->collision ();
847      // Draw
848      this->display ();
849 
850      for( int i = 0; i < 5000000; i++) {}
851      /* \todo this is to slow down the program for openGl Software emulator computers, reimplement*/
852    }
853  printf("World::mainLoop() - Exiting the main loop\n");
854}
855
856/**
857   \brief synchronize local data with remote data
858*/
859void World::synchronize ()
860{
861  // Get remote input
862  // Update synchronizables
863}
864
865/**
866   \brief run all input processing
867
868   the command node is the central input event dispatcher. the node uses the even-queue from
869   sdl and has its own event-passing-queue.
870*/
871void World::handleInput ()
872{
873  // localinput
874  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
875  cn->process();
876  // remoteinput
877}
878
879/**
880   \brief advance the timeline
881
882   this calculates the time used to process one frame (with all input handling, drawing, etc)
883   the time is mesured in ms and passed to all world-entities and other classes that need
884   a heart-beat.
885*/
886void World::timeSlice ()
887{
888  Uint32 currentFrame = SDL_GetTicks();
889  if(!this->bPause)
890    {
891      Uint32 dt = currentFrame - this->lastFrame;
892     
893      if(dt > 0)
894        {
895          float fps = 1000/dt;
896          printf("fps = %f\n", fps);
897        }
898      else
899        {
900          /* the frame-rate is limited to 100 frames per second, all other things are for
901             nothing.
902          */
903          printf("fps = 1000 - frame rate is adjusted\n");
904          SDL_Delay(10);
905          dt = 10;
906        }
907      this->timeSlice (dt);
908      this->update ();
909      this->localCamera->timeSlice(dt);
910    }
911  this->lastFrame = currentFrame;
912}
913
914
915/**
916   \brief compute collision detection
917*/
918void World::collision ()
919{
920  this->collide ();
921}
922
923
924/**
925   \brief render the current frame
926   
927   clear all buffers and draw the world
928*/
929void World::display ()
930{
931  // clear buffer
932  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
933  // set camera
934  this->localCamera->apply ();
935  // draw world
936  this->draw();
937  // draw HUD
938  /* \todo draw HUD */
939  // flip buffers
940  SDL_GL_SwapBuffers();
941  //SDL_Surface* screen = Orxonox::getInstance()->getScreen ();
942  //SDL_Flip (screen);
943}
944
945/**
946   \brief give back active camera
947   
948   this passes back the actualy active camera
949   \todo ability to define more than one camera or camera-places
950*/
951Camera* World::getCamera()
952{
953  return this->localCamera;
954}
955
956
957/**
958   \brief add and spawn a new entity to this world
959   \param entity to be added
960*/
961void World::spawn(WorldEntity* entity)
962{
963  if( this->nullParent != NULL && entity->parent == NULL)
964    this->nullParent->addChild (entity);
965
966  this->entities->add (entity);
967
968  entity->postSpawn ();
969}
970
971
972/**
973   \brief add and spawn a new entity to this world
974   \param entity to be added
975   \param location where to add
976*/
977void World::spawn(WorldEntity* entity, Vector* absCoor, Quaternion* absDir)
978{
979  entity->setAbsCoor (absCoor);
980  entity->setAbsDir (absDir);
981 
982  if( this->nullParent != NULL && entity->parent == NULL)
983    this->nullParent->addChild (entity);
984
985  this->entities->add (entity);
986
987  entity->postSpawn ();
988}
989
990
991
992/*
993  \brief commands that the world must catch
994  \returns false if not used by the world
995*/
996bool World::command(Command* cmd)
997{
998  return false;
999}
1000
1001
1002
1003
1004void World::swap (unsigned char &a, unsigned char &b)
1005{
1006  unsigned char temp;
1007  temp = a;
1008  a    = b;
1009  b    = temp;
1010}
Note: See TracBrowser for help on using the repository browser.