Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/orxonox.cc @ 9766

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

new_class_id: much faster and nicer self-cleaning Factory

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