Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

better debug, (non-threaded again)

File size: 14.0 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  if (this->dataTank->music != NULL)
233    this->dataTank->music->playback();
234
235  this->run();
236}
237
238
239/**
240 *  stops the world.
241 */
242bool GameWorld::stop()
243{
244  PRINTF(3)("GameWorld::stop() - got stop signal\n");
245  this->bRunning = false;
246
247  if (this->audioThread != NULL)
248  {
249    this->bRunning = false;
250    SDL_WaitThread(this->audioThread, NULL);
251    this->audioThread = NULL;
252  }
253
254  this->audioThread = NULL;
255}
256
257
258/**
259 *  pauses the game
260 */
261bool GameWorld::pause()
262{
263  this->bPaused = true;
264}
265
266
267/**
268 *  ends the pause Phase
269 */
270bool GameWorld::resume()
271{
272  this->bPaused = false;
273}
274
275
276/**
277 *  main loop of the world: executing all world relevant function
278 *
279 * in this loop we synchronize (if networked), handle input events, give the heart-beat to
280 * all other member-entities of the world (tick to player, enemies etc.), checking for
281 * collisions drawing everything to the screen.
282 */
283void GameWorld::run()
284{
285  PRINTF(3)("GameWorld::mainLoop() - Entering main loop\n");
286
287  // initialize Timing
288  this->cycle = 0;
289  for (unsigned int i = 0; i < TICK_SMOOTH_VALUE; i++)
290    this->frameTimes[i] = 100;
291  this->dtS = 0.0f;
292  this->lastFrame = SDL_GetTicks ();
293
294  while( this->bRunning) /* @todo implement pause */
295  {
296    /* process intput */
297    this->handleInput ();
298    if( !this->bRunning)
299      break;
300
301    /* network synchronisation */
302    this->synchronize ();
303    /* process time */
304    this->tick ();
305    /* process collision */
306    this->collide ();
307    /* update the state */
308    this->update ();
309    /* draw everything */
310    this->display ();
311  }
312
313  PRINTF(3)("GameWorld::mainLoop() - Exiting the main loop\n");
314}
315
316
317/**
318 *  synchronize local data with remote data
319*/
320void GameWorld::synchronize ()
321{}
322
323
324/**
325 *  run all input processing
326
327   the command node is the central input event dispatcher. the node uses the even-queue from
328   sdl and has its own event-passing-queue.
329*/
330void GameWorld::handleInput ()
331{
332  EventHandler::getInstance()->process();
333}
334
335
336/**
337 *  ticks a WorldEntity list
338 * @param entityList list of the WorldEntities
339 * @param dt time passed since last frame
340 */
341void GameWorld::tick(std::list<WorldEntity*> entityList, float dt)
342{
343  std::list<WorldEntity*>::iterator entity, next;
344  next = entityList.begin();
345  while (next != entityList.end())
346  {
347    entity = next++;
348    (*entity)->tick(dt);
349  }
350}
351
352
353/**
354 *  advance the timeline
355 *
356 * this calculates the time used to process one frame (with all input handling, drawing, etc)
357 * the time is mesured in ms and passed to all world-entities and other classes that need
358 * a heart-beat.
359 */
360void GameWorld::tick ()
361{
362  Uint32 currentFrame = SDL_GetTicks();
363
364  if( !this->bPaused)
365  {
366    // CALCULATE FRAMERATE
367    Uint32 frameTimesIndex;
368    Uint32 getTicks;
369    Uint32 i;
370
371    frameTimesIndex = this->cycle % TICK_SMOOTH_VALUE;
372    getTicks = SDL_GetTicks();
373    this->frameTimes[frameTimesIndex] = getTicks - this->lastFrame;
374    this->lastFrame = getTicks;
375    ++this->cycle;
376    this->dtS = 0;
377    for (i = 0; i < TICK_SMOOTH_VALUE; i++)
378      this->dtS += this->frameTimes[i];
379    this->dtS = this->dtS / TICK_SMOOTH_VALUE / 1000.0f * speed;
380
381    // TICK everything
382    this->tick(this->dataTank->objectManager->getObjectList(OM_DEAD_TICK), this->dtS);
383    this->tick(this->dataTank->objectManager->getObjectList(OM_ENVIRON), this->dtS);
384    this->tick(this->dataTank->objectManager->getObjectList(OM_COMMON), this->dtS);
385    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00), this->dtS);
386    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ), this->dtS);
387    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_01), this->dtS);
388    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ), this->dtS);
389
390    /* update tick the rest */
391    this->dataTank->localCamera->tick(this->dtS);
392    AnimationPlayer::getInstance()->tick(this->dtS);
393    PhysicsEngine::getInstance()->tick(this->dtS);
394
395    GraphicsEngine::getInstance()->tick(this->dtS);
396
397    if( likely(this->dataTank->gameRule != NULL))
398      this->dataTank->gameRule->tick(this->dtS);
399  }
400  this->lastFrame = currentFrame;
401}
402
403
404/**
405 *  this function gives the world a consistant state
406 *
407 * after ticking (updating the world state) this will give a constistant
408 * state to the whole system.
409 */
410void GameWorld::update()
411{
412  GraphicsEngine::getInstance()->update(this->dtS);
413  PNode::getNullParent()->updateNode (this->dtS);
414  SoundEngine::getInstance()->update();
415
416  if (this->dataTank->music != NULL)
417    this->dataTank->music->update();
418
419}
420
421
422/**
423 * kicks the CDEngine to detect the collisions between the object groups in the world
424 */
425void GameWorld::collide()
426{
427  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
428      this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ));
429  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
430      this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ));
431  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
432      this->dataTank->objectManager->getObjectList(OM_GROUP_01));
433
434  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
435      this->dataTank->objectManager->getObjectList(OM_COMMON));
436  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
437      this->dataTank->objectManager->getObjectList(OM_COMMON));
438
439}
440
441/**
442 *  render the current frame
443 *
444 * clear all buffers and draw the world
445 */
446void GameWorld::display ()
447{
448  // clear buffer
449  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
450  // draw world
451  this->draw();
452  // flip buffers
453  GraphicsEngine::swapBuffers();
454}
455
456
457/**
458 *  runs through all entities calling their draw() methods
459 */
460void GameWorld::draw ()
461{
462  GraphicsEngine* engine = GraphicsEngine::getInstance();
463
464  // set camera
465  this->dataTank->localCamera->apply ();
466  this->dataTank->localCamera->project ();
467  LightManager::getInstance()->draw();
468
469  /* draw all WorldEntiy groups */
470  engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON_NOTICK));
471  engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON));
472  engine->draw(State::getObjectManager()->getObjectList(OM_COMMON));
473  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00));
474  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00_PROJ));
475  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01));
476  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ));
477
478
479  if( unlikely( this->showBV))
480  {
481    CDEngine* engine = CDEngine::getInstance();
482    engine->drawBV(State::getObjectManager()->getObjectList(OM_ENVIRON_NOTICK));
483    engine->drawBV(State::getObjectManager()->getObjectList(OM_ENVIRON));
484    engine->drawBV(State::getObjectManager()->getObjectList(OM_COMMON));
485    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_00));
486    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_01));
487    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ));
488  }
489
490  if( unlikely(this->showPNodes))
491    PNode::getNullParent()->debugDraw(0);
492
493  engine->draw();
494
495
496  // draw the game ruls
497  if( likely(this->dataTank->gameRule != NULL))
498    this->dataTank->gameRule->draw();
499}
500
501/**
502 *  shows the loading screen
503 */
504void GameWorld::displayLoadScreen ()
505{
506  PRINTF(3)("GameWorld::displayLoadScreen - start\n");
507  this->dataTank->glmis = new GLMenuImageScreen();
508  this->dataTank->glmis->setMaximum(8);
509  PRINTF(3)("GameWorld::displayLoadScreen - end\n");
510}
511
512
513/**
514 *  removes the loadscreen, and changes over to the game
515 */
516void GameWorld::releaseLoadScreen ()
517{
518  PRINTF(3)("GameWorld::releaseLoadScreen - start\n");
519  this->dataTank->glmis->setValue(this->dataTank->glmis->getMaximum());
520  PRINTF(3)("GameWorld::releaseLoadScreen - end\n");
521}
522
523
524
525/**
526 * @brief toggles the PNode visibility in the world (drawn as boxes)
527 */
528void GameWorld::togglePNodeVisibility()
529{
530  if (State::getCurrentStoryEntity() != NULL &&
531      State::getCurrentStoryEntity()->isA(CL_GAME_WORLD))
532  {
533    dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showPNodes = !dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showPNodes;
534  }
535  else
536    PRINTF(2)("The Current Story entity is not a GameWorld\n");
537};
538
539
540/**
541 * @brief toggles the bounding volume (BV) visibility
542*/
543void GameWorld::toggleBVVisibility()
544{
545  if (State::getCurrentStoryEntity() != NULL &&
546      State::getCurrentStoryEntity()->isA(CL_GAME_WORLD))
547  {
548    dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showBV = !dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showBV;
549  }
550  else
551    PRINTF(2)("The Current Story entity is not a GameWorld\n");
552};
553
Note: See TracBrowser for help on using the repository browser.