Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/camera/src/story_entities/game_world_data.cc @ 10272

Last change on this file since 10272 was 10272, checked in by gfilip, 17 years ago

evrything finally solved

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"
[10272]32#include "cameraman.h"
[7287]33#include "terrain.h"
[7286]34#include "skybox.h"
[8490]35#include "md2/md2Model.h"
[7287]36#include "world_entities/projectiles/projectile.h"
37#include "npcs/npc_test1.h"
[6402]38#include "playable.h"
39
40#include "light.h"
41
[7193]42#include "util/loading/factory.h"
[9869]43#include "loading/fast_factory.h"
44#include "util/loading/load_param_xml.h"
[6402]45
46#include "graphics_engine.h"
[9869]47#include "graphics_effect.h"
48#include "weather_effects/atmospheric_engine.h"
[6402]49#include "event_handler.h"
50#include "sound_engine.h"
51#include "cd_engine.h"
52#include "network_manager.h"
53#include "physics_engine.h"
54
55#include "glmenu_imagescreen.h"
56
[7020]57#include "game_rules.h"
58
[6402]59#include "ogg_player.h"
60#include "shader.h"
61
62
[9869]63ObjectListDefinition(GameWorldData);
[6402]64/**
65 * constructor of the GameWorldData
66 */
67GameWorldData::GameWorldData()
68{
[9869]69  this->registerObject(this, GameWorldData::_objectList);
[6402]70
71  this->glmis = NULL;
72
[10259]73
[6402]74  this->localPlayer = NULL;
75  this->sky = NULL;
76  this->terrain = NULL;
77
78  this->music = NULL;
79  this->objectManager = NULL;
[7035]80  this->gameRule = NULL;
[6402]81}
82
83
84/**
85 * destructor for the GameWorldData
86 */
87GameWorldData::~GameWorldData()
[6404]88{}
[6402]89
90
91
92/**
93 *  initialize the GameWorldData
94 */
95ErrorMessage GameWorldData::init()
96{
97  this->objectManager = new ObjectManager();
98  State::setObjectManager(this->objectManager);
99
100  PNode::getNullParent();
[10259]101  Camera* localCamera = new Camera();
[10272]102    localCamera->setName ("GameWorld-Camera");
[10259]103  State::setCamera(localCamera, localCamera->getTarget());
[10272]104  cameraman* camMan = new cameraman();
105  State::setCameraman(camMan);
[6402]106  LightManager::getInstance();
107
[8975]108//  GraphicsEngine::getInstance()->displayFPS(true);
[8717]109
110  return ErrorMessage();
[6402]111}
112
113
114/**
115 *  loads the data from the xml file
116 * @param root reference to the xml root element
117 */
[7370]118ErrorMessage GameWorldData::loadData(const TiXmlElement* root)
[6402]119{
[9869]120  assert (root != NULL);
[6402]121  // load the parameters
122  // name
[9869]123  std::string string = LoadParamBase::grabParameter( root, "name");
[7221]124  if( string.empty() )
[6402]125  {
126    PRINTF(2)("GameWorld is missing a proper 'name'\n");
127    this->setName("Unknown");
128  }
129  else
[7221]130    this->setName(string.c_str());
[6402]131
132  this->loadGUI(root);
133  this->loadWorldEntities(root);
134  this->loadScene(root);
[8717]135
136  return ErrorMessage();
[6402]137}
138
139
140/**
141 *  unloads the data from the xml file
142 */
143ErrorMessage GameWorldData::unloadData()
144{
145  this->unloadGUI();
146  this->unloadWorldEntities();
147  this->unloadScene();
[8717]148
149  return ErrorMessage();
[6402]150}
151
152
153/**
[7370]154 * @brief loads the GUI data
[6402]155 * @param root reference to the xml root element
156 */
[7370]157ErrorMessage GameWorldData::loadGUI(const TiXmlElement* root)
[6402]158{
[7370]159  const TiXmlElement* element = root->FirstChildElement("LoadScreen");
[6402]160  if( element == NULL)
161  {
162    PRINTF(2)("no LoadScreen specified, loading default\n");
163
164    glmis->setBackgroundImage("pictures/load_screen.jpg");
165    this->glmis->setMaximum(8);
[6988]166    //     this->glmis->draw();
[6402]167  }
168  else
169  {
170    this->glmis->loadParams(element);
[6988]171    //     this->glmis->draw();
[6402]172  }
173  this->glmis->draw();
[8717]174
175  return ErrorMessage();
[6402]176}
177
178
179/**
[7370]180 * @brief unloads the GUI data
[6402]181 */
182ErrorMessage GameWorldData::unloadGUI()
183{
184  delete this->glmis;
[8717]185
186  return ErrorMessage();
[6402]187}
188
189
190/**
[7370]191 * @brief loads the world entities from the xml file
[6402]192 * @param root reference to the xml root parameter
193 */
[7370]194ErrorMessage GameWorldData::loadWorldEntities(const TiXmlElement* root)
[6402]195{
[7370]196  const TiXmlElement* element = root->FirstChildElement("WorldEntities");
[9406]197  bool mouseCaptured = EventHandler::getInstance()->grabbedEvents();
198  EventHandler::getInstance()->grabEvents(false);
[6402]199
200  if( element == NULL)
201  {
202    PRINTF(1)("GameWorld is missing 'WorldEntities'\n");
203  }
204  else
205  {
206    element = element->FirstChildElement();
207    // load Players/Objects/Whatever
208    PRINTF(4)("Loading WorldEntities\n");
209    while( element != NULL)
210    {
211      BaseObject* created = Factory::fabricate(element);
212      if( created != NULL )
[9406]213        PRINTF(4)("Created a %s: %s\n", created->getClassCName(), created->getCName());
[6402]214
215      //todo do this more elegant
[9869]216      if( element->Value() == "SkyBox" && created->isA(SkyBox::staticClassID()))
[6771]217      {
[6402]218        this->sky = dynamic_cast<WorldEntity*>(created);
[6771]219        State::setSkyBox(dynamic_cast<SkyBox*>(this->sky));
220      }
[9869]221      if( element->Value() == "Terrain" && created->isA(Terrain::staticClassID()))
[6402]222      {
223        this->terrain = dynamic_cast<Terrain*>(created);
224        CDEngine::getInstance()->setTerrain(terrain);
225      }
226      element = element->NextSiblingElement();
227      this->glmis->step(); //! @todo temporary
228    }
229    PRINTF(4)("Done loading WorldEntities\n");
230  }
231
232  // Create a Player
233  this->localPlayer = new Player();
[7076]234  State::setPlayer(this->localPlayer);
[6402]235
[9869]236  if (!Playable::objectList().empty())
[6402]237  {
[7370]238    /// TODO Make this also loadable
[9869]239    this->localPlayer->setPlayable(Playable::objectList().front());
[6402]240  }
241
[7370]242  // Fill the EntityLists. Tick then Draw:
243  this->tickLists.push_back(OM_DEAD_TICK);
244  this->tickLists.push_back(OM_ENVIRON);
245  this->tickLists.push_back(OM_COMMON);
246  this->tickLists.push_back(OM_GROUP_00);
247  this->tickLists.push_back(OM_GROUP_00_PROJ);
248  this->tickLists.push_back(OM_GROUP_01);
249  this->tickLists.push_back(OM_GROUP_01_PROJ);
[9235]250  this->tickLists.push_back(OM_GROUP_02);
[7370]251
252  this->drawLists.push_back(OM_ENVIRON_NOTICK);
253  this->drawLists.push_back(OM_ENVIRON);
254  this->drawLists.push_back(OM_GROUP_00);
255  this->drawLists.push_back(OM_GROUP_00_PROJ);
256  this->drawLists.push_back(OM_GROUP_01);
257  this->drawLists.push_back(OM_GROUP_01_PROJ);
[9235]258  this->drawLists.push_back(OM_GROUP_02);
259  this->drawLists.push_back(OM_COMMON);
[7370]260
[6402]261  /* init the pnode tree */
262  PNode::getNullParent()->init();
[8717]263
[9406]264  EventHandler::getInstance()->grabEvents(mouseCaptured);
265
[8717]266  return ErrorMessage();
[6402]267}
268
269
270/**
[6404]271 *  unloads the world entities
[6402]272 */
273ErrorMessage GameWorldData::unloadWorldEntities()
274{
275  FastFactory::flushAll(true);
[7029]276  GraphicsEngine::getInstance()->displayFPS(false);
[6402]277  // erease everything that is left.
278  //secondary cleanup of PNodes;
[9869]279  while (!PNode::objectList().empty())
280    delete PNode::objectList().front();
281
[6402]282  /* remove the player object */
283  if( this->localPlayer)
284    delete this->localPlayer;
[7126]285  State::setPlayer(NULL);
[7311]286  this->localPlayer = NULL;
287  State::setCamera(NULL, NULL);
288  this->sky = NULL;
289  this->terrain = NULL;
[6402]290
[9869]291  while (!GraphicsEffect::objectList().empty())
292    delete GraphicsEffect::objectList().front();
[6981]293
294
[9869]295  while (!Element2D::objectList().empty())
296    delete Element2D::objectList().front();
[6981]297
[7370]298  // At this Point all the WorldEntites should be unloaded.
299  this->tickLists.clear();
300  this->drawLists.clear();
[6981]301
[7370]302  // unload the resources loaded in this Level !!
[9869]303  /// FIXME TODO HACK!!
304//  ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL);
[6402]305
306  if (State::getObjectManager() == this->objectManager)
307  {
308    State::setObjectManager(NULL);
309    delete this->objectManager;
310  }
[7311]311  this->objectManager = NULL;
[6863]312
[6771]313  if(State::getSkyBox())
314    State::setSkyBox(NULL);
[6863]315
[7311]316  this->glmis = NULL;
[8717]317
318  return ErrorMessage();
[6402]319}
320
321
322/**
[7370]323 * @brief loads the scene data
[6402]324 * @param root reference to the xml root element
325 */
[7370]326ErrorMessage GameWorldData::loadScene(const TiXmlElement* root)
[6402]327{
328  LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams);
[6815]329  LoadParamXML(root, "GraphicsEngine", GraphicsEngine::getInstance(), GraphicsEngine, loadParams);
[7810]330  LoadParamXML(root, "AtmosphericEngine", AtmosphericEngine::getInstance(), AtmosphericEngine, loadParams);
[6402]331
[6827]332  LoadParam(root, "Music", this, GameWorldData, setSoundTrack);
333
[7020]334  LoadParamXML(root, "GameRule", this, GameWorldData, loadGameRule);
335
[10272]336  LoadParam(root, "clip-region", State::getCameraman(), cameraman, setClipRegion);
[7020]337
[9235]338
[7020]339  //LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams);
[6402]340  //LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams);
341
[8717]342
[10259]343  OrxSound::SoundEngine::getInstance()->setListener(State::getCamera());
344
[8717]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.