Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/hud/src/lib/util/loading/game_loader.cc @ 10647

Last change on this file since 10647 was 10618, checked in by bknecht, 18 years ago

merged cleanup into trunk (only improvements)

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