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
Line 
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
19#include "game_world.h"
20#include "game_world_data.h"
21
22#include "util/loading/resource_manager.h"
23#include "state.h"
24#include "class_list.h"
25
26#include "util/loading/game_loader.h"
27
28#include "player.h"
29#include "camera.h"
30#include "environment.h"
31#include "terrain.h"
32#include "test_entity.h"
33#include "terrain.h"
34#include "playable.h"
35
36#include "light.h"
37
38#include "util/loading/factory.h"
39#include "util/loading/load_param.h"
40#include "fast_factory.h"
41#include "shell_command.h"
42
43#include "graphics_engine.h"
44#include "effects/atmospheric_engine.h"
45#include "event_handler.h"
46#include "sound_engine.h"
47#include "cd_engine.h"
48#include "network_manager.h"
49#include "physics_engine.h"
50#include "fields.h"
51
52#include "glmenu_imagescreen.h"
53#include "shell.h"
54
55#include "ogg_player.h"
56#include "shader.h"
57
58#include "animation_player.h"
59
60#include "game_rules.h"
61
62using namespace std;
63
64
65SHELL_COMMAND(speed, GameWorld, setSpeed);
66SHELL_COMMAND(playmode, GameWorld, setPlaymode);
67SHELL_COMMAND_STATIC(togglePNodeVisibility, GameWorld, GameWorld::togglePNodeVisibility);
68SHELL_COMMAND_STATIC(toggleBVVisibility, GameWorld, GameWorld::toggleBVVisibility);
69
70
71
72GameWorld::GameWorld()
73    : StoryEntity()
74{
75  this->setClassID(CL_GAME_WORLD, "GameWorld");
76  this->setName("Preloaded World - no name yet");
77
78  this->gameTime = 0.0f;
79  this->setSpeed(1.0f);
80  this->shell = NULL;
81
82  this->showPNodes = false;
83  this->showBV = false;
84
85  this->dataXML = NULL;
86}
87
88/**
89 *  remove the GameWorld from memory
90 *
91 *  delete everything explicitly, that isn't contained in the parenting tree!
92 *  things contained in the tree are deleted automaticaly
93 */
94GameWorld::~GameWorld ()
95{
96  PRINTF(4)("Deleted GameWorld\n");
97
98}
99
100
101
102/**
103 * loads the parameters of a GameWorld from an XML-element
104 * @param root the XML-element to load from
105 */
106void GameWorld::loadParams(const TiXmlElement* root)
107{
108  StoryEntity::loadParams(root);
109
110  PRINTF(4)("Loaded GameWorld specific stuff\n");
111}
112
113
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*/
120ErrorMessage GameWorld::init()
121{
122  /* init the world interface */
123  this->shell = new OrxShell::Shell();
124
125  State::setCurrentStoryEntity(dynamic_cast<StoryEntity*>(this));
126  this->dataTank->init();
127
128  /* initialize some engines and graphical elements */
129  AnimationPlayer::getInstance();
130  PhysicsEngine::getInstance();
131
132}
133
134
135/**
136 *  loads the GameWorld by initializing all resources, and set their default values.
137 */
138ErrorMessage GameWorld::loadData()
139{
140  this->displayLoadScreen();
141
142  PRINTF(0)("Loading the GameWorld\n");
143
144  PRINTF(3)("> Loading world: '%s'\n", getLoadFile());
145  TiXmlElement* element;
146  GameLoader* loader = GameLoader::getInstance();
147
148  if( getLoadFile().empty())
149  {
150    PRINTF(1)("GameWorld has no path specified for loading\n");
151    return (ErrorMessage){213,"Path not specified","GameWorld::load()"};
152  }
153
154  TiXmlDocument* XMLDoc = new TiXmlDocument( getLoadFile());
155  // load the xml world file for further loading
156  if( !XMLDoc->LoadFile())
157  {
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;
160    return (ErrorMessage){213,"XML File parsing error","GameWorld::load()"};
161  }
162  // check basic validity
163  TiXmlElement* root = XMLDoc->RootElement();
164  assert( root != NULL);
165  if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile"))
166  {
167    // report an error
168    PRINTF(1)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n");
169    delete XMLDoc;
170    return (ErrorMessage){213,"Path not a WorldDataFile","GameWorld::load()"};
171  }
172  /* the whole loading process for the GameWorld */
173  this->dataTank->loadData(root);
174  this->dataXML = (TiXmlElement*)root->Clone();
175
176  delete XMLDoc;
177  this->releaseLoadScreen();
178}
179
180
181/**
182 *  unload the data of this GameWorld
183 */
184ErrorMessage GameWorld::unloadData()
185{
186  PRINTF(3)("GameWorld::~GameWorld() - unloading the current GameWorld\n");
187  delete this->shell;
188
189  this->dataTank->unloadData();
190
191  this->shell = NULL;
192  delete AnimationPlayer::getInstance();
193  delete PhysicsEngine::getInstance();
194
195  State::setCurrentStoryEntity(NULL);
196  if (this->dataXML)
197    delete this->dataXML;
198}
199
200
201/**
202 *  starts the GameWorld
203 */
204bool GameWorld::start()
205{
206  this->bPaused = false;
207  this->bRunning = true;
208
209  this->run();
210}
211
212
213/**
214 *  stops the world.
215 */
216bool GameWorld::stop()
217{
218  PRINTF(3)("GameWorld::stop() - got stop signal\n");
219  this->bRunning = false;
220}
221
222
223/**
224 *  pauses the game
225 */
226bool GameWorld::pause()
227{
228  this->bPaused = true;
229}
230
231
232/**
233 *  ends the pause Phase
234 */
235bool GameWorld::resume()
236{
237  this->bPaused = false;
238}
239
240
241/**
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()
249{
250  PRINTF(3)("GameWorld::mainLoop() - Entering main loop\n");
251
252  // initialize Timing
253  this->cycle = 0;
254  for (unsigned int i = 0; i < TICK_SMOOTH_VALUE; i++)
255    this->frameTimes[i] = 100;
256  this->dtS = 0.0f;
257  this->lastFrame = SDL_GetTicks ();
258
259  if (this->dataTank->music != NULL)
260    this->dataTank->music->play();
261
262  while( this->bRunning) /* @todo implement pause */
263  {
264    /* process intput */
265    this->handleInput ();
266    if( !this->bRunning)
267      break;
268
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 ();
279
280  }
281
282  PRINTF(0)("GameWorld::mainLoop() - Exiting the main loop\n");
283}
284
285
286void GameWorld::setPlaymode(Playable::Playmode playmode)
287{
288  if (this->dataTank->localPlayer &&
289      this->dataTank->localPlayer->getPlayable() &&
290      this->dataTank->localPlayer->getPlayable()->setPlaymode(playmode))
291  {
292    PRINTF(3)("Set Playmode to %d:%s\n", playmode, Playable::playmodeToString(playmode));
293  }
294  else
295  {
296    PRINTF(1)("Unable to set Playmode %d:%s\n", playmode, Playable::playmodeToString(playmode));
297  }
298}
299
300void GameWorld::setPlaymode(const std::string& playmode)
301{
302  this->setPlaymode(Playable::stringToPlaymode(playmode));
303}
304
305/**
306 *  synchronize local data with remote data
307*/
308void GameWorld::synchronize ()
309{}
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*/
318void GameWorld::handleInput ()
319{
320  EventHandler::getInstance()->process();
321}
322
323
324/**
325 * @brief ticks a WorldEntity list
326 * @param entityList list of the WorldEntities
327 * @param dt time passed since last frame
328 */
329void GameWorld::tick(ObjectManager::EntityList entityList, float dt)
330{
331  ObjectManager::EntityList::iterator entity, next;
332  next = entityList.begin();
333  while (next != entityList.end())
334  {
335    entity = next++;
336    (*entity)->tick(dt);
337  }
338}
339
340
341/**
342 *  advance the timeline
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 */
348void GameWorld::tick ()
349{
350  Uint32 currentFrame = SDL_GetTicks();
351
352  if( !this->bPaused)
353  {
354    // CALCULATE FRAMERATE
355    Uint32 frameTimesIndex;
356    Uint32 getTicks;
357    Uint32 i;
358
359    frameTimesIndex = this->cycle % TICK_SMOOTH_VALUE;
360    getTicks = SDL_GetTicks();
361    this->frameTimes[frameTimesIndex] = getTicks - this->lastFrame;
362    this->lastFrame = getTicks;
363    ++this->cycle;
364    this->dtS = 0;
365    for (i = 0; i < TICK_SMOOTH_VALUE; i++)
366      this->dtS += this->frameTimes[i];
367    this->dtS = this->dtS / TICK_SMOOTH_VALUE / 1000.0f * speed;
368
369    // TICK everything
370    for (i = 0; i < this->dataTank->tickLists.size(); ++i)
371      this->tick(this->dataTank->objectManager->getObjectList(this->dataTank->tickLists[i]), this->dtS);
372
373    /* update tick the rest */
374    this->dataTank->localCamera->tick(this->dtS);
375    AnimationPlayer::getInstance()->tick(this->dtS);
376    PhysicsEngine::getInstance()->tick(this->dtS);
377
378    GraphicsEngine::getInstance()->tick(this->dtS);
379    AtmosphericEngine::getInstance()->tick(this->dtS);
380
381    if( likely(this->dataTank->gameRule != NULL))
382      this->dataTank->gameRule->tick(this->dtS);
383  }
384  this->lastFrame = currentFrame;
385}
386
387
388/**
389 *  this function gives the world a consistant state
390 *
391 * after ticking (updating the world state) this will give a constistant
392 * state to the whole system.
393 */
394void GameWorld::update()
395{
396  GraphicsEngine::getInstance()->update(this->dtS);
397  PNode::getNullParent()->updateNode (this->dtS);
398  SoundEngine::getInstance()->update();
399}
400
401
402/**
403 * kicks the CDEngine to detect the collisions between the object groups in the world
404 */
405void GameWorld::collide()
406{
407  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
408      this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ));
409  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
410      this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ));
411  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
412      this->dataTank->objectManager->getObjectList(OM_GROUP_01));
413
414  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
415      this->dataTank->objectManager->getObjectList(OM_COMMON));
416  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
417      this->dataTank->objectManager->getObjectList(OM_COMMON));
418
419}
420
421/**
422 *  render the current frame
423 *
424 * clear all buffers and draw the world
425 */
426void GameWorld::display ()
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/**
438 * @brief draws all entities in the list drawList
439 * @param drawList the List of entities to draw.
440 */
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 */
452void GameWorld::draw ()
453{
454  GraphicsEngine* engine = GraphicsEngine::getInstance();
455
456  AtmosphericEngine::getInstance()->draw();
457
458  // set camera
459  this->dataTank->localCamera->apply ();
460  this->dataTank->localCamera->project ();
461  LightManager::getInstance()->draw();
462
463  /* draw all WorldEntiy groups */
464  for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
465   this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
466
467
468  if( unlikely( this->showBV))
469  {
470    CDEngine* engine = CDEngine::getInstance();
471    for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
472      engine->drawBV(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
473  }
474
475  if( unlikely(this->showPNodes))
476    PNode::getNullParent()->debugDraw(0);
477
478  engine->draw();
479
480
481  // draw the game ruls
482  if( likely(this->dataTank->gameRule != NULL))
483    this->dataTank->gameRule->draw();
484}
485
486/**
487 *  shows the loading screen
488 */
489void GameWorld::displayLoadScreen ()
490{
491  PRINTF(3)("GameWorld::displayLoadScreen - start\n");
492  this->dataTank->glmis = new GLMenuImageScreen();
493  this->dataTank->glmis->setMaximum(8);
494  PRINTF(3)("GameWorld::displayLoadScreen - end\n");
495}
496
497
498/**
499 *  removes the loadscreen, and changes over to the game
500 */
501void GameWorld::releaseLoadScreen ()
502{
503  PRINTF(3)("GameWorld::releaseLoadScreen - start\n");
504  this->dataTank->glmis->setValue(this->dataTank->glmis->getMaximum());
505  PRINTF(3)("GameWorld::releaseLoadScreen - end\n");
506}
507
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.