Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: added a mutex, that should prevent the SoundEngine from failing, when jumping around in the file

File size: 13.7 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#include "substring.h"
26
27#include "util/loading/game_loader.h"
28
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"
37#include "md2Model.h"
38#include "world_entities/projectiles/projectile.h"
39#include "npcs/npc_test1.h"
40#include "playable.h"
41
42#include "light.h"
43
44#include "util/loading/factory.h"
45#include "fast_factory.h"
46#include "util/loading/load_param.h"
47#include "shell_command.h"
48
49#include "graphics_engine.h"
50#include "event_handler.h"
51#include "sound_engine.h"
52#include "cd_engine.h"
53#include "network_manager.h"
54#include "physics_engine.h"
55#include "fields.h"
56
57#include "glmenu_imagescreen.h"
58#include "shell.h"
59
60#include "animation_player.h"
61#include "animation3d.h"
62
63#include "ogg_player.h"
64#include "shader.h"
65
66#include "game_rules.h"
67
68using namespace std;
69
70
71SHELL_COMMAND(speed, GameWorld, setSpeed);
72SHELL_COMMAND_STATIC(togglePNodeVisibility, GameWorld, GameWorld::togglePNodeVisibility);
73SHELL_COMMAND_STATIC(toggleBVVisibility, GameWorld, GameWorld::toggleBVVisibility);
74
75
76
77GameWorld::GameWorld()
78    : StoryEntity()
79{
80  this->setClassID(CL_GAME_WORLD, "GameWorld");
81  this->setName("Preloaded World - no name yet");
82
83  this->gameTime = 0.0f;
84  this->setSpeed(1.0f);
85  this->shell = NULL;
86
87  this->showPNodes = false;
88  this->showBV = false;
89
90  this->dataXML = NULL;
91}
92
93/**
94 *  remove the GameWorld from memory
95 *
96 *  delete everything explicitly, that isn't contained in the parenting tree!
97 *  things contained in the tree are deleted automaticaly
98 */
99GameWorld::~GameWorld ()
100{
101  PRINTF(4)("Deleted GameWorld\n");
102
103}
104
105
106
107/**
108 * loads the parameters of a GameWorld from an XML-element
109 * @param root the XML-element to load from
110 */
111void GameWorld::loadParams(const TiXmlElement* root)
112{
113  StoryEntity::loadParams(root);
114
115  PRINTF(4)("Loaded GameWorld specific stuff\n");
116}
117
118
119/**
120 * this is executed just before load
121 *
122 * since the load function sometimes needs data, that has been initialized
123 * before the load and after the proceeding storyentity has finished
124*/
125ErrorMessage GameWorld::init()
126{
127  /* init the world interface */
128  this->shell = new Shell();
129
130  State::setCurrentStoryEntity(dynamic_cast<StoryEntity*>(this));
131  this->dataTank->init();
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  delete this->shell;
187  PRINTF(3)("GameWorld::~GameWorld() - unloading the current GameWorld\n");
188
189  this->dataTank->unloadData();
190
191  State::setCurrentStoryEntity(NULL);
192  if (this->dataXML)
193    delete this->dataXML;
194}
195
196
197/**
198 *  starts the GameWorld
199 */
200bool GameWorld::start()
201{
202  this->bPaused = false;
203  this->bRunning = true;
204
205  this->run();
206}
207
208
209/**
210 *  stops the world.
211 */
212bool GameWorld::stop()
213{
214  PRINTF(3)("GameWorld::stop() - got stop signal\n");
215  this->bRunning = false;
216}
217
218
219/**
220 *  pauses the game
221 */
222bool GameWorld::pause()
223{
224  this->bPaused = true;
225}
226
227
228/**
229 *  ends the pause Phase
230 */
231bool GameWorld::resume()
232{
233  this->bPaused = false;
234}
235
236
237/**
238 *  main loop of the world: executing all world relevant function
239 *
240 * in this loop we synchronize (if networked), handle input events, give the heart-beat to
241 * all other member-entities of the world (tick to player, enemies etc.), checking for
242 * collisions drawing everything to the screen.
243 */
244void GameWorld::run()
245{
246  PRINTF(3)("GameWorld::mainLoop() - Entering main loop\n");
247
248  // initialize Timing
249  this->cycle = 0;
250  for (unsigned int i = 0; i < TICK_SMOOTH_VALUE; i++)
251    this->frameTimes[i] = 100;
252  this->dtS = 0.0f;
253  this->lastFrame = SDL_GetTicks ();
254
255  if (this->dataTank->music != NULL)
256    this->dataTank->music->play();
257
258  while( this->bRunning) /* @todo implement pause */
259  {
260    /* process intput */
261    this->handleInput ();
262    if( !this->bRunning)
263      break;
264
265    /* network synchronisation */
266    this->synchronize ();
267    /* process time */
268    this->tick ();
269    /* process collision */
270    this->collide ();
271    /* update the state */
272    this->update ();
273    /* draw everything */
274    this->display ();
275
276    /// TODO REMOVE THIS CRAP
277    if (this->dataTank->music)
278    {
279/*      if (this->cycle%100 == 0)
280      {
281        printf("Cycle %d  ", this->cycle);
282        this->dataTank->music->printState();
283      }
284      if (this->cycle %10)
285        this->dataTank->music->jumpTo((float)this->cycle*(float)rand()/(float)RAND_MAX);
286      if (this->cycle % 4 == 0)
287        this->dataTank->music->pause();
288      if (this->cycle % 5 == 0)
289        this->dataTank->music->play();
290      if (this->cycle % 11 == 0)
291      this->dataTank->music->stop(); */
292    }
293  }
294
295  PRINTF(3)("GameWorld::mainLoop() - Exiting the main loop\n");
296}
297
298
299/**
300 *  synchronize local data with remote data
301*/
302void GameWorld::synchronize ()
303{}
304
305
306/**
307 *  run all input processing
308
309   the command node is the central input event dispatcher. the node uses the even-queue from
310   sdl and has its own event-passing-queue.
311*/
312void GameWorld::handleInput ()
313{
314  EventHandler::getInstance()->process();
315}
316
317
318/**
319 *  ticks a WorldEntity list
320 * @param entityList list of the WorldEntities
321 * @param dt time passed since last frame
322 */
323void GameWorld::tick(std::list<WorldEntity*> entityList, float dt)
324{
325  std::list<WorldEntity*>::iterator entity, next;
326  next = entityList.begin();
327  while (next != entityList.end())
328  {
329    entity = next++;
330    (*entity)->tick(dt);
331  }
332}
333
334
335/**
336 *  advance the timeline
337 *
338 * this calculates the time used to process one frame (with all input handling, drawing, etc)
339 * the time is mesured in ms and passed to all world-entities and other classes that need
340 * a heart-beat.
341 */
342void GameWorld::tick ()
343{
344  Uint32 currentFrame = SDL_GetTicks();
345
346  if( !this->bPaused)
347  {
348    // CALCULATE FRAMERATE
349    Uint32 frameTimesIndex;
350    Uint32 getTicks;
351    Uint32 i;
352
353    frameTimesIndex = this->cycle % TICK_SMOOTH_VALUE;
354    getTicks = SDL_GetTicks();
355    this->frameTimes[frameTimesIndex] = getTicks - this->lastFrame;
356    this->lastFrame = getTicks;
357    ++this->cycle;
358    this->dtS = 0;
359    for (i = 0; i < TICK_SMOOTH_VALUE; i++)
360      this->dtS += this->frameTimes[i];
361    this->dtS = this->dtS / TICK_SMOOTH_VALUE / 1000.0f * speed;
362
363    // TICK everything
364    this->tick(this->dataTank->objectManager->getObjectList(OM_DEAD_TICK), this->dtS);
365    this->tick(this->dataTank->objectManager->getObjectList(OM_ENVIRON), this->dtS);
366    this->tick(this->dataTank->objectManager->getObjectList(OM_COMMON), this->dtS);
367    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00), this->dtS);
368    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ), this->dtS);
369    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_01), this->dtS);
370    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ), this->dtS);
371
372    /* update tick the rest */
373    this->dataTank->localCamera->tick(this->dtS);
374    AnimationPlayer::getInstance()->tick(this->dtS);
375    PhysicsEngine::getInstance()->tick(this->dtS);
376
377    GraphicsEngine::getInstance()->tick(this->dtS);
378
379    if( likely(this->dataTank->gameRule != NULL))
380      this->dataTank->gameRule->tick(this->dtS);
381  }
382  this->lastFrame = currentFrame;
383}
384
385
386/**
387 *  this function gives the world a consistant state
388 *
389 * after ticking (updating the world state) this will give a constistant
390 * state to the whole system.
391 */
392void GameWorld::update()
393{
394  GraphicsEngine::getInstance()->update(this->dtS);
395  PNode::getNullParent()->updateNode (this->dtS);
396  SoundEngine::getInstance()->update();
397}
398
399
400/**
401 * kicks the CDEngine to detect the collisions between the object groups in the world
402 */
403void GameWorld::collide()
404{
405  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
406      this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ));
407  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
408      this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ));
409  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
410      this->dataTank->objectManager->getObjectList(OM_GROUP_01));
411
412  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
413      this->dataTank->objectManager->getObjectList(OM_COMMON));
414  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
415      this->dataTank->objectManager->getObjectList(OM_COMMON));
416
417}
418
419/**
420 *  render the current frame
421 *
422 * clear all buffers and draw the world
423 */
424void GameWorld::display ()
425{
426  // clear buffer
427  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
428  // draw world
429  this->draw();
430  // flip buffers
431  GraphicsEngine::swapBuffers();
432}
433
434
435/**
436 *  runs through all entities calling their draw() methods
437 */
438void GameWorld::draw ()
439{
440  GraphicsEngine* engine = GraphicsEngine::getInstance();
441
442  // set camera
443  this->dataTank->localCamera->apply ();
444  this->dataTank->localCamera->project ();
445  LightManager::getInstance()->draw();
446
447  /* draw all WorldEntiy groups */
448  engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON_NOTICK));
449  engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON));
450  engine->draw(State::getObjectManager()->getObjectList(OM_COMMON));
451  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00));
452  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00_PROJ));
453  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01));
454  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ));
455
456
457  if( unlikely( this->showBV))
458  {
459    CDEngine* engine = CDEngine::getInstance();
460    engine->drawBV(State::getObjectManager()->getObjectList(OM_ENVIRON_NOTICK));
461    engine->drawBV(State::getObjectManager()->getObjectList(OM_ENVIRON));
462    engine->drawBV(State::getObjectManager()->getObjectList(OM_COMMON));
463    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_00));
464    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_01));
465    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ));
466  }
467
468  if( unlikely(this->showPNodes))
469    PNode::getNullParent()->debugDraw(0);
470
471  engine->draw();
472
473
474  // draw the game ruls
475  if( likely(this->dataTank->gameRule != NULL))
476    this->dataTank->gameRule->draw();
477}
478
479/**
480 *  shows the loading screen
481 */
482void GameWorld::displayLoadScreen ()
483{
484  PRINTF(3)("GameWorld::displayLoadScreen - start\n");
485  this->dataTank->glmis = new GLMenuImageScreen();
486  this->dataTank->glmis->setMaximum(8);
487  PRINTF(3)("GameWorld::displayLoadScreen - end\n");
488}
489
490
491/**
492 *  removes the loadscreen, and changes over to the game
493 */
494void GameWorld::releaseLoadScreen ()
495{
496  PRINTF(3)("GameWorld::releaseLoadScreen - start\n");
497  this->dataTank->glmis->setValue(this->dataTank->glmis->getMaximum());
498  PRINTF(3)("GameWorld::releaseLoadScreen - end\n");
499}
500
501
502
503/**
504 * @brief toggles the PNode visibility in the world (drawn as boxes)
505 */
506void GameWorld::togglePNodeVisibility()
507{
508  if (State::getCurrentStoryEntity() != NULL &&
509      State::getCurrentStoryEntity()->isA(CL_GAME_WORLD))
510  {
511    dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showPNodes = !dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showPNodes;
512  }
513  else
514    PRINTF(2)("The Current Story entity is not a GameWorld\n");
515};
516
517
518/**
519 * @brief toggles the bounding volume (BV) visibility
520*/
521void GameWorld::toggleBVVisibility()
522{
523  if (State::getCurrentStoryEntity() != NULL &&
524      State::getCurrentStoryEntity()->isA(CL_GAME_WORLD))
525  {
526    dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showBV = !dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showBV;
527  }
528  else
529    PRINTF(2)("The Current Story entity is not a GameWorld\n");
530};
531
Note: See TracBrowser for help on using the repository browser.