Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added some command line arguments

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