Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/water/src/story_entities/game_world.cc @ 8034

Last change on this file since 8034 was 8031, checked in by stefalie, 18 years ago

water: ok, so far

File size: 15.5 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Patrick Boenzli
13   co-programmer: Christian Meyer
14   co-programmer: Benjamin Grauer
15*/
16
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD
18
19#include "game_world.h"
20#include "game_world_data.h"
21
22#include "util/loading/resource_manager.h"
23#include "state.h"
24#include "class_list.h"
25
26#include "util/loading/game_loader.h"
27
28#include "player.h"
29#include "camera.h"
30#include "environment.h"
31#include "terrain.h"
32#include "test_entity.h"
33#include "terrain.h"
34#include "playable.h"
35#include "environments/mapped_water.h"
36
37#include "light.h"
38
39#include "util/loading/factory.h"
40#include "util/loading/load_param.h"
41#include "fast_factory.h"
42#include "shell_command.h"
43
44#include "graphics_engine.h"
45#include "event_handler.h"
46#include "sound_engine.h"
47#include "cd_engine.h"
48#include "network_manager.h"
49#include "physics_engine.h"
50#include "fields.h"
51
52#include "glmenu_imagescreen.h"
53#include "shell.h"
54
55#include "ogg_player.h"
56#include "shader.h"
57
58#include "animation_player.h"
59
60#include "game_rules.h"
61
62using namespace std;
63
64
65SHELL_COMMAND(speed, GameWorld, setSpeed) ->describe("set the Speed of the Level");
66SHELL_COMMAND(playmode, GameWorld, setPlaymode)
67->describe("Set the Playmode of the current Level")
68->completionPlugin(0, OrxShell::CompletorStringArray(Playable::playmodeNames, Playable::PlaymodeCount));
69
70SHELL_COMMAND(togglePNodeVisibility, GameWorld, togglePNodeVisibility);
71SHELL_COMMAND(showBVLevel, GameWorld, toggleBVVisibility);
72
73
74
75GameWorld::GameWorld()
76    : StoryEntity()
77{
78  this->setClassID(CL_GAME_WORLD, "GameWorld");
79  this->setName("Preloaded World - no name yet");
80
81  this->gameTime = 0.0f;
82  this->setSpeed(1.0f);
83  this->shell = NULL;
84
85  this->showPNodes = false;
86  this->showBV = false;
87  this->showBVLevel = 3;
88
89  this->dataXML = NULL;
90  this->gameRules = NULL;
91}
92
93/**
94 *  remove the GameWorld from memory
95 *
96 *  delete everything explicitly, that isn't contained in the parenting tree!
97 *  things contained in the tree are deleted automaticaly
98 */
99GameWorld::~GameWorld ()
100{
101  PRINTF(4)("Deleted GameWorld\n");
102
103}
104
105
106
107/**
108 * loads the parameters of a GameWorld from an XML-element
109 * @param root the XML-element to load from
110 */
111void GameWorld::loadParams(const TiXmlElement* root)
112{
113  StoryEntity::loadParams(root);
114
115  PRINTF(4)("Loaded GameWorld specific stuff\n");
116}
117
118
119/**
120 * this is executed just before load
121 *
122 * since the load function sometimes needs data, that has been initialized
123 * before the load and after the proceeding storyentity has finished
124*/
125ErrorMessage GameWorld::init()
126{
127  /* init the world interface */
128  this->shell = new OrxShell::Shell();
129
130  State::setCurrentStoryEntity(dynamic_cast<StoryEntity*>(this));
131  this->dataTank->init();
132
133  /* initialize some engines and graphical elements */
134  AnimationPlayer::getInstance();
135  PhysicsEngine::getInstance();
136
137}
138
139
140/**
141 *  loads the GameWorld by initializing all resources, and set their default values.
142 */
143ErrorMessage GameWorld::loadData()
144{
145  this->displayLoadScreen();
146
147  PRINTF(0)("Loading the GameWorld\n");
148
149  PRINTF(3)("> Loading world: '%s'\n", getLoadFile().c_str());
150  TiXmlElement* element;
151  GameLoader* loader = GameLoader::getInstance();
152
153  if( getLoadFile().empty())
154  {
155    PRINTF(1)("GameWorld has no path specified for loading\n");
156    return (ErrorMessage){213,"Path not specified","GameWorld::load()"};
157  }
158
159  TiXmlDocument* XMLDoc = new TiXmlDocument( getLoadFile());
160  // load the xml world file for further loading
161  if( !XMLDoc->LoadFile())
162  {
163    PRINTF(1)("loading XML File: %s @ %s:l%d:c%d\n", XMLDoc->ErrorDesc(), this->getLoadFile().c_str(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
164    delete XMLDoc;
165    return (ErrorMessage){213,"XML File parsing error","GameWorld::load()"};
166  }
167  // check basic validity
168  TiXmlElement* root = XMLDoc->RootElement();
169  assert( root != NULL);
170  if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile"))
171  {
172    // report an error
173    PRINTF(1)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n");
174    delete XMLDoc;
175    return (ErrorMessage){213,"Path not a WorldDataFile","GameWorld::load()"};
176  }
177  /* the whole loading process for the GameWorld */
178  this->dataTank->loadData(root);
179  this->dataXML = (TiXmlElement*)root->Clone();
180
181  delete XMLDoc;
182  this->releaseLoadScreen();
183}
184
185
186/**
187 *  unload the data of this GameWorld
188 */
189ErrorMessage GameWorld::unloadData()
190{
191  PRINTF(3)("GameWorld::~GameWorld() - unloading the current GameWorld\n");
192  delete this->shell;
193
194  this->dataTank->unloadData();
195
196  this->shell = NULL;
197  delete AnimationPlayer::getInstance();
198  delete PhysicsEngine::getInstance();
199
200  State::setCurrentStoryEntity(NULL);
201  if (this->dataXML)
202    delete this->dataXML;
203}
204
205
206/**
207 *  starts the GameWorld
208 */
209bool GameWorld::start()
210{
211  this->bPaused = false;
212  this->bRunning = true;
213
214  this->run();
215}
216
217
218/**
219 *  stops the world.
220 */
221bool GameWorld::stop()
222{
223  PRINTF(3)("GameWorld::stop() - got stop signal\n");
224  this->bRunning = false;
225}
226
227
228/**
229 *  pauses the game
230 */
231bool GameWorld::pause()
232{
233  this->bPaused = true;
234}
235
236
237/**
238 *  ends the pause Phase
239 */
240bool GameWorld::resume()
241{
242  this->bPaused = false;
243}
244
245
246/**
247 *  main loop of the world: executing all world relevant function
248 *
249 * in this loop we synchronize (if networked), handle input events, give the heart-beat to
250 * all other member-entities of the world (tick to player, enemies etc.), checking for
251 * collisions drawing everything to the screen.
252 */
253void GameWorld::run()
254{
255  PRINTF(3)("GameWorld::mainLoop() - Entering main loop\n");
256
257  // initialize Timing
258  this->cycle = 0;
259  for (unsigned int i = 0; i < TICK_SMOOTH_VALUE; i++)
260    this->frameTimes[i] = 100;
261  this->dtS = 0.0f;
262  this->lastFrame = SDL_GetTicks ();
263
264  if (this->dataTank->music != NULL)
265    this->dataTank->music->play();
266
267  while( this->bRunning) /* @todo implement pause */
268  {
269    /* process intput */
270    this->handleInput ();
271    if( !this->bRunning)
272      break;
273
274    /* network synchronisation */
275    this->synchronize ();
276    /* process time */
277    this->tick ();
278    /* process collision */
279    this->collide ();
280    /* update the state */
281    this->update ();
282    /* check the game rules */
283    this->checkGameRules();
284    /* draw everything */
285    this->display ();
286
287  }
288
289  PRINTF(0)("GameWorld::mainLoop() - Exiting the main loop\n");
290}
291
292
293void GameWorld::setPlaymode(Playable::Playmode playmode)
294{
295  if (this->dataTank->localPlayer &&
296      this->dataTank->localPlayer->getPlayable() &&
297      this->dataTank->localPlayer->getPlayable()->setPlaymode(playmode))
298  {
299    PRINTF(3)("Set Playmode to %d:%s\n", playmode, Playable::playmodeToString(playmode).c_str());
300  }
301  else
302  {
303    PRINTF(1)("Unable to set Playmode %d:'%s'\n", playmode, Playable::playmodeToString(playmode).c_str());
304  }
305}
306
307void GameWorld::setPlaymode(const std::string& playmode)
308{
309  this->setPlaymode(Playable::stringToPlaymode(playmode));
310}
311
312/**
313 *  synchronize local data with remote data
314*/
315void GameWorld::synchronize ()
316{}
317
318
319/**
320 *  run all input processing
321
322   the command node is the central input event dispatcher. the node uses the even-queue from
323   sdl and has its own event-passing-queue.
324*/
325void GameWorld::handleInput ()
326{
327  EventHandler::getInstance()->process();
328}
329
330
331/**
332 * @brief ticks a WorldEntity list
333 * @param entityList list of the WorldEntities
334 * @param dt time passed since last frame
335 */
336void GameWorld::tick(ObjectManager::EntityList entityList, float dt)
337{
338  ObjectManager::EntityList::iterator entity, next;
339  next = entityList.begin();
340  while (next != entityList.end())
341  {
342    entity = next++;
343    (*entity)->tick(dt);
344  }
345}
346
347
348/**
349 *  advance the timeline
350 *
351 * this calculates the time used to process one frame (with all input handling, drawing, etc)
352 * the time is mesured in ms and passed to all world-entities and other classes that need
353 * a heart-beat.
354 */
355void GameWorld::tick ()
356{
357  Uint32 currentFrame = SDL_GetTicks();
358
359  if( !this->bPaused)
360  {
361    // CALCULATE FRAMERATE
362    Uint32 frameTimesIndex;
363    Uint32 getTicks;
364    Uint32 i;
365
366    frameTimesIndex = this->cycle % TICK_SMOOTH_VALUE;
367    getTicks = SDL_GetTicks();
368    this->frameTimes[frameTimesIndex] = getTicks - this->lastFrame;
369    this->lastFrame = getTicks;
370    ++this->cycle;
371    this->dtS = 0;
372    for (i = 0; i < TICK_SMOOTH_VALUE; i++)
373      this->dtS += this->frameTimes[i];
374    this->dtS = this->dtS / TICK_SMOOTH_VALUE / 1000.0f * speed;
375
376    // TICK everything
377    for (i = 0; i < this->dataTank->tickLists.size(); ++i)
378      this->tick(this->dataTank->objectManager->getObjectList(this->dataTank->tickLists[i]), this->dtS);
379
380    /* update tick the rest */
381    this->dataTank->localCamera->tick(this->dtS);
382    AnimationPlayer::getInstance()->tick(this->dtS);
383    PhysicsEngine::getInstance()->tick(this->dtS);
384
385    GraphicsEngine::getInstance()->tick(this->dtS);
386
387    if( likely(this->dataTank->gameRule != NULL))
388      this->dataTank->gameRule->tick(this->dtS);
389  }
390  this->lastFrame = currentFrame;
391}
392
393
394/**
395 *  this function gives the world a consistant state
396 *
397 * after ticking (updating the world state) this will give a constistant
398 * state to the whole system.
399 */
400void GameWorld::update()
401{
402  GraphicsEngine::getInstance()->update(this->dtS);
403  PNode::getNullParent()->updateNode (this->dtS);
404  OrxSound::SoundEngine::getInstance()->update();
405}
406
407
408/**
409 * kicks the CDEngine to detect the collisions between the object groups in the world
410 */
411void GameWorld::collide()
412{
413  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
414      this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ));
415  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
416      this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ));
417  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
418      this->dataTank->objectManager->getObjectList(OM_GROUP_01));
419
420  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
421      this->dataTank->objectManager->getObjectList(OM_COMMON));
422  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
423      this->dataTank->objectManager->getObjectList(OM_COMMON));
424
425}
426
427
428/**
429 *  check the game rules: winning conditions, etc.
430 *
431 */
432void GameWorld::checkGameRules()
433{
434  if( this->gameRules)
435    this->gameRules->tick(this->dtS);
436}
437
438
439/**
440 *  render the current frame
441 *
442 * clear all buffers and draw the world
443 */
444void GameWorld::display ()
445{
446  // render the reflection texture
447  this->renderPassReflection();
448  // redner the refraction texture
449  this->renderPassRefraction();
450  // render all
451  this->renderPassAll();
452
453  // flip buffers
454  GraphicsEngine::swapBuffers();
455}
456
457
458/**
459 * @brief draws all entities in the list drawList
460 * @param drawList the List of entities to draw.
461 */
462void GameWorld::drawEntityList(const ObjectManager::EntityList& drawList) const
463{
464  ObjectManager::EntityList::const_iterator entity;
465  for (entity = drawList.begin(); entity != drawList.end(); entity++)
466    if ((*entity)->isVisible())
467      (*entity)->draw();
468}
469
470
471
472/**
473 * reflection rendering for water surfaces
474 */
475void GameWorld::renderPassReflection()
476{
477    // clear buffer
478  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
479  glLoadIdentity();
480
481  const std::list<BaseObject*>* reflectedWaters;
482  MappedWater* mw;
483
484  if( (reflectedWaters = ClassList::getList(CL_MAPPED_WATER)) != NULL)
485  {
486    std::list<BaseObject*>::const_iterator it;
487    for (it = reflectedWaters->begin(); it != reflectedWaters->end(); it++)
488    {
489      mw =  dynamic_cast<MappedWater*>(*it);
490
491      // prepare for reflection rendering
492      mw->activateReflection();
493
494      //camera and light
495      this->dataTank->localCamera->apply ();
496      this->dataTank->localCamera->project ();
497      LightManager::getInstance()->draw();
498      // draw everything to be included in the reflection
499      this->drawEntityList(State::getObjectManager()->getReflectionList());
500//       for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
501//         this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
502
503      // clean up from reflection rendering
504      mw->deactivateReflection();
505    }
506  }
507
508}
509
510
511/**
512 *  refraction rendering for water surfaces
513 */
514void GameWorld::renderPassRefraction()
515{
516
517    // clear buffer
518  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
519  glLoadIdentity();
520
521  const std::list<BaseObject*>* reflectedWaters;
522  MappedWater* mw;
523
524  if( (reflectedWaters = ClassList::getList(CL_MAPPED_WATER)) != NULL)
525  {
526    std::list<BaseObject*>::const_iterator it;
527    for (it = reflectedWaters->begin(); it != reflectedWaters->end(); it++)
528    {
529      mw =  dynamic_cast<MappedWater*>(*it);
530
531      // prepare for reflection rendering
532      mw->activateRefraction();
533
534      //camera and light
535      this->dataTank->localCamera->apply ();
536      this->dataTank->localCamera->project ();
537      LightManager::getInstance()->draw();
538      // draw everything to be included in the reflection
539      this->drawEntityList(State::getObjectManager()->getReflectionList());
540//       for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
541//         this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
542
543      // clean up from reflection rendering
544      mw->deactivateRefraction();
545    }
546  }
547}
548
549
550/**
551 *  this render pass renders the whole wolrd
552 */
553void GameWorld::renderPassAll()
554{
555  // clear buffer
556  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
557  GraphicsEngine* engine = GraphicsEngine::getInstance();
558
559  // set camera
560  this->dataTank->localCamera->apply ();
561  this->dataTank->localCamera->project ();
562  LightManager::getInstance()->draw();
563
564  /* draw all WorldEntiy groups */
565  for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
566    this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
567
568
569  if( unlikely( this->showBV))
570  {
571    CDEngine* engine = CDEngine::getInstance();
572    for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
573      engine->drawBV(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]), this->showBVLevel);
574  }
575
576  if( unlikely(this->showPNodes))
577    PNode::getNullParent()->debugDraw(0);
578
579  engine->draw();
580
581  // draw the game ruls
582  if( likely(this->dataTank->gameRule != NULL))
583    this->dataTank->gameRule->draw();
584}
585
586
587/**
588 *  shows the loading screen
589 */
590void GameWorld::displayLoadScreen ()
591{
592  PRINTF(3)("GameWorld::displayLoadScreen - start\n");
593  this->dataTank->glmis = new GLMenuImageScreen();
594  this->dataTank->glmis->setMaximum(8);
595  PRINTF(3)("GameWorld::displayLoadScreen - end\n");
596}
597
598
599/**
600 *  removes the loadscreen, and changes over to the game
601 */
602void GameWorld::releaseLoadScreen ()
603{
604  PRINTF(3)("GameWorld::releaseLoadScreen - start\n");
605  this->dataTank->glmis->setValue(this->dataTank->glmis->getMaximum());
606  PRINTF(3)("GameWorld::releaseLoadScreen - end\n");
607}
608
609
610
611/**
612 * @brief toggles the PNode visibility in the world (drawn as boxes)
613 */
614void GameWorld::togglePNodeVisibility()
615{
616  this->showPNodes = !this->showPNodes;
617};
618
619
620/**
621 * @brief toggles the bounding volume (BV) visibility
622*/
623void GameWorld::toggleBVVisibility(int level)
624{
625  if( level < 1)
626    this->showBV = false;
627  else
628  {
629    this->showBV = true;
630    this->showBVLevel = level;
631  }
632
633};
634
Note: See TracBrowser for help on using the repository browser.