Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: element2d's should be cleanly deleted at the end now :)

File size: 8.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*/
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 "game_rules.h"
61
62#include "ogg_player.h"
63#include "shader.h"
64
65
66using namespace std;
67
68
69/**
70 * constructor of the GameWorldData
71 */
72GameWorldData::GameWorldData()
73{
74  this->setClassID(CL_GAME_WORLD_DATA, "GameWorldData");
75
76  this->glmis = NULL;
77
78  this->localCamera = NULL;
79  this->localPlayer = NULL;
80  this->sky = NULL;
81  this->terrain = NULL;
82
83  this->music = NULL;
84  this->objectManager = NULL;
85}
86
87
88/**
89 * destructor for the GameWorldData
90 */
91GameWorldData::~GameWorldData()
92{}
93
94
95
96/**
97 *  initialize the GameWorldData
98 */
99ErrorMessage GameWorldData::init()
100{
101  this->objectManager = new ObjectManager();
102  State::setObjectManager(this->objectManager);
103
104  PNode::getNullParent();
105  this->localCamera = new Camera();
106  this->localCamera->setName ("GameWorld-Camera");
107  State::setCamera(this->localCamera, this->localCamera->getTarget());
108
109  LightManager::getInstance();
110
111  GraphicsEngine::getInstance()->displayFPS(true);
112
113  /* initialize some graphics engines and graphical elements */
114  AnimationPlayer::getInstance();
115  PhysicsEngine::getInstance();
116}
117
118
119/**
120 *  loads the data from the xml file
121 * @param root reference to the xml root element
122 */
123ErrorMessage GameWorldData::loadData(TiXmlElement* root)
124{
125  // load the parameters
126  // name
127  const char* string = grabParameter( root, "name");
128  if( string == NULL)
129  {
130    PRINTF(2)("GameWorld is missing a proper 'name'\n");
131    this->setName("Unknown");
132  }
133  else
134    this->setName(string);
135
136  this->loadGUI(root);
137  this->loadWorldEntities(root);
138  this->loadScene(root);
139}
140
141
142/**
143 *  unloads the data from the xml file
144 */
145ErrorMessage GameWorldData::unloadData()
146{
147  this->unloadGUI();
148  this->unloadWorldEntities();
149  this->unloadScene();
150}
151
152
153/**
154 *  loads the GUI data
155 * @param root reference to the xml root element
156 */
157ErrorMessage GameWorldData::loadGUI(TiXmlElement* root)
158{
159  TiXmlElement* element = root->FirstChildElement("LoadScreen");
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);
166    //     this->glmis->draw();
167  }
168  else
169  {
170    this->glmis->loadParams(element);
171    //     this->glmis->draw();
172  }
173  this->glmis->draw();
174}
175
176
177/**
178 *  unloads the GUI data
179 */
180ErrorMessage GameWorldData::unloadGUI()
181{
182  delete this->glmis;
183}
184
185
186/**
187 *  loads the world entities from the xml file
188 * @param root reference to the xml root parameter
189 */
190ErrorMessage GameWorldData::loadWorldEntities(TiXmlElement* root)
191{
192  TiXmlElement* element = root->FirstChildElement("WorldEntities");
193
194  if( element == NULL)
195  {
196    PRINTF(1)("GameWorld is missing 'WorldEntities'\n");
197  }
198  else
199  {
200    element = element->FirstChildElement();
201    // load Players/Objects/Whatever
202    PRINTF(4)("Loading WorldEntities\n");
203    while( element != NULL)
204    {
205      BaseObject* created = Factory::fabricate(element);
206      if( created != NULL )
207        PRINTF(4)("Created a %s: %s\n", created->getClassName(), created->getName());
208
209      //todo do this more elegant
210      if( element->Value() != NULL && !strcmp( element->Value(), "SkyBox"))
211      {
212        this->sky = dynamic_cast<WorldEntity*>(created);
213        State::setSkyBox(dynamic_cast<SkyBox*>(this->sky));
214      }
215      if( element->Value() != NULL && !strcmp( element->Value(), "Terrain"))
216      {
217        this->terrain = dynamic_cast<Terrain*>(created);
218        CDEngine::getInstance()->setTerrain(terrain);
219      }
220      element = element->NextSiblingElement();
221      this->glmis->step(); //! @todo temporary
222    }
223    PRINTF(4)("Done loading WorldEntities\n");
224  }
225
226  // Create a Player
227  this->localPlayer = new Player();
228
229  Playable* playable;
230  const list<BaseObject*>* playableList = ClassList::getList(CL_PLAYABLE);
231  if (playableList != NULL)
232  {
233    playable = dynamic_cast<Playable*>(playableList->front());
234    this->localPlayer->setPlayable(playable);
235  }
236
237  /* init the pnode tree */
238  PNode::getNullParent()->init();
239}
240
241
242/**
243 *  unloads the world entities
244 */
245ErrorMessage GameWorldData::unloadWorldEntities()
246{
247  FastFactory::flushAll(true);
248  GraphicsEngine::getInstance()->displayFPS(false);
249
250  // erease everything that is left.
251  // delete PNode::getNullParent(); // not needed as this is also done in the next step (and also much cleaner)
252  const std::list<BaseObject*>* nodeList;
253  //secondary cleanup of PNodes;
254  nodeList = ClassList::getList(CL_PARENT_NODE);
255  if (nodeList != NULL)
256    while (!nodeList->empty())
257      delete nodeList->front();
258  /* remove the player object */
259  if( this->localPlayer)
260    delete this->localPlayer;
261
262  nodeList = ClassList::getList(CL_GRAPHICS_EFFECT);
263  if (nodeList != NULL)
264    while (!nodeList->empty())
265      delete nodeList->front();
266
267
268  nodeList = ClassList::getList(CL_ELEMENT_2D);
269    if (nodeList != NULL)
270       while (!nodeList->empty())
271         delete nodeList->front();
272
273
274
275
276  // unload the resources !!
277  ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL);
278
279  if (State::getObjectManager() == this->objectManager)
280  {
281    State::setObjectManager(NULL);
282    delete this->objectManager;
283  }
284
285  if(State::getSkyBox())
286    State::setSkyBox(NULL);
287
288  glmis = NULL;
289  localCamera = NULL;
290  localPlayer = NULL;
291  sky = NULL;
292  terrain = NULL;
293  objectManager = NULL;
294}
295
296
297/**
298 *  loads the scene data
299 * @param root reference to the xml root element
300 */
301ErrorMessage GameWorldData::loadScene(TiXmlElement* root)
302{
303  LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams);
304  LoadParamXML(root, "GraphicsEngine", GraphicsEngine::getInstance(), GraphicsEngine, loadParams);
305
306  LoadParam(root, "Music", this, GameWorldData, setSoundTrack);
307
308
309  LoadParamXML(root, "GameRule", this, GameWorldData, loadGameRule);
310
311
312  //LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams);
313  //LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams);
314
315  this->localCamera->setClipRegion(1, 10000.0);
316  if( this->sky != NULL)
317    this->localCamera->addChild(this->sky);
318  SoundEngine::getInstance()->setListener(this->localCamera);
319}
320
321
322
323/**
324 *  unloads the scene data
325 */
326ErrorMessage GameWorldData::unloadScene()
327{
328  /* delete some garphics and scene eingines */
329  delete LightManager::getInstance();
330  delete AnimationPlayer::getInstance();
331  delete PhysicsEngine::getInstance();
332
333  if (this->music != NULL)
334    this->setSoundTrack(NULL);
335  this->music = NULL;
336  /* stop the sound eninge */
337  SoundEngine::getInstance()->flushAllBuffers();
338  SoundEngine::getInstance()->flushAllSources();
339
340  /* unload the shaders */
341  Shader::suspendShader();
342}
343
344
345void GameWorldData::setSoundTrack(const char* name)
346{
347  if (this->music != NULL)
348    delete this->music;
349  this->music = NULL;
350
351  if (name != NULL)
352  {
353    PRINTF(3)("Setting Sound Track to %s\n", name);
354    char* oggFile = ResourceManager::getFullName(name); /// FIXME
355    this->music = new OggPlayer(oggFile);
356    delete[] oggFile;
357
358    //(OggPlayer*)ResourceManager::getInstance()->load(name, OGG, RP_LEVEL);
359    //assert(this->music->isA(CL_SOUND_OGG_PLAYER));
360  }
361}
362
363
364void GameWorldData::loadGameRule(const TiXmlElement* root)
365{
366
367  const TiXmlElement* element = root->FirstChildElement("GameRule");
368
369  if( element == NULL)
370  {
371    PRINTF(1)("GameWorld is missing 'GameRule'\n");
372  }
373  else
374  {
375    element = element->FirstChildElement();
376
377    while( element != NULL)
378    {
379      BaseObject* created = Factory::fabricate(element);
380      if (created == NULL /*|| !created->isA(CL_GAME_RULE)*/)
381        delete created;
382      else
383      {
384        this->gameRule = dynamic_cast<GameRules*>(created);
385        element = element->NextSiblingElement();
386      }
387    }
388  }
389
390
391
392}
393
394
395
Note: See TracBrowser for help on using the repository browser.