Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/shared_lib/src/orxonox.cc @ 7267

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

linking, but i am not proud of this :/

File size: 14.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.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#include "util/loading/dynamic_loader.h"
57
58#include "network_manager.h"
59
60#include "state.h"
61#include "lib/parser/preferences/cmd_line_prefs_reader.h"
62#include "lib/parser/preferences/ini_file_prefs_reader.h"
63#include <string.h>
64
65int verbose = 4;
66
67using namespace std;
68
69SHELL_COMMAND(restart, Orxonox, restart);
70
71REGISTER_ARG_FLAG( l, license,    "misc",  "showLicenseAndExit", "Prints the license and exit",      "1" );
72REGISTER_ARG_FLAG( c, client,     "game",  "gameType",           "Connect to Server (-H)",           "multiplayer_client" );
73REGISTER_ARG_FLAG( s, server,     "game",  "gameType",           "Start Orxonox as Game Server",     "multiplayer_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");
81
82REGISTER_ARG_FLAG( a, audio,      "audio", "Disable-Audio",      "Enable audio",                     "0" );
83REGISTER_ARG_FLAG( m, mute ,      "audio", "Disable-Audio",      "Disable audio",                    "1" );
84REGISTER_ARG_ARG(  _, audio_channels, "audio", "Audio-Channels", "Sets # audio channels", "num" );
85REGISTER_ARG_ARG(  _, music_volume, "audio", "Music-Volume", "Sets music volume", "vol" );
86REGISTER_ARG_ARG(  _, effects_volume, "audio", "Effects-Volume", "Sets effects volume", "vol" );
87
88/**
89 *  create a new Orxonox
90
91   In this funcitons only global values are set. The game will not be started here.
92*/
93Orxonox::Orxonox ()
94{
95  this->setClassID(CL_ORXONOX, "Orxonox");
96  this->setName("orxonox-main");
97
98  this->argc = 0;
99  this->argv = NULL;
100
101  /* this way, there is no network enabled: */
102  this->serverName = "";
103  this->port = -1;
104
105  this->configFileName = "";
106}
107
108/**
109 *  remove Orxonox from memory
110*/
111Orxonox::~Orxonox ()
112{
113  // game-specific
114  delete GameLoader::getInstance();
115
116  // class-less services/factories
117  Factory::deleteFactories();
118  FastFactory::deleteAll();
119  ShellCommandClass::unregisterAllCommands();
120
121  LoadClassDescription::deleteAllDescriptions();
122
123  // engines
124  delete CDEngine::getInstance();
125  delete SoundEngine::getInstance();
126  delete GraphicsEngine::getInstance(); // deleting the Graphics
127  delete EventHandler::getInstance();
128
129  // handlers
130  delete ResourceManager::getInstance(); // deletes the Resource Manager
131  // output-buffer
132  delete 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)
296  {    // 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 * 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
344
345  if (!DynamicLoader::addSearchDirRelative("./world_entities/libs"))
346    if (!DynamicLoader::addSearchDirRelative("./src/world_entities/libs"))
347      DynamicLoader::addSearchDir(PKG_LIBDIR);
348
349  /// FIXME make this more modular (search for himself for all the libraries
350  PRINTF(0)("We are searching for plugins in %s\n", DynamicLoader::getSearchDir());
351  DynamicLoader::loadDyLib("libORXplayables.la");
352  DynamicLoader::loadDyLib("libORXweapons.la");
353  DynamicLoader::loadDyLib("libORXprojectiles.la");
354  DynamicLoader::loadDyLib("libORXpower_ups.la");
355
356  DynamicLoader::loadDyLib("libORXenvironmentals.la");
357
358  DynamicLoader::loadDyLib("libORXnpcs.la");
359
360  DynamicLoader::loadDyLib("libORXmisc_entities.la");
361  printf("%s\n", DynamicLoader::getSearchDir());
362
363  // start the collision detection engine
364  CDEngine::getInstance();
365  return 0;
366}
367
368/**
369 * initializes miscelaneous features
370 * @return -1 on failure
371 */
372int Orxonox::initMisc()
373{
374  ShellBuffer::getInstance();
375  return 0;
376}
377
378/**
379 *  starts the orxonox game or menu
380 * here is the central orxonox state manager. There are currently two states
381 * - menu
382 * - game-play
383 * both states manage their states themselfs again.
384*/
385void Orxonox::start()
386{
387
388  this->gameLoader = GameLoader::getInstance();
389
390  if( this->port != -1)
391    this->gameLoader->loadNetworkCampaign("worlds/DefaultNetworkCampaign.oxc");
392  else
393    this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");                       /* start orxonox in single player mode */
394
395  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
396  this->gameLoader->init();
397  this->gameLoader->start();
398}
399
400
401/**
402 * handles sprecial events from localinput
403 * @param event: an event not handled by the CommandNode
404 */
405// void Orxonox::graphicsHandler(SDL_Event* event)
406// {
407//   // Handle special events such as reshape, quit, focus changes
408//   switch (event->type)
409//     {
410//     case SDL_VIDEORESIZE:
411//       GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
412//       tmpGEngine->resolutionChanged(event->resize);
413//       break;
414//     }
415// }
416
417
418
419
420
421
422bool showGui = false;
423
424
425
426/**********************************
427*** ORXONOX MAIN STARTING POINT ***
428**********************************/
429/**
430 *
431 *  main function
432 *
433 * here the journey begins
434*/
435int main(int argc, char** argv)
436{
437  CmdLinePrefsReader prefs;
438
439  IniFilePrefsReader ini(ResourceManager::homeDirCheck(DEFAULT_CONFIG_FILE));
440
441  prefs.parse(argc, argv);
442
443  if ( Preferences::getInstance()->getString("misc", "showLicenseAndExit", "") == "1" )
444  {
445    PRINT(0)(ORXONOX_LICENSE_SHORT);
446    return 0;
447  }
448
449  if( Preferences::getInstance()->getString("game", "showGui", "") == "1" )
450    showGui = true;
451  else if( Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_server" ||
452           Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_client" )
453    return startNetworkOrxonox(argc, argv);
454
455  return startOrxonox(argc, argv, "", -1);
456  return 0;
457}
458
459
460
461/**
462 * starts orxonox in network mode
463 * @param argc parameters count given to orxonox
464 * @param argv parameters given to orxonox
465 */
466int startNetworkOrxonox(int argc, char** argv)
467{
468
469  std::string gameType = Preferences::getInstance()->getString( "game", "gameType", "" );
470
471  if ( gameType == "multiplayer_client" )
472  {
473    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
474    std::string host = Preferences::getInstance()->getString( "game", "host", "" );
475
476    if ( host == "" )
477    {
478      printf("You need to specify a host to connect to ( -H <host> )\n");
479      return 1;
480    }
481
482    printf("Starting Orxonox as client: connecting to %s, on port %i\n", host.c_str(), port);
483
484    startOrxonox(argc, argv, host.c_str(), port);
485  }
486  else if ( gameType == "multiplayer_server" )
487  {
488    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
489
490    printf("Starting Orxonox as server: listening on port %i\n", port);
491
492    startOrxonox(argc, argv, "", port);
493  }
494}
495
496
497
498/**
499 * starts orxonox
500 * @param argc parameters count given to orxonox
501 * @param argv parameters given to orxonox
502 */
503int startOrxonox(int argc, char** argv, const std::string & name, int port)
504{
505  // checking for existence of the configuration-files, or if the lock file is still used
506  if (showGui || (!ResourceManager::isFile("./orxonox.conf") &&
507                  !ResourceManager::isFile(DEFAULT_CONFIG_FILE))
508#if DEBUG < 3 // developers do not need to see the GUI, when orxonox fails
509      || ResourceManager::isFile(DEFAULT_LOCK_FILE)
510#endif
511     )
512  {
513    if (ResourceManager::isFile(DEFAULT_LOCK_FILE))
514      ResourceManager::deleteFile(DEFAULT_LOCK_FILE);
515
516    // starting the GUI
517    Gui* gui = new Gui(argc, argv);
518    gui->startGui();
519
520    if (! gui->startOrxonox)
521      return 0;
522
523    delete gui;
524  }
525
526  PRINT(0)(">>> Starting Orxonox <<<\n");
527
528  ResourceManager::touchFile(DEFAULT_LOCK_FILE);
529
530  Orxonox *orx = Orxonox::getInstance();
531
532  if( orx->init(argc, argv, name, port) == -1)
533  {
534    PRINTF(1)("! Orxonox initialization failed\n");
535    return -1;
536  }
537
538  printf("finished inizialisation\n");
539  orx->start();
540
541  delete orx;
542  ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
543}
Note: See TracBrowser for help on using the repository browser.