Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/util/loading/game_loader.cc @ 5972

Last change on this file since 5972 was 5972, checked in by patrick, 18 years ago

network: started renice work on the game_load, making it network friendly:D

File size: 8.4 KB
Line 
1
2
3/*
4   orxonox - the future of 3D-vertical-scrollers
5
6   Copyright (C) 2004 orx
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2, or (at your option)
11   any later version.
12
13   ### File Specific:
14   main-programmer: Patrick Boenzli
15   co-programmer: ...
16*/
17
18#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD
19
20#include "game_loader.h"
21
22#include "shell_command.h"
23#include "campaign.h"
24#include "world.h"
25#include "orxonox.h"
26#include "camera.h"
27#include "vector.h"
28#include "resource_manager.h"
29#include "factory.h"
30#include "event.h"
31#include "event_handler.h"
32#include <string.h>
33
34
35using namespace std;
36
37
38SHELL_COMMAND(quit, GameLoader, stop)
39    ->describe("quits the game")
40    ->setAlias("orxoquit");
41
42
43GameLoader* GameLoader::singletonRef = NULL;
44
45
46/**
47 *  simple constructor
48 */
49GameLoader::GameLoader ()
50{
51  this->setClassID(CL_GAME_LOADER, "GameLoader");
52  this->setName("GameLoader");
53
54}
55
56
57
58/**
59 *  simple deconstructor
60 */
61GameLoader::~GameLoader ()
62{
63  if( this->currentCampaign)
64    delete this->currentCampaign;
65  this->currentCampaign = NULL;
66}
67
68
69
70/**
71 *  initializes the GameLoader
72 */
73ErrorMessage GameLoader::init()
74{
75  if(this->currentCampaign != NULL)
76    this->currentCampaign->init();
77
78  this->eventHandler = EventHandler::getInstance();
79  this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_PAUSE);
80  this->eventHandler->subscribe(this, ES_ALL, EV_MAIN_QUIT);          //< External Quit Event
81  this->eventHandler->subscribe(this, ES_ALL, KeyMapper::PEV_QUIT);
82  this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_NEXT_WORLD);
83  this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_PREVIOUS_WORLD);
84}
85
86
87
88/**
89 *  reads a campaign definition file into a campaign class
90 * @param fileName to be loaded
91 * @returns the loaded campaign
92 *
93 * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns
94 */
95ErrorMessage GameLoader::loadCampaign(const char* fileName)
96{
97  ErrorMessage errorCode;
98  char* campaignName = ResourceManager::getFullName(fileName);
99  if (campaignName)
100    {
101      this->currentCampaign = this->fileToCampaign(campaignName);
102      delete[] campaignName;
103    }
104//   World* world0 = new World(DEBUG_WORLD_0);
105//   world0->setNextStoryID(WORLD_ID_GAMEEND);
106//   this->currentCampaign->addEntity(world0, WORLD_ID_2);
107}
108
109
110
111/**
112 *  reads a campaign definition file into a campaign class
113 * @param fileName to be loaded
114 * @returns the loaded campaign
115 *
116 * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns
117 */
118ErrorMessage loadNetworkCampaign(const char* name)
119{}
120
121
122
123/**
124 *  loads a debug campaign for test purposes only.
125 * @param campaignID the identifier of the campaign.
126 * @returns error message if not able to do so.
127 */
128ErrorMessage GameLoader::loadDebugCampaign(Uint32 campaignID)
129{
130  switch(campaignID)
131    {
132      /*
133         Debug Level 0: Debug level used to test the base frame work.
134         As you can see, all storyentity data is allocated before game
135         start. the storyentity will load themselfs shortly before start
136         through the StoryEntity::init() funtion.
137      */
138    case DEBUG_CAMPAIGN_0:
139      {
140        Campaign* debugCampaign = new Campaign();
141
142        World* world0 = new World(DEBUG_WORLD_0);
143        world0->setNextStoryID(WORLD_ID_1);
144        debugCampaign->addEntity(world0, WORLD_ID_0);
145
146        World* world1 = new World(DEBUG_WORLD_1);
147        world1->setNextStoryID(WORLD_ID_2);
148        debugCampaign->addEntity(world1, WORLD_ID_1);
149
150        World* world2 = new World(DEBUG_WORLD_2);
151        world2->setNextStoryID(WORLD_ID_GAMEEND);
152        debugCampaign->addEntity(world2, WORLD_ID_2);
153
154        this->currentCampaign = debugCampaign;
155        break;
156      }
157    }
158}
159
160
161/**
162 *  starts the current entity
163 * @returns error code if this action has caused a error
164 */
165ErrorMessage GameLoader::start()
166{
167  if(this->currentCampaign != NULL)
168    this->currentCampaign->start();
169}
170
171
172/**
173 *  stops the current entity
174 * @returns error code if this action has caused a error
175 *
176 * ATTENTION: this function shouldn't call other functions, or if so, they must return
177 * after finishing. If you ignore or forget to do so, the current entity is not able to
178 * terminate and it will run in the background or the ressources can't be freed or even
179 * worse: are freed and the program will end in a segmentation fault!
180 * hehehe, have dja seen it... :)
181 */
182void GameLoader::stop()
183{
184  if(this->currentCampaign != NULL)
185    this->currentCampaign->stop();
186}
187
188
189/**
190 *  pause the current entity
191 * @returns error code if this action has caused a error
192 *
193 * this pauses the current entity or passes this call forth to the running entity.
194 */
195ErrorMessage GameLoader::pause()
196{
197  this->isPaused = true;
198  if(this->currentCampaign != NULL)
199    this->currentCampaign->pause();
200}
201
202
203/**
204 *  resumes a pause
205 * @returns error code if this action has caused a error
206 *
207 * this resumess the current entity or passes this call forth to the running entity.
208 */
209ErrorMessage GameLoader::resume()
210{
211  this->isPaused = false;
212  if(this->currentCampaign != NULL)
213    this->currentCampaign->resume();
214}
215
216
217/**
218 *  release the mem ATTENTION: not implemented
219 */
220ErrorMessage GameLoader::destroy()
221{
222
223}
224
225
226/**
227 *  reads a campaign definition file into a campaign class
228 * @param fileName to be loaded
229 * @returns the loaded campaign
230 *
231 * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns
232 */
233Campaign* GameLoader::fileToCampaign(const char* fileName)
234{
235  /* do not entirely load the campaign. just the current world
236     before start of each world, it has to be initialized so it
237     can load everything it needs into memory then.
238  */
239
240  if( fileName == NULL)
241    {
242      PRINTF(2)("No filename specified for loading");
243      return NULL;
244    }
245
246  TiXmlDocument* XMLDoc = new TiXmlDocument( fileName);
247  // load the campaign document
248  if( !XMLDoc->LoadFile())
249    {
250      // report an error
251      PRINTF(1)("Could not load XML File %s: %s @ %d:%d\n", fileName, XMLDoc->ErrorDesc(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
252      delete XMLDoc;
253      return NULL;
254    }
255
256  // check basic validity
257  TiXmlElement* root = XMLDoc->RootElement();
258  assert( root != NULL);
259
260  if( strcmp( root->Value(), "Campaign"))
261    {
262      // report an error
263      PRINTF(2)("Specified XML File is not an orxonox campaign file (Campaign element missing)\n");
264      delete XMLDoc;
265      return NULL;
266    }
267
268  // construct campaign
269  Campaign* c = new Campaign( root);
270
271  // free the XML data
272  delete XMLDoc;
273
274  return c;
275}
276
277
278/**
279 *  handle keyboard commands
280 * @param event the event to handle
281 */
282void GameLoader::process(const Event& event)
283{
284  if( event.type == KeyMapper::PEV_NEXT_WORLD)
285  {
286    if( likely(event.bPressed))
287    {
288      this->nextLevel();
289    }
290  }
291  else if( event.type == KeyMapper::PEV_PREVIOUS_WORLD)
292  {
293    if( likely(event.bPressed))
294    {
295      this->previousLevel();
296    }
297  }
298  else if( event.type == KeyMapper::PEV_PAUSE)
299  {
300    if( likely(event.bPressed))
301    {
302      if(this->isPaused)
303        this->resume();
304      else
305        this->pause();
306    }
307  }
308  else if( event.type == KeyMapper::PEV_QUIT)
309  {
310    if( event.bPressed) this->stop();
311  }
312  else if (event.type == EV_MAIN_QUIT)
313    this->stop();
314}
315
316
317/**
318 *  this changes to the next level
319 */
320void GameLoader::nextLevel()
321{
322  if(this->currentCampaign != NULL)
323    this->currentCampaign->nextLevel();
324}
325
326
327/**
328 *  change to the previous level - not implemented
329 *
330 * this propably useless
331 */
332void GameLoader::previousLevel()
333{
334  if(this->currentCampaign != NULL)
335    this->currentCampaign->previousLevel();
336}
337
338/**
339 *  load a StoryEntity
340 * @param element a XMLElement containing all the needed info
341 */
342BaseObject* GameLoader::fabricate(const TiXmlElement* element)
343{
344  assert( element != NULL);
345
346  if( Factory::getFirst() == NULL)
347    {
348      PRINTF(1)("GameLoader does not know any factories, fabricate() failed\n");
349      return NULL;
350    }
351
352  if( element->Value() != NULL)
353    {
354      PRINTF(4)("Attempting fabrication of a '%s'\n", element->Value());
355      BaseObject* b = Factory::getFirst()->fabricate( element);
356      if( b == NULL)
357        PRINTF(2)("Failed to fabricate a '%s'\n", element->Value());
358      else
359        PRINTF(4)("Successfully fabricated a '%s'\n", element->Value());
360      return b;
361    }
362
363  PRINTF(2)("Fabricate failed, TiElement did not contain a value\n");
364
365  return NULL;
366}
Note: See TracBrowser for help on using the repository browser.