Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7440 was 7440, checked in by bensch, 18 years ago

orxonox/trunk: merged the Signal-Handler-Branche back to the TRUNK

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