Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: POD's

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