Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

branches/dave/src: Textur ist jetzt drauf

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