Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged back with:
svn merge . . -rHEAD:PREV

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