Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/qt_gui/src/orxonox.cc @ 7603

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

orxonox/qt_gui: QtGuiComboBox saveable

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