Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/story_entities/game_world.cc @ 6459

Last change on this file since 6459 was 6459, checked in by patrick, 18 years ago

network: skybox parenting fix

File size: 11.6 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 "weapons/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 "particle_engine.h"
50#include "graphics_engine.h"
51#include "event_handler.h"
52#include "sound_engine.h"
53#include "cd_engine.h"
54#include "network_manager.h"
55#include "physics_engine.h"
56#include "fields.h"
57
58#include "glmenu_imagescreen.h"
59#include "shell.h"
60
61#include "animation_player.h"
62#include "animation3d.h"
63
64#include "ogg_player.h"
65#include "shader.h"
66
67
68using namespace std;
69
70
71SHELL_COMMAND(speed, GameWorld, setSpeed);
72SHELL_COMMAND(togglePNodeVisibility, GameWorld, togglePNodeVisibility);
73SHELL_COMMAND(toggleBVVisibility, GameWorld, toggleBVVisibility);
74
75
76
77GameWorld::GameWorld(const TiXmlElement* root)
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->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  static_cast<StoryEntity*>(this)->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  this->dataTank->init();
134}
135
136
137/**
138 *  loads the GameWorld by initializing all resources, and set their default values.
139 */
140ErrorMessage GameWorld::loadData()
141{
142  this->displayLoadScreen();
143
144  PRINTF(0)("Loading the GameWorld\n");
145
146  PRINTF(3)("> Loading world: '%s'\n", getPath());
147  TiXmlElement* element;
148  GameLoader* loader = GameLoader::getInstance();
149
150  if( getPath() == NULL)
151  {
152    PRINTF(1)("GameWorld has no path specified for loading");
153    return (ErrorMessage){213,"Path not specified","GameWorld::load()"};
154  }
155
156  TiXmlDocument* XMLDoc = new TiXmlDocument( getPath());
157  // load the xml world file for further loading
158  if( !XMLDoc->LoadFile())
159  {
160    PRINTF(1)("loading XML File: %s @ %s:l%d:c%d\n", XMLDoc->ErrorDesc(), this->getPath(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
161    delete XMLDoc;
162    return (ErrorMessage){213,"XML File parsing error","GameWorld::load()"};
163  }
164  // check basic validity
165  TiXmlElement* root = XMLDoc->RootElement();
166  assert( root != NULL);
167  if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile"))
168  {
169    // report an error
170    PRINTF(1)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n");
171    delete XMLDoc;
172    return (ErrorMessage){213,"Path not a WorldDataFile","GameWorld::load()"};
173  }
174  /* the whole loading process for the GameWorld */
175  this->dataTank->loadData(root);
176
177  delete XMLDoc;
178  this->releaseLoadScreen();
179}
180
181
182/**
183 *  unload the data of this GameWorld
184 */
185ErrorMessage GameWorld::unloadData()
186{
187  delete this->shell;
188  PRINTF(3)("GameWorld::~GameWorld() - unloading the current GameWorld\n");
189
190  this->dataTank->unloadData();
191}
192
193
194/**
195 *  starts the GameWorld
196 */
197bool GameWorld::start()
198{
199  this->isPaused = false;
200  this->isRunning = true;
201
202  State::setCurrentStoryEntity(dynamic_cast<StoryEntity*>(this));
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  this->lastFrame = SDL_GetTicks ();
246  PRINTF(3)("GameWorld::mainLoop() - Entering main loop\n");
247
248  while( this->isRunning) /* @todo implement pause */
249  {
250    ++this->cycle;
251    /* process intput */
252    this->handleInput ();
253    if( !this->isRunning)
254      break;
255
256    /* network synchronisation */
257    this->synchronize ();
258    /* process time */
259    this->tick ();
260    /* process collision */
261    this->collide ();
262    /* update the state */
263    this->update ();
264    /* draw everything */
265    this->display ();
266  }
267
268  PRINTF(3)("GameWorld::mainLoop() - Exiting the main loop\n");
269}
270
271
272/**
273 *  synchronize local data with remote data
274*/
275void GameWorld::synchronize ()
276{}
277
278
279/**
280 *  run all input processing
281
282   the command node is the central input event dispatcher. the node uses the even-queue from
283   sdl and has its own event-passing-queue.
284*/
285void GameWorld::handleInput ()
286{
287  EventHandler::getInstance()->process();
288}
289
290
291/**
292 *  ticks a WorldEntity list
293 * @param entityList list of the WorldEntities
294 * @param dt time passed since last frame
295 */
296void GameWorld::tick(std::list<WorldEntity*> entityList, float dt)
297{
298  std::list<WorldEntity*>::iterator entity;
299  for (entity = entityList.begin(); entity != entityList.end(); entity++)
300    (*entity)->tick(dt);
301
302}
303
304/**
305 *  advance the timeline
306 *
307 * this calculates the time used to process one frame (with all input handling, drawing, etc)
308 * the time is mesured in ms and passed to all world-entities and other classes that need
309 * a heart-beat.
310 */
311void GameWorld::tick ()
312{
313  Uint32 currentFrame = SDL_GetTicks();
314
315  if( !this->isPaused)
316  {
317    this->dt = currentFrame - this->lastFrame;
318
319    /* limit the the frame rate to 100 frames per second (fps) */
320    if( this->dt < 10)
321    {
322      /* the frame-rate is limited to 100 frames per second, all other things are for nothing. */
323      //PRINTF(0)("fps = 1000 - frame rate is adjusted\n");
324      SDL_Delay(10 - dt);
325      this->dt = 10;
326    }
327
328    this->dtS = (float)this->dt / 1000.0f * this->speed;
329    this->gameTime += this->dtS;
330
331    this->tick(this->dataTank->objectManager->getObjectList(OM_DEAD_TICK), this->dtS);
332    this->tick(this->dataTank->objectManager->getObjectList(OM_COMMON), this->dtS);
333    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00), this->dtS);
334    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_01), this->dtS);
335    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ), this->dtS);
336
337    /* update tick the rest */
338    this->dataTank->localCamera->tick(this->dtS);
339    AnimationPlayer::getInstance()->tick(this->dtS);
340    ParticleEngine::getInstance()->tick(this->dtS);
341    PhysicsEngine::getInstance()->tick(this->dtS);
342
343    GraphicsEngine::getInstance()->tick(this->dtS);
344  }
345  this->lastFrame = currentFrame;
346}
347
348
349/**
350 *  this function gives the world a consistant state
351 *
352 * after ticking (updating the world state) this will give a constistant
353 * state to the whole system.
354 */
355void GameWorld::update()
356{
357  GraphicsEngine::getInstance()->update(this->dtS);
358  PNode::getNullParent()->updateNode (this->dtS);
359  SoundEngine::getInstance()->update();
360  //music->update();
361}
362
363
364/**
365 * kicks the CDEngine to detect the collisions between the object groups in the world
366 */
367void GameWorld::collide()
368{
369  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
370      this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ));
371  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
372      this->dataTank->objectManager->getObjectList(OM_COMMON));
373}
374
375/**
376 *  render the current frame
377 *
378 * clear all buffers and draw the world
379 */
380void GameWorld::display ()
381{
382  // clear buffer
383  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
384  // set camera
385  this->dataTank->localCamera->apply ();
386  // draw world
387  this->draw();
388  // flip buffers
389  GraphicsEngine::swapBuffers();
390}
391
392
393/**
394 *  runs through all entities calling their draw() methods
395 */
396void GameWorld::draw ()
397{
398  GraphicsEngine* engine = GraphicsEngine::getInstance();
399
400   /* to draw the bounding boxes of the objects at level 2 for debug purp */
401
402  /* debug draw the PNodes */
403
404  /* draw all WorldEntiy groups */
405  engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON_NOTICK));
406  engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON));
407  engine->draw(State::getObjectManager()->getObjectList(OM_COMMON));
408  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00));
409  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01));
410  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ));
411  /* draws the particles */
412  ParticleEngine::getInstance()->draw();
413
414  if( unlikely( this->showBV))
415  {
416    CDEngine* engine = CDEngine::getInstance();
417    engine->drawBV(State::getObjectManager()->getObjectList(OM_ENVIRON_NOTICK));
418    engine->drawBV(State::getObjectManager()->getObjectList(OM_ENVIRON));
419    engine->drawBV(State::getObjectManager()->getObjectList(OM_COMMON));
420    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_00));
421    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_01));
422    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ));
423  }
424
425  if( unlikely(this->showPNodes))
426    PNode::getNullParent()->debugDraw(0);
427
428  /* final draw command */
429  engine->draw();
430}
431
432
433/**
434 *  sets the track path of this world
435 * @param name the name of the path
436 */
437void GameWorld::setPath( const char* name)
438{
439  if (this->path)
440    delete this->path;
441  if (ResourceManager::isFile(name))
442  {
443    this->path = new char[strlen(name)+1];
444    strcpy(this->path, name);
445  }
446  else
447  {
448    this->path = new char[strlen(ResourceManager::getInstance()->getDataDir()) + strlen(name) +1];
449    sprintf(this->path, "%s%s", ResourceManager::getInstance()->getDataDir(), name);
450  }
451}
452
453
454/**
455 *  shows the loading screen
456 */
457void GameWorld::displayLoadScreen ()
458{
459  PRINTF(3)("GameWorld::displayLoadScreen - start\n");
460  this->dataTank->glmis = new GLMenuImageScreen();
461  this->dataTank->glmis->setMaximum(8);
462  PRINTF(3)("GameWorld::displayLoadScreen - end\n");
463}
464
465
466/**
467 *  removes the loadscreen, and changes over to the game
468 */
469void GameWorld::releaseLoadScreen ()
470{
471  PRINTF(3)("GameWorld::releaseLoadScreen - start\n");
472  this->dataTank->glmis->setValue(this->dataTank->glmis->getMaximum());
473  PRINTF(3)("GameWorld::releaseLoadScreen - end\n");
474}
475
Note: See TracBrowser for help on using the repository browser.