Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/orxonox.cc @ 9187

Last change on this file since 9187 was 9115, checked in by rennerc, 18 years ago

last server is saved now

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