Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/current_cd/src/story_entities/game_world.cc @ 7005

Last change on this file since 7005 was 7005, checked in by patrick, 18 years ago

cd: work flush

File size: 12.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
22#include "resource_manager.h"
23#include "state.h"
[6404]24#include "class_list.h"
25#include "substring.h"
[6352]26
[6404]27#include "game_loader.h"
28
[6352]29#include "p_node.h"
30#include "world_entity.h"
31#include "player.h"
32#include "camera.h"
33#include "environment.h"
34#include "terrain.h"
35#include "test_entity.h"
36#include "terrain.h"
[6404]37#include "md2Model.h"
[6434]38#include "world_entities/projectiles/projectile.h"
[6404]39#include "npcs/npc_test1.h"
40#include "playable.h"
41
[6352]42#include "light.h"
[6404]43
44#include "factory.h"
45#include "fast_factory.h"
[6352]46#include "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"
55#include "fields.h"
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
66
[6404]67using namespace std;
[6352]68
69
[6358]70SHELL_COMMAND(speed, GameWorld, setSpeed);
71SHELL_COMMAND(togglePNodeVisibility, GameWorld, togglePNodeVisibility);
72SHELL_COMMAND(toggleBVVisibility, GameWorld, toggleBVVisibility);
[6352]73
74
75
[6358]76GameWorld::GameWorld(const TiXmlElement* root)
[6402]77    : StoryEntity()
[6352]78{
[6368]79  this->setClassID(CL_GAME_WORLD, "GameWorld");
80  this->setName("Preloaded World - no name yet");
81
82  this->gameTime = 0.0f;
[6370]83  this->setSpeed(1.0f);
[6368]84  this->shell = NULL;
85
86  this->showPNodes = false;
87  this->showBV = false;
88
[6845]89  this->dataXML = NULL;
[6352]90  this->path = NULL;
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");
[6352]102}
103
104
105
106/**
[6358]107 * loads the parameters of a GameWorld from an XML-element
[6352]108 * @param root the XML-element to load from
109 */
[6358]110void GameWorld::loadParams(const TiXmlElement* root)
[6352]111{
[6512]112  StoryEntity::loadParams(root);
[6352]113
[6358]114  LoadParam(root, "path", this, GameWorld, setPath)
[6835]115      .describe("The Filename of this GameWorld (relative from the data-dir)");
[6376]116
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  this->cycle = 0;
130  /* init the world interface */
131  this->shell = new Shell();
132
[6498]133  State::setCurrentStoryEntity(dynamic_cast<StoryEntity*>(this));
[6407]134  this->dataTank->init();
[6352]135}
136
137
138/**
[6358]139 *  loads the GameWorld by initializing all resources, and set their default values.
[6352]140 */
[6372]141ErrorMessage GameWorld::loadData()
[6352]142{
[6370]143  this->displayLoadScreen();
144
[6407]145  PRINTF(0)("Loading the GameWorld\n");
146
[6352]147  PRINTF(3)("> Loading world: '%s'\n", getPath());
148  TiXmlElement* element;
149  GameLoader* loader = GameLoader::getInstance();
150
151  if( getPath() == NULL)
[6402]152  {
[6634]153    PRINTF(1)("GameWorld has no path specified for loading\n");
[6402]154    return (ErrorMessage){213,"Path not specified","GameWorld::load()"};
155  }
[6352]156
157  TiXmlDocument* XMLDoc = new TiXmlDocument( getPath());
[6402]158  // load the xml world file for further loading
[6352]159  if( !XMLDoc->LoadFile())
160  {
161    PRINTF(1)("loading XML File: %s @ %s:l%d:c%d\n", XMLDoc->ErrorDesc(), this->getPath(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
162    delete XMLDoc;
[6358]163    return (ErrorMessage){213,"XML File parsing error","GameWorld::load()"};
[6352]164  }
165  // check basic validity
166  TiXmlElement* root = XMLDoc->RootElement();
167  assert( root != NULL);
168  if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile"))
169  {
[6402]170    // report an error
171    PRINTF(1)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n");
172    delete XMLDoc;
173    return (ErrorMessage){213,"Path not a WorldDataFile","GameWorld::load()"};
[6352]174  }
[6402]175  /* the whole loading process for the GameWorld */
[6407]176  this->dataTank->loadData(root);
[6845]177  this->dataXML = (TiXmlElement*)root->Clone();
[6352]178
[6402]179  delete XMLDoc;
[6386]180  this->releaseLoadScreen();
[6352]181}
182
183
184/**
[6387]185 *  unload the data of this GameWorld
186 */
187ErrorMessage GameWorld::unloadData()
188{
189  delete this->shell;
190  PRINTF(3)("GameWorld::~GameWorld() - unloading the current GameWorld\n");
191
[6845]192  if (this->dataXML)
193    delete this->dataXML;
194
[6407]195  this->dataTank->unloadData();
[6387]196}
197
198
199/**
[6358]200 *  starts the GameWorld
[6352]201 */
[6387]202bool GameWorld::start()
[6352]203{
[6386]204  this->isPaused = false;
205  this->isRunning = true;
[6387]206
207  this->run();
[6352]208}
209
[6402]210
[6352]211/**
212 *  stops the world.
[6402]213 */
[6387]214bool GameWorld::stop()
[6352]215{
[6358]216  PRINTF(3)("GameWorld::stop() - got stop signal\n");
[6386]217  this->isRunning = false;
[6352]218}
219
[6402]220
[6352]221/**
[6402]222 *  pauses the game
223 */
[6387]224bool GameWorld::pause()
[6352]225{
226  this->isPaused = true;
227}
228
[6402]229
[6352]230/**
231 *  ends the pause Phase
[6409]232 */
[6387]233bool GameWorld::resume()
[6352]234{
235  this->isPaused = false;
236}
237
238
239/**
[6402]240 *  main loop of the world: executing all world relevant function
241 *
242 * in this loop we synchronize (if networked), handle input events, give the heart-beat to
243 * all other member-entities of the world (tick to player, enemies etc.), checking for
244 * collisions drawing everything to the screen.
245 */
246void GameWorld::run()
[6352]247{
[6634]248  /* start the music */
249  if(this->dataTank->music != NULL)
250    this->dataTank->music->playback();
251
[6402]252  this->lastFrame = SDL_GetTicks ();
253  PRINTF(3)("GameWorld::mainLoop() - Entering main loop\n");
[6352]254
[6402]255  while( this->isRunning) /* @todo implement pause */
256  {
257    ++this->cycle;
258    /* process intput */
259    this->handleInput ();
260    if( !this->isRunning)
261      break;
[6386]262
[6402]263    /* network synchronisation */
264    this->synchronize ();
265    /* process time */
266    this->tick ();
267    /* process collision */
268    this->collide ();
269    /* update the state */
270    this->update ();
271    /* draw everything */
272    this->display ();
273  }
274
275  PRINTF(3)("GameWorld::mainLoop() - Exiting the main loop\n");
[6352]276}
277
278
279/**
280 *  synchronize local data with remote data
281*/
[6358]282void GameWorld::synchronize ()
[6402]283{}
[6352]284
285
286/**
287 *  run all input processing
288
289   the command node is the central input event dispatcher. the node uses the even-queue from
290   sdl and has its own event-passing-queue.
291*/
[6358]292void GameWorld::handleInput ()
[6352]293{
294  EventHandler::getInstance()->process();
295}
296
[6402]297
298/**
299 *  ticks a WorldEntity list
300 * @param entityList list of the WorldEntities
301 * @param dt time passed since last frame
302 */
[6358]303void GameWorld::tick(std::list<WorldEntity*> entityList, float dt)
[6352]304{
[6710]305  std::list<WorldEntity*>::iterator entity, next;
306  next = entityList.begin();
307  while (next != entityList.end())
308  {
309    entity = next++;
[6352]310    (*entity)->tick(dt);
[6710]311  }
[6352]312}
313
314/**
315 *  advance the timeline
[6402]316 *
317 * this calculates the time used to process one frame (with all input handling, drawing, etc)
318 * the time is mesured in ms and passed to all world-entities and other classes that need
319 * a heart-beat.
320 */
[6358]321void GameWorld::tick ()
[6352]322{
323  Uint32 currentFrame = SDL_GetTicks();
[6402]324
[6386]325  if( !this->isPaused)
[6402]326  {
327    this->dt = currentFrame - this->lastFrame;
328
329    /* limit the the frame rate to 100 frames per second (fps) */
330    if( this->dt < 10)
[6352]331    {
[6402]332      /* the frame-rate is limited to 100 frames per second, all other things are for nothing. */
[6412]333      //PRINTF(0)("fps = 1000 - frame rate is adjusted\n");
[6402]334      SDL_Delay(10 - dt);
335      this->dt = 10;
336    }
[6352]337
[6402]338    this->dtS = (float)this->dt / 1000.0f * this->speed;
339    this->gameTime += this->dtS;
[6352]340
[6407]341    this->tick(this->dataTank->objectManager->getObjectList(OM_DEAD_TICK), this->dtS);
[6457]342    this->tick(this->dataTank->objectManager->getObjectList(OM_ENVIRON), this->dtS);
[6407]343    this->tick(this->dataTank->objectManager->getObjectList(OM_COMMON), this->dtS);
344    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00), this->dtS);
[6433]345    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ), this->dtS);
[6407]346    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_01), this->dtS);
347    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ), this->dtS);
[6352]348
[6402]349    /* update tick the rest */
[6407]350    this->dataTank->localCamera->tick(this->dtS);
[6402]351    AnimationPlayer::getInstance()->tick(this->dtS);
352    PhysicsEngine::getInstance()->tick(this->dtS);
[6352]353
[6402]354    GraphicsEngine::getInstance()->tick(this->dtS);
355  }
[6352]356  this->lastFrame = currentFrame;
357}
358
359
360/**
361 *  this function gives the world a consistant state
[6402]362 *
363 * after ticking (updating the world state) this will give a constistant
364 * state to the whole system.
365 */
[6358]366void GameWorld::update()
[6352]367{
368  GraphicsEngine::getInstance()->update(this->dtS);
369  PNode::getNullParent()->updateNode (this->dtS);
370  SoundEngine::getInstance()->update();
[6827]371
372  if (this->dataTank->music != NULL)
373    this->dataTank->music->update();
[6352]374}
375
376
[6402]377/**
378 * kicks the CDEngine to detect the collisions between the object groups in the world
379 */
[6358]380void GameWorld::collide()
[6352]381{
[6407]382  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
[7005]383    this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ));
[6407]384  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
[7005]385    this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ));
[6433]386  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
[7005]387    this->dataTank->objectManager->getObjectList(OM_COMMON));
[6352]388}
389
390/**
391 *  render the current frame
[6402]392 *
393 * clear all buffers and draw the world
394 */
[6358]395void GameWorld::display ()
[6352]396{
397  // clear buffer
398  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
399  // draw world
400  this->draw();
401  // flip buffers
402  GraphicsEngine::swapBuffers();
403}
404
405
406/**
407 *  runs through all entities calling their draw() methods
408 */
[6358]409void GameWorld::draw ()
[6352]410{
411  GraphicsEngine* engine = GraphicsEngine::getInstance();
[6402]412
[6780]413  // set camera
414  this->dataTank->localCamera->apply ();
[6416]415
416  /* draw all WorldEntiy groups */
417  engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON_NOTICK));
418  engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON));
419  engine->draw(State::getObjectManager()->getObjectList(OM_COMMON));
420  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00));
[6433]421  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00_PROJ));
[6416]422  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01));
423  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ));
424
[6780]425
[6402]426  if( unlikely( this->showBV))
427  {
428    CDEngine* engine = CDEngine::getInstance();
[7005]429    engine->drawBV(State::getObjectManager()->getObjectList(OM_ENVIRON_NOTICK));
[6402]430    engine->drawBV(State::getObjectManager()->getObjectList(OM_ENVIRON));
431    engine->drawBV(State::getObjectManager()->getObjectList(OM_COMMON));
432    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_00));
[7005]433    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_00_PROJ));
[6402]434    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_01));
435    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ));
436  }
[6416]437
[6402]438  if( unlikely(this->showPNodes))
439    PNode::getNullParent()->debugDraw(0);
440
[6780]441  engine->draw();
[6352]442}
443
444
445/**
[6366]446 *  sets the track path of this world
447 * @param name the name of the path
448 */
[6358]449void GameWorld::setPath( const char* name)
[6352]450{
451  if (this->path)
452    delete this->path;
453  if (ResourceManager::isFile(name))
454  {
455    this->path = new char[strlen(name)+1];
456    strcpy(this->path, name);
457  }
458  else
[6402]459  {
460    this->path = new char[strlen(ResourceManager::getInstance()->getDataDir()) + strlen(name) +1];
461    sprintf(this->path, "%s%s", ResourceManager::getInstance()->getDataDir(), name);
462  }
[6352]463}
464
[6387]465
[6402]466/**
467 *  shows the loading screen
468 */
469void GameWorld::displayLoadScreen ()
[6387]470{
[6402]471  PRINTF(3)("GameWorld::displayLoadScreen - start\n");
[6407]472  this->dataTank->glmis = new GLMenuImageScreen();
473  this->dataTank->glmis->setMaximum(8);
[6402]474  PRINTF(3)("GameWorld::displayLoadScreen - end\n");
[6387]475}
476
477
[6402]478/**
479 *  removes the loadscreen, and changes over to the game
480 */
481void GameWorld::releaseLoadScreen ()
[6387]482{
[6402]483  PRINTF(3)("GameWorld::releaseLoadScreen - start\n");
[6407]484  this->dataTank->glmis->setValue(this->dataTank->glmis->getMaximum());
[6402]485  PRINTF(3)("GameWorld::releaseLoadScreen - end\n");
[6387]486}
487
Note: See TracBrowser for help on using the repository browser.