Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

option -l/—license is now handled in orxonox.cc

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