Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Namespaces for sound

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