Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: framerate calculation is much more 'accurate' meaning, if it is falling from one value into another one, the rate will be averanged to the middle.

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