Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6626 was 6626, checked in by bensch, 18 years ago

trunk: segfault prevention

File size: 7.0 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 "md2Model.h"
36#include "world_entities/projectiles/projectile.h"
37#include "npcs/npc_test1.h"
38#include "playable.h"
39
40#include "light.h"
41
42#include "factory.h"
43#include "fast_factory.h"
44#include "load_param.h"
45
46#include "graphics_engine.h"
47#include "event_handler.h"
48#include "sound_engine.h"
49#include "cd_engine.h"
50#include "network_manager.h"
51#include "physics_engine.h"
52#include "fields.h"
53
54#include "glmenu_imagescreen.h"
55
56#include "animation_player.h"
57#include "animation3d.h"
58
59#include "ogg_player.h"
60#include "shader.h"
61
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->localPlayer = NULL;
75  this->localCamera = NULL;
76
77  this->glmis = NULL;
78
79  this->localCamera = NULL;
80  this->localPlayer = NULL;
81  this->sky = NULL;
82  this->terrain = NULL;
83
84  this->music = NULL;
85  this->objectManager = NULL;
86}
87
88
89/**
90 * destructor for the GameWorldData
91 */
92GameWorldData::~GameWorldData()
93{}
94
95
96
97/**
98 *  initialize the GameWorldData
99 */
100ErrorMessage GameWorldData::init()
101{
102  this->objectManager = new ObjectManager();
103  State::setObjectManager(this->objectManager);
104
105  PNode::getNullParent();
106  this->localCamera = new Camera();
107  this->localCamera->setName ("GameWorld-Camera");
108  State::setCamera(this->localCamera, this->localCamera->getTarget());
109
110  LightManager::getInstance();
111
112  GraphicsEngine::getInstance()->displayFPS(true);
113
114  /* initialize some graphics engines and graphical elements */
115  AnimationPlayer::getInstance();
116  PhysicsEngine::getInstance();
117}
118
119
120/**
121 *  loads the data from the xml file
122 * @param root reference to the xml root element
123 */
124ErrorMessage GameWorldData::loadData(TiXmlElement* root)
125{
126  // load the parameters
127  // name
128  const char* string = grabParameter( root, "name");
129  if( string == NULL)
130  {
131    PRINTF(2)("GameWorld is missing a proper 'name'\n");
132    this->setName("Unknown");
133  }
134  else
135    this->setName(string);
136
137  this->loadGUI(root);
138  this->loadWorldEntities(root);
139  this->loadScene(root);
140}
141
142
143/**
144 *  unloads the data from the xml file
145 */
146ErrorMessage GameWorldData::unloadData()
147{
148  this->unloadGUI();
149  this->unloadWorldEntities();
150  this->unloadScene();
151}
152
153
154/**
155 *  loads the GUI data
156 * @param root reference to the xml root element
157 */
158ErrorMessage GameWorldData::loadGUI(TiXmlElement* root)
159{
160  TiXmlElement* element = root->FirstChildElement("LoadScreen");
161  if( element == NULL)
162  {
163    PRINTF(2)("no LoadScreen specified, loading default\n");
164
165    glmis->setBackgroundImage("pictures/load_screen.jpg");
166    this->glmis->setMaximum(8);
167//     this->glmis->draw();
168  }
169  else
170  {
171    this->glmis->loadParams(element);
172//     this->glmis->draw();
173  }
174  this->glmis->draw();
175}
176
177
178/**
179 *  unloads the GUI data
180 */
181ErrorMessage GameWorldData::unloadGUI()
182{
183  delete this->glmis;
184}
185
186
187/**
188 *  loads the world entities from the xml file
189 * @param root reference to the xml root parameter
190 */
191ErrorMessage GameWorldData::loadWorldEntities(TiXmlElement* root)
192{
193  TiXmlElement* element = root->FirstChildElement("WorldEntities");
194
195  if( element == NULL)
196  {
197    PRINTF(1)("GameWorld is missing 'WorldEntities'\n");
198  }
199  else
200  {
201    element = element->FirstChildElement();
202    // load Players/Objects/Whatever
203    PRINTF(4)("Loading WorldEntities\n");
204    while( element != NULL)
205    {
206      BaseObject* created = Factory::fabricate(element);
207      if( created != NULL )
208        printf("Created a %s: %s\n", created->getClassName(), created->getName());
209
210      //todo do this more elegant
211      if( element->Value() != NULL && !strcmp( element->Value(), "SkyBox"))
212        this->sky = dynamic_cast<WorldEntity*>(created);
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
270
271/**
272 *  loads the scene data
273 * @param root reference to the xml root element
274 */
275ErrorMessage GameWorldData::loadScene(TiXmlElement* root)
276{
277  LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams);
278
279//  LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams);
280  //LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams);
281
282  this->localCamera->setClipRegion(1, 10000.0);
283  if( this->sky != NULL)
284    this->localCamera->addChild(this->sky);
285
286  /* sound loading */
287  this->music = NULL;
288  //(OggPlayer*)ResourceManager::getInstance()->load("sound/00-luke_grey_-_hypermode.ogg", OGG, RP_LEVEL);
289  //music->playback();
290  SoundEngine::getInstance()->setListener(this->localCamera);
291}
292
293
294/**
295 *  unloads the scene data
296 */
297ErrorMessage GameWorldData::unloadScene()
298{
299  /* delete some garphics and scene eingines */
300  delete LightManager::getInstance();
301  delete AnimationPlayer::getInstance();
302  delete PhysicsEngine::getInstance();
303
304  /* stop the sound eninge */
305  SoundEngine::getInstance()->flushAllBuffers();
306  SoundEngine::getInstance()->flushAllSources();
307
308  /* unload the shaders */
309  Shader::suspendShader();
310}
311
Note: See TracBrowser for help on using the repository browser.