Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/util/loading/game_loader.cc @ 6000

Last change on this file since 6000 was 6000, checked in by patrick, 18 years ago

network: implemented the gameloader network loading functions again

btw: this is commit 6k so take yourself a minute time to think about Orxonox and all the furiouse coders. Then lay back and take a beer to celebrate it!

On 10k we will make a party.. count on it! :D

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