Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/water/src/story_entities/game_world.cc @ 7741

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

water: i think i eliminated some more bugs. the texture now displayed is strange… it's a simple texture. perhaps bensch knows what happened here :D

File size: 14.8 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
26#include "util/loading/game_loader.h"
27
28#include "player.h"
29#include "camera.h"
30#include "environment.h"
31#include "terrain.h"
32#include "test_entity.h"
33#include "terrain.h"
34#include "playable.h"
35#include "environments/mapped_water.h"
36
37#include "light.h"
38
39#include "util/loading/factory.h"
40#include "util/loading/load_param.h"
41#include "fast_factory.h"
42#include "shell_command.h"
43
44#include "graphics_engine.h"
45#include "event_handler.h"
46#include "sound_engine.h"
47#include "cd_engine.h"
48#include "network_manager.h"
49#include "physics_engine.h"
50#include "fields.h"
51
52#include "glmenu_imagescreen.h"
53#include "shell.h"
54
55#include "ogg_player.h"
56#include "shader.h"
57
58#include "animation_player.h"
59
60#include "game_rules.h"
61
62using namespace std;
63
64
65SHELL_COMMAND(speed, GameWorld, setSpeed) ->describe("set the Speed of the Level");
66SHELL_COMMAND(playmode, GameWorld, setPlaymode)
67    ->describe("Set the Playmode of the current Level")
68    ->completionPlugin(0, OrxShell::CompletorStringArray(Playable::playmodeNames, Playable::PlaymodeCount));
69
70SHELL_COMMAND_STATIC(togglePNodeVisibility, GameWorld, GameWorld::togglePNodeVisibility);
71SHELL_COMMAND_STATIC(toggleBVVisibility, GameWorld, GameWorld::toggleBVVisibility);
72
73
74
75GameWorld::GameWorld()
76    : StoryEntity()
77{
78  this->setClassID(CL_GAME_WORLD, "GameWorld");
79  this->setName("Preloaded World - no name yet");
80
81  this->gameTime = 0.0f;
82  this->setSpeed(1.0f);
83  this->shell = NULL;
84
85  this->showPNodes = false;
86  this->showBV = false;
87
88  this->dataXML = NULL;
89  this->gameRules = NULL;
90}
91
92/**
93 *  remove the GameWorld from memory
94 *
95 *  delete everything explicitly, that isn't contained in the parenting tree!
96 *  things contained in the tree are deleted automaticaly
97 */
98GameWorld::~GameWorld ()
99{
100  PRINTF(4)("Deleted GameWorld\n");
101
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 OrxShell::Shell();
128
129  State::setCurrentStoryEntity(dynamic_cast<StoryEntity*>(this));
130  this->dataTank->init();
131
132  /* initialize some engines and graphical elements */
133  AnimationPlayer::getInstance();
134  PhysicsEngine::getInstance();
135
136}
137
138
139/**
140 *  loads the GameWorld by initializing all resources, and set their default values.
141 */
142ErrorMessage GameWorld::loadData()
143{
144  this->displayLoadScreen();
145
146  PRINTF(0)("Loading the GameWorld\n");
147
148  PRINTF(3)("> Loading world: '%s'\n", getLoadFile());
149  TiXmlElement* element;
150  GameLoader* loader = GameLoader::getInstance();
151
152  if( getLoadFile().empty())
153  {
154    PRINTF(1)("GameWorld has no path specified for loading\n");
155    return (ErrorMessage){213,"Path not specified","GameWorld::load()"};
156  }
157
158  TiXmlDocument* XMLDoc = new TiXmlDocument( getLoadFile());
159  // load the xml world file for further loading
160  if( !XMLDoc->LoadFile())
161  {
162    PRINTF(1)("loading XML File: %s @ %s:l%d:c%d\n", XMLDoc->ErrorDesc(), this->getLoadFile().c_str(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
163    delete XMLDoc;
164    return (ErrorMessage){213,"XML File parsing error","GameWorld::load()"};
165  }
166  // check basic validity
167  TiXmlElement* root = XMLDoc->RootElement();
168  assert( root != NULL);
169  if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile"))
170  {
171    // report an error
172    PRINTF(1)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n");
173    delete XMLDoc;
174    return (ErrorMessage){213,"Path not a WorldDataFile","GameWorld::load()"};
175  }
176  /* the whole loading process for the GameWorld */
177  this->dataTank->loadData(root);
178  this->dataXML = (TiXmlElement*)root->Clone();
179
180  delete XMLDoc;
181  this->releaseLoadScreen();
182}
183
184
185/**
186 *  unload the data of this GameWorld
187 */
188ErrorMessage GameWorld::unloadData()
189{
190  PRINTF(3)("GameWorld::~GameWorld() - unloading the current GameWorld\n");
191  delete this->shell;
192
193  this->dataTank->unloadData();
194
195  this->shell = NULL;
196  delete AnimationPlayer::getInstance();
197  delete PhysicsEngine::getInstance();
198
199  State::setCurrentStoryEntity(NULL);
200  if (this->dataXML)
201    delete this->dataXML;
202}
203
204
205/**
206 *  starts the GameWorld
207 */
208bool GameWorld::start()
209{
210  this->bPaused = false;
211  this->bRunning = true;
212
213  this->run();
214}
215
216
217/**
218 *  stops the world.
219 */
220bool GameWorld::stop()
221{
222  PRINTF(3)("GameWorld::stop() - got stop signal\n");
223  this->bRunning = false;
224}
225
226
227/**
228 *  pauses the game
229 */
230bool GameWorld::pause()
231{
232  this->bPaused = true;
233}
234
235
236/**
237 *  ends the pause Phase
238 */
239bool GameWorld::resume()
240{
241  this->bPaused = false;
242}
243
244
245/**
246 *  main loop of the world: executing all world relevant function
247 *
248 * in this loop we synchronize (if networked), handle input events, give the heart-beat to
249 * all other member-entities of the world (tick to player, enemies etc.), checking for
250 * collisions drawing everything to the screen.
251 */
252void GameWorld::run()
253{
254  PRINTF(3)("GameWorld::mainLoop() - Entering main loop\n");
255
256  // initialize Timing
257  this->cycle = 0;
258  for (unsigned int i = 0; i < TICK_SMOOTH_VALUE; i++)
259    this->frameTimes[i] = 100;
260  this->dtS = 0.0f;
261  this->lastFrame = SDL_GetTicks ();
262
263  if (this->dataTank->music != NULL)
264    this->dataTank->music->play();
265
266  while( this->bRunning) /* @todo implement pause */
267  {
268    /* process intput */
269    this->handleInput ();
270    if( !this->bRunning)
271      break;
272
273    /* network synchronisation */
274    this->synchronize ();
275    /* process time */
276    this->tick ();
277    /* process collision */
278    this->collide ();
279    /* update the state */
280    this->update ();
281    /* check the game rules */
282    this->checkGameRules();
283    /* draw everything */
284    this->display ();
285
286  }
287
288  PRINTF(0)("GameWorld::mainLoop() - Exiting the main loop\n");
289}
290
291
292void GameWorld::setPlaymode(Playable::Playmode playmode)
293{
294  if (this->dataTank->localPlayer &&
295      this->dataTank->localPlayer->getPlayable() &&
296      this->dataTank->localPlayer->getPlayable()->setPlaymode(playmode))
297  {
298    PRINTF(3)("Set Playmode to %d:%s\n", playmode, Playable::playmodeToString(playmode).c_str());
299  }
300  else
301  {
302    PRINTF(1)("Unable to set Playmode %d:'%s'\n", playmode, Playable::playmodeToString(playmode).c_str());
303  }
304}
305
306void GameWorld::setPlaymode(const std::string& playmode)
307{
308  this->setPlaymode(Playable::stringToPlaymode(playmode));
309}
310
311/**
312 *  synchronize local data with remote data
313*/
314void GameWorld::synchronize ()
315{}
316
317
318/**
319 *  run all input processing
320
321   the command node is the central input event dispatcher. the node uses the even-queue from
322   sdl and has its own event-passing-queue.
323*/
324void GameWorld::handleInput ()
325{
326  EventHandler::getInstance()->process();
327}
328
329
330/**
331 * @brief ticks a WorldEntity list
332 * @param entityList list of the WorldEntities
333 * @param dt time passed since last frame
334 */
335void GameWorld::tick(ObjectManager::EntityList entityList, float dt)
336{
337  ObjectManager::EntityList::iterator entity, next;
338  next = entityList.begin();
339  while (next != entityList.end())
340  {
341    entity = next++;
342    (*entity)->tick(dt);
343  }
344}
345
346
347/**
348 *  advance the timeline
349 *
350 * this calculates the time used to process one frame (with all input handling, drawing, etc)
351 * the time is mesured in ms and passed to all world-entities and other classes that need
352 * a heart-beat.
353 */
354void GameWorld::tick ()
355{
356  Uint32 currentFrame = SDL_GetTicks();
357
358  if( !this->bPaused)
359  {
360    // CALCULATE FRAMERATE
361    Uint32 frameTimesIndex;
362    Uint32 getTicks;
363    Uint32 i;
364
365    frameTimesIndex = this->cycle % TICK_SMOOTH_VALUE;
366    getTicks = SDL_GetTicks();
367    this->frameTimes[frameTimesIndex] = getTicks - this->lastFrame;
368    this->lastFrame = getTicks;
369    ++this->cycle;
370    this->dtS = 0;
371    for (i = 0; i < TICK_SMOOTH_VALUE; i++)
372      this->dtS += this->frameTimes[i];
373    this->dtS = this->dtS / TICK_SMOOTH_VALUE / 1000.0f * speed;
374
375    // TICK everything
376    for (i = 0; i < this->dataTank->tickLists.size(); ++i)
377      this->tick(this->dataTank->objectManager->getObjectList(this->dataTank->tickLists[i]), this->dtS);
378
379    /* update tick the rest */
380    this->dataTank->localCamera->tick(this->dtS);
381    AnimationPlayer::getInstance()->tick(this->dtS);
382    PhysicsEngine::getInstance()->tick(this->dtS);
383
384    GraphicsEngine::getInstance()->tick(this->dtS);
385
386    if( likely(this->dataTank->gameRule != NULL))
387      this->dataTank->gameRule->tick(this->dtS);
388  }
389  this->lastFrame = currentFrame;
390}
391
392
393/**
394 *  this function gives the world a consistant state
395 *
396 * after ticking (updating the world state) this will give a constistant
397 * state to the whole system.
398 */
399void GameWorld::update()
400{
401  GraphicsEngine::getInstance()->update(this->dtS);
402  PNode::getNullParent()->updateNode (this->dtS);
403  OrxSound::SoundEngine::getInstance()->update();
404}
405
406
407/**
408 * kicks the CDEngine to detect the collisions between the object groups in the world
409 */
410void GameWorld::collide()
411{
412  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
413      this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ));
414  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
415      this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ));
416  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
417      this->dataTank->objectManager->getObjectList(OM_GROUP_01));
418
419  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
420      this->dataTank->objectManager->getObjectList(OM_COMMON));
421  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
422      this->dataTank->objectManager->getObjectList(OM_COMMON));
423
424}
425
426
427/**
428 *  check the game rules: winning conditions, etc.
429 *
430 */
431void GameWorld::checkGameRules()
432{
433  if( this->gameRules)
434    this->gameRules->tick(this->dtS);
435}
436
437
438/**
439 *  render the current frame
440 *
441 * clear all buffers and draw the world
442 */
443void GameWorld::display ()
444{
445  // render the reflection texture
446  this->renderPassReflection();
447  // redner the refraction texture
448  this->renderPassRefraction();
449  // render all
450  this->renderPassAll();
451
452  // flip buffers
453  GraphicsEngine::swapBuffers();
454}
455
456
457/**
458 * @brief draws all entities in the list drawList
459 * @param drawList the List of entities to draw.
460 */
461void GameWorld::drawEntityList(const ObjectManager::EntityList& drawList) const
462{
463  ObjectManager::EntityList::const_iterator entity;
464  for (entity = drawList.begin(); entity != drawList.end(); entity++)
465    if ((*entity)->isVisible())
466      (*entity)->draw();
467}
468
469
470
471/**
472 * reflection rendering for water surfaces
473 */
474void GameWorld::renderPassReflection()
475{
476    // clear buffer
477  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
478
479  const std::list<BaseObject*>* reflectedWaters;
480  MappedWater* mw;
481
482  if( (reflectedWaters = ClassList::getList(CL_MAPPED_WATER)) != NULL)
483  {
484    std::list<BaseObject*>::const_iterator it;
485    for (it = reflectedWaters->begin(); it != reflectedWaters->end(); it++)
486    {
487      mw =  dynamic_cast<MappedWater*>(*it);
488
489      // prepare for reflection rendering
490      mw->activateReflection();
491
492      //camera and light
493      this->dataTank->localCamera->apply ();
494      this->dataTank->localCamera->project ();
495      LightManager::getInstance()->draw();
496      // draw everything to be included in the reflection
497      for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
498        this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
499
500      // clean up from reflection rendering
501      mw->deactivateReflection();
502    }
503  }
504
505}
506
507
508/**
509 *  refraction rendering for water surfaces
510 */
511void GameWorld::renderPassRefraction()
512{}
513
514
515/**
516 *  this render pass renders the whole wolrd
517 */
518void GameWorld::renderPassAll()
519{
520  // clear buffer
521  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
522  GraphicsEngine* engine = GraphicsEngine::getInstance();
523
524  // set camera
525  this->dataTank->localCamera->apply ();
526  this->dataTank->localCamera->project ();
527  LightManager::getInstance()->draw();
528
529  /* draw all WorldEntiy groups */
530  for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
531    this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
532
533
534  if( unlikely( this->showBV))
535  {
536    CDEngine* engine = CDEngine::getInstance();
537    for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
538      engine->drawBV(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
539  }
540
541  if( unlikely(this->showPNodes))
542    PNode::getNullParent()->debugDraw(0);
543
544  engine->draw();
545
546  // draw the game ruls
547  if( likely(this->dataTank->gameRule != NULL))
548    this->dataTank->gameRule->draw();
549}
550
551
552/**
553 *  shows the loading screen
554 */
555void GameWorld::displayLoadScreen ()
556{
557  PRINTF(3)("GameWorld::displayLoadScreen - start\n");
558  this->dataTank->glmis = new GLMenuImageScreen();
559  this->dataTank->glmis->setMaximum(8);
560  PRINTF(3)("GameWorld::displayLoadScreen - end\n");
561}
562
563
564/**
565 *  removes the loadscreen, and changes over to the game
566 */
567void GameWorld::releaseLoadScreen ()
568{
569  PRINTF(3)("GameWorld::releaseLoadScreen - start\n");
570  this->dataTank->glmis->setValue(this->dataTank->glmis->getMaximum());
571  PRINTF(3)("GameWorld::releaseLoadScreen - end\n");
572}
573
574
575
576/**
577 * @brief toggles the PNode visibility in the world (drawn as boxes)
578 */
579void GameWorld::togglePNodeVisibility()
580{
581  if (State::getCurrentStoryEntity() != NULL &&
582      State::getCurrentStoryEntity()->isA(CL_GAME_WORLD))
583  {
584    dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showPNodes = !dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showPNodes;
585  }
586  else
587    PRINTF(2)("The Current Story entity is not a GameWorld\n");
588};
589
590
591/**
592 * @brief toggles the bounding volume (BV) visibility
593*/
594void GameWorld::toggleBVVisibility()
595{
596  if (State::getCurrentStoryEntity() != NULL &&
597      State::getCurrentStoryEntity()->isA(CL_GAME_WORLD))
598  {
599    dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showBV = !dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showBV;
600  }
601  else
602    PRINTF(2)("The Current Story entity is not a GameWorld\n");
603};
604
Note: See TracBrowser for help on using the repository browser.