Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7246 was 7246, checked in by rennerc, 19 years ago

prefs are loaded from ini and commandline

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