Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9802 was 9800, checked in by bensch, 18 years ago

namespaces

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