Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7476 was 7427, checked in by bensch, 19 years ago

orxonox/trunk: TextEngine gets Cleanly unloaded now

File size: 13.7 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.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()->init();
277  EventHandler::getInstance()->subscribe(GraphicsEngine::getInstance(), ES_ALL, EV_VIDEO_RESIZE);
278
279  return 0;
280}
281
282
283/**
284 * initializes network system
285 */
286int Orxonox::initNetworking()
287{
288  PRINT(3)("> Initializing networking\n");
289
290  if( this->serverName != "") // we are a client
291  {
292    State::setOnline(true);
293    NetworkManager::getInstance()->establishConnection(this->serverName, port);
294  }
295  else if( this->port > 0) {    // we are a server
296    State::setOnline(true);
297    NetworkManager::getInstance()->createServer(port);
298  }
299  return 0;
300}
301
302//#include "util/loading/dynamic_loader.h"
303
304/**
305 * initializes and loads resource files
306 */
307int Orxonox::initResources()
308{
309  PRINTF(3)("> Initializing resources\n");
310
311  PRINT(3)("initializing ResourceManager\n");
312
313  // init the resource manager
314  std::string dataPath;
315  if ((dataPath = Preferences::getInstance()->getString(CONFIG_SECTION_DATA, CONFIG_NAME_DATADIR, ""))!= "")
316  {
317    if (!ResourceManager::getInstance()->setDataDir(dataPath) &&
318        !ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
319    {
320      PRINTF(1)("Data Could not be located in %s\n", dataPath.c_str());
321    }
322  }
323
324  if (!ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
325  {
326    PRINTF(1)("The DataDirectory %s could not be verified\n\nh" \
327              "!!!  Please Change in File %s Section %s Entry %s to a suitable value !!!\n",
328    ResourceManager::getInstance()->getDataDir().c_str(),
329    this->configFileName.c_str(),
330              CONFIG_SECTION_DATA,
331              CONFIG_NAME_DATADIR );
332    Gui* gui = new Gui(argc, argv);
333    gui->startGui();
334    delete gui;
335    exit(-1);
336  }
337  //! @todo this is a hack and should be loadable
338  std::string imageDir = ResourceManager::getInstance()->getFullName("maps");
339  ResourceManager::getInstance()->addImageDir(imageDir);
340  imageDir = ResourceManager::getInstance()->getFullName("pictures");
341  ResourceManager::getInstance()->addImageDir(imageDir);
342
343  //  DynamicLoader::loadDyLib("libtest.so");
344
345  // start the collision detection engine
346  CDEngine::getInstance();
347  return 0;
348}
349
350/**
351 * initializes miscelaneous features
352 * @return -1 on failure
353 */
354int Orxonox::initMisc()
355{
356  OrxShell::ShellBuffer::getInstance();
357  return 0;
358}
359
360/**
361 *  starts the orxonox game or menu
362 * here is the central orxonox state manager. There are currently two states
363 * - menu
364 * - game-play
365 * both states manage their states themselfs again.
366*/
367void Orxonox::start()
368{
369
370  this->gameLoader = GameLoader::getInstance();
371
372  if( this->port != -1)
373    this->gameLoader->loadNetworkCampaign("worlds/DefaultNetworkCampaign.oxc");
374  else
375    this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");                       /* start orxonox in single player mode */
376
377  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
378  this->gameLoader->init();
379  this->gameLoader->start();
380}
381
382
383/**
384 * handles sprecial events from localinput
385 * @param event: an event not handled by the CommandNode
386 */
387// void Orxonox::graphicsHandler(SDL_Event* event)
388// {
389//   // Handle special events such as reshape, quit, focus changes
390//   switch (event->type)
391//     {
392//     case SDL_VIDEORESIZE:
393//       GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
394//       tmpGEngine->resolutionChanged(event->resize);
395//       break;
396//     }
397// }
398
399
400
401
402
403
404bool showGui = false;
405
406
407
408/**********************************
409*** ORXONOX MAIN STARTING POINT ***
410**********************************/
411/**
412 *
413 *  main function
414 *
415 * here the journey begins
416*/
417int main(int argc, char** argv)
418{
419  CmdLinePrefsReader prefs;
420
421  IniFilePrefsReader ini(ResourceManager::homeDirCheck(DEFAULT_CONFIG_FILE));
422
423  prefs.parse(argc, argv);
424
425  if ( Preferences::getInstance()->getString("misc", "showLicenseAndExit", "") == "1" )
426  {
427    PRINT(0)(ORXONOX_LICENSE_SHORT);
428    return 0;
429  }
430
431  if( Preferences::getInstance()->getString("game", "showGui", "") == "1" )
432    showGui = true;
433  else if( Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_server" ||
434           Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_client" )
435    return startNetworkOrxonox(argc, argv);
436
437  return startOrxonox(argc, argv, "", -1);
438  return 0;
439}
440
441
442
443/**
444 * starts orxonox in network mode
445 * @param argc parameters count given to orxonox
446 * @param argv parameters given to orxonox
447 */
448int startNetworkOrxonox(int argc, char** argv)
449{
450
451  std::string gameType = Preferences::getInstance()->getString( "game", "gameType", "" );
452
453  if ( gameType == "multiplayer_client" )
454  {
455    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
456    std::string host = Preferences::getInstance()->getString( "game", "host", "" );
457
458    if ( host == "" )
459    {
460      printf("You need to specify a host to connect to ( -H <host> )\n");
461      return 1;
462    }
463
464    printf("Starting Orxonox as client: connecting to %s, on port %i\n", host.c_str(), port);
465
466    startOrxonox(argc, argv, host.c_str(), port);
467  }
468  else if ( gameType == "multiplayer_server" )
469  {
470    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
471
472    printf("Starting Orxonox as server: listening on port %i\n", port);
473
474    startOrxonox(argc, argv, "", port);
475  }
476}
477
478
479
480/**
481 * starts orxonox
482 * @param argc parameters count given to orxonox
483 * @param argv parameters given to orxonox
484 */
485int startOrxonox(int argc, char** argv, const std::string & name, int port)
486{
487  // checking for existence of the configuration-files, or if the lock file is still used
488  if (showGui || (!ResourceManager::isFile("./orxonox.conf") &&
489                  !ResourceManager::isFile(DEFAULT_CONFIG_FILE))
490#if DEBUG < 3 // developers do not need to see the GUI, when orxonox fails
491      || ResourceManager::isFile(DEFAULT_LOCK_FILE)
492#endif
493     )
494  {
495    if (ResourceManager::isFile(DEFAULT_LOCK_FILE))
496      ResourceManager::deleteFile(DEFAULT_LOCK_FILE);
497
498    // starting the GUI
499    Gui* gui = new Gui(argc, argv);
500    gui->startGui();
501
502    if (! gui->startOrxonox)
503      return 0;
504
505    delete gui;
506  }
507
508  PRINT(0)(">>> Starting Orxonox <<<\n");
509
510  ResourceManager::touchFile(DEFAULT_LOCK_FILE);
511
512  Orxonox *orx = Orxonox::getInstance();
513
514  if( orx->init(argc, argv, name, port) == -1)
515  {
516    PRINTF(1)("! Orxonox initialization failed\n");
517    return -1;
518  }
519
520  printf("finished inizialisation\n");
521  orx->start();
522
523  delete orx;
524  ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
525}
Note: See TracBrowser for help on using the repository browser.