Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: LoadParam had some major memory-leak… fixed them :)

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