Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: thread for audio, and 'is' to 'b'

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  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  if (this->audioThread != NULL)
106    SDL_KillThread(this->audioThread);
107}
108
109
110
111/**
112 * loads the parameters of a GameWorld from an XML-element
113 * @param root the XML-element to load from
114 */
115void GameWorld::loadParams(const TiXmlElement* root)
116{
117  StoryEntity::loadParams(root);
118
119  PRINTF(4)("Loaded GameWorld specific stuff\n");
120}
121
122
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*/
129ErrorMessage GameWorld::init()
130{
131  /* init the world interface */
132  this->shell = new Shell();
133
134  State::setCurrentStoryEntity(dynamic_cast<StoryEntity*>(this));
135  this->dataTank->init();
136}
137
138int GameWorld::createAudioThread(void* gameWorld)
139{
140  GameWorld* gw = (GameWorld*)gameWorld;
141  printf("STARTIG AUDIO THREAD\n");
142  while (gw->bRunning)
143  {
144    if(gw->dataTank->music != NULL)
145      gw->dataTank->music->playback();
146    SDL_Delay(5);
147  }
148  printf("End the AudioThread\n");
149}
150
151/**
152 *  loads the GameWorld by initializing all resources, and set their default values.
153 */
154ErrorMessage GameWorld::loadData()
155{
156  this->displayLoadScreen();
157
158  PRINTF(0)("Loading the GameWorld\n");
159
160  PRINTF(3)("> Loading world: '%s'\n", getLoadFile());
161  TiXmlElement* element;
162  GameLoader* loader = GameLoader::getInstance();
163
164  if( getLoadFile().empty())
165  {
166    PRINTF(1)("GameWorld has no path specified for loading\n");
167    return (ErrorMessage){213,"Path not specified","GameWorld::load()"};
168  }
169
170  TiXmlDocument* XMLDoc = new TiXmlDocument( getLoadFile());
171  // load the xml world file for further loading
172  if( !XMLDoc->LoadFile())
173  {
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;
176    return (ErrorMessage){213,"XML File parsing error","GameWorld::load()"};
177  }
178  // check basic validity
179  TiXmlElement* root = XMLDoc->RootElement();
180  assert( root != NULL);
181  if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile"))
182  {
183    // report an error
184    PRINTF(1)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n");
185    delete XMLDoc;
186    return (ErrorMessage){213,"Path not a WorldDataFile","GameWorld::load()"};
187  }
188  /* the whole loading process for the GameWorld */
189  this->dataTank->loadData(root);
190  this->dataXML = (TiXmlElement*)root->Clone();
191
192  delete XMLDoc;
193  this->releaseLoadScreen();
194}
195
196
197/**
198 *  unload the data of this GameWorld
199 */
200ErrorMessage GameWorld::unloadData()
201{
202  delete this->shell;
203  PRINTF(3)("GameWorld::~GameWorld() - unloading the current GameWorld\n");
204
205  this->dataTank->unloadData();
206
207  State::setCurrentStoryEntity(NULL);
208  if (this->dataXML)
209    delete this->dataXML;
210}
211
212
213/**
214 *  starts the GameWorld
215 */
216bool GameWorld::start()
217{
218  this->bPaused = false;
219  this->bRunning = true;
220
221  if (this->audioThread == NULL)
222    this->audioThread = SDL_CreateThread(GameWorld::createAudioThread, (void*)this);
223
224  this->run();
225}
226
227
228/**
229 *  stops the world.
230 */
231bool GameWorld::stop()
232{
233  PRINTF(3)("GameWorld::stop() - got stop signal\n");
234  this->bRunning = false;
235
236//  SDL_KillThread(this->audioThread);
237  this->audioThread = NULL;
238}
239
240
241/**
242 *  pauses the game
243 */
244bool GameWorld::pause()
245{
246  this->bPaused = true;
247}
248
249
250/**
251 *  ends the pause Phase
252 */
253bool GameWorld::resume()
254{
255  this->bPaused = false;
256}
257
258
259/**
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()
267{
268  PRINTF(3)("GameWorld::mainLoop() - Entering main loop\n");
269
270  // initialize Timing
271  this->cycle = 0;
272  for (unsigned int i = 0; i < TICK_SMOOTH_VALUE; i++)
273    this->frameTimes[i] = 100;
274  this->dtS = 0.0f;
275  this->lastFrame = SDL_GetTicks ();
276
277  while( this->bRunning) /* @todo implement pause */
278  {
279    /* process intput */
280    this->handleInput ();
281    if( !this->bRunning)
282      break;
283
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");
297}
298
299
300/**
301 *  synchronize local data with remote data
302*/
303void GameWorld::synchronize ()
304{}
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*/
313void GameWorld::handleInput ()
314{
315  EventHandler::getInstance()->process();
316}
317
318
319/**
320 *  ticks a WorldEntity list
321 * @param entityList list of the WorldEntities
322 * @param dt time passed since last frame
323 */
324void GameWorld::tick(std::list<WorldEntity*> entityList, float dt)
325{
326  std::list<WorldEntity*>::iterator entity, next;
327  next = entityList.begin();
328  while (next != entityList.end())
329  {
330    entity = next++;
331    (*entity)->tick(dt);
332  }
333}
334
335
336/**
337 *  advance the timeline
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 */
343void GameWorld::tick ()
344{
345  Uint32 currentFrame = SDL_GetTicks();
346
347  if( !this->bPaused)
348  {
349    // CALCULATE FRAMERATE
350    Uint32 frameTimesIndex;
351    Uint32 getTicks;
352    Uint32 i;
353
354    frameTimesIndex = this->cycle % TICK_SMOOTH_VALUE;
355    getTicks = SDL_GetTicks();
356    this->frameTimes[frameTimesIndex] = getTicks - this->lastFrame;
357    this->lastFrame = getTicks;
358    ++this->cycle;
359    this->dtS = 0;
360    for (i = 0; i < TICK_SMOOTH_VALUE; i++)
361      this->dtS += this->frameTimes[i];
362    this->dtS = this->dtS / TICK_SMOOTH_VALUE / 1000.0f * speed;
363
364    // TICK everything
365    this->tick(this->dataTank->objectManager->getObjectList(OM_DEAD_TICK), this->dtS);
366    this->tick(this->dataTank->objectManager->getObjectList(OM_ENVIRON), this->dtS);
367    this->tick(this->dataTank->objectManager->getObjectList(OM_COMMON), this->dtS);
368    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00), this->dtS);
369    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ), this->dtS);
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);
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
380    if( likely(this->dataTank->gameRule != NULL))
381      this->dataTank->gameRule->tick(this->dtS);
382  }
383  this->lastFrame = currentFrame;
384}
385
386
387/**
388 *  this function gives the world a consistant state
389 *
390 * after ticking (updating the world state) this will give a constistant
391 * state to the whole system.
392 */
393void GameWorld::update()
394{
395  GraphicsEngine::getInstance()->update(this->dtS);
396  PNode::getNullParent()->updateNode (this->dtS);
397  SoundEngine::getInstance()->update();
398
399  if (this->dataTank->music != NULL)
400    this->dataTank->music->update();
401}
402
403
404/**
405 * kicks the CDEngine to detect the collisions between the object groups in the world
406 */
407void GameWorld::collide()
408{
409  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
410      this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ));
411  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
412      this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ));
413  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
414  this->dataTank->objectManager->getObjectList(OM_GROUP_01));
415
416  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
417      this->dataTank->objectManager->getObjectList(OM_COMMON));
418  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
419      this->dataTank->objectManager->getObjectList(OM_COMMON));
420
421}
422
423/**
424 *  render the current frame
425 *
426 * clear all buffers and draw the world
427 */
428void GameWorld::display ()
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 */
442void GameWorld::draw ()
443{
444  GraphicsEngine* engine = GraphicsEngine::getInstance();
445
446  // set camera
447  this->dataTank->localCamera->apply ();
448  this->dataTank->localCamera->project ();
449  LightManager::getInstance()->draw();
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));
456  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00_PROJ));
457  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01));
458  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ));
459
460
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  }
471
472  if( unlikely(this->showPNodes))
473    PNode::getNullParent()->debugDraw(0);
474
475  engine->draw();
476
477
478  // draw the game ruls
479  if( likely(this->dataTank->gameRule != NULL))
480    this->dataTank->gameRule->draw();
481}
482
483/**
484 *  shows the loading screen
485 */
486void GameWorld::displayLoadScreen ()
487{
488  PRINTF(3)("GameWorld::displayLoadScreen - start\n");
489  this->dataTank->glmis = new GLMenuImageScreen();
490  this->dataTank->glmis->setMaximum(8);
491  PRINTF(3)("GameWorld::displayLoadScreen - end\n");
492}
493
494
495/**
496 *  removes the loadscreen, and changes over to the game
497 */
498void GameWorld::releaseLoadScreen ()
499{
500  PRINTF(3)("GameWorld::releaseLoadScreen - start\n");
501  this->dataTank->glmis->setValue(this->dataTank->glmis->getMaximum());
502  PRINTF(3)("GameWorld::releaseLoadScreen - end\n");
503}
504
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.