Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/atmospheric_engine/src/story_entities/game_world.cc @ 7530

Last change on this file since 7530 was 7530, checked in by hdavid, 18 years ago

branches/atmospheric_engine: ticking and drawing from the AtmosphericEngine works

File size: 13.2 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"
35
[6352]36#include "light.h"
[6404]37
[7193]38#include "util/loading/factory.h"
[7368]39#include "util/loading/load_param.h"
[6404]40#include "fast_factory.h"
41#include "shell_command.h"
[6352]42
43#include "graphics_engine.h"
[7530]44#include "effects/atmospheric_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
[6358]65SHELL_COMMAND(speed, GameWorld, setSpeed);
[7339]66SHELL_COMMAND(playmode, GameWorld, setPlaymode);
[7004]67SHELL_COMMAND_STATIC(togglePNodeVisibility, GameWorld, GameWorld::togglePNodeVisibility);
68SHELL_COMMAND_STATIC(toggleBVVisibility, GameWorld, GameWorld::toggleBVVisibility);
[6352]69
70
71
[6989]72GameWorld::GameWorld()
[6402]73    : StoryEntity()
[6352]74{
[6368]75  this->setClassID(CL_GAME_WORLD, "GameWorld");
76  this->setName("Preloaded World - no name yet");
77
78  this->gameTime = 0.0f;
[6370]79  this->setSpeed(1.0f);
[6368]80  this->shell = NULL;
81
82  this->showPNodes = false;
83  this->showBV = false;
84
[6845]85  this->dataXML = NULL;
[6352]86}
87
88/**
[6358]89 *  remove the GameWorld from memory
[6352]90 *
91 *  delete everything explicitly, that isn't contained in the parenting tree!
92 *  things contained in the tree are deleted automaticaly
93 */
[6358]94GameWorld::~GameWorld ()
[6352]95{
[6408]96  PRINTF(4)("Deleted GameWorld\n");
[7283]97
[6352]98}
99
100
101
102/**
[6358]103 * loads the parameters of a GameWorld from an XML-element
[6352]104 * @param root the XML-element to load from
105 */
[6358]106void GameWorld::loadParams(const TiXmlElement* root)
[6352]107{
[6512]108  StoryEntity::loadParams(root);
[6352]109
[6376]110  PRINTF(4)("Loaded GameWorld specific stuff\n");
[6352]111}
112
[6368]113
[6352]114/**
115 * this is executed just before load
116 *
117 * since the load function sometimes needs data, that has been initialized
118 * before the load and after the proceeding storyentity has finished
119*/
[6370]120ErrorMessage GameWorld::init()
[6352]121{
122  /* init the world interface */
[7374]123  this->shell = new OrxShell::Shell();
[6352]124
[6498]125  State::setCurrentStoryEntity(dynamic_cast<StoryEntity*>(this));
[6407]126  this->dataTank->init();
[7369]127
128  /* initialize some engines and graphical elements */
129  AnimationPlayer::getInstance();
130  PhysicsEngine::getInstance();
131
[6352]132}
133
[7288]134
[6352]135/**
[6358]136 *  loads the GameWorld by initializing all resources, and set their default values.
[6352]137 */
[6372]138ErrorMessage GameWorld::loadData()
[6352]139{
[6370]140  this->displayLoadScreen();
141
[6407]142  PRINTF(0)("Loading the GameWorld\n");
143
[6992]144  PRINTF(3)("> Loading world: '%s'\n", getLoadFile());
[6352]145  TiXmlElement* element;
146  GameLoader* loader = GameLoader::getInstance();
147
[7221]148  if( getLoadFile().empty())
[6402]149  {
[6634]150    PRINTF(1)("GameWorld has no path specified for loading\n");
[6402]151    return (ErrorMessage){213,"Path not specified","GameWorld::load()"};
152  }
[6352]153
[7287]154  TiXmlDocument* XMLDoc = new TiXmlDocument( getLoadFile());
[6402]155  // load the xml world file for further loading
[7287]156  if( !XMLDoc->LoadFile())
[6352]157  {
[7287]158    PRINTF(1)("loading XML File: %s @ %s:l%d:c%d\n", XMLDoc->ErrorDesc(), this->getLoadFile().c_str(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
159    delete XMLDoc;
[6358]160    return (ErrorMessage){213,"XML File parsing error","GameWorld::load()"};
[6352]161  }
162  // check basic validity
[7287]163  TiXmlElement* root = XMLDoc->RootElement();
[6352]164  assert( root != NULL);
165  if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile"))
166  {
[6402]167    // report an error
168    PRINTF(1)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n");
[7287]169    delete XMLDoc;
[6402]170    return (ErrorMessage){213,"Path not a WorldDataFile","GameWorld::load()"};
[6352]171  }
[6402]172  /* the whole loading process for the GameWorld */
[6407]173  this->dataTank->loadData(root);
[6845]174  this->dataXML = (TiXmlElement*)root->Clone();
[6352]175
[7287]176  delete XMLDoc;
[6386]177  this->releaseLoadScreen();
[6352]178}
179
180
181/**
[6387]182 *  unload the data of this GameWorld
183 */
184ErrorMessage GameWorld::unloadData()
185{
[7287]186  PRINTF(3)("GameWorld::~GameWorld() - unloading the current GameWorld\n");
[7370]187  delete this->shell;
[6387]188
[6981]189  this->dataTank->unloadData();
190
[7369]191  this->shell = NULL;
192  delete AnimationPlayer::getInstance();
193  delete PhysicsEngine::getInstance();
194
[6988]195  State::setCurrentStoryEntity(NULL);
[6845]196  if (this->dataXML)
197    delete this->dataXML;
[6387]198}
199
200
201/**
[6358]202 *  starts the GameWorld
[6352]203 */
[6387]204bool GameWorld::start()
[6352]205{
[7283]206  this->bPaused = false;
207  this->bRunning = true;
[6387]208
209  this->run();
[6352]210}
211
[6402]212
[6352]213/**
214 *  stops the world.
[6402]215 */
[6387]216bool GameWorld::stop()
[6352]217{
[6358]218  PRINTF(3)("GameWorld::stop() - got stop signal\n");
[7283]219  this->bRunning = false;
[6352]220}
221
[6402]222
[6352]223/**
[6402]224 *  pauses the game
225 */
[6387]226bool GameWorld::pause()
[6352]227{
[7283]228  this->bPaused = true;
[6352]229}
230
[6402]231
[6352]232/**
233 *  ends the pause Phase
[6409]234 */
[6387]235bool GameWorld::resume()
[6352]236{
[7283]237  this->bPaused = false;
[6352]238}
239
240
241/**
[6402]242 *  main loop of the world: executing all world relevant function
243 *
244 * in this loop we synchronize (if networked), handle input events, give the heart-beat to
245 * all other member-entities of the world (tick to player, enemies etc.), checking for
246 * collisions drawing everything to the screen.
247 */
248void GameWorld::run()
[6352]249{
[6402]250  PRINTF(3)("GameWorld::mainLoop() - Entering main loop\n");
[6352]251
[7131]252  // initialize Timing
253  this->cycle = 0;
254  for (unsigned int i = 0; i < TICK_SMOOTH_VALUE; i++)
[7132]255    this->frameTimes[i] = 100;
[7131]256  this->dtS = 0.0f;
257  this->lastFrame = SDL_GetTicks ();
258
[7304]259  if (this->dataTank->music != NULL)
260    this->dataTank->music->play();
261
[7283]262  while( this->bRunning) /* @todo implement pause */
[6402]263  {
264    /* process intput */
265    this->handleInput ();
[7322]266    if( !this->bRunning)
267      break;
[6386]268
[6402]269    /* network synchronisation */
270    this->synchronize ();
271    /* process time */
272    this->tick ();
273    /* process collision */
274    this->collide ();
275    /* update the state */
276    this->update ();
277    /* draw everything */
278    this->display ();
[7306]279
[6402]280  }
281
[7330]282  PRINTF(0)("GameWorld::mainLoop() - Exiting the main loop\n");
[6352]283}
284
285
[7338]286void GameWorld::setPlaymode(Playable::Playmode playmode)
287{
288  if (this->dataTank->localPlayer &&
289      this->dataTank->localPlayer->getPlayable() &&
[7339]290      this->dataTank->localPlayer->getPlayable()->setPlaymode(playmode))
[7338]291  {
[7339]292    PRINTF(3)("Set Playmode to %d:%s\n", playmode, Playable::playmodeToString(playmode));
[7338]293  }
[7339]294  else
295  {
296    PRINTF(1)("Unable to set Playmode %d:%s\n", playmode, Playable::playmodeToString(playmode));
297  }
[7338]298}
299
[7339]300void GameWorld::setPlaymode(const std::string& playmode)
301{
302  this->setPlaymode(Playable::stringToPlaymode(playmode));
303}
[7338]304
[6352]305/**
306 *  synchronize local data with remote data
307*/
[6358]308void GameWorld::synchronize ()
[6402]309{}
[6352]310
311
312/**
313 *  run all input processing
314
315   the command node is the central input event dispatcher. the node uses the even-queue from
316   sdl and has its own event-passing-queue.
317*/
[6358]318void GameWorld::handleInput ()
[6352]319{
320  EventHandler::getInstance()->process();
321}
322
[6402]323
324/**
[7370]325 * @brief ticks a WorldEntity list
[6402]326 * @param entityList list of the WorldEntities
327 * @param dt time passed since last frame
328 */
[7370]329void GameWorld::tick(ObjectManager::EntityList entityList, float dt)
[6352]330{
[7370]331  ObjectManager::EntityList::iterator entity, next;
[6710]332  next = entityList.begin();
333  while (next != entityList.end())
334  {
335    entity = next++;
[6352]336    (*entity)->tick(dt);
[6710]337  }
[6352]338}
339
[7131]340
[6352]341/**
342 *  advance the timeline
[6402]343 *
344 * this calculates the time used to process one frame (with all input handling, drawing, etc)
345 * the time is mesured in ms and passed to all world-entities and other classes that need
346 * a heart-beat.
347 */
[6358]348void GameWorld::tick ()
[6352]349{
350  Uint32 currentFrame = SDL_GetTicks();
[6402]351
[7283]352  if( !this->bPaused)
[6402]353  {
[7131]354    // CALCULATE FRAMERATE
355    Uint32 frameTimesIndex;
356    Uint32 getTicks;
357    Uint32 i;
[6402]358
[7131]359    frameTimesIndex = this->cycle % TICK_SMOOTH_VALUE;
360    getTicks = SDL_GetTicks();
361    this->frameTimes[frameTimesIndex] = getTicks - this->lastFrame;
362    this->lastFrame = getTicks;
[7132]363    ++this->cycle;
[7131]364    this->dtS = 0;
[7132]365    for (i = 0; i < TICK_SMOOTH_VALUE; i++)
[7131]366      this->dtS += this->frameTimes[i];
[7132]367    this->dtS = this->dtS / TICK_SMOOTH_VALUE / 1000.0f * speed;
[6352]368
[7131]369    // TICK everything
[7370]370    for (i = 0; i < this->dataTank->tickLists.size(); ++i)
371      this->tick(this->dataTank->objectManager->getObjectList(this->dataTank->tickLists[i]), this->dtS);
[6352]372
[6402]373    /* update tick the rest */
[6407]374    this->dataTank->localCamera->tick(this->dtS);
[6402]375    AnimationPlayer::getInstance()->tick(this->dtS);
376    PhysicsEngine::getInstance()->tick(this->dtS);
[6352]377
[6402]378    GraphicsEngine::getInstance()->tick(this->dtS);
[7530]379    AtmosphericEngine::getInstance()->tick(this->dtS);
[7035]380
381    if( likely(this->dataTank->gameRule != NULL))
382      this->dataTank->gameRule->tick(this->dtS);
[6402]383  }
[6352]384  this->lastFrame = currentFrame;
385}
386
387
388/**
389 *  this function gives the world a consistant state
[6402]390 *
391 * after ticking (updating the world state) this will give a constistant
392 * state to the whole system.
393 */
[6358]394void GameWorld::update()
[6352]395{
396  GraphicsEngine::getInstance()->update(this->dtS);
397  PNode::getNullParent()->updateNode (this->dtS);
398  SoundEngine::getInstance()->update();
399}
400
401
[6402]402/**
403 * kicks the CDEngine to detect the collisions between the object groups in the world
404 */
[6358]405void GameWorld::collide()
[6352]406{
[6407]407  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
[7004]408      this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ));
[6407]409  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
[7004]410      this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ));
[7078]411  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
[7288]412      this->dataTank->objectManager->getObjectList(OM_GROUP_01));
[6433]413
[7083]414  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
415      this->dataTank->objectManager->getObjectList(OM_COMMON));
[6433]416  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
[6407]417      this->dataTank->objectManager->getObjectList(OM_COMMON));
[7083]418
[6352]419}
420
421/**
422 *  render the current frame
[6402]423 *
424 * clear all buffers and draw the world
425 */
[6358]426void GameWorld::display ()
[6352]427{
428  // clear buffer
429  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
430  // draw world
431  this->draw();
432  // flip buffers
433  GraphicsEngine::swapBuffers();
434}
435
436
437/**
[7370]438 * @brief draws all entities in the list drawList
439 * @param drawList the List of entities to draw.
[6352]440 */
[7370]441void GameWorld::drawEntityList(const ObjectManager::EntityList& drawList) const
442{
443  ObjectManager::EntityList::const_iterator entity;
444  for (entity = drawList.begin(); entity != drawList.end(); entity++)
445    if ((*entity)->isVisible())
446      (*entity)->draw();
447}
448
449/**
450 * @brief runs through all entities calling their draw() methods
451 */
[6358]452void GameWorld::draw ()
[6352]453{
454  GraphicsEngine* engine = GraphicsEngine::getInstance();
[6402]455
[7530]456  AtmosphericEngine::getInstance()->draw();
457
[6780]458  // set camera
459  this->dataTank->localCamera->apply ();
[7108]460  this->dataTank->localCamera->project ();
461  LightManager::getInstance()->draw();
[6416]462
463  /* draw all WorldEntiy groups */
[7370]464  for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
465   this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
[6416]466
[6780]467
[6402]468  if( unlikely( this->showBV))
469  {
470    CDEngine* engine = CDEngine::getInstance();
[7370]471    for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
472      engine->drawBV(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
[6402]473  }
[6416]474
[6402]475  if( unlikely(this->showPNodes))
476    PNode::getNullParent()->debugDraw(0);
477
[6780]478  engine->draw();
[7035]479
[7108]480
[7035]481  // draw the game ruls
482  if( likely(this->dataTank->gameRule != NULL))
483    this->dataTank->gameRule->draw();
[6352]484}
485
486/**
[6402]487 *  shows the loading screen
488 */
489void GameWorld::displayLoadScreen ()
[6387]490{
[6402]491  PRINTF(3)("GameWorld::displayLoadScreen - start\n");
[6407]492  this->dataTank->glmis = new GLMenuImageScreen();
493  this->dataTank->glmis->setMaximum(8);
[6402]494  PRINTF(3)("GameWorld::displayLoadScreen - end\n");
[6387]495}
496
497
[6402]498/**
499 *  removes the loadscreen, and changes over to the game
500 */
501void GameWorld::releaseLoadScreen ()
[6387]502{
[6402]503  PRINTF(3)("GameWorld::releaseLoadScreen - start\n");
[6407]504  this->dataTank->glmis->setValue(this->dataTank->glmis->getMaximum());
[6402]505  PRINTF(3)("GameWorld::releaseLoadScreen - end\n");
[6387]506}
507
[7004]508
509
510/**
511 * @brief toggles the PNode visibility in the world (drawn as boxes)
512 */
513void GameWorld::togglePNodeVisibility()
514{
515  if (State::getCurrentStoryEntity() != NULL &&
516      State::getCurrentStoryEntity()->isA(CL_GAME_WORLD))
517  {
518    dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showPNodes = !dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showPNodes;
519  }
520  else
521    PRINTF(2)("The Current Story entity is not a GameWorld\n");
522};
523
524
525/**
526 * @brief toggles the bounding volume (BV) visibility
527*/
528void GameWorld::toggleBVVisibility()
529{
530  if (State::getCurrentStoryEntity() != NULL &&
531      State::getCurrentStoryEntity()->isA(CL_GAME_WORLD))
532  {
533    dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showBV = !dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showBV;
534  }
535  else
536    PRINTF(2)("The Current Story entity is not a GameWorld\n");
537};
538
Note: See TracBrowser for help on using the repository browser.