Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

shared_lib, ok loading everything (strangely npc is not a default dependency for orxonox, nowhere is it referenced… (but in NPC's)

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("libORXmisc_entities.la");
359  DynamicLoader::loadDyLib("libORXnpcs.la");
360  printf("%s\n", DynamicLoader::getSearchDir());
361
362  // start the collision detection engine
363  CDEngine::getInstance();
364  return 0;
365}
366
367/**
368 * initializes miscelaneous features
369 * @return -1 on failure
370 */
371int Orxonox::initMisc()
372{
373  ShellBuffer::getInstance();
374  return 0;
375}
376
377/**
378 *  starts the orxonox game or menu
379 * here is the central orxonox state manager. There are currently two states
380 * - menu
381 * - game-play
382 * both states manage their states themselfs again.
383*/
384void Orxonox::start()
385{
386
387  this->gameLoader = GameLoader::getInstance();
388
389  if( this->port != -1)
390    this->gameLoader->loadNetworkCampaign("worlds/DefaultNetworkCampaign.oxc");
391  else
392    this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");                       /* start orxonox in single player mode */
393
394  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
395  this->gameLoader->init();
396  this->gameLoader->start();
397}
398
399
400/**
401 * handles sprecial events from localinput
402 * @param event: an event not handled by the CommandNode
403 */
404// void Orxonox::graphicsHandler(SDL_Event* event)
405// {
406//   // Handle special events such as reshape, quit, focus changes
407//   switch (event->type)
408//     {
409//     case SDL_VIDEORESIZE:
410//       GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
411//       tmpGEngine->resolutionChanged(event->resize);
412//       break;
413//     }
414// }
415
416
417
418
419
420
421bool showGui = false;
422
423
424
425/**********************************
426*** ORXONOX MAIN STARTING POINT ***
427**********************************/
428/**
429 *
430 *  main function
431 *
432 * here the journey begins
433*/
434int main(int argc, char** argv)
435{
436  CmdLinePrefsReader prefs;
437
438  IniFilePrefsReader ini(ResourceManager::homeDirCheck(DEFAULT_CONFIG_FILE));
439
440  prefs.parse(argc, argv);
441
442  if ( Preferences::getInstance()->getString("misc", "showLicenseAndExit", "") == "1" )
443  {
444    PRINT(0)(ORXONOX_LICENSE_SHORT);
445    return 0;
446  }
447
448  if( Preferences::getInstance()->getString("game", "showGui", "") == "1" )
449    showGui = true;
450  else if( Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_server" ||
451           Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_client" )
452    return startNetworkOrxonox(argc, argv);
453
454  return startOrxonox(argc, argv, "", -1);
455  return 0;
456}
457
458
459
460/**
461 * starts orxonox in network mode
462 * @param argc parameters count given to orxonox
463 * @param argv parameters given to orxonox
464 */
465int startNetworkOrxonox(int argc, char** argv)
466{
467
468  std::string gameType = Preferences::getInstance()->getString( "game", "gameType", "" );
469
470  if ( gameType == "multiplayer_client" )
471  {
472    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
473    std::string host = Preferences::getInstance()->getString( "game", "host", "" );
474
475    if ( host == "" )
476    {
477      printf("You need to specify a host to connect to ( -H <host> )\n");
478      return 1;
479    }
480
481    printf("Starting Orxonox as client: connecting to %s, on port %i\n", host.c_str(), port);
482
483    startOrxonox(argc, argv, host.c_str(), port);
484  }
485  else if ( gameType == "multiplayer_server" )
486  {
487    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
488
489    printf("Starting Orxonox as server: listening on port %i\n", port);
490
491    startOrxonox(argc, argv, "", port);
492  }
493}
494
495
496
497/**
498 * starts orxonox
499 * @param argc parameters count given to orxonox
500 * @param argv parameters given to orxonox
501 */
502int startOrxonox(int argc, char** argv, const std::string & name, int port)
503{
504  // checking for existence of the configuration-files, or if the lock file is still used
505  if (showGui || (!ResourceManager::isFile("./orxonox.conf") &&
506                  !ResourceManager::isFile(DEFAULT_CONFIG_FILE))
507#if DEBUG < 3 // developers do not need to see the GUI, when orxonox fails
508      || ResourceManager::isFile(DEFAULT_LOCK_FILE)
509#endif
510     )
511  {
512    if (ResourceManager::isFile(DEFAULT_LOCK_FILE))
513      ResourceManager::deleteFile(DEFAULT_LOCK_FILE);
514
515    // starting the GUI
516    Gui* gui = new Gui(argc, argv);
517    gui->startGui();
518
519    if (! gui->startOrxonox)
520      return 0;
521
522    delete gui;
523  }
524
525  PRINT(0)(">>> Starting Orxonox <<<\n");
526
527  ResourceManager::touchFile(DEFAULT_LOCK_FILE);
528
529  Orxonox *orx = Orxonox::getInstance();
530
531  if( orx->init(argc, argv, name, port) == -1)
532  {
533    PRINTF(1)("! Orxonox initialization failed\n");
534    return -1;
535  }
536
537  printf("finished inizialisation\n");
538  orx->start();
539
540  delete orx;
541  ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
542}
Note: See TracBrowser for help on using the repository browser.