Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/story_entities/game_world_data.cc @ 6944

Last change on this file since 6944 was 6863, checked in by patrick, 19 years ago

trunk: fixed the segfault problem, when restarting world

File size: 7.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*/
14
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD
17
18#include "game_world_data.h"
19
20#include "resource_manager.h"
21#include "state.h"
22#include "class_list.h"
23#include "substring.h"
24
25#include "game_loader.h"
26
27#include "p_node.h"
28#include "world_entity.h"
29#include "player.h"
30#include "camera.h"
31#include "environment.h"
32#include "terrain.h"
33#include "test_entity.h"
34#include "terrain.h"
35#include "skybox.h"
36#include "md2Model.h"
37#include "world_entities/projectiles/projectile.h"
38#include "npcs/npc_test1.h"
39#include "playable.h"
40
41#include "light.h"
42
43#include "factory.h"
44#include "fast_factory.h"
45#include "load_param.h"
46
47#include "graphics_engine.h"
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#include "fields.h"
54
55#include "glmenu_imagescreen.h"
56
57#include "animation_player.h"
58#include "animation3d.h"
59
60#include "ogg_player.h"
61#include "shader.h"
62
63
64using namespace std;
65
66
67/**
68 * constructor of the GameWorldData
69 */
70GameWorldData::GameWorldData()
71{
72  this->setClassID(CL_GAME_WORLD_DATA, "GameWorldData");
73
74  this->glmis = NULL;
75
76  this->localCamera = NULL;
77  this->localPlayer = NULL;
78  this->sky = NULL;
79  this->terrain = NULL;
80
81  this->music = NULL;
82  this->objectManager = NULL;
83}
84
85
86/**
87 * destructor for the GameWorldData
88 */
89GameWorldData::~GameWorldData()
90{}
91
92
93
94/**
95 *  initialize the GameWorldData
96 */
97ErrorMessage GameWorldData::init()
98{
99  this->objectManager = new ObjectManager();
100  State::setObjectManager(this->objectManager);
101
102  PNode::getNullParent();
103  this->localCamera = new Camera();
104  this->localCamera->setName ("GameWorld-Camera");
105  State::setCamera(this->localCamera, this->localCamera->getTarget());
106
107  LightManager::getInstance();
108
109  GraphicsEngine::getInstance()->displayFPS(true);
110
111  /* initialize some graphics engines and graphical elements */
112  AnimationPlayer::getInstance();
113  PhysicsEngine::getInstance();
114}
115
116
117/**
118 *  loads the data from the xml file
119 * @param root reference to the xml root element
120 */
121ErrorMessage GameWorldData::loadData(TiXmlElement* root)
122{
123  // load the parameters
124  // name
125  const char* string = grabParameter( root, "name");
126  if( string == NULL)
127  {
128    PRINTF(2)("GameWorld is missing a proper 'name'\n");
129    this->setName("Unknown");
130  }
131  else
132    this->setName(string);
133
134  this->loadGUI(root);
135  this->loadWorldEntities(root);
136  this->loadScene(root);
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();
148}
149
150
151/**
152 *  loads the GUI data
153 * @param root reference to the xml root element
154 */
155ErrorMessage GameWorldData::loadGUI(TiXmlElement* root)
156{
157  TiXmlElement* element = root->FirstChildElement("LoadScreen");
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);
164//     this->glmis->draw();
165  }
166  else
167  {
168    this->glmis->loadParams(element);
169//     this->glmis->draw();
170  }
171  this->glmis->draw();
172}
173
174
175/**
176 *  unloads the GUI data
177 */
178ErrorMessage GameWorldData::unloadGUI()
179{
180  delete this->glmis;
181}
182
183
184/**
185 *  loads the world entities from the xml file
186 * @param root reference to the xml root parameter
187 */
188ErrorMessage GameWorldData::loadWorldEntities(TiXmlElement* root)
189{
190  TiXmlElement* element = root->FirstChildElement("WorldEntities");
191
192  if( element == NULL)
193  {
194    PRINTF(1)("GameWorld is missing 'WorldEntities'\n");
195  }
196  else
197  {
198    element = element->FirstChildElement();
199    // load Players/Objects/Whatever
200    PRINTF(4)("Loading WorldEntities\n");
201    while( element != NULL)
202    {
203      BaseObject* created = Factory::fabricate(element);
204      if( created != NULL )
205        PRINTF(4)("Created a %s: %s\n", created->getClassName(), created->getName());
206
207      //todo do this more elegant
208      if( element->Value() != NULL && !strcmp( element->Value(), "SkyBox"))
209      {
210        this->sky = dynamic_cast<WorldEntity*>(created);
211        State::setSkyBox(dynamic_cast<SkyBox*>(this->sky));
212      }
213      if( element->Value() != NULL && !strcmp( element->Value(), "Terrain"))
214      {
215        this->terrain = dynamic_cast<Terrain*>(created);
216        CDEngine::getInstance()->setTerrain(terrain);
217      }
218      element = element->NextSiblingElement();
219      this->glmis->step(); //! @todo temporary
220    }
221    PRINTF(4)("Done loading WorldEntities\n");
222  }
223
224  // Create a Player
225  this->localPlayer = new Player();
226
227  Playable* playable;
228  const list<BaseObject*>* playableList = ClassList::getList(CL_PLAYABLE);
229  if (playableList != NULL)
230  {
231    playable = dynamic_cast<Playable*>(playableList->front());
232    this->localPlayer->setControllable(playable);
233  }
234
235  /* init the pnode tree */
236  PNode::getNullParent()->init();
237}
238
239
240/**
241 *  unloads the world entities
242 */
243ErrorMessage GameWorldData::unloadWorldEntities()
244{
245  FastFactory::flushAll(true);
246
247  // erease everything that is left.
248  // delete PNode::getNullParent(); // not needed as this is also done in the next step (and also much cleaner)
249
250  //secondary cleanup of PNodes;
251  const std::list<BaseObject*>* nodeList = ClassList::getList(CL_PARENT_NODE);
252  if (nodeList != NULL)
253    while (!nodeList->empty())
254      delete nodeList->front();
255
256  /* remove the player object */
257  if( this->localPlayer)
258    delete this->localPlayer;
259
260  // unload the resources !!
261  ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL);
262
263  if (State::getObjectManager() == this->objectManager)
264  {
265    State::setObjectManager(NULL);
266    delete this->objectManager;
267  }
268
269  if(State::getSkyBox())
270    State::setSkyBox(NULL);
271
272  glmis = NULL;
273  localCamera = NULL;
274  localPlayer = NULL;
275  sky = NULL;
276  terrain = NULL;
277  music = NULL;
278  objectManager = NULL;
279}
280
281
282/**
283 *  loads the scene data
284 * @param root reference to the xml root element
285 */
286ErrorMessage GameWorldData::loadScene(TiXmlElement* root)
287{
288  LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams);
289  LoadParamXML(root, "GraphicsEngine", GraphicsEngine::getInstance(), GraphicsEngine, loadParams);
290
291  LoadParam(root, "Music", this, GameWorldData, setSoundTrack);
292
293//  LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams);
294  //LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams);
295
296  this->localCamera->setClipRegion(1, 10000.0);
297  if( this->sky != NULL)
298    this->localCamera->addChild(this->sky);
299  SoundEngine::getInstance()->setListener(this->localCamera);
300}
301
302
303/**
304 *  unloads the scene data
305 */
306ErrorMessage GameWorldData::unloadScene()
307{
308  /* delete some garphics and scene eingines */
309  delete LightManager::getInstance();
310  delete AnimationPlayer::getInstance();
311  delete PhysicsEngine::getInstance();
312
313  if (this->music != NULL)
314    delete this->music;
315  this->music = NULL;
316  /* stop the sound eninge */
317  SoundEngine::getInstance()->flushAllBuffers();
318  SoundEngine::getInstance()->flushAllSources();
319
320  /* unload the shaders */
321  Shader::suspendShader();
322}
323
324
325void GameWorldData::setSoundTrack(const char* name)
326{
327  if (this->music != NULL)
328    delete this->music;
329
330  PRINTF(3)("Setting Sound Track to %s\n", name);
331  this->music = (OggPlayer*)ResourceManager::getInstance()->load(name, OGG, RP_LEVEL);
332}
333
334
Note: See TracBrowser for help on using the repository browser.