Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6150 was 6150, checked in by bensch, 18 years ago

orxonox/trunk: cleanup of the world begin

File size: 9.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"
[5139]21
22#include "shell_command.h"
[2636]23#include "campaign.h"
24#include "world.h"
25#include "orxonox.h"
26#include "camera.h"
27#include "vector.h"
[4094]28#include "resource_manager.h"
[4010]29#include "factory.h"
[4410]30#include "event.h"
31#include "event_handler.h"
[2636]32#include <string.h>
33
34
35using namespace std;
36
37
[5195]38SHELL_COMMAND(quit, GameLoader, stop)
39    ->describe("quits the game")
40    ->setAlias("orxoquit");
[2636]41
[5162]42
43GameLoader* GameLoader::singletonRef = NULL;
44
45
[4445]46/**
[4836]47 *  simple constructor
[6139]48 */
[4597]49GameLoader::GameLoader ()
[4017]50{
[4597]51  this->setClassID(CL_GAME_LOADER, "GameLoader");
52  this->setName("GameLoader");
[5139]53
[4017]54}
[2636]55
56
[4445]57/**
[4836]58 *  simple deconstructor
[6139]59 */
[4816]60GameLoader::~GameLoader ()
61{
62  if( this->currentCampaign)
63    delete this->currentCampaign;
64  this->currentCampaign = NULL;
65}
[2636]66
67
[3225]68/**
[4836]69 *  this class is a singleton class
70 * @returns an instance of itself
[6139]71 *
72 * if you are unsure about singleton classes, check the theory out on the internet :)
73 */
[2636]74GameLoader* GameLoader::getInstance()
75{
76  if(singletonRef == NULL)
77    singletonRef = new GameLoader();
78  return singletonRef;
79}
80
[4487]81/**
[4836]82 *  initializes the GameLoader
[6139]83 */
[3222]84ErrorMessage GameLoader::init()
[2636]85{
86  if(this->currentCampaign != NULL)
87    this->currentCampaign->init();
[4411]88
89  this->eventHandler = EventHandler::getInstance();
[5093]90  this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_PAUSE);
[5553]91  this->eventHandler->subscribe(this, ES_ALL, EV_MAIN_QUIT);          //< External Quit Event
[4411]92  this->eventHandler->subscribe(this, ES_ALL, KeyMapper::PEV_QUIT);
[5093]93  this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_NEXT_WORLD);
94  this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_PREVIOUS_WORLD);
[2636]95}
96
97
[3225]98/**
[4836]99 *  reads a campaign definition file into a campaign class
100 * @param fileName to be loaded
101 * @returns the loaded campaign
[6139]102 *
103 * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns
104 */
[4487]105ErrorMessage GameLoader::loadCampaign(const char* fileName)
[2636]106{
[3222]107  ErrorMessage errorCode;
[4487]108  char* campaignName = ResourceManager::getFullName(fileName);
[4216]109  if (campaignName)
[4094]110    {
111      this->currentCampaign = this->fileToCampaign(campaignName);
[5208]112      delete[] campaignName;
[4094]113    }
[2636]114}
115
[6139]116
[3225]117/**
[6139]118 *  reads a campaign definition file into a campaign class
119 * @param fileName to be loaded
120 * @returns the loaded campaign
121 *
122 * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns
123 */
124ErrorMessage GameLoader::loadNetworkCampaign(const char* fileName)
125{
126  ErrorMessage errorCode;
127  char* campaignName = ResourceManager::getFullName(fileName);
128  if (campaignName)
129  {
130    this->currentCampaign = this->fileToNetworkCampaign(campaignName);
131    delete[] campaignName;
132  }
133}
134
135
136/**
[4836]137 *  loads a debug campaign for test purposes only.
138 * @param campaignID the identifier of the campaign.
139 * @returns error message if not able to do so.
[6139]140 */
[3222]141ErrorMessage GameLoader::loadDebugCampaign(Uint32 campaignID)
[2636]142{
143  switch(campaignID)
144    {
[4597]145      /*
146         Debug Level 0: Debug level used to test the base frame work.
147         As you can see, all storyentity data is allocated before game
148         start. the storyentity will load themselfs shortly before start
149         through the StoryEntity::init() funtion.
[3629]150      */
[2636]151    case DEBUG_CAMPAIGN_0:
152      {
[6150]153        // FIXME
154/*        Campaign* debugCampaign = new Campaign();
[3220]155
[4597]156        World* world0 = new World(DEBUG_WORLD_0);
157        world0->setNextStoryID(WORLD_ID_1);
158        debugCampaign->addEntity(world0, WORLD_ID_0);
[3220]159
[4597]160        World* world1 = new World(DEBUG_WORLD_1);
161        world1->setNextStoryID(WORLD_ID_2);
162        debugCampaign->addEntity(world1, WORLD_ID_1);
[2636]163
[4597]164        World* world2 = new World(DEBUG_WORLD_2);
165        world2->setNextStoryID(WORLD_ID_GAMEEND);
166        debugCampaign->addEntity(world2, WORLD_ID_2);
[3727]167
[4597]168        this->currentCampaign = debugCampaign;
[6150]169        break;*/
[2636]170      }
171    }
172}
173
[4443]174
[4597]175/**
[6139]176 *  starts the current entity
177 * @returns error code if this action has caused a error
178 */
[3222]179ErrorMessage GameLoader::start()
[2636]180{
181  if(this->currentCampaign != NULL)
182    this->currentCampaign->start();
183}
184
185
[4597]186/**
[6139]187 *  stops the current entity
188 * @returns error code if this action has caused a error
189 *
190 *  ATTENTION: this function shouldn't call other functions, or if so, they must return
191 *  after finishing. If you ignore or forget to do so, the current entity is not able to
192 *  terminate and it will run in the background or the ressources can't be freed or even
193 *  worse: are freed and the program will end in a segmentation fault!
194 *  hehehe, have ya seen it... :)
195 */
[5139]196void GameLoader::stop()
[2636]197{
198  if(this->currentCampaign != NULL)
199    this->currentCampaign->stop();
200}
201
202
[4597]203/**
[6139]204 *  pause the current entity
205 * @returns error code if this action has caused a error
206 *
207 * this pauses the current entity or passes this call forth to the running entity.
208 */
[3222]209ErrorMessage GameLoader::pause()
[2636]210{
211  this->isPaused = true;
212  if(this->currentCampaign != NULL)
213    this->currentCampaign->pause();
214}
215
216
[4597]217/**
[6139]218 *  resumes a pause
219 * @returns error code if this action has caused a error
220 *
221 *  this resumess the current entity or passes this call forth to the running entity.
222 */
[3222]223ErrorMessage GameLoader::resume()
[2636]224{
225  this->isPaused = false;
226  if(this->currentCampaign != NULL)
227    this->currentCampaign->resume();
228}
229
[4443]230
[3225]231/**
[4836]232 *  release the mem ATTENTION: not implemented
[3225]233 */
234ErrorMessage GameLoader::destroy()
[4487]235{
[2636]236
[4487]237}
[2636]238
[4487]239
[3225]240/**
[4836]241 *  reads a campaign definition file into a campaign class
242 * @param fileName to be loaded
243 * @returns the loaded campaign
[6139]244 *
245 * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns
246 */
[4487]247Campaign* GameLoader::fileToCampaign(const char* fileName)
[2636]248{
249  /* do not entirely load the campaign. just the current world
250     before start of each world, it has to be initialized so it
251     can load everything it needs into memory then.
252  */
[4597]253
[4487]254  if( fileName == NULL)
[4010]255    {
[4113]256      PRINTF(2)("No filename specified for loading");
[4010]257      return NULL;
258    }
[4597]259
[4487]260  TiXmlDocument* XMLDoc = new TiXmlDocument( fileName);
[4010]261  // load the campaign document
262  if( !XMLDoc->LoadFile())
263    {
264      // report an error
[4487]265      PRINTF(1)("Could not load XML File %s: %s @ %d:%d\n", fileName, XMLDoc->ErrorDesc(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
[4010]266      delete XMLDoc;
267      return NULL;
268    }
[4597]269
[4010]270  // check basic validity
271  TiXmlElement* root = XMLDoc->RootElement();
272  assert( root != NULL);
[4597]273
[4010]274  if( strcmp( root->Value(), "Campaign"))
275    {
276      // report an error
[4113]277      PRINTF(2)("Specified XML File is not an orxonox campaign file (Campaign element missing)\n");
[4010]278      delete XMLDoc;
279      return NULL;
280    }
[4597]281
[4010]282  // construct campaign
283  Campaign* c = new Campaign( root);
[4597]284
[4010]285  // free the XML data
286  delete XMLDoc;
[4496]287
[4010]288  return c;
[2636]289}
290
291
292/**
[6139]293 *  reads a campaign definition file into a campaign class
294 * @param fileName to be loaded
295 * @returns the loaded campaign
296 *
297 *  this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns
298 */
299Campaign* GameLoader::fileToNetworkCampaign(const char* fileName)
300{
301  /* do not entirely load the campaign. just the current world
302  before start of each world, it has to be initialized so it
303  can load everything it needs into memory then.
304  */
305
306  if( fileName == NULL)
307  {
308    PRINTF(2)("No filename specified for loading");
309    return NULL;
310  }
311
312  TiXmlDocument* XMLDoc = new TiXmlDocument( fileName);
313  // load the campaign document
314  if( !XMLDoc->LoadFile())
315  {
316      // report an error
317    PRINTF(1)("Could not load XML File %s: %s @ %d:%d\n", fileName, XMLDoc->ErrorDesc(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
318    delete XMLDoc;
319    return NULL;
320  }
321
322  // check basic validity
323  TiXmlElement* root = XMLDoc->RootElement();
324  assert( root != NULL);
325
326  if( strcmp( root->Value(), "Campaign"))
327  {
328      // report an error
329    PRINTF(2)("Specified XML File is not an orxonox campaign file (Campaign element missing)\n");
330    delete XMLDoc;
331    return NULL;
332  }
333
334  // construct campaign
335  Campaign* c = new Campaign( root);
336
337  // free the XML data
338  delete XMLDoc;
339
340  return c;
341}
342
343
344/**
[4836]345 *  handle keyboard commands
346 * @param event the event to handle
[5553]347 */
[4410]348void GameLoader::process(const Event& event)
349{
350  if( event.type == KeyMapper::PEV_NEXT_WORLD)
[5553]351  {
352    if( likely(event.bPressed))
[4410]353    {
[5553]354      this->nextLevel();
[4410]355    }
[5553]356  }
[4410]357  else if( event.type == KeyMapper::PEV_PREVIOUS_WORLD)
[5553]358  {
359    if( likely(event.bPressed))
[4410]360    {
[5553]361      this->previousLevel();
[4410]362    }
[5553]363  }
[4410]364  else if( event.type == KeyMapper::PEV_PAUSE)
[5553]365  {
366    if( likely(event.bPressed))
[4410]367    {
[5553]368      if(this->isPaused)
369        this->resume();
370      else
371        this->pause();
[4410]372    }
[5553]373  }
[4410]374  else if( event.type == KeyMapper::PEV_QUIT)
[5553]375  {
376    if( event.bPressed) this->stop();
377  }
378  else if (event.type == EV_MAIN_QUIT)
379    this->stop();
[4410]380}
381
[4443]382
[4487]383/**
[6139]384 *  \brief this changes to the next level
385 */
[2636]386void GameLoader::nextLevel()
387{
388  if(this->currentCampaign != NULL)
389    this->currentCampaign->nextLevel();
390}
391
[3225]392
[4487]393/**
[6139]394 *  change to the previous level - not implemented
395 *
396 * this propably useless
397 */
[2636]398void GameLoader::previousLevel()
399{
400  if(this->currentCampaign != NULL)
401    this->currentCampaign->previousLevel();
402}
Note: See TracBrowser for help on using the repository browser.