Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7300 was 7297, checked in by bensch, 19 years ago

orxonox/trunk: now the sound gets correcty detached from the source, when a SoundSource releses its alSource

File size: 13.9 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  this->audioThread = NULL;
93}
94
95/**
96 *  remove the GameWorld from memory
97 *
98 *  delete everything explicitly, that isn't contained in the parenting tree!
99 *  things contained in the tree are deleted automaticaly
100 */
101GameWorld::~GameWorld ()
102{
103  PRINTF(4)("Deleted GameWorld\n");
104
105}
106
107
108
109/**
110 * loads the parameters of a GameWorld from an XML-element
111 * @param root the XML-element to load from
112 */
113void GameWorld::loadParams(const TiXmlElement* root)
114{
115  StoryEntity::loadParams(root);
116
117  PRINTF(4)("Loaded GameWorld specific stuff\n");
118}
119
120
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*/
127ErrorMessage GameWorld::init()
128{
129  /* init the world interface */
130  this->shell = new Shell();
131
132  State::setCurrentStoryEntity(dynamic_cast<StoryEntity*>(this));
133  this->dataTank->init();
134}
135
136int GameWorld::createAudioThread(void* gameWorld)
137{
138  GameWorld* gw = (GameWorld*)gameWorld;
139  printf("STARTIG AUDIO THREAD\n");
140  if(gw->dataTank && gw->dataTank->music != NULL)
141    gw->dataTank->music->playback();
142
143  while (gw->bRunning)
144  {
145    if(gw->dataTank && gw->dataTank->music != NULL)
146      gw->dataTank->music->update();
147    SDL_Delay(1);
148  }
149  printf("End the AudioThread\n");
150}
151
152/**
153 *  loads the GameWorld by initializing all resources, and set their default values.
154 */
155ErrorMessage GameWorld::loadData()
156{
157  this->displayLoadScreen();
158
159  PRINTF(0)("Loading the GameWorld\n");
160
161  PRINTF(3)("> Loading world: '%s'\n", getLoadFile());
162  TiXmlElement* element;
163  GameLoader* loader = GameLoader::getInstance();
164
165  if( getLoadFile().empty())
166  {
167    PRINTF(1)("GameWorld has no path specified for loading\n");
168    return (ErrorMessage){213,"Path not specified","GameWorld::load()"};
169  }
170
171  TiXmlDocument* XMLDoc = new TiXmlDocument( getLoadFile());
172  // load the xml world file for further loading
173  if( !XMLDoc->LoadFile())
174  {
175    PRINTF(1)("loading XML File: %s @ %s:l%d:c%d\n", XMLDoc->ErrorDesc(), this->getLoadFile().c_str(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
176    delete XMLDoc;
177    return (ErrorMessage){213,"XML File parsing error","GameWorld::load()"};
178  }
179  // check basic validity
180  TiXmlElement* root = XMLDoc->RootElement();
181  assert( root != NULL);
182  if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile"))
183  {
184    // report an error
185    PRINTF(1)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n");
186    delete XMLDoc;
187    return (ErrorMessage){213,"Path not a WorldDataFile","GameWorld::load()"};
188  }
189  /* the whole loading process for the GameWorld */
190  this->dataTank->loadData(root);
191  this->dataXML = (TiXmlElement*)root->Clone();
192
193  delete XMLDoc;
194  this->releaseLoadScreen();
195}
196
197
198/**
199 *  unload the data of this GameWorld
200 */
201ErrorMessage GameWorld::unloadData()
202{
203  delete this->shell;
204  PRINTF(3)("GameWorld::~GameWorld() - unloading the current GameWorld\n");
205
206  if (this->audioThread != NULL)
207  {
208    this->bRunning = false;
209    SDL_WaitThread(this->audioThread, NULL);
210    this->audioThread = NULL;
211  }
212
213  this->dataTank->unloadData();
214
215  State::setCurrentStoryEntity(NULL);
216  if (this->dataXML)
217    delete this->dataXML;
218}
219
220
221/**
222 *  starts the GameWorld
223 */
224bool GameWorld::start()
225{
226  this->bPaused = false;
227  this->bRunning = true;
228
229  if (this->audioThread == NULL)
230    this->audioThread = SDL_CreateThread(GameWorld::createAudioThread, (void*)this);
231
232  this->run();
233}
234
235
236/**
237 *  stops the world.
238 */
239bool GameWorld::stop()
240{
241  PRINTF(3)("GameWorld::stop() - got stop signal\n");
242  this->bRunning = false;
243
244  if (this->audioThread != NULL)
245  {
246    this->bRunning = false;
247    SDL_WaitThread(this->audioThread, NULL);
248    this->audioThread = NULL;
249  }
250
251  this->audioThread = NULL;
252}
253
254
255/**
256 *  pauses the game
257 */
258bool GameWorld::pause()
259{
260  this->bPaused = true;
261}
262
263
264/**
265 *  ends the pause Phase
266 */
267bool GameWorld::resume()
268{
269  this->bPaused = false;
270}
271
272
273/**
274 *  main loop of the world: executing all world relevant function
275 *
276 * in this loop we synchronize (if networked), handle input events, give the heart-beat to
277 * all other member-entities of the world (tick to player, enemies etc.), checking for
278 * collisions drawing everything to the screen.
279 */
280void GameWorld::run()
281{
282  PRINTF(3)("GameWorld::mainLoop() - Entering main loop\n");
283
284  // initialize Timing
285  this->cycle = 0;
286  for (unsigned int i = 0; i < TICK_SMOOTH_VALUE; i++)
287    this->frameTimes[i] = 100;
288  this->dtS = 0.0f;
289  this->lastFrame = SDL_GetTicks ();
290
291  while( this->bRunning) /* @todo implement pause */
292  {
293    /* process intput */
294    this->handleInput ();
295    if( !this->bRunning)
296      break;
297
298    /* network synchronisation */
299    this->synchronize ();
300    /* process time */
301    this->tick ();
302    /* process collision */
303    this->collide ();
304    /* update the state */
305    this->update ();
306    /* draw everything */
307    this->display ();
308  }
309
310  PRINTF(3)("GameWorld::mainLoop() - Exiting the main loop\n");
311}
312
313
314/**
315 *  synchronize local data with remote data
316*/
317void GameWorld::synchronize ()
318{}
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*/
327void GameWorld::handleInput ()
328{
329  EventHandler::getInstance()->process();
330}
331
332
333/**
334 *  ticks a WorldEntity list
335 * @param entityList list of the WorldEntities
336 * @param dt time passed since last frame
337 */
338void GameWorld::tick(std::list<WorldEntity*> entityList, float dt)
339{
340  std::list<WorldEntity*>::iterator entity, next;
341  next = entityList.begin();
342  while (next != entityList.end())
343  {
344    entity = next++;
345    (*entity)->tick(dt);
346  }
347}
348
349
350/**
351 *  advance the timeline
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 */
357void GameWorld::tick ()
358{
359  Uint32 currentFrame = SDL_GetTicks();
360
361  if( !this->bPaused)
362  {
363    // CALCULATE FRAMERATE
364    Uint32 frameTimesIndex;
365    Uint32 getTicks;
366    Uint32 i;
367
368    frameTimesIndex = this->cycle % TICK_SMOOTH_VALUE;
369    getTicks = SDL_GetTicks();
370    this->frameTimes[frameTimesIndex] = getTicks - this->lastFrame;
371    this->lastFrame = getTicks;
372    ++this->cycle;
373    this->dtS = 0;
374    for (i = 0; i < TICK_SMOOTH_VALUE; i++)
375      this->dtS += this->frameTimes[i];
376    this->dtS = this->dtS / TICK_SMOOTH_VALUE / 1000.0f * speed;
377
378    // TICK everything
379    this->tick(this->dataTank->objectManager->getObjectList(OM_DEAD_TICK), this->dtS);
380    this->tick(this->dataTank->objectManager->getObjectList(OM_ENVIRON), this->dtS);
381    this->tick(this->dataTank->objectManager->getObjectList(OM_COMMON), this->dtS);
382    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00), this->dtS);
383    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ), this->dtS);
384    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_01), this->dtS);
385    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ), this->dtS);
386
387    /* update tick the rest */
388    this->dataTank->localCamera->tick(this->dtS);
389    AnimationPlayer::getInstance()->tick(this->dtS);
390    PhysicsEngine::getInstance()->tick(this->dtS);
391
392    GraphicsEngine::getInstance()->tick(this->dtS);
393
394    if( likely(this->dataTank->gameRule != NULL))
395      this->dataTank->gameRule->tick(this->dtS);
396  }
397  this->lastFrame = currentFrame;
398}
399
400
401/**
402 *  this function gives the world a consistant state
403 *
404 * after ticking (updating the world state) this will give a constistant
405 * state to the whole system.
406 */
407void GameWorld::update()
408{
409  GraphicsEngine::getInstance()->update(this->dtS);
410  PNode::getNullParent()->updateNode (this->dtS);
411  SoundEngine::getInstance()->update();
412}
413
414
415/**
416 * kicks the CDEngine to detect the collisions between the object groups in the world
417 */
418void GameWorld::collide()
419{
420  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
421      this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ));
422  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
423      this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ));
424  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
425      this->dataTank->objectManager->getObjectList(OM_GROUP_01));
426
427  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
428      this->dataTank->objectManager->getObjectList(OM_COMMON));
429  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
430      this->dataTank->objectManager->getObjectList(OM_COMMON));
431
432}
433
434/**
435 *  render the current frame
436 *
437 * clear all buffers and draw the world
438 */
439void GameWorld::display ()
440{
441  // clear buffer
442  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
443  // draw world
444  this->draw();
445  // flip buffers
446  GraphicsEngine::swapBuffers();
447}
448
449
450/**
451 *  runs through all entities calling their draw() methods
452 */
453void GameWorld::draw ()
454{
455  GraphicsEngine* engine = GraphicsEngine::getInstance();
456
457  // set camera
458  this->dataTank->localCamera->apply ();
459  this->dataTank->localCamera->project ();
460  LightManager::getInstance()->draw();
461
462  /* draw all WorldEntiy groups */
463  engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON_NOTICK));
464  engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON));
465  engine->draw(State::getObjectManager()->getObjectList(OM_COMMON));
466  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00));
467  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00_PROJ));
468  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01));
469  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ));
470
471
472  if( unlikely( this->showBV))
473  {
474    CDEngine* engine = CDEngine::getInstance();
475    engine->drawBV(State::getObjectManager()->getObjectList(OM_ENVIRON_NOTICK));
476    engine->drawBV(State::getObjectManager()->getObjectList(OM_ENVIRON));
477    engine->drawBV(State::getObjectManager()->getObjectList(OM_COMMON));
478    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_00));
479    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_01));
480    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ));
481  }
482
483  if( unlikely(this->showPNodes))
484    PNode::getNullParent()->debugDraw(0);
485
486  engine->draw();
487
488
489  // draw the game ruls
490  if( likely(this->dataTank->gameRule != NULL))
491    this->dataTank->gameRule->draw();
492}
493
494/**
495 *  shows the loading screen
496 */
497void GameWorld::displayLoadScreen ()
498{
499  PRINTF(3)("GameWorld::displayLoadScreen - start\n");
500  this->dataTank->glmis = new GLMenuImageScreen();
501  this->dataTank->glmis->setMaximum(8);
502  PRINTF(3)("GameWorld::displayLoadScreen - end\n");
503}
504
505
506/**
507 *  removes the loadscreen, and changes over to the game
508 */
509void GameWorld::releaseLoadScreen ()
510{
511  PRINTF(3)("GameWorld::releaseLoadScreen - start\n");
512  this->dataTank->glmis->setValue(this->dataTank->glmis->getMaximum());
513  PRINTF(3)("GameWorld::releaseLoadScreen - end\n");
514}
515
516
517
518/**
519 * @brief toggles the PNode visibility in the world (drawn as boxes)
520 */
521void GameWorld::togglePNodeVisibility()
522{
523  if (State::getCurrentStoryEntity() != NULL &&
524      State::getCurrentStoryEntity()->isA(CL_GAME_WORLD))
525  {
526    dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showPNodes = !dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showPNodes;
527  }
528  else
529    PRINTF(2)("The Current Story entity is not a GameWorld\n");
530};
531
532
533/**
534 * @brief toggles the bounding volume (BV) visibility
535*/
536void GameWorld::toggleBVVisibility()
537{
538  if (State::getCurrentStoryEntity() != NULL &&
539      State::getCurrentStoryEntity()->isA(CL_GAME_WORLD))
540  {
541    dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showBV = !dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showBV;
542  }
543  else
544    PRINTF(2)("The Current Story entity is not a GameWorld\n");
545};
546
Note: See TracBrowser for help on using the repository browser.