Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/ll2trunktemp/src/game_loader.cc @ 4007

Last change on this file since 4007 was 4004, checked in by bensch, 20 years ago

orxonox/branches/ll2trunktemp: changed the Factory-Macro into a Template, this code will be easyer to read and to debug
also found the segfault when deleting orxonox.
each factory tried to delete itself, because they were initialized implicitely (not with new).
now we only have to delete the first factory at the beginning

File size: 6.5 KB
RevLine 
[2636]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
19#include "game_loader.h"
20#include "campaign.h"
21#include "world.h"
22#include "player.h"
23#include "orxonox.h"
24#include "camera.h"
25#include "command_node.h"
26#include "vector.h"
[4004]27#include "factory.h"
[2636]28
29#include <string.h>
30
31
32using namespace std;
33
34
35GameLoader* GameLoader::singletonRef = 0;
36
37
38GameLoader::GameLoader () {}
39
40
41GameLoader::~GameLoader () {}
42
43
[3225]44/**
45   \brief this class is a singleton class
46   \returns an instance of itself
47
48   if you are unsure about singleton classes, check the theory out on the internet :)
49*/
[2636]50GameLoader* GameLoader::getInstance()
51{
52  if(singletonRef == NULL)
53    singletonRef = new GameLoader();
54  return singletonRef;
55}
56
57
[3222]58ErrorMessage GameLoader::init()
[2636]59{
60  if(this->currentCampaign != NULL)
61    this->currentCampaign->init();
62}
63
64
[3225]65/**
66   \brief reads a campaign definition file into a campaign class
67   \param filename to be loaded
68   \returns the loaded campaign
69
70   this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns
71*/
[3222]72ErrorMessage GameLoader::loadCampaign(char* name)
[2636]73{
[3222]74  ErrorMessage errorCode;
[2636]75 
76  this->currentCampaign = this->fileToCampaign(name);
77}
78
[3225]79
80/**
81   \brief loads a debug campaign for test purposes only.
82   \param the identifier of the campaign.
83   \returns error message if not able to do so.
84*/
[3222]85ErrorMessage GameLoader::loadDebugCampaign(Uint32 campaignID)
[2636]86{
87  switch(campaignID)
88    {
[3629]89      /*
90         Debug Level 0: Debug level used to test the base frame work.
91         As you can see, all storyentity data is allocated before game
92         start. the storyentity will load themselfs shortly before start
93         through the StoryEntity::init() funtion.
94      */
[2636]95    case DEBUG_CAMPAIGN_0:
96      {
97        Campaign* debugCampaign = new Campaign();
[3220]98
[2636]99        World* world0 = new World(DEBUG_WORLD_0);
[3220]100        world0->setNextStoryID(WORLD_ID_1);
[2636]101        debugCampaign->addEntity(world0, WORLD_ID_0);
[3220]102
[2636]103        World* world1 = new World(DEBUG_WORLD_1);
[3727]104        world1->setNextStoryID(WORLD_ID_2);
[2636]105        debugCampaign->addEntity(world1, WORLD_ID_1);
106
[3727]107        World* world2 = new World(DEBUG_WORLD_2);
108        world2->setNextStoryID(WORLD_ID_GAMEEND);
109        debugCampaign->addEntity(world2, WORLD_ID_2);
110
[2636]111        this->currentCampaign = debugCampaign;
112        break;
113      }
114    }
115}
116
[3222]117ErrorMessage GameLoader::start()
[2636]118{
119  if(this->currentCampaign != NULL)
120    this->currentCampaign->start();
121}
122
123
[3222]124ErrorMessage GameLoader::stop()
[2636]125{
126  if(this->currentCampaign != NULL)
127    this->currentCampaign->stop();
128  this->currentCampaign = NULL;
129}
130
131
[3222]132ErrorMessage GameLoader::pause()
[2636]133{
134  this->isPaused = true;
135  if(this->currentCampaign != NULL)
136    this->currentCampaign->pause();
137}
138
139
[3222]140ErrorMessage GameLoader::resume()
[2636]141{
142  this->isPaused = false;
143  if(this->currentCampaign != NULL)
144    this->currentCampaign->resume();
145}
146
[3225]147/**
148   \brief release the mem
149 */
150ErrorMessage GameLoader::destroy()
[2636]151{}
152
153
[3225]154/**
155   \brief reads a campaign definition file into a campaign class
156   \param filename to be loaded
157   \returns the loaded campaign
158
159   this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns
160*/
[2636]161Campaign* GameLoader::fileToCampaign(char *name)
162{
163  /* do not entirely load the campaign. just the current world
164     before start of each world, it has to be initialized so it
165     can load everything it needs into memory then.
166  */
[3989]167 
168  if( name == NULL)
[4003]169    {
170      PRINTF0("No filename specified for loading");
171      return NULL;
172    }
[3989]173 
[4003]174  TiXmlDocument* XMLDoc = new TiXmlDocument( name);
175  // load the campaign document
176  if( !XMLDoc->LoadFile())
177    {
178      // report an error
179      PRINTF0("Error loading XML File: %s @ %d:%d\n", XMLDoc->ErrorDesc(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
180      delete XMLDoc;
181      return NULL;
182    }
[3989]183       
[4003]184  // check basic validity
185  TiXmlElement* root = XMLDoc->RootElement();
186  assert( root != NULL);
[3989]187       
[4003]188  if( strcmp( root->Value(), "Campaign"))
189    {
190      // report an error
[4004]191      PRINTF(0)("Specified XML File is not an orxonox campaign file (Campaign element missing)\n");
[4003]192      delete XMLDoc;
193      return NULL;
194    }
[3989]195       
[4003]196  // construct campaign
197  Campaign* c = new Campaign( root);
[3989]198       
[4003]199  // free the XML data
200  delete XMLDoc;
[3989]201       
[4003]202  return c;
[2636]203}
204
205
206/**
207   \brief handle keyboard commands
208   \param cmd: the command to handle
[3225]209   \returns true if the command was handled by the system
[2636]210*/
211bool GameLoader::worldCommand (Command* cmd)
212{
213  if( !strcmp( cmd->cmd, "up_world"))
214    {
215      if( !cmd->bUp) 
216        {
217          this->nextLevel();
218        }
219      return true;
220    }
221  else if( !strcmp( cmd->cmd, "down_world"))
222    {
223      if( !cmd->bUp)
224        {
225          this->previousLevel();
226        }
227      return true;
228    }
229  else if( !strcmp( cmd->cmd, "pause"))
230    {
231      if( !cmd->bUp)
232        {
233          if(this->isPaused)
234            this->resume();
235          else
236            this->pause();
237        }
238      return true;
239    }
[3220]240  else if( !strcmp( cmd->cmd, "quit"))
241    {
242      if( !cmd->bUp) this->stop();
243      return true;
244    }
[2636]245  return false;
246}
247
[3225]248
249/*
250  \brief this changes to the next level
251*/
[2636]252void GameLoader::nextLevel()
253{
254  if(this->currentCampaign != NULL)
255    this->currentCampaign->nextLevel();
256}
257
[3225]258
259/*
260  \brief change to the previous level - not implemented
261
262  this propably useless
263*/
[2636]264void GameLoader::previousLevel()
265{
266  if(this->currentCampaign != NULL)
267    this->currentCampaign->previousLevel();
268}
[3940]269
270/**
271   \brief add a Factory to the Factory Q
272   \param factory a Factory to be registered
273*/
274void GameLoader::registerFactory( Factory* factory)
275{
276        assert( factory != NULL);
277       
278        PRINTF0("Registered factory for '%s'\n", factory->getClassname());
279       
280        if( first == NULL) first = factory;
281        else first->registerFactory( factory);
282}
283
284/**
285   \brief load a StoryEntity
286   \param element a XMLElement containing all the needed info
287*/
288BaseObject* GameLoader::fabricate( TiXmlElement* element)
289{
290        assert( element != NULL);
291       
292        if( first == NULL)
293        {
294                PRINTF0("GameLoader does not know any factories, fabricate() failed\n");
295                return NULL;
296        }
297       
298        if( element->Value() != NULL)
299        {
300                PRINTF0("Attempting fabrication of a '%s'\n", element->Value());
301                BaseObject* b = first->fabricate( element);
302                if( b == NULL) PRINTF0("Failed to fabricate a '%s'\n", element->Value());
303                else PRINTF0("Successfully fabricated a '%s'\n", element->Value());
304                return b;
305        }
306       
307        PRINTF0("Fabricate failed, TiXmlElement did not contain a value\n");
308       
309        return NULL;
310}
Note: See TracBrowser for help on using the repository browser.