Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Passing Reference inastead of Pointer to create ShellCommand

File size: 10.5 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#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ORXONOX
28#include "orxonox.h"
29
30#include "gui.h"
31
32#include "world.h"
33#include "ini_parser.h"
34#include "game_loader.h"
35
36//ENGINES
37#include "graphics_engine.h"
38#include "sound_engine.h"
39#include "resource_manager.h"
40#include "cd_engine.h"
41#include "text_engine.h"
42#include "event_handler.h"
43#include "garbage_collector.h"
44
45#include "factory.h"
46#include "fast_factory.h"
47
48#include "benchmark.h"
49
50#include "class_list.h"
51#include "shell_command_class.h"
52#include "shell_command.h"
53#include "shell_buffer.h"
54
55#include "load_param_description.h"
56
57#include <string.h>
58
59int verbose = 4;
60
61using namespace std;
62
63SHELL_COMMAND(restart, Orxonox, restart);
64
65/**
66 *  create a new Orxonox
67
68   In this funcitons only global values are set. The game will not be started here.
69*/
70Orxonox::Orxonox ()
71{
72  this->setClassID(CL_ORXONOX, "Orxonox");
73  this->setName("orxonox-main");
74
75  this->iniParser = NULL;
76
77  this->argc = 0;
78  this->argv = NULL;
79
80  this->configFileName = NULL;
81}
82
83/**
84 *  remove Orxonox from memory
85*/
86Orxonox::~Orxonox ()
87{
88  // game-specific
89  delete GameLoader::getInstance();
90  delete GarbageCollector::getInstance();
91
92  // class-less services/factories
93  delete Factory::getFirst();
94  FastFactory::deleteAll();
95  ShellCommandClass::unregisterAllCommands();
96
97  LoadClassDescription::deleteAllDescriptions();
98
99  // engines
100  delete CDEngine::getInstance();
101  delete SoundEngine::getInstance();
102  delete GraphicsEngine::getInstance(); // deleting the Graphics
103  delete EventHandler::getInstance();
104
105  // handlers
106  delete ResourceManager::getInstance(); // deletes the Resource Manager
107  // output-buffer
108  delete ShellBuffer::getInstance();
109
110  // orxonox class-stuff
111  delete this->iniParser;
112  delete[] this->configFileName;
113
114  SDL_QuitSubSystem(SDL_INIT_TIMER);
115  ClassList::debug();
116
117  PRINT(3)
118      (
119      "===================================================\n" \
120      "Thanks for playing orxonox.\n" \
121      "visit: http://www.orxonox.net for new versions.\n" \
122      "===================================================\n" \
123      ORXONOX_LICENSE_SHORT
124      );
125
126  Orxonox::singletonRef = NULL;
127}
128
129/**
130 *  this is a singleton class to prevent duplicates
131 */
132Orxonox* Orxonox::singletonRef = NULL;
133
134// DANGEROUS
135void Orxonox::restart()
136{
137//   int argc = this->argc;
138//   char** argv = this->argv;
139//
140//   Orxonox *orx = Orxonox::getInstance();
141//
142//   delete orx;
143//
144//   orx = Orxonox::getInstance();
145//
146//   if((*orx).init(argc, argv) == -1)
147//   {
148//     PRINTF(1)("! Orxonox initialization failed\n");
149//     return;
150//   }
151//
152//   printf("finished inizialisation\n");
153//   orx->start();
154}
155
156/**
157 *  this finds the config file
158 * @returns the new config-fileName
159 * Since the config file varies from user to user and since one may want to specify different config files
160 * for certain occasions or platforms this function finds the right config file for every occasion and stores
161 * it's path and name into configfilename
162*/
163const char* Orxonox::getConfigFile ()
164{
165  if (ResourceManager::isFile("orxonox.conf"))
166  {
167    this->configFileName = new char[strlen("orxonox.conf")+1];
168    strcpy(this->configFileName, "orxonox.conf");
169  }
170  else
171    this->configFileName = ResourceManager::homeDirCheck(DEFAULT_CONFIG_FILE);
172  this->iniParser = new IniParser(this->configFileName);
173  PRINTF(3)("Parsed Config File: '%s'\n", this->configFileName);
174}
175
176/**
177 * initialize Orxonox with command line
178 */
179int Orxonox::init (int argc, char** argv)
180{
181  this->argc = argc;
182  this->argv = argv;
183  // parse command line
184  // config file
185
186  // initialize the Config-file
187  this->getConfigFile();
188
189  // initialize everything
190  SDL_Init(SDL_INIT_TIMER);
191  if( initResources () == -1) return -1;
192  if( initVideo() == -1) return -1;
193  if( initSound() == -1) return -1;
194  if( initInput() == -1) return -1;
195  if( initNetworking () == -1) return -1;
196  if( initMisc () == -1) return -1;
197
198  return 0;
199}
200
201/**
202 * initializes SDL and OpenGL
203*/
204int Orxonox::initVideo()
205{
206  PRINTF(3)("> Initializing video\n");
207
208  GraphicsEngine::getInstance();
209
210  GraphicsEngine::getInstance()->initFromIniFile(this->iniParser);
211
212  char* iconName = ResourceManager::getFullName("pictures/fighter-top-32x32.bmp");
213  if (iconName != NULL)
214  {
215    GraphicsEngine::getInstance()->setWindowName(PACKAGE_NAME " " PACKAGE_VERSION, iconName);
216    delete[] iconName;
217  }
218  return 0;
219}
220
221/**
222 * initializes the sound engine
223 */
224int Orxonox::initSound()
225{
226  PRINT(3)("> Initializing sound\n");
227  // SDL_InitSubSystem(SDL_INIT_AUDIO);
228  SoundEngine::getInstance()->initAudio();
229
230  SoundEngine::getInstance()->loadSettings(this->iniParser);
231  return 0;
232}
233
234
235/**
236 * initializes input functions
237 */
238int Orxonox::initInput()
239{
240  PRINT(3)("> Initializing input\n");
241
242  EventHandler::getInstance()->init(this->iniParser);
243  EventHandler::getInstance()->subscribe(GraphicsEngine::getInstance(), ES_ALL, EV_VIDEO_RESIZE);
244
245  return 0;
246}
247
248
249/**
250 * initializes network system
251 */
252int Orxonox::initNetworking()
253{
254  PRINT(3)("> Initializing networking\n");
255
256  printf("  ---Not yet implemented-FIXME--\n");
257  return 0;
258}
259
260
261/**
262 * initializes and loads resource files
263 */
264int Orxonox::initResources()
265{
266  PRINTF(3)("> Initializing resources\n");
267
268  PRINT(3)("initializing ResourceManager\n");
269
270  // init the resource manager
271  const char* dataPath;
272  if ((dataPath = this->iniParser->getVar(CONFIG_NAME_DATADIR, CONFIG_SECTION_DATA))!= NULL)
273  {
274    if (!ResourceManager::getInstance()->setDataDir(dataPath) &&
275         !ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
276    {
277      PRINTF(1)("Data Could not be located in %s\n", dataPath);
278    }
279  }
280
281  if (!ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
282  {
283    PRINTF(1)("The DataDirectory %s could not be verified\n\nh" \
284              "!!!  Please Change in File %s Section %s Entry %s to a suitable value !!!\n",
285              ResourceManager::getInstance()->getDataDir(),
286              this->configFileName,
287              CONFIG_SECTION_DATA,
288              CONFIG_NAME_DATADIR );
289    Gui* gui = new Gui(argc, argv);
290    gui->startGui();
291    delete gui;
292    exit(-1);
293  }
294   //! @todo this is a hack and should be loadable
295  char* imageDir = ResourceManager::getInstance()->getFullName("maps");
296  ResourceManager::getInstance()->addImageDir(imageDir);
297  delete[] imageDir;
298
299  // start the collision detection engine
300  CDEngine::getInstance();
301  return 0;
302}
303
304/**
305 * initializes miscelaneous features
306 * @return -1 on failure
307 */
308int Orxonox::initMisc()
309{
310  ShellBuffer::getInstance();
311  return 0;
312}
313
314/**
315 *  starts the orxonox game or menu
316 * here is the central orxonox state manager. There are currently two states
317 * - menu
318 * - game-play
319 * both states manage their states themselfs again.
320*/
321void Orxonox::start()
322{
323
324  this->gameLoader = GameLoader::getInstance();
325  this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");
326  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
327  this->gameLoader->init();
328  this->gameLoader->start();
329}
330
331
332/**
333 * handles sprecial events from localinput
334 * @param event: an event not handled by the CommandNode
335 */
336// void Orxonox::graphicsHandler(SDL_Event* event)
337// {
338//   // Handle special events such as reshape, quit, focus changes
339//   switch (event->type)
340//     {
341//     case SDL_VIDEORESIZE:
342//       GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
343//       tmpGEngine->resolutionChanged(event->resize);
344//       break;
345//     }
346// }
347
348
349
350
351
352
353bool showGui = false;
354
355
356
357/**********************************
358*** ORXONOX MAIN STARTING POINT ***
359**********************************/
360/**
361 *
362 *  main function
363 *
364 * here the journey begins
365*/
366int main(int argc, char** argv)
367{
368  // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark.
369  int i;
370  for(i = 1; i < argc; ++i)
371    {
372      if(! strcmp( "--help", argv[i]) || !strcmp("-h", argv[i])) return startHelp(argc, argv);
373      else if(!strcmp( "--benchmark", argv[i]) || !strcmp("-b", argv[i])) return startBenchmarks();
374      else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true;
375      //      else PRINTF(2)("Orxonox does not understand the arguments %s\n", argv[i]);
376    }
377
378  return startOrxonox(argc, argv);
379}
380
381
382
383int startHelp(int argc, char** argv)
384{
385  PRINT(0)("orxonox: starts the orxonox game - rules\n");
386  PRINT(0)("usage: orxonox [arg [arg...]]\n\n");
387  PRINT(0)("valid options:\n");
388  {
389    Gui* gui = new Gui(argc, argv);
390    gui->printHelp();
391    delete gui;
392  }
393  PRINT(0)(" -b|--benchmark:\t\tstarts the orxonox benchmark\n");
394  PRINT(0)(" -h|--help:\t\t\tshows this help\n");
395}
396
397
398
399/**
400 * starts orxonox
401 * @param argc parameters count given to orxonox
402 * @param argv parameters given to orxonox
403 */
404int startOrxonox(int argc, char** argv)
405{
406  // checking for existence of the configuration-files, or if the lock file is still used
407  if (showGui || (!ResourceManager::isFile("./orxonox.conf") &&
408      !ResourceManager::isFile(DEFAULT_CONFIG_FILE))
409#if DEBUG < 3 // developers do not need to see the GUI, when orxonox fails
410       || ResourceManager::isFile(DEFAULT_LOCK_FILE)
411#endif
412     )
413    {
414      if (ResourceManager::isFile(DEFAULT_LOCK_FILE))
415        ResourceManager::deleteFile(DEFAULT_LOCK_FILE);
416
417      // starting the GUI
418      Gui* gui = new Gui(argc, argv);
419      gui->startGui();
420
421      if (! gui->startOrxonox)
422        return 0;
423
424      delete gui;
425    }
426
427  PRINT(0)(">>> Starting Orxonox <<<\n");
428
429  ResourceManager::touchFile(DEFAULT_LOCK_FILE);
430
431  Orxonox *orx = Orxonox::getInstance();
432
433  if(orx->init(argc, argv) == -1)
434    {
435      PRINTF(1)("! Orxonox initialization failed\n");
436      return -1;
437    }
438
439    printf("finished inizialisation\n");
440  orx->start();
441
442  delete orx;
443  ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
444}
Note: See TracBrowser for help on using the repository browser.