Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/parenting: resolved a svn-conflict, corrected come bugs in the world.cc code

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