Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: soundEngine works again

File size: 8.1 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->setPlayable(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  const std::list<BaseObject*>* nodeList;
250  //secondary cleanup of PNodes;
251  nodeList = ClassList::getList(CL_PARENT_NODE);
252  if (nodeList != NULL)
253    while (!nodeList->empty())
254      delete nodeList->front();
255  /* remove the player object */
256  if( this->localPlayer)
257    delete this->localPlayer;
258
259  nodeList = ClassList::getList(CL_GRAPHICS_EFFECT);
260  if (nodeList != NULL)
261    while (!nodeList->empty())
262      delete nodeList->front();
263  //
264  //   nodeList = ClassList::getList(CL_ELEMENT_2D);
265  //   if (nodeList != NULL)
266  //     while (!nodeList->empty())
267  //       delete nodeList->front();
268
269
270
271
272  // unload the resources !!
273  ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL);
274
275  if (State::getObjectManager() == this->objectManager)
276  {
277    State::setObjectManager(NULL);
278    delete this->objectManager;
279  }
280
281  if(State::getSkyBox())
282    State::setSkyBox(NULL);
283
284  glmis = NULL;
285  localCamera = NULL;
286  localPlayer = NULL;
287  sky = NULL;
288  terrain = NULL;
289  objectManager = NULL;
290}
291
292
293/**
294 *  loads the scene data
295 * @param root reference to the xml root element
296 */
297ErrorMessage GameWorldData::loadScene(TiXmlElement* root)
298{
299  LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams);
300  LoadParamXML(root, "GraphicsEngine", GraphicsEngine::getInstance(), GraphicsEngine, loadParams);
301
302  LoadParam(root, "Music", this, GameWorldData, setSoundTrack);
303
304  //  LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams);
305  //LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams);
306
307  this->localCamera->setClipRegion(1, 10000.0);
308  if( this->sky != NULL)
309    this->localCamera->addChild(this->sky);
310  SoundEngine::getInstance()->setListener(this->localCamera);
311}
312
313
314/**
315 *  unloads the scene data
316 */
317ErrorMessage GameWorldData::unloadScene()
318{
319  /* delete some garphics and scene eingines */
320  delete LightManager::getInstance();
321  delete AnimationPlayer::getInstance();
322  delete PhysicsEngine::getInstance();
323
324  if (this->music != NULL)
325    this->setSoundTrack(NULL);
326  this->music = NULL;
327  /* stop the sound eninge */
328  SoundEngine::getInstance()->flushAllBuffers();
329  SoundEngine::getInstance()->flushAllSources();
330
331  /* unload the shaders */
332  Shader::suspendShader();
333}
334
335
336void GameWorldData::setSoundTrack(const char* name)
337{
338  if (this->music != NULL)
339    delete this->music;
340  this->music = NULL;
341
342  if (name != NULL)
343  {
344    PRINTF(3)("Setting Sound Track to %s\n", name);
345    char* oggFile = ResourceManager::getFullName(name); /// FIXME
346    this->music = new OggPlayer(oggFile);
347    delete[] oggFile;
348
349    //(OggPlayer*)ResourceManager::getInstance()->load(name, OGG, RP_LEVEL);
350    //assert(this->music->isA(CL_SOUND_OGG_PLAYER));
351  }
352}
353
354
Note: See TracBrowser for help on using the repository browser.