Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8749 was 8749, checked in by bensch, 19 years ago

orxonox/trunk: fallback test for ORXONOX-data-directory

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