Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/gui/src/story_entities/game_world.cc @ 7900

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

gui: implemented the timer right here. now we calc in microseconds

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