Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged cleanup into trunk (only improvements)

File size: 7.3 KB
RevLine 
[2636]1
2
[4597]3/*
[2636]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
[5300]18#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD
19
[2636]20#include "game_loader.h"
[7193]21#include "util/loading/load_param.h"
[9869]22#include "util/loading/resource_manager.h"
23#include "debug.h"
[2636]24#include "campaign.h"
[10618]25#include "compiler.h"
[6152]26
[7868]27#include "key_mapper.h"
[2636]28
[9869]29ObjectListDefinition(GameLoader);
[2636]30
[7868]31GameLoader* GameLoader::singletonRef = NULL;
[5162]32
33
[7868]34/**
35 *  simple constructor
36 */
37GameLoader::GameLoader ()
38{
[9869]39  this->registerObject(this, GameLoader::_objectList);
[7868]40  this->setName("GameLoader");
41  this->bRun = true;
42}
[5162]43
[2636]44
[7868]45/**
46 *  simple deconstructor
47 */
48GameLoader::~GameLoader ()
49{
50  if( this->currentCampaign)
51    delete this->currentCampaign;
52  this->currentCampaign = NULL;
[9869]53
[9110]54  GameLoader::singletonRef = NULL;
[7868]55}
[2636]56
57
[7868]58/**
59 *  initializes the GameLoader
60 */
61ErrorMessage GameLoader::init()
62{
63  if(this->currentCampaign != NULL)
64    this->currentCampaign->init();
[2636]65
[7868]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);
[8717]71
72  return ErrorMessage();
[7868]73}
[4411]74
[2636]75
[7868]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;
[9869]86  std::string campaignName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
[7868]87  if (!campaignName.empty())
[7374]88  {
[7868]89    this->currentCampaign = this->fileToCampaign(campaignName);
[7374]90  }
[8717]91
92  return ErrorMessage();
[7868]93}
[2636]94
[6139]95
[7868]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;
[9869]106  std::string campaignName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
[7868]107  if (!campaignName.empty())
[6139]108  {
[7868]109    this->currentCampaign = this->fileToCampaign(campaignName);
[6139]110  }
[8717]111
112  return ErrorMessage();
[7868]113}
[6139]114
115
[7868]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)
[7374]124  {
[7868]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();
[3220]134
[7868]135                World* world0 = new World(DEBUG_WORLD_0);
136                world0->setNextStoryID(WORLD_ID_1);
137                debugCampaign->addEntity(world0, WORLD_ID_0);
[3220]138
[7868]139                World* world1 = new World(DEBUG_WORLD_1);
140                world1->setNextStoryID(WORLD_ID_2);
141                debugCampaign->addEntity(world1, WORLD_ID_1);
[2636]142
[7868]143                World* world2 = new World(DEBUG_WORLD_2);
144                world2->setNextStoryID(WORLD_ID_GAMEEND);
145                debugCampaign->addEntity(world2, WORLD_ID_2);
[3727]146
[7868]147                this->currentCampaign = debugCampaign;
148                break;*/
149      }
[7374]150  }
[8717]151
152  return ErrorMessage();
[7868]153}
[2636]154
[4443]155
[7868]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)
[6862]163  {
[7868]164    this->currentCampaign->start();
[6862]165  }
[8717]166
167  return ErrorMessage();
[7868]168}
[2636]169
170
[7868]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}
[2636]186
187
[7868]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();
[8717]199
200  return ErrorMessage();
[7868]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();
[8717]215
216  return ErrorMessage();
[7868]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())
[7374]235  {
[7868]236    PRINTF(2)("No filename specified for loading");
237    return NULL;
[7374]238  }
[2636]239
[7868]240  TiXmlDocument XMLDoc(fileName);
241  // load the campaign document
242  if( !XMLDoc.LoadFile(fileName))
[7374]243  {
[7868]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;
[7374]247  }
[2636]248
[7868]249  // check basic validity
250  TiXmlElement* root = XMLDoc.RootElement();
251  assert( root != NULL);
[4443]252
[7868]253  if( strcmp( root->Value(), "Campaign"))
[7374]254  {
[7868]255    // report an error
256    PRINTF(2)("Specified XML File is not an orxonox campaign file (Campaign element missing)\n");
257    return NULL;
258  }
[4597]259
[7868]260  // construct campaign
261  return new Campaign( root);
262}
[4597]263
264
265
[7868]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))
[4010]275    {
[7868]276      this->switchToNextLevel();
[4010]277    }
[7374]278  }
[7868]279  else if( event.type == KeyMapper::PEV_PAUSE)
[5553]280  {
[7868]281    if( likely(event.bPressed))
[4410]282    {
[7868]283      if(this->isPaused)
284        this->resume();
285      else
286        this->pause();
[4410]287    }
[5553]288  }
[7868]289  else if( event.type == KeyMapper::PEV_QUIT)
[5553]290  {
[7868]291    if( event.bPressed) this->stop();
[5553]292  }
[7868]293  else if (event.type == EV_MAIN_QUIT)
294    this->stop();
295}
[4410]296
[7868]297
298/**
299 *  this changes to the next level
300 */
301void GameLoader::switchToNextLevel()
302{
303  if(this->currentCampaign != NULL)
304    this->currentCampaign->switchToNextLevel();
[2636]305}
[7868]306
Note: See TracBrowser for help on using the repository browser.