Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: other minor valgrid found error-fixes

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  ResourceManager::getInstance()->addImageDir(ResourceManager::getInstance()->getFullName("maps/"));
274  ResourceManager::getInstance()->debug();
275
276  PRINT(3)("initializing TextEngine\n");
277  TextEngine::getInstance();
278
279  CDEngine::getInstance();
280  return 0;
281}
282
283/**
284 * initializes miscelaneous features
285 * @return -1 on failure
286 */
287int Orxonox::initMisc()
288{
289  ShellBuffer::getInstance();
290  return 0;
291}
292
293/**
294 *  starts the orxonox game or menu
295 * here is the central orxonox state manager. There are currently two states
296 * - menu
297 * - game-play
298 * both states manage their states themselfs again.
299*/
300void Orxonox::start()
301{
302
303  this->gameLoader = GameLoader::getInstance();
304  this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");
305  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
306  this->gameLoader->init();
307  this->gameLoader->start();
308}
309
310
311/**
312 * handles sprecial events from localinput
313 * @param event: an event not handled by the CommandNode
314 */
315// void Orxonox::graphicsHandler(SDL_Event* event)
316// {
317//   // Handle special events such as reshape, quit, focus changes
318//   switch (event->type)
319//     {
320//     case SDL_VIDEORESIZE:
321//       GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
322//       tmpGEngine->resolutionChanged(event->resize);
323//       break;
324//     }
325// }
326
327
328
329
330
331
332bool showGui = false;
333
334
335
336/**********************************
337*** ORXONOX MAIN STARTING POINT ***
338**********************************/
339/**
340 *
341 *  main function
342 *
343 * here the journey begins
344*/
345int main(int argc, char** argv)
346{
347  // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark.
348  int i;
349  for(i = 1; i < argc; ++i)
350    {
351      if(! strcmp( "--help", argv[i]) || !strcmp("-h", argv[i])) return startHelp(argc, argv);
352      else if(!strcmp( "--benchmark", argv[i]) || !strcmp("-b", argv[i])) return startBenchmarks();
353      else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true;
354      //      else PRINTF(2)("Orxonox does not understand the arguments %s\n", argv[i]);
355    }
356
357  return startOrxonox(argc, argv);
358}
359
360
361
362int startHelp(int argc, char** argv)
363{
364  PRINT(0)("orxonox: starts the orxonox game - rules\n");
365  PRINT(0)("usage: orxonox [arg [arg...]]\n\n");
366  PRINT(0)("valid options:\n");
367  {
368    Gui* gui = new Gui(argc, argv);
369    gui->printHelp();
370    delete gui;
371  }
372  PRINT(0)(" -b|--benchmark:\t\tstarts the orxonox benchmark\n");
373  PRINT(0)(" -h|--help:\t\t\tshows this help\n");
374}
375
376
377
378/**
379 * starts orxonox
380 * @param argc parameters count given to orxonox
381 * @param argv parameters given to orxonox
382 */
383int startOrxonox(int argc, char** argv)
384{
385  // checking for existence of the configuration-files, or if the lock file is still used
386  if (showGui ||
387      !ResourceManager::isFile(DEFAULT_CONFIG_FILE)
388#if DEBUG < 3
389       || ResourceManager::isFile(DEFAULT_LOCK_FILE)
390#endif
391     )
392    {
393      if (ResourceManager::isFile(DEFAULT_LOCK_FILE))
394        ResourceManager::deleteFile(DEFAULT_LOCK_FILE);
395
396      // starting the GUI
397      Gui* gui = new Gui(argc, argv);
398      gui->startGui();
399
400      if (! gui->startOrxonox)
401        return 0;
402
403      delete gui;
404    }
405
406  PRINT(0)(">>> Starting Orxonox <<<\n");
407
408  ResourceManager::touchFile(DEFAULT_LOCK_FILE);
409
410  Orxonox *orx = Orxonox::getInstance();
411
412  if((*orx).init(argc, argv) == -1)
413    {
414      PRINTF(1)("! Orxonox initialization failed\n");
415      return -1;
416    }
417
418    printf("finished inizialisation\n");
419  orx->start();
420
421  delete orx;
422  ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
423
424}
Note: See TracBrowser for help on using the repository browser.