Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/orxonox.cc @ 4135

Last change on this file since 4135 was 4135, checked in by bensch, 19 years ago

orxonox/trunk: minor

File size: 9.4 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software Foundation,
18   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
19
20
21   ### File Specific:
22   main-programmer: Patrick Boenzli
23   co-programmer: Christian Meyer
24   co-programmer: Benjamin Grauer: injected ResourceManager/GraphicsEngine/GUI
25*/
26
27#include "orxonox.h"
28
29#include "gui.h"
30
31#include "world.h"
32#include "data_tank.h"
33#include "command_node.h"
34#include "ini_parser.h"
35#include "game_loader.h"
36#include "graphics_engine.h"
37#include "resource_manager.h"
38#include "text_engine.h"
39#include "factory.h"
40#include "benchmark.h"
41
42#include <string.h>
43
44int verbose = 4;
45
46using namespace std;
47
48/**
49   \brief create a new Orxonox
50
51   In this funcitons only global values are set. The game will not be started here.
52*/
53Orxonox::Orxonox ()
54{
55  this->pause = false;
56
57  this->world = NULL;
58  this->resources = NULL;
59  this->localinput = NULL;
60
61  this->argc = 0;
62  this->argv = NULL;
63}
64
65/**
66   \brief remove Orxonox from memory
67*/
68Orxonox::~Orxonox () 
69{
70  int i =0;
71  Orxonox::singletonRef = NULL;
72  if( world != NULL) delete world;
73  if( localinput != NULL) delete localinput;
74  if( resources != NULL) delete resources;
75  delete GraphicsEngine::getInstance(); // deleting the Graphics
76  delete ResourceManager::getInstance(); // deletes the Resource Manager
77  delete TextEngine::getInstance();
78}
79
80/** \brief this is a singleton class to prevent duplicates */
81Orxonox* Orxonox::singletonRef = 0;
82
83/**
84   \returns reference or new Object of Orxonox if not existent.
85*/
86Orxonox* Orxonox::getInstance (void)
87{
88  if (singletonRef == NULL)
89    singletonRef = new Orxonox();
90  return singletonRef;
91}
92
93/**
94   \brief this finds the config file
95   
96   Since the config file varies from user to user and since one may want to specify different config files
97   for certain occasions or platforms this function finds the right config file for every occasion and stores
98   it's path and name into configfilename
99*/
100void Orxonox::getConfigFile (int argc, char** argv)
101{
102  strcpy (configfilename, "~/.orxonox/orxonox.conf");
103}
104
105/**
106   \brief initialize Orxonox with command line
107*/
108int Orxonox::init (int argc, char** argv)
109{
110  this->argc = argc;
111  this->argv = argv;
112  // parse command line
113  // config file
114 
115  getConfigFile (argc, argv);
116  SDL_Init (SDL_INIT_TIMER);
117  // initialize everything
118  printf("> Initializing resources\n");
119  if( initResources () == -1) return -1;
120
121  if( initVideo() == -1) return -1;
122  if( initSound() == -1) return -1;
123  printf("> Initializing input\n");
124  if( initInput() == -1) return -1;
125  printf("> Initializing networking\n");
126  if( initNetworking () == -1) return -1;
127  //printf("> Initializing world\n");
128  //if( init_world () == -1) return -1; PB: world will be initialized when started
129 
130  return 0;
131}
132
133/**
134   \brief initializes SDL and OpenGL
135*/
136int Orxonox::initVideo() 
137{
138  PRINTF(3)("> Initializing video\n");
139 
140  GraphicsEngine::getInstance();
141
142  /*
143 int i;
144  for(i = 1; i < this->argc; ++i)
145    {
146      if (!strncmp (this->argv[i], "-r", 2))
147        {
148          char* tmp = strchr (argv[i], 'x');
149          //      GraphicsEngine::getInstance()->setResolution(atoi(argv[i]), atoi(tmp+1), 24);
150        }
151      else if (!strncmp(this->argv[i], "--resolution=", 13))
152        {
153        }
154    }
155  */
156  return 0;
157}
158
159
160/**
161   \brief initializes the sound engine
162*/
163int Orxonox::initSound() 
164{
165  printf("> Initializing sound\n");
166  // SDL_Init(SDL_INIT_AUDIO);
167  printf("Not yet implemented\n");
168  return 0;
169}
170
171
172/**
173   \brief initializes input functions
174*/
175int Orxonox::initInput() 
176{
177  // create localinput
178  localinput = new CommandNode(configfilename);
179 
180  return 0;
181}
182
183
184/**
185   \brief initializes network system
186*/
187int Orxonox::initNetworking() 
188{
189  printf("Not yet implemented\n");
190  return 0;
191}
192
193
194/**
195   \brief initializes and loads resource files
196*/
197int Orxonox::initResources() 
198{
199  PRINT(3)("initializing ResourceManager\n");
200  resourceManager = ResourceManager::getInstance();
201
202  // create parser
203  IniParser parser (DEFAULT_CONFIG_FILE);
204  if( parser.getSection (CONFIG_SECTION_DATA) == -1)
205    {
206      PRINTF(1)("Could not find Section %s in %s\n", CONFIG_SECTION_DATA, DEFAULT_CONFIG_FILE);
207      return -1;
208    }
209  char namebuf[256];
210  char valuebuf[256];
211  memset (namebuf, 0, 256);
212  memset (valuebuf, 0, 256);
213 
214  while( parser.nextVar (namebuf, valuebuf) != -1)
215    {
216      if (!strcmp(namebuf, CONFIG_NAME_DATADIR))
217        {
218          //  printf("Not yet implemented\n");
219          if (!resourceManager->setDataDir(valuebuf))
220            {
221              PRINTF(1)("Data Could not be located\n");
222              exit(-1);
223            }
224        }
225     
226      memset (namebuf, 0, 256);
227      memset (valuebuf, 0, 256);
228    }
229 
230  if (!resourceManager->checkDataDir(DEFAULT_DATA_DIR_CHECKFILE))
231    {
232      PRINTF(1)("The DataDirectory %s could not be verified\nPlease Change in File %s Section %s Entry %s to a suitable value\n",
233                resourceManager->getDataDir(),
234                DEFAULT_CONFIG_FILE,
235                CONFIG_SECTION_DATA,
236                CONFIG_NAME_DATADIR);
237      exit(-1);
238    }
239
240
241  PRINT(3)("initializing TextEngine\n");
242  TextEngine::getInstance();
243
244  return 0;
245}
246
247
248/**
249   \brief initializes the world
250*/
251int Orxonox::initWorld() 
252{
253  //world = new World();
254 
255  // TO DO: replace this with a menu/intro
256  //world->load_debug_level();
257 
258  return 0;
259}
260
261
262/**
263   \brief starts the orxonox game or menu
264
265   here is the central orxonox state manager. There are currently two states
266   - menu
267   - game-play
268   both states manage their states themselfs again.
269*/
270void Orxonox::start()
271{
272 
273  this->gameLoader = GameLoader::getInstance();
274  this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");
275  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
276  this->gameLoader->init();
277  this->gameLoader->start();
278}
279
280
281/**
282   \brief exits Orxonox
283*/
284void Orxonox::quitGame() 
285{
286  bQuitOrxonox = true;
287}
288
289
290
291/**
292   \brief handles sprecial events from localinput
293   \param event: an event not handled by the CommandNode
294*/
295void Orxonox::eventHandler(SDL_Event* event)
296{
297  // Handle special events such as reshape, quit, focus changes
298  switch (event->type)
299    {
300    case SDL_VIDEORESIZE:
301      GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
302      tmpGEngine->resolutionChanged(&event->resize);
303      break;
304    }
305}
306 
307
308/**
309   \brief handle keyboard commands that are not meant for WorldEntities
310   \param cmd: the command to handle
311   \return true if the command was handled by the system or false if it may be passed to the WorldEntities
312*/
313bool Orxonox::systemCommand(Command* cmd)
314{
315  /*
316  if( !strcmp( cmd->cmd, "quit"))
317    {
318      if( !cmd->bUp) this->gameLoader->stop();
319      return true;
320    }
321  return false;
322  */
323  return false;
324}
325
326/**
327   \brief retrieve a pointer to the local CommandNode
328   \return a pointer to localinput
329*/
330CommandNode* Orxonox::getLocalInput()
331{
332  return localinput;
333}
334
335
336/**
337   \brief retrieve a pointer to the local World
338   \return a pointer to world
339*/
340World* Orxonox::getWorld()
341{
342  return world;
343}
344
345/**
346   \return The reference of the SDL-screen of orxonox
347*/
348SDL_Surface* Orxonox::getScreen ()
349{
350  return this->screen;
351}
352
353
354bool showGui = false;
355
356/**
357   \brief main function
358
359   here the journey begins
360*/
361int main(int argc, char** argv) 
362{ 
363
364  // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark.
365  int i;
366  for(i = 1; i < argc; ++i)
367    {
368      if(! strcmp( "--help", argv[i]) || !strcmp("-h", argv[i])) return startHelp(argc, argv);
369      else if(!strcmp( "--benchmark", argv[i]) || !strcmp("-b", argv[i])) return startBenchmarks();
370      else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true;
371      //      else PRINTF(2)("Orxonox does not understand the arguments %s\n", argv[i]);
372    }
373
374  return startOrxonox(argc, argv);
375}
376
377
378
379int startHelp(int argc, char** argv)
380{
381  PRINT(0)("orxonox: starts the orxonox game - rules\n");
382  PRINT(0)("usage: orxonox [arg [arg...]]\n\n");
383  PRINT(0)("valid options:\n");
384  {
385    Gui* gui = new Gui(argc, argv);
386    gui->printHelp();
387    delete gui;
388  }
389  PRINT(0)(" -b|--benchmark:\t\tstarts the orxonox benchmark\n");
390  PRINT(0)(" -h|--help:\t\t\tshows this help\n");
391}
392
393
394int startOrxonox(int argc, char** argv)
395{
396  // checking for existence of the configuration-files
397  if (showGui ||
398      !ResourceManager::isFile("~/.orxonox/orxonox.conf") ||
399      ResourceManager::isFile("~/.orxonox/orxonox.lock"))
400    {
401      if (ResourceManager::isFile("~/.orxonox/orxonox.lock"))
402        ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
403     
404      // starting the GUI
405      Gui* gui = new Gui(argc, argv);
406      gui->startGui();
407
408      if (! gui->startOrxonox)
409        return 0;
410     
411      delete gui;
412    }
413 
414  PRINT(0)(">>> Starting Orxonox <<<\n");
415
416  ResourceManager::touchFile("~/.orxonox/orxonox.lock");
417
418  Orxonox *orx = Orxonox::getInstance();
419 
420  if((*orx).init(argc, argv) == -1)
421    {
422      PRINTF(1)("! Orxonox initialization failed\n");
423      return -1;
424    }
425 
426  orx->start();
427 
428  delete orx;
429  ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
430 
431}
Note: See TracBrowser for help on using the repository browser.