Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/dave/src/world.cc @ 3403

Last change on this file since 3403 was 3403, checked in by dave, 19 years ago

branches/dave/src/world.cc: Zeile 379 bin ich am Textur draufladenprobieren

File size: 23.8 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 "glmenu_imagescreen.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->trackManager;
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
96
97
98ErrorMessage World::init()
99{
100  this->bPause = false;
101  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
102  cn->addToWorld(this);
103  cn->enable(true);
104
105  //glMap1f (GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &ctrlpoints[0][0]);
106  //glEnable (GL_MAP1_VERTEX_3);
107 
108  //theNurb = gluNewNurbsRenderer ();
109  //gluNurbsProperty (theNurb, GLU_NURBS_MODE, GLU_NURBS_TESSELLATOR);
110  //gluNurbsProperty (theNurb, GLU_NURBS_VERTEX, vertexCallback );
111
112}
113
114
115
116ErrorMessage World::start()
117{
118  printf("World::start() - starting current World: nr %i\n", this->debugWorldNr);
119  this->bQuitOrxonox = false;
120  this->bQuitCurrentGame = false;
121  this->mainLoop();
122}
123
124ErrorMessage World::stop()
125{
126  printf("World::stop() - got stop signal\n");
127  this->bQuitCurrentGame = true;
128}
129
130ErrorMessage World::pause()
131{
132  this->isPaused = true;
133}
134
135
136ErrorMessage World::resume()
137{
138  this->isPaused = false;
139}
140
141
142void World::destroy()
143{
144}
145
146
147void World::displayLoadScreen ()
148{
149  printf ("World::displayLoadScreen - start\n"); 
150 
151  //GLMenuImageScreen*
152  this->glmis = new GLMenuImageScreen();
153  this->glmis->init();
154  this->glmis->setMaximum(10);
155  this->glmis->draw();
156 
157  printf ("World::displayLoadScreen - end\n"); 
158}
159
160
161void World::releaseLoadScreen ()
162{
163  printf ("World::releaseLoadScreen - start\n"); 
164  this->glmis->setValue(this->glmis->getMaximum());
165  SDL_Delay(500);
166  printf ("World::releaseLoadScreen - end\n"); 
167}
168
169
170void World::load()
171{
172  //  BezierCurve* tmpCurve = new BezierCurve();
173  if(this->debugWorldNr != -1)
174    {
175      trackManager = TrackManager::getInstance();
176      trackManager->addPoint(Vector(0,-5,0));
177      trackManager->addPoint(Vector(10,0,5));
178      trackManager->addPoint(Vector(20,0,-5));
179      trackManager->addPoint(Vector(30,0,5));
180      trackManager->addPoint(Vector(40,0,5));
181      trackManager->setDuration(.5);
182      trackManager->setSavePoint();
183      trackManager->addPoint(Vector(50,10,10));
184      trackManager->addPoint(Vector(60,0, 10));
185      trackManager->addPoint(Vector(70,0, 10));
186      trackManager->addPoint(Vector(80,0,-10));
187      trackManager->addPoint(Vector(90,0,-10));
188      trackManager->setDuration(.5);
189      trackManager->setSavePoint();
190      trackManager->addPoint(Vector(110,0,5));
191      trackManager->addPoint(Vector(120,0, 10));
192      trackManager->addPoint(Vector(130,0, 10));
193      trackManager->addPoint(Vector(140,0,-10));
194      trackManager->addPoint(Vector(150,0,-10));
195      trackManager->setDuration(.5);
196      int fork11, fork12, fork13, fork14;
197      trackManager->fork(4, &fork11, &fork12, &fork13, &fork14);
198      trackManager->workOn(fork11);
199      trackManager->addPoint(Vector(170, 0, -15));
200      trackManager->addPoint(Vector(180, 0, -15));
201      trackManager->workOn(fork12);
202      trackManager->addPoint(Vector(170, 0, 10));
203      trackManager->addPoint(Vector(180, 0, 10));
204      trackManager->addPoint(Vector(190,2,5));
205      trackManager->addPoint(Vector(200,2,5));
206      int fork21, fork22;
207      trackManager->fork(2, &fork21, &fork22);
208      trackManager->workOn(fork21);
209      trackManager->addPoint(Vector(220, 10,-10));
210      trackManager->addPoint(Vector(230, 0,-10));
211      trackManager->addPoint(Vector(240, 0, 2));
212      trackManager->addPoint(Vector(250, 0, 0));
213      trackManager->addPoint(Vector(260, 0, 5));
214      trackManager->join(2, fork12, fork11);
215      trackManager->workOn(fork22);
216      trackManager->addPoint(Vector(220, -10,10));
217      trackManager->addPoint(Vector(230, 0, 10));
218      trackManager->addPoint(Vector(240, 0, 10));
219      trackManager->addPoint(Vector(250, 0, 5));
220      trackManager->workOn(fork13);
221      trackManager->addPoint(Vector(200,-10,5));
222      trackManager->addPoint(Vector(250,-10,5));
223      printf("fork14: %d\n", fork14);
224      trackManager->workOn(fork14);
225      trackManager->addPoint(Vector(200,15,0));
226      trackManager->addPoint(Vector(210,0,10));
227
228
229
230      trackManager->join(4, fork21, fork22, fork13, fork14);
231
232      /*monitor progress*/
233      this->glmis->step();
234
235      /*
236      tmpCurve->addNode(Vector(10,0,-10));
237      //tmpCurve->addNode(Vector(10,2,5));
238      //tmpCurve->addNode(Vector(10,3,-5));
239      //      tmpCurve->addNode(Vector(10,1,5));
240      tmpCurve->addNode(Vector(10,0,5));
241      */
242      switch(this->debugWorldNr)
243        {
244          /*
245            this loads the hard-coded debug world. this only for simplicity and will be
246            removed by a reald world-loader, which interprets a world-file.
247            if you want to add an own debug world, just add a case DEBUG_WORLD_[nr] and
248            make whatever you want...
249           */
250        case DEBUG_WORLD_0:
251          {
252            this->nullParent = NullParent::getInstance ();
253            this->nullParent->setName ("NullParent");
254
255            // create some path nodes
256            this->pathnodes = new Vector[6];
257            this->pathnodes[0] = Vector(0, 0, 0);
258            this->pathnodes[1] = Vector(1000, 0, 0);
259            //      this->pathnodes[2] = Vector(-100, 140, 0);
260            //      this->pathnodes[3] = Vector(0, 180, 0);
261            //      this->pathnodes[4] = Vector(100, 140, 0);
262            //      this->pathnodes[5] = Vector(100, 40, 0);
263           
264            // create the tracks
265            this->tracklen = 2;
266            this->track = new Track[2];
267            for( int i = 0; i < this->tracklen; i++)
268              {
269                this->track[i] = Track( i, (i+1)%this->tracklen, &this->pathnodes[i], &this->pathnodes[(i+1)%this->tracklen]);
270              }
271            // !\todo old track-system has to be removed
272
273            //create helper for player
274            HelperParent* hp = new HelperParent ();
275            /* the player has to be added to this helper */
276
277            // create a player
278            WorldEntity* myPlayer = new Player ();
279            myPlayer->setName ("player");
280            this->spawn (myPlayer);
281            this->localPlayer = myPlayer;
282
283            /*monitor progress*/
284            this->glmis->step();           
285
286            // bind input
287            Orxonox *orx = Orxonox::getInstance ();
288            orx->getLocalInput()->bind (myPlayer);
289           
290            // bind camera
291            this->localCamera = new Camera(this);
292            this->localCamera->setName ("camera");
293            this->getCamera()->bind (myPlayer);
294            this->localPlayer->addChild (this->localCamera);
295
296            /*monitor progress*/
297            this->glmis->step();
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            /*monitor progress*/
306            this->glmis->step();
307
308            break;
309          }
310        case DEBUG_WORLD_1:
311          {
312            /*
313            this->testCurve = new UPointCurve();
314            this->testCurve->addNode(Vector( 0, 0, 0));
315            this->testCurve->addNode(Vector(10, 0, 5));
316            this->testCurve->addNode(Vector(20, -5,-5));
317            this->testCurve->addNode(Vector(30, 5, 10));
318            this->testCurve->addNode(Vector(40, 0,-10));
319            this->testCurve->addNode(Vector(50, 0,-10));
320            */
321
322            this->nullParent = NullParent::getInstance ();
323            this->nullParent->setName ("NullParent");
324
325            // create some path nodes
326            this->pathnodes = new Vector[6];
327            this->pathnodes[0] = Vector(0, 0, 0);
328            this->pathnodes[1] = Vector(20, 10, 10);
329            this->pathnodes[2] = Vector(40, 0, 10);
330            this->pathnodes[3] = Vector(60, 10, 0);
331            this->pathnodes[4] = Vector(80, 20, 10);
332            this->pathnodes[5] = Vector(30, 50, 0);
333           
334
335
336
337            // create the tracks
338            this->tracklen = 6;
339            this->track = new Track[6];
340            for( int i = 0; i < this->tracklen; i++)
341              {
342                this->track[i] = Track( i, (i+1)%this->tracklen, &this->pathnodes[i], &this->pathnodes[(i+1)%this->tracklen]);
343              }
344
345            // create a player
346            WorldEntity* myPlayer = new Player();
347            myPlayer->setName ("player");
348            this->spawn(myPlayer);
349            this->localPlayer = myPlayer;           
350           
351            // bind input
352            Orxonox *orx = Orxonox::getInstance();
353            orx->getLocalInput()->bind (myPlayer);
354           
355            // bind camera
356            this->localCamera = new Camera (this);
357            this->localCamera->setName ("camera");
358            this->getCamera()->bind (myPlayer); 
359            this->localPlayer->addChild (this->localCamera);
360            break;
361          }
362        default:
363          printf("World::load() - no world with ID %i found", this->debugWorldNr );
364        }
365    }
366  else if(this->worldName != NULL)
367    {
368
369    }
370
371  // initialize debug coord system
372   
373 
374  objectList = glGenLists(1);
375  glNewList (objectList, GL_COMPILE);
376  glLoadIdentity();
377  //glColor3f(1.0,0,0);
378 
379  //BodenTextur reinladen
380  Texture* boden=new Texture();
381  boden->loadImage("../data/models/ground.tga");
382 
383 
384 
385  glBegin(GL_QUADS);
386 
387  int sizeX = 100;
388  int sizeZ = 80;
389  float length = 1000;
390  float width = 300;
391  float widthX = float (length /sizeX);
392  float widthZ = float (width /sizeZ);
393 
394  float height [sizeX][sizeZ];
395  Vector normal_vectors[sizeX][sizeZ];
396 
397 
398  for ( int i = 0; i<sizeX-1; i+=1)
399    for (int j = 0; j<sizeZ-1;j+=1)
400      //height[i][j] = rand()/20046 + (j-25)*(j-25)/30;
401#ifdef __WIN32__
402      height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5;
403#else
404      height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5;
405#endif
406
407  //Die Huegel ein wenig glaetten
408  for (int h=1; h<2;h++)
409    for (int i=1;i<sizeX-2 ;i+=1 )
410      for(int j=1;j<sizeZ-2;j+=1)
411        height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4;
412 
413  //Berechnung von normalen Vektoren
414  for(int i=1;i<sizeX-2;i+=1)
415    for(int j=1;j<sizeZ-2 ;j+=1)
416      {
417        Vector v1 = Vector (widthX*(1),      height[i][j],      widthZ*(j) );
418        Vector v2 = Vector (widthX*(i-1),    height[i-1][j],    widthZ*(j));
419        Vector v3 = Vector (widthX*(i),      height[i][j+1],    widthZ*(j+1));
420        Vector v4 = Vector (widthX*(i+1),    height[i+1][j],    widthZ*(j));
421        Vector v5 = Vector (widthX*(i),      height[i][j-1],    widthZ*(j-1));
422       
423        Vector c1 = v2 - v1;
424        Vector c2 = v3 - v1;
425        Vector c3=  v4 - v1;
426        Vector c4 = v5 - v1;
427        Vector zero = Vector (0,0,0);
428        normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4);
429        normal_vectors[i][j].normalize();
430      }
431
432  int snowheight=13;
433  for ( int i = 0; i<sizeX; i+=1)
434    for (int j = 0; j<sizeZ;j+=1)
435      {   
436        Vector v1 = Vector (widthX*(i),      height[i][j]-20,       widthZ*(j)  -width/2);
437        Vector v2 = Vector (widthX*(i+1),    height[i+1][j]-20,     widthZ*(j)  -width/2);
438        Vector v3 = Vector (widthX*(i+1),    height[i+1][j+1]-20,   widthZ*(j+1)-width/2);
439        Vector v4 = Vector (widthX*(i),      height[i][j+1]-20,     widthZ*(j+1)-width/2);
440        float a[3];
441        if(height[i][j]<snowheight){
442          a[0]=0;
443          a[1]=1.0-height[i][j]/10-.3;
444          a[2]=0;
445          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
446        }
447        else{
448            a[0]=1.0;
449            a[1]=1.0;
450            a[2]=1.0;
451            glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
452           
453        }
454        glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z);
455        glTexCoord2f(0.0f,0.0f);
456        glVertex3f(v1.x, v1.y, v1.z);
457        if(height[i+1][j]<snowheight){
458          a[0]=0;
459          a[1] =1.0-height[i+1][j]/10-.3;
460          a[2]=0;
461          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
462        }
463        else{
464          a[0]=1.0;
465          a[1]=1.0;
466          a[2]=1.0;
467          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
468         
469        }
470        glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z);
471        glTexCoord2f(1.0f,0.0f);
472        glVertex3f(v2.x, v2.y, v2.z);
473        if(height[i+1][j+1]<snowheight){
474          a[0]=0;
475          a[1] =1.0-height[i+1][j+1]/10-.3;
476          a[2]=0;
477          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
478        }
479        else{
480          a[0]=1.0;
481          a[1]=1.0;
482          a[2]=1.0;
483          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
484         
485         
486        }
487        glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z);
488        glTexCoord2f(1.0f,1.0f);
489        glVertex3f(v3.x, v3.y, v3.z);
490        if(height[i][j+1]<snowheight){
491          a[0]=0;
492          a[1] =1.0-height[i+1][j+1]/10-.3;
493          a[2]=0;
494          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
495        }
496        else{
497          a[0]=1.0;
498          a[1]=1.0;
499          a[2]=1.0;
500          glMaterialfv(GL_FRONT,GL_DIFFUSE,a);
501        }
502        glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z);
503        glTexCoord2f(0.0f,1.0f);
504        glVertex3f(v4.x, v4.y, v4.z);
505       
506      }
507  glEnd();
508  /* 
509  glBegin(GL_LINES);
510  for( float x = -128.0; x < 128.0; x += 25.0)
511    {
512      for( float y = -128.0; y < 128.0; y += 25.0)
513        {
514          glColor3f(1,0,0);
515          glVertex3f(x,y,-128.0);
516          glVertex3f(x,y,0.0);
517          glColor3f(0.5,0,0);
518          glVertex3f(x,y,0.0);
519          glVertex3f(x,y,128.0);
520        }
521    }
522  for( float y = -128.0; y < 128.0; y += 25.0)
523    {
524      for( float z = -128.0; z < 128.0; z += 25.0)
525        {
526          glColor3f(0,1,0);
527          glVertex3f(-128.0,y,z);
528          glVertex3f(0.0,y,z);
529          glColor3f(0,0.5,0);
530          glVertex3f(0.0,y,z);
531          glVertex3f(128.0,y,z);
532        }
533    }
534  for( float x = -128.0; x < 128.0; x += 25.0)
535    {
536      for( float z = -128.0; z < 128.0; z += 25.0)
537        {
538          glColor3f(0,0,1);
539          glVertex3f(x,-128.0,z);
540          glVertex3f(x,0.0,z);
541          glColor3f(0,0,0.5);
542          glVertex3f(x,0.0,z);
543          glVertex3f(x,128.0,z);
544        }
545     
546    }
547  */ 
548  //draw track
549  glBegin(GL_LINES);
550  glColor3f(0.0, 1.0, 1.0);
551  for( int i = 0; i < tracklen; i++)
552    {
553      glVertex3f(pathnodes[i].x,pathnodes[i].y,pathnodes[i].z);
554      glVertex3f(pathnodes[(i+1)%tracklen].x,pathnodes[(i+1)%tracklen].y,pathnodes[(i+1)%tracklen].z);
555    }
556  glEnd();
557 
558  /*
559  glBegin(GL_LINE_STRIP);
560  glColor3f(1.0, 5.0, 1.0);
561  for( int i = 0; i <= 30; i++)
562    {
563      glEvalCoord1f ((GLfloat) i/30.0);
564    }
565  glEnd();
566  */
567
568  trackManager->drawGraph(.01);
569  trackManager->debug(2);
570  delete trackManager;
571 
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  glEnd();
581  */
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  //glmis = new GLMenuImageScreen();
644  ///glmis->init();
645
646  // draw debug coord system
647  glCallList (objectList);
648
649}
650
651/**
652    \brief updates Placements and notifies entities when they left the
653    world
654   
655    This runs trough all WorldEntities and maps Locations to Placements
656    if they are bound, checks whether they left the level boundaries
657    and calls appropriate functions.
658*/
659void World::update ()
660{
661  /*
662  //List<WorldEntity> *l;
663  WorldEntity* entity;
664  Location* loc;
665  Placement* plc;
666  Uint32 t;
667 
668  //  l = entities->enumerate();
669  entity = this->entities->enumerate();
670  while( entity != NULL )
671    {
672
673     
674      if( !entity->isFree() )
675        {
676          loc = entity->getLocation();
677          plc = entity->getPlacement();
678          t = loc->part;
679         
680          if( t >= tracklen )
681            {
682              printf("An entity is out of the game area\n");
683              entity->leftWorld ();
684            }
685          else
686            {
687              while( track[t].mapCoords( loc, plc) )
688                {
689                  track[t].postLeave (entity);
690                  if( loc->part >= tracklen )
691                    {
692                      printf("An entity has left the game area\n");
693                      entity->leftWorld ();
694                      break;
695                    }
696                  track[loc->part].postEnter (entity);
697                }
698            }
699        }
700      else
701        {
702        }
703     
704      entity = entities->nextElement();
705    }
706  */ 
707}
708
709/**
710    \brief relays the passed time since the last frame to entities and Track parts
711    \param deltaT: the time passed since the last frame in milliseconds
712*/
713void World::timeSlice (Uint32 deltaT)
714{
715  //List<WorldEntity> *l;
716  WorldEntity* entity;
717  float seconds = deltaT / 1000.0;
718 
719  this->nullParent->update (seconds);
720  //this->nullParent->processTick (seconds);
721
722  entity = entities->enumerate(); 
723  while( entity != NULL) 
724    { 
725      entity->tick (seconds);
726      entity = entities->nextElement();
727    }
728
729  //for( int i = 0; i < tracklen; i++) track[i].tick (seconds);
730}
731
732/**
733   \brief removes level data from memory
734*/
735void World::unload()
736{
737  if( pathnodes) delete []pathnodes;
738  if( track) delete []pathnodes;
739}
740
741
742void World::setTrackLen(Uint32 len)
743{
744  this->tracklen = len;
745}
746
747int World::getTrackLen()
748{
749  return this->tracklen;
750}
751
752
753
754/**
755   \brief function to put your own debug stuff into it. it can display informations about
756   the current class/procedure
757*/
758void World::debug()
759{
760  printf ("World::debug() - starting debug\n");
761  PNode* p1 = NullParent::getInstance ();
762  PNode* p2 = new PNode (new Vector(2, 2, 2), p1);
763  PNode* p3 = new PNode (new Vector(4, 4, 4), p1);
764  PNode* p4 = new PNode (new Vector(6, 6, 6), p2);
765
766  p1->debug ();
767  p2->debug ();
768  p3->debug ();
769  p4->debug ();
770
771  p1->shiftCoor (new Vector(-1, -1, -1));
772
773  printf("World::debug() - shift\n");
774  p1->debug ();
775  p2->debug ();
776  p3->debug ();
777  p4->debug ();
778 
779  p1->update (1);
780
781  printf ("World::debug() - update\n");
782  p1->debug ();
783  p2->debug ();
784  p3->debug ();
785  p4->debug ();
786
787  p2->shiftCoor (new Vector(-1, -1, -1));
788  p1->update (2);
789
790  p1->debug ();
791  p2->debug ();
792  p3->debug ();
793  p4->debug ();
794
795  p2->setAbsCoor (new Vector(1,2,3));
796
797
798 p1->update (2);
799
800  p1->debug ();
801  p2->debug ();
802  p3->debug ();
803  p4->debug ();
804
805  p1->destroy ();
806 
807 
808  /*
809  WorldEntity* entity;
810  printf("counting all entities\n");
811  printf("World::debug() - enumerate()\n");
812  entity = entities->enumerate(); 
813  while( entity != NULL )
814    {
815      if( entity->bDraw ) printf("got an entity\n");
816      entity = entities->nextElement();
817    }
818  */
819}
820
821
822/*
823  \brief main loop of the world: executing all world relevant function
824
825  in this loop we synchronize (if networked), handle input events, give the heart-beat to
826  all other member-entities of the world (tick to player, enemies etc.), checking for
827  collisions drawing everything to the screen.
828*/
829void World::mainLoop()
830{
831  this->lastFrame = SDL_GetTicks ();
832  printf("World::mainLoop() - Entering main loop\n");
833  while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* \todo implement pause */
834    {
835      // Network
836      this->synchronize ();
837      // Process input
838      this->handleInput ();
839      if( this->bQuitCurrentGame || this->bQuitOrxonox)
840        {
841          printf("World::mainLoop() - leaving loop earlier...\n");
842          break;
843        }
844      // Process time
845      this->timeSlice ();
846      // Process collision
847      this->collision ();
848      // Draw
849      this->display ();
850 
851      for( int i = 0; i < 5000000; i++) {}
852      /* \todo this is to slow down the program for openGl Software emulator computers, reimplement*/
853    }
854  printf("World::mainLoop() - Exiting the main loop\n");
855}
856
857/**
858   \brief synchronize local data with remote data
859*/
860void World::synchronize ()
861{
862  // Get remote input
863  // Update synchronizables
864}
865
866/**
867   \brief run all input processing
868
869   the command node is the central input event dispatcher. the node uses the even-queue from
870   sdl and has its own event-passing-queue.
871*/
872void World::handleInput ()
873{
874  // localinput
875  CommandNode* cn = Orxonox::getInstance()->getLocalInput();
876  cn->process();
877  // remoteinput
878}
879
880/**
881   \brief advance the timeline
882
883   this calculates the time used to process one frame (with all input handling, drawing, etc)
884   the time is mesured in ms and passed to all world-entities and other classes that need
885   a heart-beat.
886*/
887void World::timeSlice ()
888{
889  Uint32 currentFrame = SDL_GetTicks();
890  if(!this->bPause)
891    {
892      Uint32 dt = currentFrame - this->lastFrame;
893     
894      if(dt > 0)
895        {
896          float fps = 1000/dt;
897          printf("fps = %f\n", fps);
898        }
899      else
900        {
901          /* the frame-rate is limited to 100 frames per second, all other things are for
902             nothing.
903          */
904          printf("fps = 1000 - frame rate is adjusted\n");
905          SDL_Delay(10);
906          dt = 10;
907        }
908      this->timeSlice (dt);
909      this->update ();
910      this->localCamera->timeSlice(dt);
911    }
912  this->lastFrame = currentFrame;
913}
914
915
916/**
917   \brief compute collision detection
918*/
919void World::collision ()
920{
921  this->collide ();
922}
923
924
925/**
926   \brief render the current frame
927   
928   clear all buffers and draw the world
929*/
930void World::display ()
931{
932  // clear buffer
933  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
934  // set camera
935  this->localCamera->apply ();
936  // draw world
937  this->draw();
938  // draw HUD
939  /* \todo draw HUD */
940  // flip buffers
941  SDL_GL_SwapBuffers();
942  //SDL_Surface* screen = Orxonox::getInstance()->getScreen ();
943  //SDL_Flip (screen);
944}
945
946/**
947   \brief give back active camera
948   
949   this passes back the actualy active camera
950   \todo ability to define more than one camera or camera-places
951*/
952Camera* World::getCamera()
953{
954  return this->localCamera;
955}
956
957
958/**
959   \brief add and spawn a new entity to this world
960   \param entity to be added
961*/
962void World::spawn(WorldEntity* entity)
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   \brief add and spawn a new entity to this world
975   \param entity to be added
976   \param location where to add
977*/
978void World::spawn(WorldEntity* entity, Vector* absCoor, Quaternion* absDir)
979{
980  entity->setAbsCoor (absCoor);
981  entity->setAbsDir (absDir);
982 
983  if( this->nullParent != NULL && entity->parent == NULL)
984    this->nullParent->addChild (entity);
985
986  this->entities->add (entity);
987
988  entity->postSpawn ();
989}
990
991
992
993/*
994  \brief commands that the world must catch
995  \returns false if not used by the world
996*/
997bool World::command(Command* cmd)
998{
999  return false;
1000}
1001
1002
1003
1004
1005void World::swap (unsigned char &a, unsigned char &b)
1006{
1007  unsigned char temp;
1008  temp = a;
1009  a    = b;
1010  b    = temp;
1011}
Note: See TracBrowser for help on using the repository browser.