Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/preferences/src/orxonox.cc @ 7248

Last change on this file since 7248 was 7248, checked in by rennerc, 18 years ago

removed some more iniParsers

File size: 13.4 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 "parser/ini_parser/ini_parser.h"
35#include "util/loading/game_loader.h"
36
37//ENGINES
38#include "graphics_engine.h"
39#include "sound_engine.h"
40#include "util/loading/resource_manager.h"
41#include "cd_engine.h"
42#include "text_engine.h"
43#include "event_handler.h"
44
45#include "util/loading/factory.h"
46#include "fast_factory.h"
47
48#include "benchmark.h"
49
50#include "class_list.h"
51#include "shell_command_class.h"
52#include "shell_command.h"
53#include "shell_buffer.h"
54
55#include "util/loading/load_param_description.h"
56
57#include "network_manager.h"
58
59#include "state.h"
60#include "lib/parser/preferences/cmd_line_prefs_reader.h"
61#include "lib/parser/preferences/ini_file_prefs_reader.h"
62#include <string.h>
63
64int verbose = 4;
65
66using namespace std;
67
68SHELL_COMMAND(restart, Orxonox, restart);
69
70/**
71 *  create a new Orxonox
72
73   In this funcitons only global values are set. The game will not be started here.
74*/
75Orxonox::Orxonox ()
76{
77  this->setClassID(CL_ORXONOX, "Orxonox");
78  this->setName("orxonox-main");
79
80  this->argc = 0;
81  this->argv = NULL;
82
83  /* this way, there is no network enabled: */
84  this->serverName = NULL;
85  this->port = -1;
86
87  this->configFileName = "";
88}
89
90/**
91 *  remove Orxonox from memory
92*/
93Orxonox::~Orxonox ()
94{
95  // game-specific
96  delete GameLoader::getInstance();
97
98  // class-less services/factories
99  Factory::deleteFactories();
100  FastFactory::deleteAll();
101  ShellCommandClass::unregisterAllCommands();
102
103  LoadClassDescription::deleteAllDescriptions();
104
105  // engines
106  delete CDEngine::getInstance();
107  delete SoundEngine::getInstance();
108  delete GraphicsEngine::getInstance(); // deleting the Graphics
109  delete EventHandler::getInstance();
110
111  // handlers
112  delete ResourceManager::getInstance(); // deletes the Resource Manager
113  // output-buffer
114  delete ShellBuffer::getInstance();
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 * @brief 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 std::string& Orxonox::getConfigFile ()
166{
167  if (ResourceManager::isFile("orxonox.conf"))
168  {
169    this->configFileName =  "orxonox.conf";
170  }
171  else
172    this->configFileName = ResourceManager::homeDirCheck(DEFAULT_CONFIG_FILE);
173
174  PRINTF(3)("Parsed Config File: '%s'\n", this->configFileName);
175}
176
177/**
178 * initialize Orxonox with command line
179 */
180int Orxonox::init (int argc, char** argv, const char* name, int port)
181{
182  this->argc = argc;
183  this->argv = argv;
184
185  this->serverName = name;
186  this->port = port;
187
188  // initialize the Config-file
189  this->getConfigFile();
190
191  // windows must not write into stdout.txt and stderr.txt
192  /*#ifdef __WIN32__
193  freopen( "CON", "w", stdout );
194  freopen( "CON", "w", stderr );
195  #endif*/
196
197  // initialize everything
198  SDL_Init(0);
199  if( initResources () == -1)
200    return -1;
201  if( initVideo() == -1)
202    return -1;
203  if( initSound() == -1)
204    return -1;
205  if( initInput() == -1)
206    return -1;
207  if( initNetworking () == -1)
208    return -1;
209  if( initMisc () == -1)
210    return -1;
211
212  return 0;
213}
214
215
216/**
217 * initializes SDL and OpenGL
218 */
219int Orxonox::initVideo()
220{
221  PRINTF(3)("> Initializing video\n");
222
223  GraphicsEngine::getInstance();
224
225  GraphicsEngine::getInstance()->initFromPreferences();
226
227  std::string iconName = ResourceManager::getFullName("pictures/fighter-top-32x32.bmp");
228  if (!iconName.empty())
229  {
230    GraphicsEngine::getInstance()->setWindowName(PACKAGE_NAME " " PACKAGE_VERSION, iconName);
231  }
232  return 0;
233}
234
235
236/**
237 * initializes the sound engine
238 */
239int Orxonox::initSound()
240{
241  PRINT(3)("> Initializing sound\n");
242  // SDL_InitSubSystem(SDL_INIT_AUDIO);
243  SoundEngine::getInstance();
244
245  SoundEngine::getInstance()->loadSettings();
246  SoundEngine::getInstance()->initAudio();
247  return 0;
248}
249
250
251/**
252 * initializes input functions
253 */
254int Orxonox::initInput()
255{
256  PRINT(3)("> Initializing input\n");
257
258  EventHandler::getInstance()->init();
259  EventHandler::getInstance()->subscribe(GraphicsEngine::getInstance(), ES_ALL, EV_VIDEO_RESIZE);
260
261  return 0;
262}
263
264
265/**
266 * initializes network system
267 */
268int Orxonox::initNetworking()
269{
270  PRINT(3)("> Initializing networking\n");
271
272  if( this->serverName != NULL) // we are a client
273  {
274    State::setOnline(true);
275    NetworkManager::getInstance()->establishConnection(this->serverName, port);
276  }
277  else if( this->port > 0) {    // we are a server
278    State::setOnline(true);
279    NetworkManager::getInstance()->createServer(port);
280  }
281  return 0;
282}
283
284#include "util/loading/dynamic_loader.h"
285
286/**
287 * initializes and loads resource files
288 */
289int Orxonox::initResources()
290{
291  PRINTF(3)("> Initializing resources\n");
292
293  PRINT(3)("initializing ResourceManager\n");
294
295  // init the resource manager
296  std::string dataPath;
297  if ((dataPath = Preferences::getInstance()->getString(CONFIG_SECTION_DATA, CONFIG_NAME_DATADIR, ""))!= "")
298  {
299    if (!ResourceManager::getInstance()->setDataDir(dataPath) &&
300        !ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
301    {
302      PRINTF(1)("Data Could not be located in %s\n", dataPath.c_str());
303    }
304  }
305
306  if (!ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
307  {
308    PRINTF(1)("The DataDirectory %s could not be verified\n\nh" \
309              "!!!  Please Change in File %s Section %s Entry %s to a suitable value !!!\n",
310    ResourceManager::getInstance()->getDataDir().c_str(),
311    this->configFileName.c_str(),
312              CONFIG_SECTION_DATA,
313              CONFIG_NAME_DATADIR );
314    Gui* gui = new Gui(argc, argv);
315    gui->startGui();
316    delete gui;
317    exit(-1);
318  }
319  //! @todo this is a hack and should be loadable
320  std::string imageDir = ResourceManager::getInstance()->getFullName("maps");
321  ResourceManager::getInstance()->addImageDir(imageDir);
322  imageDir = ResourceManager::getInstance()->getFullName("pictures");
323  ResourceManager::getInstance()->addImageDir(imageDir);
324
325  DynamicLoader::loadDyLib("libtest.so");
326
327  // start the collision detection engine
328  CDEngine::getInstance();
329  return 0;
330}
331
332/**
333 * initializes miscelaneous features
334 * @return -1 on failure
335 */
336int Orxonox::initMisc()
337{
338  ShellBuffer::getInstance();
339  return 0;
340}
341
342/**
343 *  starts the orxonox game or menu
344 * here is the central orxonox state manager. There are currently two states
345 * - menu
346 * - game-play
347 * both states manage their states themselfs again.
348*/
349void Orxonox::start()
350{
351
352  this->gameLoader = GameLoader::getInstance();
353
354  if( this->port != -1)
355    this->gameLoader->loadNetworkCampaign("worlds/DefaultNetworkCampaign.oxc");
356  else
357    this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");                       /* start orxonox in single player mode */
358
359  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
360  this->gameLoader->init();
361  this->gameLoader->start();
362}
363
364
365/**
366 * handles sprecial events from localinput
367 * @param event: an event not handled by the CommandNode
368 */
369// void Orxonox::graphicsHandler(SDL_Event* event)
370// {
371//   // Handle special events such as reshape, quit, focus changes
372//   switch (event->type)
373//     {
374//     case SDL_VIDEORESIZE:
375//       GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
376//       tmpGEngine->resolutionChanged(event->resize);
377//       break;
378//     }
379// }
380
381
382
383
384
385
386bool showGui = false;
387
388
389
390/**********************************
391*** ORXONOX MAIN STARTING POINT ***
392**********************************/
393/**
394 *
395 *  main function
396 *
397 * here the journey begins
398*/
399int main(int argc, char** argv)
400{
401  CmdLinePrefsReader prefs;
402 
403  IniFilePrefsReader ini(ResourceManager::homeDirCheck(DEFAULT_CONFIG_FILE));
404 
405  prefs.parse(argc, argv);
406 
407  //Preferences::getInstance()->debug();
408 
409  int i;
410  for(i = 1; i < argc; ++i)
411  {
412    if(     !strcmp( "--help", argv[i])     || !strcmp("-h", argv[i]))
413      return showHelp(argc, argv);
414    else if(!strcmp( "--gui", argv[i])      || !strcmp("-g", argv[i]))
415      showGui = true;
416    else if(!strcmp( "--client", argv[i])   || !strcmp("-c", argv[i]))
417      return startNetworkOrxonox(argc, argv);
418    else if(!strcmp( "--server", argv[i])   || !strcmp("-s", argv[i]))
419      return startNetworkOrxonox(argc, argv);
420    else if(!strcmp( "--license", argv[i])  || !strcmp("-l", argv[i]))
421      return PRINT(0)(ORXONOX_LICENSE_SHORT);
422  }
423
424  return startOrxonox(argc, argv, NULL, -1);
425  return 0;
426}
427
428
429
430int showHelp(int argc, char** argv)
431{
432  PRINT(0)("Orxonox Version %s\n", PACKAGE_VERSION);
433  PRINT(0)(" Starts Orxonox - The most furious 3D Action Game :)\n");
434  PRINT(0)("\n");
435  PRINT(0)("Common options:\n");
436  PRINT(0)(" -g, --gui                        starts the orxonox with the configuration GUI \n");
437  PRINT(0)(" -h, --help                       shows this help\n");
438  PRINT(0)("\n");
439  PRINT(0)("Network options:\n");
440  PRINT(0)(" -s, --server [port]              starts Orxonox and listens on the [port] for players\n");
441  PRINT(0)(" -c, --client [hostname] [port]   starts Orxonox as a Client\n");
442  PRINT(0)(" -c, --client [ip address] [port] starts Orxonox as a Client\n");
443  PRINT(0)("\n");
444  PRINT(0)("Other options:\n");
445  PRINT(0)("     --license     prints the licence and exit\n\n");
446  PRINT(0)("\n");
447
448  //   {
449  //     Gui* gui = new Gui(argc, argv);
450  //     gui->printHelp();
451  //     delete gui;
452  //   }
453}
454
455
456
457
458/**
459 * starts orxonox in network mode
460 * @param argc parameters count given to orxonox
461 * @param argv parameters given to orxonox
462 */
463int startNetworkOrxonox(int argc, char** argv)
464{
465
466  int i;
467  for(i = 0; i < argc; ++i )
468  {
469    if( !strcmp( "--client", argv[i]) || !strcmp("-c", argv[i]))
470    {
471      if( argc <= (i+2))
472      {
473        printf(" Wrong arguments try following notations:\n");
474        printf("   --client [server ip address] [port number]\n");
475        printf("   --client [dns name] [port number]\n");
476        return 0;
477      }
478
479      const char* name = argv[i+1];
480      int port = atoi(argv[i+2]);
481      printf("Starting Orxonox as client: connecting to %s, on port %i\n", name, port);
482
483      startOrxonox(argc, argv, name, port);
484    }
485    else if( !strcmp( "--server", argv[i]) || !strcmp("-s", argv[i]))
486    {
487      if( argc <= (i+1))
488      {
489        printf(" Wrong arguments try following notations:\n");
490        printf("   --server [port number]\n");
491        return 0;
492      }
493
494      int port = atoi(argv[i+1]);
495      printf("Starting Orxonox as server, listening on port %i\n", port);
496
497      startOrxonox(argc, argv, NULL, port);
498    }
499  }
500}
501
502
503
504/**
505 * starts orxonox
506 * @param argc parameters count given to orxonox
507 * @param argv parameters given to orxonox
508 */
509int startOrxonox(int argc, char** argv, const char* name, int port)
510{
511  // checking for existence of the configuration-files, or if the lock file is still used
512  if (showGui || (!ResourceManager::isFile("./orxonox.conf") &&
513                  !ResourceManager::isFile(DEFAULT_CONFIG_FILE))
514#if DEBUG < 3 // developers do not need to see the GUI, when orxonox fails
515      || ResourceManager::isFile(DEFAULT_LOCK_FILE)
516#endif
517     )
518  {
519    if (ResourceManager::isFile(DEFAULT_LOCK_FILE))
520      ResourceManager::deleteFile(DEFAULT_LOCK_FILE);
521
522    // starting the GUI
523    Gui* gui = new Gui(argc, argv);
524    gui->startGui();
525
526    if (! gui->startOrxonox)
527      return 0;
528
529    delete gui;
530  }
531
532  PRINT(0)(">>> Starting Orxonox <<<\n");
533
534  ResourceManager::touchFile(DEFAULT_LOCK_FILE);
535
536  Orxonox *orx = Orxonox::getInstance();
537
538  if( orx->init(argc, argv, name, port) == -1)
539  {
540    PRINTF(1)("! Orxonox initialization failed\n");
541    return -1;
542  }
543
544  printf("finished inizialisation\n");
545  orx->start();
546
547  delete orx;
548  ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
549}
Note: See TracBrowser for help on using the repository browser.