Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the network branche back here
merged with command:
svn merge -r8230:HEAD https://svn.orxonox.net/orxonox/branches/network .
conflicts resolved in favour of the network branche (conflicts were in network)

File size: 14.8 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
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 = 5;
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
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  PRINT(3)
148  (
149    "===================================================\n" \
150    "Thanks for playing orxonox.\n" \
151    "visit: http://www.orxonox.net for new versions.\n" \
152    "===================================================\n" \
153    ORXONOX_LICENSE_SHORT
154  );
155
156  Orxonox::singletonRef = NULL;
157}
158
159/**
160 *  this is a singleton class to prevent duplicates
161 */
162Orxonox* Orxonox::singletonRef = NULL;
163
164// DANGEROUS
165void Orxonox::restart()
166{
167  //   int argc = this->argc;
168  //   char** argv = this->argv;
169  //
170  //   Orxonox *orx = Orxonox::getInstance();
171  //
172  //   delete orx;
173  //
174  //   orx = Orxonox::getInstance();
175  //
176  //   if((*orx).init(argc, argv) == -1)
177  //   {
178  //     PRINTF(1)("! Orxonox initialization failed\n");
179  //     return;
180  //   }
181  //
182  //   printf("finished inizialisation\n");
183  //   orx->start();
184}
185
186/**
187 * @brief this finds the config file
188 * @returns the new config-fileName
189 * Since the config file varies from user to user and since one may want to specify different config files
190 * for certain occasions or platforms this function finds the right config file for every occasion and stores
191 * it's path and name into configfilename
192*/
193const std::string& Orxonox::getConfigFile ()
194{
195  File orxConfFile("orxonox.conf");
196  if (orxConfFile.isFile())
197  {
198    this->configFileName =  "orxonox.conf";
199  }
200  else
201    this->configFileName = File(DEFAULT_CONFIG_FILE).name();
202
203  PRINTF(3)("Parsed Config File: '%s'\n", this->configFileName.c_str());
204  return this->configFileName;
205}
206
207/**
208 * initialize Orxonox with command line
209 */
210int Orxonox::init (int argc, char** argv, const std::string & name, int port)
211{
212  this->argc = argc;
213  this->argv = argv;
214
215  this->serverName = name;
216  this->port = port;
217
218  // initialize the Config-file
219  this->getConfigFile();
220
221  // windows must not write into stdout.txt and stderr.txt
222  /*#ifdef __WIN32__
223  freopen( "CON", "w", stdout );
224  freopen( "CON", "w", stderr );
225  #endif*/
226
227  // initialize everything
228  SDL_Init(0);
229  if( initResources () == -1)
230    return -1;
231  if( initVideo() == -1)
232    return -1;
233  if( initSound() == -1)
234    return -1;
235  if( initInput() == -1)
236    return -1;
237  if( initNetworking () == -1)
238    return -1;
239  if( initMisc () == -1)
240    return -1;
241
242  return 0;
243}
244
245
246/**
247 * initializes SDL and OpenGL
248 */
249int Orxonox::initVideo()
250{
251  PRINTF(3)("> Initializing video\n");
252
253  GraphicsEngine::getInstance();
254
255  GraphicsEngine::getInstance()->initFromPreferences();
256
257  std::string iconName = ResourceManager::getFullName("pictures/fighter-top-32x32.bmp");
258  if (!iconName.empty())
259  {
260    GraphicsEngine::getInstance()->setWindowName(PACKAGE_NAME " " PACKAGE_VERSION, iconName);
261  }
262  return 0;
263}
264
265
266/**
267 * initializes the sound engine
268 */
269int Orxonox::initSound()
270{
271  PRINT(3)("> Initializing sound\n");
272  // SDL_InitSubSystem(SDL_INIT_AUDIO);
273  OrxSound::SoundEngine::getInstance();
274
275  OrxSound::SoundEngine::getInstance()->loadSettings();
276  OrxSound::SoundEngine::getInstance()->initAudio();
277  return 0;
278}
279
280
281/**
282 * initializes input functions
283 */
284int Orxonox::initInput()
285{
286  PRINT(3)("> Initializing input\n");
287
288  EventHandler::getInstance()->
289  EventHandler::getInstance()->init();
290  EventHandler::getInstance()->subscribe(GraphicsEngine::getInstance(), ES_ALL, EV_VIDEO_RESIZE);
291
292  return 0;
293}
294
295
296/**
297 * initializes network system
298 */
299int Orxonox::initNetworking()
300{
301  PRINT(3)("> Initializing networking\n");
302
303  if( this->serverName != "") // we are a client
304  {
305    State::setOnline(true);
306    NetworkManager::getInstance()->establishConnection(this->serverName, port);
307  }
308  else if( this->port > 0)
309  {    // we are a server
310    State::setOnline(true);
311    NetworkManager::getInstance()->createServer(port);
312  }
313  return 0;
314}
315
316//#include "util/loading/dynamic_loader.h"
317
318/**
319 * initializes and loads resource files
320 */
321int Orxonox::initResources()
322{
323  PRINTF(3)("> Initializing resources\n");
324
325  PRINT(3)("initializing ResourceManager\n");
326
327  // init the resource manager
328  std::string dataPath;
329  if ((dataPath = Preferences::getInstance()->getString(CONFIG_SECTION_GENERAL, CONFIG_NAME_DATADIR, ""))!= "")
330  {
331    if (!ResourceManager::getInstance()->setDataDir(dataPath) &&
332        !ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
333    {
334      PRINTF(1)("Data Could not be located in %s\n", dataPath.c_str());
335    }
336  }
337
338  if (!ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
339  {
340    PRINTF(1)("The DataDirectory %s could not be verified\n\nh" \
341              "!!!  Please Change in File %s Section %s Entry %s to a suitable value !!!\n",
342              ResourceManager::getInstance()->getDataDir().c_str(),
343              this->configFileName.c_str(),
344              CONFIG_SECTION_GENERAL,
345              CONFIG_NAME_DATADIR );
346    OrxGui::Gui* gui = new OrxGui::QtGui(argc, argv);
347    gui->startGui();
348    delete gui;
349    exit(-1);
350  }
351  //! @todo this is a hack and should be loadable
352  std::string imageDir = ResourceManager::getInstance()->getFullName("maps");
353  ResourceManager::getInstance()->addImageDir(imageDir);
354  imageDir = ResourceManager::getInstance()->getFullName("pictures");
355  ResourceManager::getInstance()->addImageDir(imageDir);
356
357  //  DynamicLoader::loadDyLib("libtest.so");
358
359  // start the collision detection engine
360  CDEngine::getInstance();
361  return 0;
362}
363
364/**
365 * initializes miscelaneous features
366 * @return -1 on failure
367 */
368int Orxonox::initMisc()
369{
370  OrxShell::ShellBuffer::getInstance();
371  return 0;
372}
373
374/**
375 *  starts the orxonox game or menu
376 * here is the central orxonox state manager. There are currently two states
377 * - menu
378 * - game-play
379 * both states manage their states themselfs again.
380*/
381void Orxonox::start()
382{
383
384  this->gameLoader = GameLoader::getInstance();
385
386  if( this->port != -1)
387    this->gameLoader->loadNetworkCampaign("worlds/DefaultNetworkCampaign.oxc");
388  else
389    this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");                       /* start orxonox in single player mode */
390
391  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
392  this->gameLoader->init();
393  this->gameLoader->start();
394}
395
396
397/**
398 * handles sprecial events from localinput
399 * @param event: an event not handled by the CommandNode
400 */
401// void Orxonox::graphicsHandler(SDL_Event* event)
402// {
403//   // Handle special events such as reshape, quit, focus changes
404//   switch (event->type)
405//     {
406//     case SDL_VIDEORESIZE:
407//       GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
408//       tmpGEngine->resolutionChanged(event->resize);
409//       break;
410//     }
411// }
412
413
414
415
416
417
418bool showGui = false;
419
420/**********************************
421*** ORXONOX MAIN STARTING POINT ***
422**********************************/
423/**
424 *
425 *  main function
426 *
427 * here the journey begins
428*/
429int main(int argc, char** argv)
430{
431  CmdLinePrefsReader prefs;
432
433  IniFilePrefsReader ini(File(DEFAULT_CONFIG_FILE).name());
434  Preferences::getInstance()->setUserIni(File(DEFAULT_CONFIG_FILE).name());
435
436  prefs.parse(argc, argv);
437
438  if ( Preferences::getInstance()->getString("misc", "showLicenseAndExit", "") == "1" )
439  {
440    PRINT(0)(ORXONOX_LICENSE_SHORT);
441    return 0;
442  }
443
444  if ( Preferences::getInstance()->getString("misc", "start-gdb", "0") == "1" )
445  {
446    SignalHandler::getInstance()->doCatch( argv[0], GDB_RUN_IN_FOREGROUND );
447    SignalHandler::getInstance()->registerCallback(EventHandler::releaseMouse, NULL);
448  }
449  else if ( Preferences::getInstance()->getString("misc", "bt-to-file", "1") == "1" )
450  {
451    SignalHandler::getInstance()->doCatch( argv[0], GDB_RUN_WRITE_TO_FILE );
452
453  }
454
455  if( Preferences::getInstance()->getString("game", "showGui", "") == "1" )
456    showGui = true;
457  else if( Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_server" ||
458           Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_client" )
459    return startNetworkOrxonox(argc, argv);
460
461  return startOrxonox(argc, argv, "", -1);
462}
463
464
465
466/**
467 * starts orxonox in network mode
468 * @param argc parameters count given to orxonox
469 * @param argv parameters given to orxonox
470 */
471int startNetworkOrxonox(int argc, char** argv)
472{
473
474  std::string gameType = Preferences::getInstance()->getString( "game", "gameType", "" );
475
476  if ( gameType == "multiplayer_client" )
477  {
478    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
479    std::string host = Preferences::getInstance()->getString( "game", "host", "" );
480
481    if ( host == "" )
482    {
483      printf("You need to specify a host to connect to ( -H <host> )\n");
484      return 1;
485    }
486
487    printf("Starting Orxonox as client: connecting to %s, on port %i\n", host.c_str(), port);
488
489    startOrxonox(argc, argv, host.c_str(), port);
490  }
491  else if ( gameType == "multiplayer_server" )
492  {
493    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
494
495    printf("Starting Orxonox as server: listening on port %i\n", port);
496
497    startOrxonox(argc, argv, "", port);
498  }
499  return 1;
500}
501
502
503
504/**
505 * starts orxonox
506 * @param argc parameters count given to orxonox
507 * @param argv parameters given to orxonox
508 */
509int startOrxonox(int argc, char** argv, const std::string & name, int port)
510{
511  // checking for existence of the configuration-files, or if the lock file is still used
512  if (showGui || (!File("./orxonox.conf").isFile() &&
513                  !File(DEFAULT_CONFIG_FILE).isFile())
514#if DEBUG_LEVEL <= 3 // developers do not need to see the GUI, when orxonox fails
515      || ResourceManager::isFile(DEFAULT_LOCK_FILE)
516#endif
517     )
518  {
519    File lockFile(DEFAULT_LOCK_FILE);
520    if (lockFile.isFile())
521      lockFile.remove();
522
523    // starting the GUI
524    OrxGui::QtGui gui(argc, argv);
525    gui.startGui();
526
527    if (gui.getState() & OrxGui::Gui::Quitting)
528      return 0;
529
530  }
531
532  PRINT(0)(">>> Starting Orxonox <<<\n");
533
534  File(DEFAULT_LOCK_FILE).touch();
535
536  Orxonox *orx = Orxonox::getInstance();
537
538  if( orx->init(argc, argv, name, port) == -1)
539  {
540    PRINTF(1)("! Orxonox initialization failed\n");
541    return -1;
542  }
543
544  printf("finished inizialisation\n");
545  orx->start();
546
547  delete orx;
548  File("~/.orxonox/orxonox.lock").remove();
549  return 0;
550}
Note: See TracBrowser for help on using the repository browser.