| 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 | #include "orxonox.h" | 
|---|
| 28 |  | 
|---|
| 29 | #include "gui.h" | 
|---|
| 30 |  | 
|---|
| 31 | #include "world.h" | 
|---|
| 32 | #include "ini_parser.h" | 
|---|
| 33 | #include "game_loader.h" | 
|---|
| 34 |  | 
|---|
| 35 | //ENGINES | 
|---|
| 36 | #include "graphics_engine.h" | 
|---|
| 37 | #include "sound_engine.h" | 
|---|
| 38 | #include "resource_manager.h" | 
|---|
| 39 | #include "object_manager.h" | 
|---|
| 40 | #include "cd_engine.h" | 
|---|
| 41 | #include "text_engine.h" | 
|---|
| 42 | #include "event_handler.h" | 
|---|
| 43 |  | 
|---|
| 44 | #include "state.h" | 
|---|
| 45 | #include "event.h" | 
|---|
| 46 | #include "factory.h" | 
|---|
| 47 | #include "benchmark.h" | 
|---|
| 48 |  | 
|---|
| 49 | #include "class_list.h" | 
|---|
| 50 | #include "substring.h" | 
|---|
| 51 |  | 
|---|
| 52 | #include <string.h> | 
|---|
| 53 |  | 
|---|
| 54 | int verbose = 4; | 
|---|
| 55 |  | 
|---|
| 56 | using namespace std; | 
|---|
| 57 |  | 
|---|
| 58 | /** | 
|---|
| 59 |    \brief create a new Orxonox | 
|---|
| 60 |  | 
|---|
| 61 |    In this funcitons only global values are set. The game will not be started here. | 
|---|
| 62 | */ | 
|---|
| 63 | Orxonox::Orxonox () | 
|---|
| 64 | { | 
|---|
| 65 |   this->setClassID(CL_ORXONOX, "Orxonox"); | 
|---|
| 66 |   this->setName("orxonox-main"); | 
|---|
| 67 |  | 
|---|
| 68 |   this->resourceManager = NULL; | 
|---|
| 69 |   this->objectManager = NULL; | 
|---|
| 70 |   this->eventHandler = NULL; | 
|---|
| 71 |   this->iniParser = NULL; | 
|---|
| 72 |  | 
|---|
| 73 |   this->argc = 0; | 
|---|
| 74 |   this->argv = NULL; | 
|---|
| 75 |  | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | /** | 
|---|
| 79 |    \brief remove Orxonox from memory | 
|---|
| 80 | */ | 
|---|
| 81 | Orxonox::~Orxonox () | 
|---|
| 82 | { | 
|---|
| 83 |   delete this->iniParser; | 
|---|
| 84 |  | 
|---|
| 85 |   delete GraphicsEngine::getInstance(); // deleting the Graphics | 
|---|
| 86 |   delete TextEngine::getInstance(); | 
|---|
| 87 |   delete SoundEngine::getInstance(); | 
|---|
| 88 |   delete ResourceManager::getInstance(); // deletes the Resource Manager | 
|---|
| 89 |   delete ObjectManager::getInstance(); | 
|---|
| 90 |   delete TextEngine::getInstance(); | 
|---|
| 91 |   delete EventHandler::getInstance(); | 
|---|
| 92 |   delete Factory::getFirst(); | 
|---|
| 93 |   delete SoundEngine::getInstance(); | 
|---|
| 94 |   delete State::getInstance(); | 
|---|
| 95 |  | 
|---|
| 96 |   ClassList::debug(0); | 
|---|
| 97 |  | 
|---|
| 98 |   PRINT(3)("===================================================\n" \ | 
|---|
| 99 |       "Thanks for playing orxonox.\n" \ | 
|---|
| 100 |       "visit: http://www.orxonox.ethz.ch for new versions.\n" \ | 
|---|
| 101 |       "===================================================\n"); | 
|---|
| 102 |  | 
|---|
| 103 |   Orxonox::singletonRef = NULL; | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | /** | 
|---|
| 107 |  * @brief this is a singleton class to prevent duplicates | 
|---|
| 108 |  */ | 
|---|
| 109 | Orxonox* Orxonox::singletonRef = NULL; | 
|---|
| 110 |  | 
|---|
| 111 | /** | 
|---|
| 112 |  * @brief this finds the config file | 
|---|
| 113 |  * @returns the new config-fileName | 
|---|
| 114 |  * Since the config file varies from user to user and since one may want to specify different config files | 
|---|
| 115 |  * for certain occasions or platforms this function finds the right config file for every occasion and stores | 
|---|
| 116 |  * it's path and name into configfilename | 
|---|
| 117 | */ | 
|---|
| 118 | const char* Orxonox::getConfigFile (int argc, char** argv) | 
|---|
| 119 | { | 
|---|
| 120 |   strcpy (this->configFileName, DEFAULT_CONFIG_FILE); | 
|---|
| 121 |   this->iniParser = new IniParser(this->configFileName); | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | /** | 
|---|
| 125 |    \brief initialize Orxonox with command line | 
|---|
| 126 | */ | 
|---|
| 127 | int Orxonox::init (int argc, char** argv) | 
|---|
| 128 | { | 
|---|
| 129 |   this->argc = argc; | 
|---|
| 130 |   this->argv = argv; | 
|---|
| 131 |   // parse command line | 
|---|
| 132 |   // config file | 
|---|
| 133 |  | 
|---|
| 134 |   // initialize the Config-file | 
|---|
| 135 |   this->getConfigFile(argc, argv); | 
|---|
| 136 |  | 
|---|
| 137 |  | 
|---|
| 138 |   // initialize everything | 
|---|
| 139 |   SDL_Init (SDL_INIT_TIMER); | 
|---|
| 140 |   if( initResources () == -1) return -1; | 
|---|
| 141 |   if( initVideo() == -1) return -1; | 
|---|
| 142 |   if( initSound() == -1) return -1; | 
|---|
| 143 |   if( initInput() == -1) return -1; | 
|---|
| 144 |   if( initNetworking () == -1) return -1; | 
|---|
| 145 |  | 
|---|
| 146 |   // subscribe the resolutionChanged-event | 
|---|
| 147 |   EventHandler::getInstance()->subscribe(this, ES_GAME, EV_VIDEO_RESIZE); | 
|---|
| 148 |   return 0; | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | /** | 
|---|
| 152 |    \brief initializes SDL and OpenGL | 
|---|
| 153 | */ | 
|---|
| 154 | int Orxonox::initVideo() | 
|---|
| 155 | { | 
|---|
| 156 |   PRINTF(3)("> Initializing video\n"); | 
|---|
| 157 |  | 
|---|
| 158 |   GraphicsEngine::getInstance(); | 
|---|
| 159 |   GraphicsEngine::getInstance()->setWindowName(PACKAGE_NAME " " PACKAGE_VERSION, PACKAGE_NAME " " PACKAGE_VERSION); | 
|---|
| 160 |  | 
|---|
| 161 |   GraphicsEngine::getInstance()->initFromIniFile(this->iniParser); | 
|---|
| 162 |  | 
|---|
| 163 |   return 0; | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | /** | 
|---|
| 167 |    \brief initializes the sound engine | 
|---|
| 168 | */ | 
|---|
| 169 | int Orxonox::initSound() | 
|---|
| 170 | { | 
|---|
| 171 |   PRINT(3)("> Initializing sound\n"); | 
|---|
| 172 |   // SDL_Init(SDL_INIT_AUDIO); | 
|---|
| 173 |   SoundEngine::getInstance()->initAudio(); | 
|---|
| 174 |   return 0; | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 |  | 
|---|
| 178 | /** | 
|---|
| 179 |  * @brief initializes input functions | 
|---|
| 180 | */ | 
|---|
| 181 | int Orxonox::initInput() | 
|---|
| 182 | { | 
|---|
| 183 |   PRINT(3)("> Initializing input\n"); | 
|---|
| 184 |  | 
|---|
| 185 |   this->eventHandler = EventHandler::getInstance(); | 
|---|
| 186 |   this->eventHandler->init(); | 
|---|
| 187 |  | 
|---|
| 188 |   return 0; | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 |  | 
|---|
| 192 | /** | 
|---|
| 193 | * @brief initializes network system | 
|---|
| 194 | */ | 
|---|
| 195 | int Orxonox::initNetworking() | 
|---|
| 196 | { | 
|---|
| 197 |   PRINT(3)("> Initializing networking\n"); | 
|---|
| 198 |  | 
|---|
| 199 |   printf("  ---Not yet implemented-FIXME--\n"); | 
|---|
| 200 |   return 0; | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 |  | 
|---|
| 204 | /** | 
|---|
| 205 |  * @brief initializes and loads resource files | 
|---|
| 206 |  */ | 
|---|
| 207 |  int Orxonox::initResources() | 
|---|
| 208 | { | 
|---|
| 209 |   PRINTF(3)("> Initializing resources\n"); | 
|---|
| 210 |  | 
|---|
| 211 |   PRINT(3)("initializing ResourceManager\n"); | 
|---|
| 212 |   resourceManager = ResourceManager::getInstance(); | 
|---|
| 213 |  | 
|---|
| 214 |   // create parser | 
|---|
| 215 |   if( this->iniParser->getSection (CONFIG_SECTION_DATA) == -1) | 
|---|
| 216 |   { | 
|---|
| 217 |     PRINTF(1)("Could not find Section %s in %s\n", CONFIG_SECTION_DATA, DEFAULT_CONFIG_FILE); | 
|---|
| 218 |     return -1; | 
|---|
| 219 |   } | 
|---|
| 220 |   char namebuf[256]; | 
|---|
| 221 |   char valuebuf[256]; | 
|---|
| 222 |   memset (namebuf, 0, 256); | 
|---|
| 223 |   memset (valuebuf, 0, 256); | 
|---|
| 224 |  | 
|---|
| 225 |   while( this->iniParser->nextVar (namebuf, valuebuf) != -1) | 
|---|
| 226 |   { | 
|---|
| 227 |     if (!strcmp(namebuf, CONFIG_NAME_DATADIR)) | 
|---|
| 228 |     { | 
|---|
| 229 |           //  printf("Not yet implemented\n"); | 
|---|
| 230 |       if (!resourceManager->setDataDir(valuebuf)) | 
|---|
| 231 |       { | 
|---|
| 232 |         PRINTF(1)("Data Could not be located\n"); | 
|---|
| 233 |         exit(-1); | 
|---|
| 234 |       } | 
|---|
| 235 |     } | 
|---|
| 236 |  | 
|---|
| 237 |     memset (namebuf, 0, 256); | 
|---|
| 238 |     memset (valuebuf, 0, 256); | 
|---|
| 239 |   } | 
|---|
| 240 |  | 
|---|
| 241 |   if (!resourceManager->checkDataDir(DEFAULT_DATA_DIR_CHECKFILE)) | 
|---|
| 242 |   { | 
|---|
| 243 |     PRINTF(1)("The DataDirectory %s could not be verified\n" \ | 
|---|
| 244 |               "  Please Change in File %s Section %s Entry %s to a suitable value\n", | 
|---|
| 245 |               resourceManager->getDataDir(), | 
|---|
| 246 |               DEFAULT_CONFIG_FILE, | 
|---|
| 247 |               CONFIG_SECTION_DATA, | 
|---|
| 248 |               CONFIG_NAME_DATADIR); | 
|---|
| 249 |     exit(-1); | 
|---|
| 250 |   } | 
|---|
| 251 |    //! \todo this is a hack and should be loadable | 
|---|
| 252 |   resourceManager->addImageDir(ResourceManager::getInstance()->getFullName("maps/")); | 
|---|
| 253 |   resourceManager->debug(); | 
|---|
| 254 |  | 
|---|
| 255 |   PRINT(3)("initializing TextEngine\n"); | 
|---|
| 256 |   TextEngine::getInstance(); | 
|---|
| 257 |  | 
|---|
| 258 |   PRINT(3)("initializing ObjectManager\n"); | 
|---|
| 259 |   this->objectManager = ObjectManager::getInstance(); | 
|---|
| 260 |  | 
|---|
| 261 |   CDEngine::getInstance(); | 
|---|
| 262 |  | 
|---|
| 263 |   return 0; | 
|---|
| 264 | } | 
|---|
| 265 |  | 
|---|
| 266 | /** | 
|---|
| 267 |    \brief starts the orxonox game or menu | 
|---|
| 268 |  | 
|---|
| 269 |    here is the central orxonox state manager. There are currently two states | 
|---|
| 270 |    - menu | 
|---|
| 271 |    - game-play | 
|---|
| 272 |    both states manage their states themselfs again. | 
|---|
| 273 | */ | 
|---|
| 274 | void Orxonox::start() | 
|---|
| 275 | { | 
|---|
| 276 |  | 
|---|
| 277 |   this->gameLoader = GameLoader::getInstance(); | 
|---|
| 278 |   this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc"); | 
|---|
| 279 |   //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0); | 
|---|
| 280 |   this->gameLoader->init(); | 
|---|
| 281 |   this->gameLoader->start(); | 
|---|
| 282 | } | 
|---|
| 283 |  | 
|---|
| 284 |  | 
|---|
| 285 | /** | 
|---|
| 286 |    \brief handles sprecial events from localinput | 
|---|
| 287 |    \param event: an event not handled by the CommandNode | 
|---|
| 288 | */ | 
|---|
| 289 | void Orxonox::graphicsHandler(SDL_Event* event) | 
|---|
| 290 | { | 
|---|
| 291 |   // Handle special events such as reshape, quit, focus changes | 
|---|
| 292 |   switch (event->type) | 
|---|
| 293 |     { | 
|---|
| 294 |     case SDL_VIDEORESIZE: | 
|---|
| 295 |       GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance(); | 
|---|
| 296 |       tmpGEngine->resolutionChanged(event->resize); | 
|---|
| 297 |       break; | 
|---|
| 298 |     } | 
|---|
| 299 | } | 
|---|
| 300 |  | 
|---|
| 301 |  | 
|---|
| 302 | /** | 
|---|
| 303 |   \brief processes the events for orxonox main class | 
|---|
| 304 |   \param the event to handle | 
|---|
| 305 | */ | 
|---|
| 306 | void Orxonox::process(const Event &event) | 
|---|
| 307 | { | 
|---|
| 308 |   switch (event.type) | 
|---|
| 309 |   { | 
|---|
| 310 |     case EV_VIDEO_RESIZE: | 
|---|
| 311 |       GraphicsEngine::getInstance()->resolutionChanged(event.resize); | 
|---|
| 312 |       break; | 
|---|
| 313 |   } | 
|---|
| 314 |  | 
|---|
| 315 | } | 
|---|
| 316 |  | 
|---|
| 317 |  | 
|---|
| 318 |  | 
|---|
| 319 | bool showGui = false; | 
|---|
| 320 |  | 
|---|
| 321 |  | 
|---|
| 322 |  | 
|---|
| 323 | /********************************** | 
|---|
| 324 | *** ORXONOX MAIN STARTING POINT *** | 
|---|
| 325 | **********************************/ | 
|---|
| 326 | /** | 
|---|
| 327 |    \brief main function | 
|---|
| 328 |  | 
|---|
| 329 |    here the journey begins | 
|---|
| 330 | */ | 
|---|
| 331 | int main(int argc, char** argv) | 
|---|
| 332 | { | 
|---|
| 333 |   // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark. | 
|---|
| 334 |   int i; | 
|---|
| 335 |   for(i = 1; i < argc; ++i) | 
|---|
| 336 |     { | 
|---|
| 337 |       if(! strcmp( "--help", argv[i]) || !strcmp("-h", argv[i])) return startHelp(argc, argv); | 
|---|
| 338 |       else if(!strcmp( "--benchmark", argv[i]) || !strcmp("-b", argv[i])) return startBenchmarks(); | 
|---|
| 339 |       else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true; | 
|---|
| 340 |       //      else PRINTF(2)("Orxonox does not understand the arguments %s\n", argv[i]); | 
|---|
| 341 |     } | 
|---|
| 342 |  | 
|---|
| 343 |   return startOrxonox(argc, argv); | 
|---|
| 344 | } | 
|---|
| 345 |  | 
|---|
| 346 |  | 
|---|
| 347 |  | 
|---|
| 348 | int startHelp(int argc, char** argv) | 
|---|
| 349 | { | 
|---|
| 350 |   PRINT(0)("orxonox: starts the orxonox game - rules\n"); | 
|---|
| 351 |   PRINT(0)("usage: orxonox [arg [arg...]]\n\n"); | 
|---|
| 352 |   PRINT(0)("valid options:\n"); | 
|---|
| 353 |   { | 
|---|
| 354 |     Gui* gui = new Gui(argc, argv); | 
|---|
| 355 |     gui->printHelp(); | 
|---|
| 356 |     delete gui; | 
|---|
| 357 |   } | 
|---|
| 358 |   PRINT(0)(" -b|--benchmark:\t\tstarts the orxonox benchmark\n"); | 
|---|
| 359 |   PRINT(0)(" -h|--help:\t\t\tshows this help\n"); | 
|---|
| 360 | } | 
|---|
| 361 |  | 
|---|
| 362 |  | 
|---|
| 363 |  | 
|---|
| 364 | /** | 
|---|
| 365 |  * starts orxonox | 
|---|
| 366 |  * @param argc parameters count given to orxonox | 
|---|
| 367 |  * @param argv parameters given to orxonox | 
|---|
| 368 |  */ | 
|---|
| 369 | int startOrxonox(int argc, char** argv) | 
|---|
| 370 | { | 
|---|
| 371 |   // checking for existence of the configuration-files | 
|---|
| 372 |   if (showGui || | 
|---|
| 373 |       !ResourceManager::isFile(DEFAULT_CONFIG_FILE) || | 
|---|
| 374 |       ResourceManager::isFile(DEFAULT_LOCK_FILE)) | 
|---|
| 375 |     { | 
|---|
| 376 |       if (ResourceManager::isFile(DEFAULT_LOCK_FILE)) | 
|---|
| 377 |         ResourceManager::deleteFile(DEFAULT_LOCK_FILE); | 
|---|
| 378 |  | 
|---|
| 379 |       // starting the GUI | 
|---|
| 380 |       Gui* gui = new Gui(argc, argv); | 
|---|
| 381 |       gui->startGui(); | 
|---|
| 382 |  | 
|---|
| 383 |       if (! gui->startOrxonox) | 
|---|
| 384 |         return 0; | 
|---|
| 385 |  | 
|---|
| 386 |       delete gui; | 
|---|
| 387 |     } | 
|---|
| 388 |  | 
|---|
| 389 |   PRINT(0)(">>> Starting Orxonox <<<\n"); | 
|---|
| 390 |  | 
|---|
| 391 |   ResourceManager::touchFile(DEFAULT_LOCK_FILE); | 
|---|
| 392 |  | 
|---|
| 393 |   Orxonox *orx = Orxonox::getInstance(); | 
|---|
| 394 |  | 
|---|
| 395 |   if((*orx).init(argc, argv) == -1) | 
|---|
| 396 |     { | 
|---|
| 397 |       PRINTF(1)("! Orxonox initialization failed\n"); | 
|---|
| 398 |       return -1; | 
|---|
| 399 |     } | 
|---|
| 400 |  | 
|---|
| 401 |   orx->start(); | 
|---|
| 402 |  | 
|---|
| 403 |   delete orx; | 
|---|
| 404 |   ResourceManager::deleteFile("~/.orxonox/orxonox.lock"); | 
|---|
| 405 |  | 
|---|
| 406 | } | 
|---|