Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/orxonox.cc @ 9275

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

orxonox/trunk: better signal handler.
patched by chrigi

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