Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/coll_rect.merge/src/story_entities/game_world_data.cc @ 10010

Last change on this file since 10010 was 10010, checked in by patrick, 17 years ago

merged the temp branch

File size: 10.2 KB
RevLine 
[6402]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*/
14
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD
17
[7036]18#include <fstream>
19#include <iostream>
20
[6402]21#include "game_world_data.h"
22
23#include "state.h"
24#include "substring.h"
25
[7193]26#include "util/loading/game_loader.h"
[9869]27#include "util/loading/resource_manager.h"
[6402]28
29#include "world_entity.h"
30#include "player.h"
31#include "camera.h"
[7287]32#include "terrain.h"
[7286]33#include "skybox.h"
[8490]34#include "md2/md2Model.h"
[7287]35#include "world_entities/projectiles/projectile.h"
36#include "npcs/npc_test1.h"
[6402]37#include "playable.h"
38
39#include "light.h"
40
[7193]41#include "util/loading/factory.h"
[9869]42#include "loading/fast_factory.h"
43#include "util/loading/load_param_xml.h"
[6402]44
45#include "graphics_engine.h"
[9869]46#include "graphics_effect.h"
47#include "weather_effects/atmospheric_engine.h"
[6402]48#include "event_handler.h"
49#include "sound_engine.h"
50#include "cd_engine.h"
51#include "network_manager.h"
52#include "physics_engine.h"
53
54#include "glmenu_imagescreen.h"
55
[7020]56#include "game_rules.h"
57
[6402]58#include "ogg_player.h"
59#include "shader.h"
60
61
[9869]62ObjectListDefinition(GameWorldData);
[6402]63/**
64 * constructor of the GameWorldData
65 */
66GameWorldData::GameWorldData()
67{
[9869]68  this->registerObject(this, GameWorldData::_objectList);
[6402]69
70  this->glmis = NULL;
71
72  this->localCamera = NULL;
73  this->localPlayer = NULL;
74  this->sky = NULL;
75  this->terrain = NULL;
76
77  this->music = NULL;
78  this->objectManager = NULL;
[7035]79  this->gameRule = NULL;
[6402]80}
81
82
83/**
84 * destructor for the GameWorldData
85 */
86GameWorldData::~GameWorldData()
[6404]87{}
[6402]88
89
90
91/**
92 *  initialize the GameWorldData
93 */
94ErrorMessage GameWorldData::init()
95{
96  this->objectManager = new ObjectManager();
97  State::setObjectManager(this->objectManager);
98
99  PNode::getNullParent();
100  this->localCamera = new Camera();
101  this->localCamera->setName ("GameWorld-Camera");
102  State::setCamera(this->localCamera, this->localCamera->getTarget());
103
104  LightManager::getInstance();
105
[8975]106//  GraphicsEngine::getInstance()->displayFPS(true);
[8717]107
108  return ErrorMessage();
[6402]109}
110
111
112/**
113 *  loads the data from the xml file
114 * @param root reference to the xml root element
115 */
[7370]116ErrorMessage GameWorldData::loadData(const TiXmlElement* root)
[6402]117{
[9869]118  assert (root != NULL);
[6402]119  // load the parameters
120  // name
[9869]121  std::string string = LoadParamBase::grabParameter( root, "name");
[7221]122  if( string.empty() )
[6402]123  {
124    PRINTF(2)("GameWorld is missing a proper 'name'\n");
125    this->setName("Unknown");
126  }
127  else
[7221]128    this->setName(string.c_str());
[6402]129
130  this->loadGUI(root);
131  this->loadWorldEntities(root);
132  this->loadScene(root);
[8717]133
134  return ErrorMessage();
[6402]135}
136
137
138/**
139 *  unloads the data from the xml file
140 */
141ErrorMessage GameWorldData::unloadData()
142{
143  this->unloadGUI();
144  this->unloadWorldEntities();
145  this->unloadScene();
[8717]146
147  return ErrorMessage();
[6402]148}
149
150
151/**
[7370]152 * @brief loads the GUI data
[6402]153 * @param root reference to the xml root element
154 */
[7370]155ErrorMessage GameWorldData::loadGUI(const TiXmlElement* root)
[6402]156{
[7370]157  const TiXmlElement* element = root->FirstChildElement("LoadScreen");
[6402]158  if( element == NULL)
159  {
160    PRINTF(2)("no LoadScreen specified, loading default\n");
161
162    glmis->setBackgroundImage("pictures/load_screen.jpg");
163    this->glmis->setMaximum(8);
[6988]164    //     this->glmis->draw();
[6402]165  }
166  else
167  {
168    this->glmis->loadParams(element);
[6988]169    //     this->glmis->draw();
[6402]170  }
171  this->glmis->draw();
[8717]172
173  return ErrorMessage();
[6402]174}
175
176
177/**
[7370]178 * @brief unloads the GUI data
[6402]179 */
180ErrorMessage GameWorldData::unloadGUI()
181{
182  delete this->glmis;
[8717]183
184  return ErrorMessage();
[6402]185}
186
187
188/**
[7370]189 * @brief loads the world entities from the xml file
[6402]190 * @param root reference to the xml root parameter
191 */
[7370]192ErrorMessage GameWorldData::loadWorldEntities(const TiXmlElement* root)
[6402]193{
[7370]194  const TiXmlElement* element = root->FirstChildElement("WorldEntities");
[9406]195  bool mouseCaptured = EventHandler::getInstance()->grabbedEvents();
196  EventHandler::getInstance()->grabEvents(false);
[6402]197
198  if( element == NULL)
199  {
200    PRINTF(1)("GameWorld is missing 'WorldEntities'\n");
201  }
202  else
203  {
204    element = element->FirstChildElement();
205    // load Players/Objects/Whatever
206    PRINTF(4)("Loading WorldEntities\n");
207    while( element != NULL)
208    {
209      BaseObject* created = Factory::fabricate(element);
210      if( created != NULL )
[9406]211        PRINTF(4)("Created a %s: %s\n", created->getClassCName(), created->getCName());
[6402]212
213      //todo do this more elegant
[9869]214      if( element->Value() == "SkyBox" && created->isA(SkyBox::staticClassID()))
[6771]215      {
[6402]216        this->sky = dynamic_cast<WorldEntity*>(created);
[6771]217        State::setSkyBox(dynamic_cast<SkyBox*>(this->sky));
218      }
[9869]219      if( element->Value() == "Terrain" && created->isA(Terrain::staticClassID()))
[6402]220      {
221        this->terrain = dynamic_cast<Terrain*>(created);
222        CDEngine::getInstance()->setTerrain(terrain);
223      }
224      element = element->NextSiblingElement();
225      this->glmis->step(); //! @todo temporary
226    }
227    PRINTF(4)("Done loading WorldEntities\n");
228  }
229
230  // Create a Player
231  this->localPlayer = new Player();
[7076]232  State::setPlayer(this->localPlayer);
[6402]233
[9869]234  if (!Playable::objectList().empty())
[6402]235  {
[7370]236    /// TODO Make this also loadable
[9869]237    this->localPlayer->setPlayable(Playable::objectList().front());
[6402]238  }
239
[7370]240  // Fill the EntityLists. Tick then Draw:
241  this->tickLists.push_back(OM_DEAD_TICK);
242  this->tickLists.push_back(OM_ENVIRON);
243  this->tickLists.push_back(OM_COMMON);
244  this->tickLists.push_back(OM_GROUP_00);
245  this->tickLists.push_back(OM_GROUP_00_PROJ);
246  this->tickLists.push_back(OM_GROUP_01);
247  this->tickLists.push_back(OM_GROUP_01_PROJ);
[9235]248  this->tickLists.push_back(OM_GROUP_02);
[7370]249
250  this->drawLists.push_back(OM_ENVIRON_NOTICK);
251  this->drawLists.push_back(OM_ENVIRON);
252  this->drawLists.push_back(OM_GROUP_00);
253  this->drawLists.push_back(OM_GROUP_00_PROJ);
254  this->drawLists.push_back(OM_GROUP_01);
255  this->drawLists.push_back(OM_GROUP_01_PROJ);
[9235]256  this->drawLists.push_back(OM_GROUP_02);
257  this->drawLists.push_back(OM_COMMON);
[7370]258
[6402]259  /* init the pnode tree */
260  PNode::getNullParent()->init();
[8717]261
[9406]262  EventHandler::getInstance()->grabEvents(mouseCaptured);
263
[8717]264  return ErrorMessage();
[6402]265}
266
267
268/**
[6404]269 *  unloads the world entities
[6402]270 */
271ErrorMessage GameWorldData::unloadWorldEntities()
272{
273  FastFactory::flushAll(true);
[7029]274  GraphicsEngine::getInstance()->displayFPS(false);
[6402]275  // erease everything that is left.
276  //secondary cleanup of PNodes;
[9869]277  while (!PNode::objectList().empty())
278    delete PNode::objectList().front();
279
[6402]280  /* remove the player object */
281  if( this->localPlayer)
282    delete this->localPlayer;
[7126]283  State::setPlayer(NULL);
[7311]284  this->localPlayer = NULL;
285  this->localCamera = NULL;
286  State::setCamera(NULL, NULL);
287  this->sky = NULL;
288  this->terrain = NULL;
[6402]289
[9869]290  while (!GraphicsEffect::objectList().empty())
291    delete GraphicsEffect::objectList().front();
[6981]292
293
[9869]294  while (!Element2D::objectList().empty())
295    delete Element2D::objectList().front();
[6981]296
[7370]297  // At this Point all the WorldEntites should be unloaded.
298  this->tickLists.clear();
299  this->drawLists.clear();
[6981]300
[7370]301  // unload the resources loaded in this Level !!
[9869]302  /// FIXME TODO HACK!!
303//  ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL);
[6402]304
305  if (State::getObjectManager() == this->objectManager)
306  {
307    State::setObjectManager(NULL);
308    delete this->objectManager;
309  }
[7311]310  this->objectManager = NULL;
[6863]311
[6771]312  if(State::getSkyBox())
313    State::setSkyBox(NULL);
[6863]314
[7311]315  this->glmis = NULL;
[8717]316
317  return ErrorMessage();
[6402]318}
319
320
321/**
[7370]322 * @brief loads the scene data
[6402]323 * @param root reference to the xml root element
324 */
[7370]325ErrorMessage GameWorldData::loadScene(const TiXmlElement* root)
[6402]326{
327  LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams);
[6815]328  LoadParamXML(root, "GraphicsEngine", GraphicsEngine::getInstance(), GraphicsEngine, loadParams);
[7810]329  LoadParamXML(root, "AtmosphericEngine", AtmosphericEngine::getInstance(), AtmosphericEngine, loadParams);
[6402]330
[6827]331  LoadParam(root, "Music", this, GameWorldData, setSoundTrack);
332
[7020]333  LoadParamXML(root, "GameRule", this, GameWorldData, loadGameRule);
334
[9235]335  LoadParam(root, "clip-region", this->localCamera, Camera, setClipRegion);
[7020]336
[9235]337
[7020]338  //LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams);
[6402]339  //LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams);
340
341  if( this->sky != NULL)
342    this->localCamera->addChild(this->sky);
[7460]343  OrxSound::SoundEngine::getInstance()->setListener(this->localCamera);
[8717]344
345  return ErrorMessage();
[6402]346}
347
348
[7020]349
[6402]350/**
351 *  unloads the scene data
352 */
353ErrorMessage GameWorldData::unloadScene()
354{
355  /* delete some garphics and scene eingines */
356  delete LightManager::getInstance();
[7810]357  delete AtmosphericEngine::getInstance();
[6402]358
[7287]359  if (this->music != NULL)
360    this->setSoundTrack("");
[6402]361
362  /* unload the shaders */
363  Shader::suspendShader();
[7488]364
365  State::setGameRules(NULL);
[8717]366
367  return ErrorMessage();
[6402]368}
369
[6634]370
[7221]371void GameWorldData::setSoundTrack(const std::string& name)
[6634]372{
[6827]373  if (this->music != NULL)
[7287]374    delete this->music;
375  this->music = NULL;
[6827]376
[7221]377  if (!name.empty())
[6987]378  {
[7221]379    PRINTF(3)("Setting Sound Track to %s\n", name.c_str());
[9869]380    std::string oggFile = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(name);
[7460]381    this->music = new OrxSound::OggPlayer(oggFile);
[9019]382    if (this->localPlayer != NULL)
383      this->localPlayer->hud().notifyUser(std::string("Playing SoundTrack: ") + this->music->artist() + " - " + this->music->title());
[6988]384
385    //(OggPlayer*)ResourceManager::getInstance()->load(name, OGG, RP_LEVEL);
386    //assert(this->music->isA(CL_SOUND_OGG_PLAYER));
[6987]387  }
[6634]388}
389
390
[7020]391void GameWorldData::loadGameRule(const TiXmlElement* root)
392{
[7036]393  const TiXmlElement* element = root->FirstChildElement();
394  while( element != NULL)
[7020]395  {
[7461]396    PRINTF(2)("============ GameRules ==\n");
397    PRINTF(2)("creating %s\n", element->Value());
[7036]398    BaseObject* created = Factory::fabricate(element);
[9869]399    if (created != NULL && created->isA(GameRules::staticClassID()))
[7020]400    {
[7036]401      this->gameRule = dynamic_cast<GameRules*>(created);
[7039]402      State::setGameRules(this->gameRule);
[7482]403      // if there is a valid game rule loaded, return because it is not thought to load multiple game rules
[7462]404      return;
[7020]405    }
[7036]406    else
407    {
408      PRINTF(1)("Could not create a %s\n", element->Value());
409      delete created;
410    }
411    element = element->NextSiblingElement();
[7020]412  }
413}
414
415
416
Note: See TracBrowser for help on using the repository browser.