Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/powerups/src/orxonox.cc @ 6175

Last change on this file since 6175 was 5985, checked in by manuel, 19 years ago

merge: factory has now create from class name string function (svn merge -r 5955:HEAD ../trunk/ powerups/)

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