Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9755 was 9715, checked in by bensch, 18 years ago

renamed newclassid to classid and newobjectlist to objectlist

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