Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3351 was 3351, checked in by bensch, 19 years ago

orxonox/branches/parenting: :TrackManager: forking works.

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