Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: more fixes due to valgrind

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