Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

you can register commandline args now

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