Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3346 was 3345, checked in by bensch, 20 years ago

orxonox/branches/parenting: :loadscreen: now uses texture-class

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