Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: implemented a new kind of ini-parser
this parser first reads the file, and then one can search through without accessing the file anymore

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