Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: clone-functions

File size: 15.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 = 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
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)
305  {    // we are a server
306    State::setOnline(true);
307    NetworkManager::getInstance()->createServer(port);
308  }
309  return 0;
310}
311
312//#include "util/loading/dynamic_loader.h"
313
314/**
315 * initializes and loads resource files
316 */
317int Orxonox::initResources()
318{
319  PRINTF(3)("> Initializing resources\n");
320
321  PRINT(3)("initializing ResourceManager\n");
322
323  // init the resource manager
324  std::string dataPath;
325  if ((dataPath = Preferences::getInstance()->getString(CONFIG_SECTION_GENERAL, CONFIG_NAME_DATADIR, ""))!= "")
326  {
327    if (!ResourceManager::getInstance()->setDataDir(dataPath) &&
328        !ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
329    {
330      PRINTF(1)("Data Could not be located in %s\n", dataPath.c_str());
331    }
332  }
333
334  if (!ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
335  {
336    PRINTF(1)("The DataDirectory %s could not be verified\n\nh" \
337              "!!!  Please Change in File %s Section %s Entry %s to a suitable value !!!\n",
338              ResourceManager::getInstance()->getDataDir().c_str(),
339              this->configFileName.c_str(),
340              CONFIG_SECTION_GENERAL,
341              CONFIG_NAME_DATADIR );
342    OrxGui::Gui* gui = new OrxGui::QtGui(argc, argv);
343    gui->startGui();
344    delete gui;
345    exit(-1);
346  }
347  //! @todo this is a hack and should be loadable
348  std::string imageDir = ResourceManager::getInstance()->getFullName("maps");
349  ResourceManager::getInstance()->addImageDir(imageDir);
350  imageDir = ResourceManager::getInstance()->getFullName("pictures");
351  ResourceManager::getInstance()->addImageDir(imageDir);
352
353  //  DynamicLoader::loadDyLib("libtest.so");
354
355  // start the collision detection engine
356  CDEngine::getInstance();
357  return 0;
358}
359
360/**
361 * initializes miscelaneous features
362 * @return -1 on failure
363 */
364int Orxonox::initMisc()
365{
366  OrxShell::ShellBuffer::getInstance();
367  return 0;
368}
369
370/**
371 *  starts the orxonox game or menu
372 * here is the central orxonox state manager. There are currently two states
373 * - menu
374 * - game-play
375 * both states manage their states themselfs again.
376*/
377void Orxonox::start()
378{
379
380  this->gameLoader = GameLoader::getInstance();
381
382  if( this->port != -1)
383    this->gameLoader->loadNetworkCampaign("worlds/DefaultNetworkCampaign.oxc");
384  else
385    this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");                       /* start orxonox in single player mode */
386
387  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
388  this->gameLoader->init();
389  this->gameLoader->start();
390}
391
392
393/**
394 * handles sprecial events from localinput
395 * @param event: an event not handled by the CommandNode
396 */
397// void Orxonox::graphicsHandler(SDL_Event* event)
398// {
399//   // Handle special events such as reshape, quit, focus changes
400//   switch (event->type)
401//     {
402//     case SDL_VIDEORESIZE:
403//       GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
404//       tmpGEngine->resolutionChanged(event->resize);
405//       break;
406//     }
407// }
408
409
410
411
412
413
414bool showGui = false;
415
416/// HACK HACK TEST
417
418
419#include "executor/executor_functional.h"
420
421#define EXECUTOR_FUNCTIONAL_USE_STATIC
422#include "executor/executor_functional.h"
423
424class TestClass : public BaseObject
425{
426public:
427  TestClass() {};
428
429  void printTest() { printf ("TEST\n"); };
430  void printTestInt(int i) { printf ("%d\n", i); };
431  void printTestBool(bool b) { printf("%d\n", (int)b); };
432  void printTwoVals(bool b, int i) { printf ("%d %d\n", b, i); };
433
434  static void printStatic() { printf("STATIC\n");};
435};
436
437void TEST()
438{
439  TestClass test;
440  SubString testStrings("1, 2, 3", ",", SubString::WhiteSpaces, false);
441  (*createExecutor<TestClass>(&TestClass::printTest))(&test, testStrings);
442  (*createExecutor<TestClass>(&TestClass::printTestInt))(&test, testStrings);
443  (*createExecutor<TestClass>(&TestClass::printTestBool))(&test, testStrings);
444  (*createExecutor<TestClass>(&TestClass::printTwoVals))(&test, testStrings);
445  (*createExecutor<TestClass>(&TestClass::printStatic))(&test, testStrings);
446
447}
448
449//// HACK HACK
450
451/**********************************
452*** ORXONOX MAIN STARTING POINT ***
453**********************************/
454/**
455 *
456 *  main function
457 *
458 * here the journey begins
459*/
460int main(int argc, char** argv)
461{
462  TEST();
463  CmdLinePrefsReader prefs;
464
465  IniFilePrefsReader ini(File(DEFAULT_CONFIG_FILE).name());
466  Preferences::getInstance()->setUserIni(File(DEFAULT_CONFIG_FILE).name());
467
468  prefs.parse(argc, argv);
469
470  if ( Preferences::getInstance()->getString("misc", "showLicenseAndExit", "") == "1" )
471  {
472    PRINT(0)(ORXONOX_LICENSE_SHORT);
473    return 0;
474  }
475
476  if ( Preferences::getInstance()->getString("misc", "start-gdb", "0") == "1" )
477  {
478    SignalHandler::getInstance()->doCatch( argv[0], GDB_RUN_IN_FOREGROUND );
479  }
480  else if ( Preferences::getInstance()->getString("misc", "bt-to-file", "1") == "1" )
481  {
482    SignalHandler::getInstance()->doCatch( argv[0], GDB_RUN_WRITE_TO_FILE );
483
484  }
485
486  if( Preferences::getInstance()->getString("game", "showGui", "") == "1" )
487    showGui = true;
488  else if( Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_server" ||
489           Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_client" )
490    return startNetworkOrxonox(argc, argv);
491
492  return startOrxonox(argc, argv, "", -1);
493  return 0;
494}
495
496
497
498/**
499 * starts orxonox in network mode
500 * @param argc parameters count given to orxonox
501 * @param argv parameters given to orxonox
502 */
503int startNetworkOrxonox(int argc, char** argv)
504{
505
506  std::string gameType = Preferences::getInstance()->getString( "game", "gameType", "" );
507
508  if ( gameType == "multiplayer_client" )
509  {
510    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
511    std::string host = Preferences::getInstance()->getString( "game", "host", "" );
512
513    if ( host == "" )
514    {
515      printf("You need to specify a host to connect to ( -H <host> )\n");
516      return 1;
517    }
518
519    printf("Starting Orxonox as client: connecting to %s, on port %i\n", host.c_str(), port);
520
521    startOrxonox(argc, argv, host.c_str(), port);
522  }
523  else if ( gameType == "multiplayer_server" )
524  {
525    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
526
527    printf("Starting Orxonox as server: listening on port %i\n", port);
528
529    startOrxonox(argc, argv, "", port);
530  }
531}
532
533
534
535/**
536 * starts orxonox
537 * @param argc parameters count given to orxonox
538 * @param argv parameters given to orxonox
539 */
540int startOrxonox(int argc, char** argv, const std::string & name, int port)
541{
542  // checking for existence of the configuration-files, or if the lock file is still used
543  if (showGui || (!File("./orxonox.conf").isFile() &&
544                  !File(DEFAULT_CONFIG_FILE).isFile())
545#if DEBUG < 3 // developers do not need to see the GUI, when orxonox fails
546      || ResourceManager::isFile(DEFAULT_LOCK_FILE)
547#endif
548     )
549  {
550    File lockFile(DEFAULT_LOCK_FILE);
551    if (lockFile.isFile())
552      lockFile.remove();
553
554    // starting the GUI
555    OrxGui::QtGui gui(argc, argv);
556    gui.startGui();
557
558    if (gui.getState() & OrxGui::Gui::Quitting)
559      return 0;
560
561  }
562
563  PRINT(0)(">>> Starting Orxonox <<<\n");
564
565  File(DEFAULT_LOCK_FILE).touch();
566
567  Orxonox *orx = Orxonox::getInstance();
568
569  if( orx->init(argc, argv, name, port) == -1)
570  {
571    PRINTF(1)("! Orxonox initialization failed\n");
572    return -1;
573  }
574
575  printf("finished inizialisation\n");
576  orx->start();
577
578  delete orx;
579  File("~/.orxonox/orxonox.lock").remove();
580}
Note: See TracBrowser for help on using the repository browser.