Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/story_entities/game_world.cc @ 7785

Last change on this file since 7785 was 7785, checked in by bensch, 18 years ago

orxonox/trunk: merged the Changes from the water branche back to the trunk.

File size: 14.4 KB
RevLine 
[6352]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
[6358]19#include "game_world.h"
[6402]20#include "game_world_data.h"
[6352]21
[7287]22#include "util/loading/resource_manager.h"
[6352]23#include "state.h"
[6404]24#include "class_list.h"
[6352]25
[7193]26#include "util/loading/game_loader.h"
[6404]27
[6352]28#include "player.h"
29#include "camera.h"
30#include "environment.h"
31#include "terrain.h"
[7287]32#include "test_entity.h"
33#include "terrain.h"
[6404]34#include "playable.h"
[7785]35#include "environments/mapped_water.h"
[6404]36
[6352]37#include "light.h"
[6404]38
[7193]39#include "util/loading/factory.h"
[7368]40#include "util/loading/load_param.h"
[6404]41#include "fast_factory.h"
42#include "shell_command.h"
[6352]43
44#include "graphics_engine.h"
[6404]45#include "event_handler.h"
46#include "sound_engine.h"
47#include "cd_engine.h"
48#include "network_manager.h"
[6352]49#include "physics_engine.h"
[7287]50#include "fields.h"
[6352]51
52#include "glmenu_imagescreen.h"
[6404]53#include "shell.h"
[6352]54
55#include "ogg_player.h"
[6404]56#include "shader.h"
[6352]57
[7369]58#include "animation_player.h"
59
[7035]60#include "game_rules.h"
[6352]61
[6404]62using namespace std;
[6352]63
64
[7412]65SHELL_COMMAND(speed, GameWorld, setSpeed) ->describe("set the Speed of the Level");
66SHELL_COMMAND(playmode, GameWorld, setPlaymode)
[7723]67->describe("Set the Playmode of the current Level")
68->completionPlugin(0, OrxShell::CompletorStringArray(Playable::playmodeNames, Playable::PlaymodeCount));
[7412]69
[7739]70SHELL_COMMAND(togglePNodeVisibility, GameWorld, togglePNodeVisibility);
71SHELL_COMMAND(showBVLevel, GameWorld, toggleBVVisibility);
[6352]72
73
74
[6989]75GameWorld::GameWorld()
[6402]76    : StoryEntity()
[6352]77{
[6368]78  this->setClassID(CL_GAME_WORLD, "GameWorld");
79  this->setName("Preloaded World - no name yet");
80
81  this->gameTime = 0.0f;
[6370]82  this->setSpeed(1.0f);
[6368]83  this->shell = NULL;
84
85  this->showPNodes = false;
86  this->showBV = false;
[7739]87  this->showBVLevel = 3;
[6368]88
[6845]89  this->dataXML = NULL;
[7391]90  this->gameRules = NULL;
[6352]91}
92
93/**
[6358]94 *  remove the GameWorld from memory
[6352]95 *
96 *  delete everything explicitly, that isn't contained in the parenting tree!
97 *  things contained in the tree are deleted automaticaly
98 */
[6358]99GameWorld::~GameWorld ()
[6352]100{
[6408]101  PRINTF(4)("Deleted GameWorld\n");
[7283]102
[6352]103}
104
105
106
107/**
[6358]108 * loads the parameters of a GameWorld from an XML-element
[6352]109 * @param root the XML-element to load from
110 */
[6358]111void GameWorld::loadParams(const TiXmlElement* root)
[6352]112{
[6512]113  StoryEntity::loadParams(root);
[6352]114
[6376]115  PRINTF(4)("Loaded GameWorld specific stuff\n");
[6352]116}
117
[6368]118
[6352]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*/
[6370]125ErrorMessage GameWorld::init()
[6352]126{
127  /* init the world interface */
[7374]128  this->shell = new OrxShell::Shell();
[6352]129
[6498]130  State::setCurrentStoryEntity(dynamic_cast<StoryEntity*>(this));
[6407]131  this->dataTank->init();
[7369]132
133  /* initialize some engines and graphical elements */
134  AnimationPlayer::getInstance();
135  PhysicsEngine::getInstance();
136
[6352]137}
138
[7288]139
[6352]140/**
[6358]141 *  loads the GameWorld by initializing all resources, and set their default values.
[6352]142 */
[6372]143ErrorMessage GameWorld::loadData()
[6352]144{
[6370]145  this->displayLoadScreen();
146
[6407]147  PRINTF(0)("Loading the GameWorld\n");
148
[7677]149  PRINTF(3)("> Loading world: '%s'\n", getLoadFile().c_str());
[6352]150  TiXmlElement* element;
151  GameLoader* loader = GameLoader::getInstance();
152
[7221]153  if( getLoadFile().empty())
[6402]154  {
[6634]155    PRINTF(1)("GameWorld has no path specified for loading\n");
[6402]156    return (ErrorMessage){213,"Path not specified","GameWorld::load()"};
157  }
[6352]158
[7287]159  TiXmlDocument* XMLDoc = new TiXmlDocument( getLoadFile());
[6402]160  // load the xml world file for further loading
[7287]161  if( !XMLDoc->LoadFile())
[6352]162  {
[7287]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;
[6358]165    return (ErrorMessage){213,"XML File parsing error","GameWorld::load()"};
[6352]166  }
167  // check basic validity
[7287]168  TiXmlElement* root = XMLDoc->RootElement();
[6352]169  assert( root != NULL);
170  if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile"))
171  {
[6402]172    // report an error
173    PRINTF(1)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n");
[7287]174    delete XMLDoc;
[6402]175    return (ErrorMessage){213,"Path not a WorldDataFile","GameWorld::load()"};
[6352]176  }
[6402]177  /* the whole loading process for the GameWorld */
[6407]178  this->dataTank->loadData(root);
[6845]179  this->dataXML = (TiXmlElement*)root->Clone();
[6352]180
[7287]181  delete XMLDoc;
[6386]182  this->releaseLoadScreen();
[6352]183}
184
185
186/**
[6387]187 *  unload the data of this GameWorld
188 */
189ErrorMessage GameWorld::unloadData()
190{
[7287]191  PRINTF(3)("GameWorld::~GameWorld() - unloading the current GameWorld\n");
[7370]192  delete this->shell;
[6387]193
[6981]194  this->dataTank->unloadData();
195
[7369]196  this->shell = NULL;
197  delete AnimationPlayer::getInstance();
198  delete PhysicsEngine::getInstance();
199
[6988]200  State::setCurrentStoryEntity(NULL);
[6845]201  if (this->dataXML)
202    delete this->dataXML;
[6387]203}
204
205
206/**
[6358]207 *  starts the GameWorld
[6352]208 */
[6387]209bool GameWorld::start()
[6352]210{
[7283]211  this->bPaused = false;
212  this->bRunning = true;
[6387]213
214  this->run();
[6352]215}
216
[6402]217
[6352]218/**
219 *  stops the world.
[6402]220 */
[6387]221bool GameWorld::stop()
[6352]222{
[6358]223  PRINTF(3)("GameWorld::stop() - got stop signal\n");
[7283]224  this->bRunning = false;
[6352]225}
226
[6402]227
[6352]228/**
[6402]229 *  pauses the game
230 */
[6387]231bool GameWorld::pause()
[6352]232{
[7283]233  this->bPaused = true;
[6352]234}
235
[6402]236
[6352]237/**
238 *  ends the pause Phase
[6409]239 */
[6387]240bool GameWorld::resume()
[6352]241{
[7283]242  this->bPaused = false;
[6352]243}
244
245
246/**
[6402]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()
[6352]254{
[6402]255  PRINTF(3)("GameWorld::mainLoop() - Entering main loop\n");
[6352]256
[7131]257  // initialize Timing
258  this->cycle = 0;
259  for (unsigned int i = 0; i < TICK_SMOOTH_VALUE; i++)
[7132]260    this->frameTimes[i] = 100;
[7131]261  this->dtS = 0.0f;
262  this->lastFrame = SDL_GetTicks ();
263
[7304]264  if (this->dataTank->music != NULL)
265    this->dataTank->music->play();
266
[7283]267  while( this->bRunning) /* @todo implement pause */
[6402]268  {
269    /* process intput */
270    this->handleInput ();
[7322]271    if( !this->bRunning)
272      break;
[6386]273
[6402]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 ();
[7391]282    /* check the game rules */
283    this->checkGameRules();
[6402]284    /* draw everything */
285    this->display ();
[7306]286
[6402]287  }
288
[7330]289  PRINTF(0)("GameWorld::mainLoop() - Exiting the main loop\n");
[6352]290}
291
292
[7338]293void GameWorld::setPlaymode(Playable::Playmode playmode)
294{
295  if (this->dataTank->localPlayer &&
296      this->dataTank->localPlayer->getPlayable() &&
[7339]297      this->dataTank->localPlayer->getPlayable()->setPlaymode(playmode))
[7338]298  {
[7412]299    PRINTF(3)("Set Playmode to %d:%s\n", playmode, Playable::playmodeToString(playmode).c_str());
[7338]300  }
[7339]301  else
302  {
[7414]303    PRINTF(1)("Unable to set Playmode %d:'%s'\n", playmode, Playable::playmodeToString(playmode).c_str());
[7339]304  }
[7338]305}
306
[7339]307void GameWorld::setPlaymode(const std::string& playmode)
308{
309  this->setPlaymode(Playable::stringToPlaymode(playmode));
310}
[7338]311
[6352]312/**
313 *  synchronize local data with remote data
314*/
[6358]315void GameWorld::synchronize ()
[6402]316{}
[6352]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*/
[6358]325void GameWorld::handleInput ()
[6352]326{
327  EventHandler::getInstance()->process();
328}
329
[6402]330
331/**
[7370]332 * @brief ticks a WorldEntity list
[6402]333 * @param entityList list of the WorldEntities
334 * @param dt time passed since last frame
335 */
[7370]336void GameWorld::tick(ObjectManager::EntityList entityList, float dt)
[6352]337{
[7370]338  ObjectManager::EntityList::iterator entity, next;
[6710]339  next = entityList.begin();
340  while (next != entityList.end())
341  {
342    entity = next++;
[6352]343    (*entity)->tick(dt);
[6710]344  }
[6352]345}
346
[7131]347
[6352]348/**
349 *  advance the timeline
[6402]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 */
[6358]355void GameWorld::tick ()
[6352]356{
357  Uint32 currentFrame = SDL_GetTicks();
[6402]358
[7283]359  if( !this->bPaused)
[6402]360  {
[7131]361    // CALCULATE FRAMERATE
362    Uint32 frameTimesIndex;
363    Uint32 getTicks;
364    Uint32 i;
[6402]365
[7131]366    frameTimesIndex = this->cycle % TICK_SMOOTH_VALUE;
367    getTicks = SDL_GetTicks();
368    this->frameTimes[frameTimesIndex] = getTicks - this->lastFrame;
369    this->lastFrame = getTicks;
[7132]370    ++this->cycle;
[7131]371    this->dtS = 0;
[7132]372    for (i = 0; i < TICK_SMOOTH_VALUE; i++)
[7131]373      this->dtS += this->frameTimes[i];
[7132]374    this->dtS = this->dtS / TICK_SMOOTH_VALUE / 1000.0f * speed;
[6352]375
[7131]376    // TICK everything
[7370]377    for (i = 0; i < this->dataTank->tickLists.size(); ++i)
378      this->tick(this->dataTank->objectManager->getObjectList(this->dataTank->tickLists[i]), this->dtS);
[6352]379
[6402]380    /* update tick the rest */
[6407]381    this->dataTank->localCamera->tick(this->dtS);
[6402]382    AnimationPlayer::getInstance()->tick(this->dtS);
383    PhysicsEngine::getInstance()->tick(this->dtS);
[6352]384
[6402]385    GraphicsEngine::getInstance()->tick(this->dtS);
[7035]386
387    if( likely(this->dataTank->gameRule != NULL))
388      this->dataTank->gameRule->tick(this->dtS);
[6402]389  }
[6352]390  this->lastFrame = currentFrame;
391}
392
393
394/**
395 *  this function gives the world a consistant state
[6402]396 *
397 * after ticking (updating the world state) this will give a constistant
398 * state to the whole system.
399 */
[6358]400void GameWorld::update()
[6352]401{
402  GraphicsEngine::getInstance()->update(this->dtS);
403  PNode::getNullParent()->updateNode (this->dtS);
[7460]404  OrxSound::SoundEngine::getInstance()->update();
[6352]405}
406
407
[6402]408/**
409 * kicks the CDEngine to detect the collisions between the object groups in the world
410 */
[6358]411void GameWorld::collide()
[6352]412{
[6407]413  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
[7004]414      this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ));
[6407]415  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
[7004]416      this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ));
[7078]417  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
[7288]418      this->dataTank->objectManager->getObjectList(OM_GROUP_01));
[6433]419
[7083]420  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
421      this->dataTank->objectManager->getObjectList(OM_COMMON));
[6433]422  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
[6407]423      this->dataTank->objectManager->getObjectList(OM_COMMON));
[7083]424
[6352]425}
426
[7391]427
[6352]428/**
[7391]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/**
[6352]440 *  render the current frame
[6402]441 *
442 * clear all buffers and draw the world
443 */
[6358]444void GameWorld::display ()
[6352]445{
[7785]446  // render the reflection texture
447  this->renderPassReflection();
448  // redner the refraction texture
449  this->renderPassRefraction();
450  // render all
451  this->renderPassAll();
452
[6352]453  // flip buffers
454  GraphicsEngine::swapBuffers();
455}
456
457
458/**
[7370]459 * @brief draws all entities in the list drawList
460 * @param drawList the List of entities to draw.
[6352]461 */
[7370]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
[7785]470
471
[7370]472/**
[7785]473 * reflection rendering for water surfaces
[7370]474 */
[7785]475void GameWorld::renderPassReflection()
[6352]476{
[7785]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      for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
500        this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
501
502      // clean up from reflection rendering
503      mw->deactivateReflection();
504    }
505  }
506
507}
508
509
510/**
511 *  refraction rendering for water surfaces
512 */
513void GameWorld::renderPassRefraction()
514{}
515
516
517/**
518 *  this render pass renders the whole wolrd
519 */
520void GameWorld::renderPassAll()
521{
522  // clear buffer
523  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
[6352]524  GraphicsEngine* engine = GraphicsEngine::getInstance();
[6402]525
[6780]526  // set camera
527  this->dataTank->localCamera->apply ();
[7108]528  this->dataTank->localCamera->project ();
529  LightManager::getInstance()->draw();
[6416]530
531  /* draw all WorldEntiy groups */
[7370]532  for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
[7723]533    this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
[6416]534
[6780]535
[6402]536  if( unlikely( this->showBV))
537  {
538    CDEngine* engine = CDEngine::getInstance();
[7370]539    for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
[7739]540      engine->drawBV(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]), this->showBVLevel);
[6402]541  }
[6416]542
[6402]543  if( unlikely(this->showPNodes))
544    PNode::getNullParent()->debugDraw(0);
545
[6780]546  engine->draw();
[7035]547
548  // draw the game ruls
549  if( likely(this->dataTank->gameRule != NULL))
550    this->dataTank->gameRule->draw();
[6352]551}
552
[7785]553
[6352]554/**
[6402]555 *  shows the loading screen
556 */
557void GameWorld::displayLoadScreen ()
[6387]558{
[6402]559  PRINTF(3)("GameWorld::displayLoadScreen - start\n");
[6407]560  this->dataTank->glmis = new GLMenuImageScreen();
561  this->dataTank->glmis->setMaximum(8);
[6402]562  PRINTF(3)("GameWorld::displayLoadScreen - end\n");
[6387]563}
564
565
[6402]566/**
567 *  removes the loadscreen, and changes over to the game
568 */
569void GameWorld::releaseLoadScreen ()
[6387]570{
[6402]571  PRINTF(3)("GameWorld::releaseLoadScreen - start\n");
[6407]572  this->dataTank->glmis->setValue(this->dataTank->glmis->getMaximum());
[6402]573  PRINTF(3)("GameWorld::releaseLoadScreen - end\n");
[6387]574}
575
[7004]576
577
578/**
579 * @brief toggles the PNode visibility in the world (drawn as boxes)
580 */
581void GameWorld::togglePNodeVisibility()
582{
[7723]583  this->showPNodes = !this->showPNodes;
[7004]584};
585
586
587/**
588 * @brief toggles the bounding volume (BV) visibility
589*/
[7739]590void GameWorld::toggleBVVisibility(int level)
[7004]591{
[7739]592  if( level < 1)
593    this->showBV = false;
594  else
595  {
596    this->showBV = true;
597    this->showBVLevel = level;
598  }
599
[7004]600};
601
Note: See TracBrowser for help on using the repository browser.