Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: some virtuals

File size: 12.1 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
67using namespace std;
68
69
70SHELL_COMMAND(speed, GameWorld, setSpeed);
71SHELL_COMMAND(togglePNodeVisibility, GameWorld, togglePNodeVisibility);
72SHELL_COMMAND(toggleBVVisibility, GameWorld, toggleBVVisibility);
73
74
75
76GameWorld::GameWorld(const TiXmlElement* root)
77    : StoryEntity()
78{
79  this->setClassID(CL_GAME_WORLD, "GameWorld");
80  this->setName("Preloaded World - no name yet");
81
82  this->gameTime = 0.0f;
83  this->setSpeed(1.0f);
84  this->shell = NULL;
85
86  this->showPNodes = false;
87  this->showBV = false;
88
89  this->dataXML = NULL;
90  this->path = 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  LoadParam(root, "path", this, GameWorld, setPath)
115      .describe("The Filename of this GameWorld (relative from the data-dir)");
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  this->cycle = 0;
130  /* init the world interface */
131  this->shell = new Shell();
132
133  State::setCurrentStoryEntity(dynamic_cast<StoryEntity*>(this));
134  this->dataTank->init();
135}
136
137
138/**
139 *  loads the GameWorld by initializing all resources, and set their default values.
140 */
141ErrorMessage GameWorld::loadData()
142{
143  this->displayLoadScreen();
144
145  PRINTF(0)("Loading the GameWorld\n");
146
147  PRINTF(3)("> Loading world: '%s'\n", getPath());
148  TiXmlElement* element;
149  GameLoader* loader = GameLoader::getInstance();
150
151  if( getPath() == NULL)
152  {
153    PRINTF(1)("GameWorld has no path specified for loading\n");
154    return (ErrorMessage){213,"Path not specified","GameWorld::load()"};
155  }
156
157  TiXmlDocument* XMLDoc = new TiXmlDocument( getPath());
158  // load the xml world file for further loading
159  if( !XMLDoc->LoadFile())
160  {
161    PRINTF(1)("loading XML File: %s @ %s:l%d:c%d\n", XMLDoc->ErrorDesc(), this->getPath(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
162    delete XMLDoc;
163    return (ErrorMessage){213,"XML File parsing error","GameWorld::load()"};
164  }
165  // check basic validity
166  TiXmlElement* root = XMLDoc->RootElement();
167  assert( root != NULL);
168  if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile"))
169  {
170    // report an error
171    PRINTF(1)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n");
172    delete XMLDoc;
173    return (ErrorMessage){213,"Path not a WorldDataFile","GameWorld::load()"};
174  }
175  /* the whole loading process for the GameWorld */
176  this->dataTank->loadData(root);
177  this->dataXML = (TiXmlElement*)root->Clone();
178
179  delete XMLDoc;
180  this->releaseLoadScreen();
181}
182
183
184/**
185 *  unload the data of this GameWorld
186 */
187ErrorMessage GameWorld::unloadData()
188{
189  delete this->shell;
190  PRINTF(3)("GameWorld::~GameWorld() - unloading the current GameWorld\n");
191
192  this->dataTank->unloadData();
193
194  if (this->dataXML)
195    delete this->dataXML;
196
197}
198
199
200/**
201 *  starts the GameWorld
202 */
203bool GameWorld::start()
204{
205  this->isPaused = false;
206  this->isRunning = true;
207
208  this->run();
209}
210
211
212/**
213 *  stops the world.
214 */
215bool GameWorld::stop()
216{
217  PRINTF(3)("GameWorld::stop() - got stop signal\n");
218  this->isRunning = false;
219}
220
221
222/**
223 *  pauses the game
224 */
225bool GameWorld::pause()
226{
227  this->isPaused = true;
228}
229
230
231/**
232 *  ends the pause Phase
233 */
234bool GameWorld::resume()
235{
236  this->isPaused = false;
237}
238
239
240/**
241 *  main loop of the world: executing all world relevant function
242 *
243 * in this loop we synchronize (if networked), handle input events, give the heart-beat to
244 * all other member-entities of the world (tick to player, enemies etc.), checking for
245 * collisions drawing everything to the screen.
246 */
247void GameWorld::run()
248{
249  /* start the music */
250  if(this->dataTank->music != NULL)
251    this->dataTank->music->playback();
252
253  this->lastFrame = SDL_GetTicks ();
254  PRINTF(3)("GameWorld::mainLoop() - Entering main loop\n");
255
256  while( this->isRunning) /* @todo implement pause */
257  {
258    ++this->cycle;
259    /* process intput */
260    this->handleInput ();
261    if( !this->isRunning)
262      break;
263
264    /* network synchronisation */
265    this->synchronize ();
266    /* process time */
267    this->tick ();
268    /* process collision */
269    this->collide ();
270    /* update the state */
271    this->update ();
272    /* draw everything */
273    this->display ();
274  }
275
276  PRINTF(3)("GameWorld::mainLoop() - Exiting the main loop\n");
277}
278
279
280/**
281 *  synchronize local data with remote data
282*/
283void GameWorld::synchronize ()
284{}
285
286
287/**
288 *  run all input processing
289
290   the command node is the central input event dispatcher. the node uses the even-queue from
291   sdl and has its own event-passing-queue.
292*/
293void GameWorld::handleInput ()
294{
295  EventHandler::getInstance()->process();
296}
297
298
299/**
300 *  ticks a WorldEntity list
301 * @param entityList list of the WorldEntities
302 * @param dt time passed since last frame
303 */
304void GameWorld::tick(std::list<WorldEntity*> entityList, float dt)
305{
306  std::list<WorldEntity*>::iterator entity, next;
307  next = entityList.begin();
308  while (next != entityList.end())
309  {
310    entity = next++;
311    (*entity)->tick(dt);
312  }
313}
314
315/**
316 *  advance the timeline
317 *
318 * this calculates the time used to process one frame (with all input handling, drawing, etc)
319 * the time is mesured in ms and passed to all world-entities and other classes that need
320 * a heart-beat.
321 */
322void GameWorld::tick ()
323{
324  Uint32 currentFrame = SDL_GetTicks();
325
326  if( !this->isPaused)
327  {
328    this->dt = currentFrame - this->lastFrame;
329
330    /* limit the the frame rate to 100 frames per second (fps) */
331    if( this->dt < 10)
332    {
333      /* the frame-rate is limited to 100 frames per second, all other things are for nothing. */
334      //PRINTF(0)("fps = 1000 - frame rate is adjusted\n");
335      SDL_Delay(10 - dt);
336      this->dt = 10;
337    }
338
339    this->dtS = (float)this->dt / 1000.0f * this->speed;
340    this->gameTime += this->dtS;
341
342    this->tick(this->dataTank->objectManager->getObjectList(OM_DEAD_TICK), this->dtS);
343    this->tick(this->dataTank->objectManager->getObjectList(OM_ENVIRON), this->dtS);
344    this->tick(this->dataTank->objectManager->getObjectList(OM_COMMON), this->dtS);
345    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00), this->dtS);
346    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ), this->dtS);
347    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_01), this->dtS);
348    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ), this->dtS);
349
350    /* update tick the rest */
351    this->dataTank->localCamera->tick(this->dtS);
352    AnimationPlayer::getInstance()->tick(this->dtS);
353    PhysicsEngine::getInstance()->tick(this->dtS);
354
355    GraphicsEngine::getInstance()->tick(this->dtS);
356  }
357  this->lastFrame = currentFrame;
358}
359
360
361/**
362 *  this function gives the world a consistant state
363 *
364 * after ticking (updating the world state) this will give a constistant
365 * state to the whole system.
366 */
367void GameWorld::update()
368{
369  GraphicsEngine::getInstance()->update(this->dtS);
370  PNode::getNullParent()->updateNode (this->dtS);
371  SoundEngine::getInstance()->update();
372
373  if (this->dataTank->music != NULL)
374    this->dataTank->music->update();
375}
376
377
378/**
379 * kicks the CDEngine to detect the collisions between the object groups in the world
380 */
381void GameWorld::collide()
382{
383  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
384  this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ));
385  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
386  this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ));
387
388  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
389      this->dataTank->objectManager->getObjectList(OM_COMMON));
390}
391
392/**
393 *  render the current frame
394 *
395 * clear all buffers and draw the world
396 */
397void GameWorld::display ()
398{
399  // clear buffer
400  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
401  // draw world
402  this->draw();
403  // flip buffers
404  GraphicsEngine::swapBuffers();
405}
406
407
408/**
409 *  runs through all entities calling their draw() methods
410 */
411void GameWorld::draw ()
412{
413  GraphicsEngine* engine = GraphicsEngine::getInstance();
414
415  // set camera
416  this->dataTank->localCamera->apply ();
417
418  /* draw all WorldEntiy groups */
419  engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON_NOTICK));
420  engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON));
421  engine->draw(State::getObjectManager()->getObjectList(OM_COMMON));
422  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00));
423  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00_PROJ));
424  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01));
425  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ));
426
427
428  if( unlikely( this->showBV))
429  {
430    CDEngine* engine = CDEngine::getInstance();
431    engine->drawBV(State::getObjectManager()->getObjectList(OM_ENVIRON_NOTICK));
432    engine->drawBV(State::getObjectManager()->getObjectList(OM_ENVIRON));
433    engine->drawBV(State::getObjectManager()->getObjectList(OM_COMMON));
434    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_00));
435    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_01));
436    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ));
437  }
438
439  if( unlikely(this->showPNodes))
440    PNode::getNullParent()->debugDraw(0);
441
442  engine->draw();
443}
444
445
446/**
447 *  sets the track path of this world
448 * @param name the name of the path
449 */
450void GameWorld::setPath( const char* name)
451{
452  if (this->path)
453    delete this->path;
454  if (ResourceManager::isFile(name))
455  {
456    this->path = new char[strlen(name)+1];
457    strcpy(this->path, name);
458  }
459  else
460  {
461    this->path = new char[strlen(ResourceManager::getInstance()->getDataDir()) + strlen(name) +1];
462    sprintf(this->path, "%s%s", ResourceManager::getInstance()->getDataDir(), name);
463  }
464}
465
466
467/**
468 *  shows the loading screen
469 */
470void GameWorld::displayLoadScreen ()
471{
472  PRINTF(3)("GameWorld::displayLoadScreen - start\n");
473  this->dataTank->glmis = new GLMenuImageScreen();
474  this->dataTank->glmis->setMaximum(8);
475  PRINTF(3)("GameWorld::displayLoadScreen - end\n");
476}
477
478
479/**
480 *  removes the loadscreen, and changes over to the game
481 */
482void GameWorld::releaseLoadScreen ()
483{
484  PRINTF(3)("GameWorld::releaseLoadScreen - start\n");
485  this->dataTank->glmis->setValue(this->dataTank->glmis->getMaximum());
486  PRINTF(3)("GameWorld::releaseLoadScreen - end\n");
487}
488
Note: See TracBrowser for help on using the repository browser.