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 "particle_engine.h" |
---|
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 | |
---|
64 | |
---|
65 | using namespace std; |
---|
66 | |
---|
67 | |
---|
68 | /** |
---|
69 | * constructor of the GameWorldData |
---|
70 | */ |
---|
71 | GameWorldData::GameWorldData() |
---|
72 | { |
---|
73 | this->setClassID(CL_GAME_WORLD_DATA, "GameWorldData"); |
---|
74 | |
---|
75 | this->localPlayer = NULL; |
---|
76 | this->localCamera = NULL; |
---|
77 | |
---|
78 | this->glmis = NULL; |
---|
79 | |
---|
80 | this->localCamera = NULL; |
---|
81 | this->localPlayer = NULL; |
---|
82 | this->sky = NULL; |
---|
83 | this->terrain = NULL; |
---|
84 | |
---|
85 | this->music = NULL; |
---|
86 | this->objectManager = NULL; |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | /** |
---|
91 | * destructor for the GameWorldData |
---|
92 | */ |
---|
93 | GameWorldData::~GameWorldData() |
---|
94 | {} |
---|
95 | |
---|
96 | |
---|
97 | |
---|
98 | /** |
---|
99 | * initialize the GameWorldData |
---|
100 | */ |
---|
101 | ErrorMessage GameWorldData::init() |
---|
102 | { |
---|
103 | this->objectManager = new ObjectManager(); |
---|
104 | State::setObjectManager(this->objectManager); |
---|
105 | |
---|
106 | PNode::getNullParent(); |
---|
107 | this->localCamera = new Camera(); |
---|
108 | this->localCamera->setName ("GameWorld-Camera"); |
---|
109 | State::setCamera(this->localCamera, this->localCamera->getTarget()); |
---|
110 | |
---|
111 | LightManager::getInstance(); |
---|
112 | |
---|
113 | GraphicsEngine::getInstance()->displayFPS(true); |
---|
114 | |
---|
115 | /* initialize some graphics engines and graphical elements */ |
---|
116 | AnimationPlayer::getInstance(); |
---|
117 | ParticleEngine::getInstance(); |
---|
118 | PhysicsEngine::getInstance(); |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | /** |
---|
123 | * loads the data from the xml file |
---|
124 | * @param root reference to the xml root element |
---|
125 | */ |
---|
126 | ErrorMessage GameWorldData::loadData(TiXmlElement* root) |
---|
127 | { |
---|
128 | // load the parameters |
---|
129 | // name |
---|
130 | const char* string = grabParameter( root, "name"); |
---|
131 | if( string == NULL) |
---|
132 | { |
---|
133 | PRINTF(2)("GameWorld is missing a proper 'name'\n"); |
---|
134 | this->setName("Unknown"); |
---|
135 | } |
---|
136 | else |
---|
137 | this->setName(string); |
---|
138 | |
---|
139 | this->loadGUI(root); |
---|
140 | this->loadWorldEntities(root); |
---|
141 | this->loadScene(root); |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | /** |
---|
146 | * unloads the data from the xml file |
---|
147 | */ |
---|
148 | ErrorMessage GameWorldData::unloadData() |
---|
149 | { |
---|
150 | this->unloadGUI(); |
---|
151 | this->unloadWorldEntities(); |
---|
152 | this->unloadScene(); |
---|
153 | } |
---|
154 | |
---|
155 | |
---|
156 | /** |
---|
157 | * loads the GUI data |
---|
158 | * @param root reference to the xml root element |
---|
159 | */ |
---|
160 | ErrorMessage GameWorldData::loadGUI(TiXmlElement* root) |
---|
161 | { |
---|
162 | TiXmlElement* element = root->FirstChildElement("LoadScreen"); |
---|
163 | if( element == NULL) |
---|
164 | { |
---|
165 | PRINTF(2)("no LoadScreen specified, loading default\n"); |
---|
166 | |
---|
167 | glmis->setBackgroundImage("pictures/load_screen.jpg"); |
---|
168 | this->glmis->setMaximum(8); |
---|
169 | // this->glmis->draw(); |
---|
170 | } |
---|
171 | else |
---|
172 | { |
---|
173 | this->glmis->loadParams(element); |
---|
174 | // this->glmis->draw(); |
---|
175 | } |
---|
176 | this->glmis->draw(); |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | /** |
---|
181 | * unloads the GUI data |
---|
182 | */ |
---|
183 | ErrorMessage GameWorldData::unloadGUI() |
---|
184 | { |
---|
185 | delete this->glmis; |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | /** |
---|
190 | * loads the world entities from the xml file |
---|
191 | * @param root reference to the xml root parameter |
---|
192 | */ |
---|
193 | ErrorMessage GameWorldData::loadWorldEntities(TiXmlElement* root) |
---|
194 | { |
---|
195 | TiXmlElement* element = root->FirstChildElement("WorldEntities"); |
---|
196 | |
---|
197 | if( element == NULL) |
---|
198 | { |
---|
199 | PRINTF(1)("GameWorld is missing 'WorldEntities'\n"); |
---|
200 | } |
---|
201 | else |
---|
202 | { |
---|
203 | element = element->FirstChildElement(); |
---|
204 | // load Players/Objects/Whatever |
---|
205 | PRINTF(4)("Loading WorldEntities\n"); |
---|
206 | while( element != NULL) |
---|
207 | { |
---|
208 | BaseObject* created = Factory::fabricate(element); |
---|
209 | if( created != NULL ) |
---|
210 | printf("Created a %s: %s\n", created->getClassName(), created->getName()); |
---|
211 | |
---|
212 | //todo do this more elegant |
---|
213 | if( element->Value() != NULL && !strcmp( element->Value(), "SkyBox")) |
---|
214 | this->sky = dynamic_cast<WorldEntity*>(created); |
---|
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->setControllable(playable); |
---|
235 | } |
---|
236 | |
---|
237 | /* init the pnode tree */ |
---|
238 | PNode::getNullParent()->init(); |
---|
239 | } |
---|
240 | |
---|
241 | |
---|
242 | /** |
---|
243 | * unloads the world entities |
---|
244 | */ |
---|
245 | ErrorMessage GameWorldData::unloadWorldEntities() |
---|
246 | { |
---|
247 | FastFactory::flushAll(true); |
---|
248 | |
---|
249 | // erease everything that is left. |
---|
250 | delete PNode::getNullParent(); |
---|
251 | |
---|
252 | //secondary cleanup of PNodes; |
---|
253 | const std::list<BaseObject*>* nodeList = ClassList::getList(CL_PARENT_NODE); |
---|
254 | if (nodeList != NULL) |
---|
255 | while (!nodeList->empty()) |
---|
256 | delete nodeList->front(); |
---|
257 | |
---|
258 | /* remove the player object */ |
---|
259 | if( this->localPlayer) |
---|
260 | delete this->localPlayer; |
---|
261 | |
---|
262 | // unload the resources !! |
---|
263 | ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL); |
---|
264 | |
---|
265 | if (State::getObjectManager() == this->objectManager) |
---|
266 | { |
---|
267 | State::setObjectManager(NULL); |
---|
268 | delete this->objectManager; |
---|
269 | } |
---|
270 | } |
---|
271 | |
---|
272 | |
---|
273 | /** |
---|
274 | * loads the scene data |
---|
275 | * @param root reference to the xml root element |
---|
276 | */ |
---|
277 | ErrorMessage GameWorldData::loadScene(TiXmlElement* root) |
---|
278 | { |
---|
279 | LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams); |
---|
280 | |
---|
281 | LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams); |
---|
282 | //LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams); |
---|
283 | |
---|
284 | this->localCamera->setClipRegion(1, 10000.0); |
---|
285 | if( this->sky != NULL) |
---|
286 | this->localCamera->addChild(this->sky); |
---|
287 | |
---|
288 | /* sound loading */ |
---|
289 | this->music = NULL; |
---|
290 | //(OggPlayer*)ResourceManager::getInstance()->load("sound/00-luke_grey_-_hypermode.ogg", OGG, RP_LEVEL); |
---|
291 | //music->playback(); |
---|
292 | SoundEngine::getInstance()->setListener(this->localCamera); |
---|
293 | } |
---|
294 | |
---|
295 | |
---|
296 | /** |
---|
297 | * unloads the scene data |
---|
298 | */ |
---|
299 | ErrorMessage GameWorldData::unloadScene() |
---|
300 | { |
---|
301 | /* delete some garphics and scene eingines */ |
---|
302 | delete LightManager::getInstance(); |
---|
303 | delete ParticleEngine::getInstance(); |
---|
304 | delete AnimationPlayer::getInstance(); |
---|
305 | delete PhysicsEngine::getInstance(); |
---|
306 | |
---|
307 | /* stop the sound eninge */ |
---|
308 | SoundEngine::getInstance()->flushAllBuffers(); |
---|
309 | SoundEngine::getInstance()->flushAllSources(); |
---|
310 | |
---|
311 | /* unload the shaders */ |
---|
312 | Shader::suspendShader(); |
---|
313 | } |
---|
314 | |
---|