Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/util/loading/game_loader.cc @ 6450

Last change on this file since 6450 was 6424, checked in by bensch, 19 years ago

orxonox/trunk: merged the branche network back to the trunk
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/network . -r 6351:HEAD
no conflicts

File size: 7.2 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#include "load_param.h"
22
23#include "shell_command.h"
24#include "campaign.h"
25
26#include "resource_manager.h"
27
28#include "event_handler.h"
29
30
31using namespace std;
32
33
34SHELL_COMMAND(quit, GameLoader, stop)
35    ->describe("quits the game")
36    ->setAlias("orxoquit");
37
38
39GameLoader* GameLoader::singletonRef = NULL;
40
41
42/**
43 *  simple constructor
44 */
45GameLoader::GameLoader ()
46{
47  this->setClassID(CL_GAME_LOADER, "GameLoader");
48  this->setName("GameLoader");
49
50}
51
52
53/**
54 *  simple deconstructor
55 */
56GameLoader::~GameLoader ()
57{
58  if( this->currentCampaign)
59    delete this->currentCampaign;
60  this->currentCampaign = NULL;
61}
62
63
64/**
65 *  initializes the GameLoader
66 */
67ErrorMessage GameLoader::init()
68{
69  if(this->currentCampaign != NULL)
70    this->currentCampaign->init();
71
72  this->eventHandler = EventHandler::getInstance();
73  this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_PAUSE);
74  this->eventHandler->subscribe(this, ES_ALL, EV_MAIN_QUIT);          //< External Quit Event
75  this->eventHandler->subscribe(this, ES_ALL, KeyMapper::PEV_QUIT);
76  this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_NEXT_WORLD);
77  this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_PREVIOUS_WORLD);
78}
79
80
81/**
82 *  reads a campaign definition file into a campaign class
83 * @param fileName to be loaded
84 * @returns the loaded campaign
85 *
86 * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns
87 */
88ErrorMessage GameLoader::loadCampaign(const char* fileName)
89{
90  ErrorMessage errorCode;
91  char* campaignName = ResourceManager::getFullName(fileName);
92  if (campaignName)
93    {
94      this->currentCampaign = this->fileToCampaign(campaignName);
95      delete[] campaignName;
96    }
97}
98
99
100/**
101 *  reads a campaign definition file into a campaign class
102 * @param fileName to be loaded
103 * @returns the loaded campaign
104 *
105 * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns
106 */
107ErrorMessage GameLoader::loadNetworkCampaign(const char* fileName)
108{
109  ErrorMessage errorCode;
110  char* campaignName = ResourceManager::getFullName(fileName);
111  if (campaignName)
112  {
113    this->currentCampaign = this->fileToCampaign(campaignName);
114    delete[] campaignName;
115  }
116}
117
118
119/**
120 *  loads a debug campaign for test purposes only.
121 * @param campaignID the identifier of the campaign.
122 * @returns error message if not able to do so.
123 */
124ErrorMessage GameLoader::loadDebugCampaign(Uint32 campaignID)
125{
126  switch(campaignID)
127    {
128      /*
129         Debug Level 0: Debug level used to test the base frame work.
130         As you can see, all storyentity data is allocated before game
131         start. the storyentity will load themselfs shortly before start
132         through the StoryEntity::init() funtion.
133      */
134    case DEBUG_CAMPAIGN_0:
135      {
136/*        Campaign* debugCampaign = new Campaign();
137
138        World* world0 = new World(DEBUG_WORLD_0);
139        world0->setNextStoryID(WORLD_ID_1);
140        debugCampaign->addEntity(world0, WORLD_ID_0);
141
142        World* world1 = new World(DEBUG_WORLD_1);
143        world1->setNextStoryID(WORLD_ID_2);
144        debugCampaign->addEntity(world1, WORLD_ID_1);
145
146        World* world2 = new World(DEBUG_WORLD_2);
147        world2->setNextStoryID(WORLD_ID_GAMEEND);
148        debugCampaign->addEntity(world2, WORLD_ID_2);
149
150        this->currentCampaign = debugCampaign;
151        break;*/
152      }
153    }
154}
155
156
157/**
158 *  starts the current entity
159 * @returns error code if this action has caused a error
160 */
161ErrorMessage GameLoader::start()
162{
163  if(this->currentCampaign != NULL)
164    this->currentCampaign->start();
165}
166
167
168/**
169 *  stops the current entity
170 * @returns error code if this action has caused a error
171 *
172 *  ATTENTION: this function shouldn't call other functions, or if so, they must return
173 *  after finishing. If you ignore or forget to do so, the current entity is not able to
174 *  terminate and it will run in the background or the ressources can't be freed or even
175 *  worse: are freed and the program will end in a segmentation fault!
176 *  hehehe, have ya seen it... :)
177 */
178void GameLoader::stop()
179{
180  if(this->currentCampaign != NULL)
181    this->currentCampaign->stop();
182}
183
184
185/**
186 *  pause the current entity
187 * @returns error code if this action has caused a error
188 *
189 * this pauses the current entity or passes this call forth to the running entity.
190 */
191ErrorMessage GameLoader::pause()
192{
193  this->isPaused = true;
194  if(this->currentCampaign != NULL)
195    this->currentCampaign->pause();
196}
197
198
199/**
200 *  resumes a pause
201 * @returns error code if this action has caused a error
202 *
203 *  this resumess the current entity or passes this call forth to the running entity.
204 */
205ErrorMessage GameLoader::resume()
206{
207  this->isPaused = false;
208  if(this->currentCampaign != NULL)
209    this->currentCampaign->resume();
210}
211
212
213/**
214 *  reads a campaign definition file into a campaign class
215 * @param fileName to be loaded
216 * @returns the loaded campaign
217 *
218 * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns
219 */
220Campaign* GameLoader::fileToCampaign(const char* fileName)
221{
222  /* do not entirely load the campaign. just the current world
223     before start of each world, it has to be initialized so it
224     can load everything it needs into memory then.
225  */
226
227  if( fileName == NULL)
228    {
229      PRINTF(2)("No filename specified for loading");
230      return NULL;
231    }
232
233  TiXmlDocument* XMLDoc = new TiXmlDocument( fileName);
234  // load the campaign document
235  if( !XMLDoc->LoadFile())
236    {
237      // report an error
238      PRINTF(1)("Could not load XML File %s: %s @ %d:%d\n", fileName, XMLDoc->ErrorDesc(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
239      delete XMLDoc;
240      return NULL;
241    }
242
243  // check basic validity
244  TiXmlElement* root = XMLDoc->RootElement();
245  assert( root != NULL);
246
247  if( strcmp( root->Value(), "Campaign"))
248    {
249      // report an error
250      PRINTF(2)("Specified XML File is not an orxonox campaign file (Campaign element missing)\n");
251      delete XMLDoc;
252      return NULL;
253    }
254
255  // construct campaign
256  Campaign* c = new Campaign( root);
257
258  // free the XML data
259  delete XMLDoc;
260
261  return c;
262}
263
264
265
266/**
267 *  handle keyboard commands
268 * @param event the event to handle
269 */
270void GameLoader::process(const Event& event)
271{
272  if( event.type == KeyMapper::PEV_NEXT_WORLD)
273  {
274    if( likely(event.bPressed))
275    {
276      this->switchToNextLevel();
277    }
278  }
279  else if( event.type == KeyMapper::PEV_PAUSE)
280  {
281    if( likely(event.bPressed))
282    {
283      if(this->isPaused)
284        this->resume();
285      else
286        this->pause();
287    }
288  }
289  else if( event.type == KeyMapper::PEV_QUIT)
290  {
291    if( event.bPressed) this->stop();
292  }
293  else if (event.type == EV_MAIN_QUIT)
294    this->stop();
295}
296
297
298/**
299 *  this changes to the next level
300 */
301void GameLoader::switchToNextLevel()
302{
303  if(this->currentCampaign != NULL)
304    this->currentCampaign->switchToNextLevel();
305}
306
307
Note: See TracBrowser for help on using the repository browser.