[4982] | 1 | /* |
---|
[1850] | 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, |
---|
[4556] | 18 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
[1850] | 19 | |
---|
[1855] | 20 | |
---|
| 21 | ### File Specific: |
---|
| 22 | main-programmer: Patrick Boenzli |
---|
[5303] | 23 | co-programmer: Christian Meyer |
---|
[4054] | 24 | co-programmer: Benjamin Grauer: injected ResourceManager/GraphicsEngine/GUI |
---|
[1850] | 25 | */ |
---|
| 26 | |
---|
[5303] | 27 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ORXONOX |
---|
[2190] | 28 | #include "orxonox.h" |
---|
[3610] | 29 | |
---|
[5819] | 30 | #include "globals.h" |
---|
| 31 | |
---|
[4054] | 32 | #include "gui.h" |
---|
| 33 | |
---|
[2036] | 34 | #include "world.h" |
---|
[4091] | 35 | #include "ini_parser.h" |
---|
[5165] | 36 | #include "game_loader.h" |
---|
[4786] | 37 | |
---|
| 38 | //ENGINES |
---|
[3610] | 39 | #include "graphics_engine.h" |
---|
[4504] | 40 | #include "sound_engine.h" |
---|
[3655] | 41 | #include "resource_manager.h" |
---|
[4786] | 42 | #include "cd_engine.h" |
---|
[3790] | 43 | #include "text_engine.h" |
---|
[4786] | 44 | #include "event_handler.h" |
---|
[4815] | 45 | #include "garbage_collector.h" |
---|
[4786] | 46 | |
---|
[4010] | 47 | #include "factory.h" |
---|
[4980] | 48 | #include "fast_factory.h" |
---|
| 49 | |
---|
[4131] | 50 | #include "benchmark.h" |
---|
[3610] | 51 | |
---|
[4786] | 52 | #include "class_list.h" |
---|
[5641] | 53 | #include "shell_command_class.h" |
---|
[5165] | 54 | #include "shell_command.h" |
---|
[5175] | 55 | #include "shell_buffer.h" |
---|
[4748] | 56 | |
---|
[5546] | 57 | #include "load_param_description.h" |
---|
[5226] | 58 | |
---|
[2190] | 59 | #include <string.h> |
---|
[4032] | 60 | |
---|
[4885] | 61 | int verbose = 4; |
---|
[2036] | 62 | |
---|
[1803] | 63 | using namespace std; |
---|
| 64 | |
---|
[5207] | 65 | SHELL_COMMAND(restart, Orxonox, restart); |
---|
| 66 | |
---|
[2190] | 67 | /** |
---|
[4836] | 68 | * create a new Orxonox |
---|
[4135] | 69 | |
---|
| 70 | In this funcitons only global values are set. The game will not be started here. |
---|
[2190] | 71 | */ |
---|
| 72 | Orxonox::Orxonox () |
---|
[1872] | 73 | { |
---|
[4445] | 74 | this->setClassID(CL_ORXONOX, "Orxonox"); |
---|
[4766] | 75 | this->setName("orxonox-main"); |
---|
[4059] | 76 | |
---|
[4766] | 77 | this->iniParser = NULL; |
---|
[4135] | 78 | |
---|
| 79 | this->argc = 0; |
---|
| 80 | this->argv = NULL; |
---|
[4782] | 81 | |
---|
[4830] | 82 | this->configFileName = NULL; |
---|
[1872] | 83 | } |
---|
[1803] | 84 | |
---|
[2190] | 85 | /** |
---|
[4836] | 86 | * remove Orxonox from memory |
---|
[2190] | 87 | */ |
---|
[4556] | 88 | Orxonox::~Orxonox () |
---|
[2190] | 89 | { |
---|
[5285] | 90 | // game-specific |
---|
[4815] | 91 | delete GameLoader::getInstance(); |
---|
| 92 | delete GarbageCollector::getInstance(); |
---|
[5285] | 93 | |
---|
| 94 | // class-less services/factories |
---|
| 95 | delete Factory::getFirst(); |
---|
[4980] | 96 | FastFactory::deleteAll(); |
---|
[5171] | 97 | ShellCommandClass::unregisterAllCommands(); |
---|
[5332] | 98 | |
---|
[5226] | 99 | LoadClassDescription::deleteAllDescriptions(); |
---|
| 100 | |
---|
[5285] | 101 | // engines |
---|
| 102 | delete CDEngine::getInstance(); |
---|
| 103 | delete SoundEngine::getInstance(); |
---|
| 104 | delete GraphicsEngine::getInstance(); // deleting the Graphics |
---|
[4817] | 105 | delete EventHandler::getInstance(); |
---|
[5285] | 106 | |
---|
| 107 | // handlers |
---|
| 108 | delete ResourceManager::getInstance(); // deletes the Resource Manager |
---|
| 109 | // output-buffer |
---|
| 110 | delete ShellBuffer::getInstance(); |
---|
| 111 | |
---|
| 112 | // orxonox class-stuff |
---|
[5078] | 113 | delete this->iniParser; |
---|
[5210] | 114 | delete[] this->configFileName; |
---|
[4817] | 115 | |
---|
[5225] | 116 | SDL_QuitSubSystem(SDL_INIT_TIMER); |
---|
[4942] | 117 | ClassList::debug(); |
---|
[1850] | 118 | |
---|
[4833] | 119 | PRINT(3) |
---|
[4981] | 120 | ( |
---|
| 121 | "===================================================\n" \ |
---|
[4766] | 122 | "Thanks for playing orxonox.\n" \ |
---|
[5037] | 123 | "visit: http://www.orxonox.net for new versions.\n" \ |
---|
[4946] | 124 | "===================================================\n" \ |
---|
[4981] | 125 | ORXONOX_LICENSE_SHORT |
---|
| 126 | ); |
---|
[1872] | 127 | |
---|
[4766] | 128 | Orxonox::singletonRef = NULL; |
---|
[1850] | 129 | } |
---|
| 130 | |
---|
[2190] | 131 | /** |
---|
[4836] | 132 | * this is a singleton class to prevent duplicates |
---|
[4766] | 133 | */ |
---|
| 134 | Orxonox* Orxonox::singletonRef = NULL; |
---|
[4556] | 135 | |
---|
[5207] | 136 | // DANGEROUS |
---|
| 137 | void Orxonox::restart() |
---|
| 138 | { |
---|
| 139 | // int argc = this->argc; |
---|
| 140 | // char** argv = this->argv; |
---|
| 141 | // |
---|
| 142 | // Orxonox *orx = Orxonox::getInstance(); |
---|
| 143 | // |
---|
| 144 | // delete orx; |
---|
| 145 | // |
---|
| 146 | // orx = Orxonox::getInstance(); |
---|
| 147 | // |
---|
| 148 | // if((*orx).init(argc, argv) == -1) |
---|
| 149 | // { |
---|
| 150 | // PRINTF(1)("! Orxonox initialization failed\n"); |
---|
| 151 | // return; |
---|
| 152 | // } |
---|
| 153 | // |
---|
| 154 | // printf("finished inizialisation\n"); |
---|
| 155 | // orx->start(); |
---|
| 156 | } |
---|
| 157 | |
---|
[4766] | 158 | /** |
---|
[4836] | 159 | * this finds the config file |
---|
[4766] | 160 | * @returns the new config-fileName |
---|
| 161 | * Since the config file varies from user to user and since one may want to specify different config files |
---|
| 162 | * for certain occasions or platforms this function finds the right config file for every occasion and stores |
---|
| 163 | * it's path and name into configfilename |
---|
[2190] | 164 | */ |
---|
[4830] | 165 | const char* Orxonox::getConfigFile () |
---|
[1850] | 166 | { |
---|
[5424] | 167 | if (ResourceManager::isFile("orxonox.conf")) |
---|
| 168 | { |
---|
| 169 | this->configFileName = new char[strlen("orxonox.conf")+1]; |
---|
| 170 | strcpy(this->configFileName, "orxonox.conf"); |
---|
| 171 | } |
---|
| 172 | else |
---|
| 173 | this->configFileName = ResourceManager::homeDirCheck(DEFAULT_CONFIG_FILE); |
---|
[4766] | 174 | this->iniParser = new IniParser(this->configFileName); |
---|
[5424] | 175 | PRINTF(3)("Parsed Config File: '%s'\n", this->configFileName); |
---|
[1803] | 176 | } |
---|
| 177 | |
---|
[2190] | 178 | /** |
---|
[4833] | 179 | * initialize Orxonox with command line |
---|
| 180 | */ |
---|
[2190] | 181 | int Orxonox::init (int argc, char** argv) |
---|
[1803] | 182 | { |
---|
[4135] | 183 | this->argc = argc; |
---|
| 184 | this->argv = argv; |
---|
[2636] | 185 | // parse command line |
---|
| 186 | // config file |
---|
[4556] | 187 | |
---|
[4766] | 188 | // initialize the Config-file |
---|
[4830] | 189 | this->getConfigFile(); |
---|
[4766] | 190 | |
---|
[4782] | 191 | // initialize everything |
---|
[5227] | 192 | SDL_Init(SDL_INIT_TIMER); |
---|
[5788] | 193 | // windows must not write into stdout.txt and stderr.txt |
---|
| 194 | #ifdef __WIN32__ |
---|
| 195 | freopen( "CON", "w", stdout ); |
---|
| 196 | freopen( "CON", "w", stderr ); |
---|
| 197 | #endif |
---|
| 198 | |
---|
[4113] | 199 | if( initResources () == -1) return -1; |
---|
[3226] | 200 | if( initVideo() == -1) return -1; |
---|
| 201 | if( initSound() == -1) return -1; |
---|
| 202 | if( initInput() == -1) return -1; |
---|
| 203 | if( initNetworking () == -1) return -1; |
---|
[5074] | 204 | if( initMisc () == -1) return -1; |
---|
[4556] | 205 | |
---|
[2636] | 206 | return 0; |
---|
[1850] | 207 | } |
---|
[1849] | 208 | |
---|
[2190] | 209 | /** |
---|
[4833] | 210 | * initializes SDL and OpenGL |
---|
[2190] | 211 | */ |
---|
[4556] | 212 | int Orxonox::initVideo() |
---|
[2190] | 213 | { |
---|
[3611] | 214 | PRINTF(3)("> Initializing video\n"); |
---|
[4556] | 215 | |
---|
[3610] | 216 | GraphicsEngine::getInstance(); |
---|
[4556] | 217 | |
---|
[4784] | 218 | GraphicsEngine::getInstance()->initFromIniFile(this->iniParser); |
---|
[4766] | 219 | |
---|
[5219] | 220 | char* iconName = ResourceManager::getFullName("pictures/fighter-top-32x32.bmp"); |
---|
[5225] | 221 | if (iconName != NULL) |
---|
| 222 | { |
---|
| 223 | GraphicsEngine::getInstance()->setWindowName(PACKAGE_NAME " " PACKAGE_VERSION, iconName); |
---|
| 224 | delete[] iconName; |
---|
| 225 | } |
---|
[2190] | 226 | return 0; |
---|
| 227 | } |
---|
[1850] | 228 | |
---|
[2190] | 229 | /** |
---|
[4833] | 230 | * initializes the sound engine |
---|
| 231 | */ |
---|
[4556] | 232 | int Orxonox::initSound() |
---|
[2190] | 233 | { |
---|
[4504] | 234 | PRINT(3)("> Initializing sound\n"); |
---|
[5225] | 235 | // SDL_InitSubSystem(SDL_INIT_AUDIO); |
---|
[4504] | 236 | SoundEngine::getInstance()->initAudio(); |
---|
[4985] | 237 | |
---|
| 238 | SoundEngine::getInstance()->loadSettings(this->iniParser); |
---|
[2636] | 239 | return 0; |
---|
[2190] | 240 | } |
---|
[1900] | 241 | |
---|
[3214] | 242 | |
---|
[2190] | 243 | /** |
---|
[4833] | 244 | * initializes input functions |
---|
| 245 | */ |
---|
[4556] | 246 | int Orxonox::initInput() |
---|
[2190] | 247 | { |
---|
[4766] | 248 | PRINT(3)("> Initializing input\n"); |
---|
| 249 | |
---|
[4866] | 250 | EventHandler::getInstance()->init(this->iniParser); |
---|
[4833] | 251 | EventHandler::getInstance()->subscribe(GraphicsEngine::getInstance(), ES_ALL, EV_VIDEO_RESIZE); |
---|
[4556] | 252 | |
---|
[2636] | 253 | return 0; |
---|
[1803] | 254 | } |
---|
| 255 | |
---|
[3214] | 256 | |
---|
[2190] | 257 | /** |
---|
[4833] | 258 | * initializes network system |
---|
| 259 | */ |
---|
[4556] | 260 | int Orxonox::initNetworking() |
---|
[1897] | 261 | { |
---|
[4766] | 262 | PRINT(3)("> Initializing networking\n"); |
---|
| 263 | |
---|
| 264 | printf(" ---Not yet implemented-FIXME--\n"); |
---|
[2636] | 265 | return 0; |
---|
[1897] | 266 | } |
---|
| 267 | |
---|
[3214] | 268 | |
---|
[2190] | 269 | /** |
---|
[4833] | 270 | * initializes and loads resource files |
---|
[4766] | 271 | */ |
---|
[4833] | 272 | int Orxonox::initResources() |
---|
[1858] | 273 | { |
---|
[4766] | 274 | PRINTF(3)("> Initializing resources\n"); |
---|
[4091] | 275 | |
---|
[4766] | 276 | PRINT(3)("initializing ResourceManager\n"); |
---|
| 277 | |
---|
[5488] | 278 | // init the resource manager |
---|
[5014] | 279 | const char* dataPath; |
---|
| 280 | if ((dataPath = this->iniParser->getVar(CONFIG_NAME_DATADIR, CONFIG_SECTION_DATA))!= NULL) |
---|
[4766] | 281 | { |
---|
[5480] | 282 | if (!ResourceManager::getInstance()->setDataDir(dataPath) && |
---|
| 283 | !ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE)) |
---|
[4766] | 284 | { |
---|
[5423] | 285 | PRINTF(1)("Data Could not be located in %s\n", dataPath); |
---|
[4766] | 286 | } |
---|
| 287 | } |
---|
[4556] | 288 | |
---|
[5480] | 289 | if (!ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE)) |
---|
[4766] | 290 | { |
---|
[5510] | 291 | PRINTF(1)("The DataDirectory %s could not be verified\n\nh" \ |
---|
| 292 | "!!! Please Change in File %s Section %s Entry %s to a suitable value !!!\n", |
---|
[4822] | 293 | ResourceManager::getInstance()->getDataDir(), |
---|
[5424] | 294 | this->configFileName, |
---|
[4766] | 295 | CONFIG_SECTION_DATA, |
---|
[5510] | 296 | CONFIG_NAME_DATADIR ); |
---|
[5479] | 297 | Gui* gui = new Gui(argc, argv); |
---|
| 298 | gui->startGui(); |
---|
| 299 | delete gui; |
---|
[4766] | 300 | exit(-1); |
---|
| 301 | } |
---|
[4836] | 302 | //! @todo this is a hack and should be loadable |
---|
[5335] | 303 | char* imageDir = ResourceManager::getInstance()->getFullName("maps"); |
---|
[5216] | 304 | ResourceManager::getInstance()->addImageDir(imageDir); |
---|
| 305 | delete[] imageDir; |
---|
[4009] | 306 | |
---|
[5488] | 307 | // start the collision detection engine |
---|
[4766] | 308 | CDEngine::getInstance(); |
---|
[5074] | 309 | return 0; |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | /** |
---|
| 313 | * initializes miscelaneous features |
---|
| 314 | * @return -1 on failure |
---|
| 315 | */ |
---|
| 316 | int Orxonox::initMisc() |
---|
| 317 | { |
---|
[5183] | 318 | ShellBuffer::getInstance(); |
---|
[4766] | 319 | return 0; |
---|
[1858] | 320 | } |
---|
[1849] | 321 | |
---|
[2190] | 322 | /** |
---|
[4836] | 323 | * starts the orxonox game or menu |
---|
[4833] | 324 | * here is the central orxonox state manager. There are currently two states |
---|
| 325 | * - menu |
---|
| 326 | * - game-play |
---|
| 327 | * both states manage their states themselfs again. |
---|
[2190] | 328 | */ |
---|
[2636] | 329 | void Orxonox::start() |
---|
| 330 | { |
---|
[4556] | 331 | |
---|
[2636] | 332 | this->gameLoader = GameLoader::getInstance(); |
---|
[4094] | 333 | this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc"); |
---|
[4010] | 334 | // this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0); |
---|
[2636] | 335 | this->gameLoader->init(); |
---|
| 336 | this->gameLoader->start(); |
---|
| 337 | } |
---|
| 338 | |
---|
[3214] | 339 | |
---|
[2636] | 340 | /** |
---|
[4833] | 341 | * handles sprecial events from localinput |
---|
| 342 | * @param event: an event not handled by the CommandNode |
---|
| 343 | */ |
---|
[4817] | 344 | // void Orxonox::graphicsHandler(SDL_Event* event) |
---|
| 345 | // { |
---|
| 346 | // // Handle special events such as reshape, quit, focus changes |
---|
| 347 | // switch (event->type) |
---|
| 348 | // { |
---|
| 349 | // case SDL_VIDEORESIZE: |
---|
| 350 | // GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance(); |
---|
| 351 | // tmpGEngine->resolutionChanged(event->resize); |
---|
| 352 | // break; |
---|
| 353 | // } |
---|
| 354 | // } |
---|
[1875] | 355 | |
---|
[4556] | 356 | |
---|
[4408] | 357 | |
---|
[1803] | 358 | |
---|
[3214] | 359 | |
---|
[4782] | 360 | |
---|
[4059] | 361 | bool showGui = false; |
---|
[3648] | 362 | |
---|
[4766] | 363 | |
---|
| 364 | |
---|
| 365 | /********************************** |
---|
| 366 | *** ORXONOX MAIN STARTING POINT *** |
---|
| 367 | **********************************/ |
---|
[3449] | 368 | /** |
---|
[4833] | 369 | * |
---|
[4836] | 370 | * main function |
---|
[4833] | 371 | * |
---|
| 372 | * here the journey begins |
---|
[3449] | 373 | */ |
---|
[4556] | 374 | int main(int argc, char** argv) |
---|
| 375 | { |
---|
[4135] | 376 | // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark. |
---|
[3648] | 377 | int i; |
---|
[4032] | 378 | for(i = 1; i < argc; ++i) |
---|
[3648] | 379 | { |
---|
[4135] | 380 | if(! strcmp( "--help", argv[i]) || !strcmp("-h", argv[i])) return startHelp(argc, argv); |
---|
[5822] | 381 | // else if(!strcmp( "--benchmark", argv[i]) || !strcmp("-b", argv[i])) return startBenchmarks(); |
---|
[4135] | 382 | else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true; |
---|
| 383 | // else PRINTF(2)("Orxonox does not understand the arguments %s\n", argv[i]); |
---|
[3648] | 384 | } |
---|
| 385 | |
---|
| 386 | return startOrxonox(argc, argv); |
---|
| 387 | } |
---|
| 388 | |
---|
| 389 | |
---|
| 390 | |
---|
[4132] | 391 | int startHelp(int argc, char** argv) |
---|
[3648] | 392 | { |
---|
[4032] | 393 | PRINT(0)("orxonox: starts the orxonox game - rules\n"); |
---|
[4134] | 394 | PRINT(0)("usage: orxonox [arg [arg...]]\n\n"); |
---|
[4032] | 395 | PRINT(0)("valid options:\n"); |
---|
[4132] | 396 | { |
---|
| 397 | Gui* gui = new Gui(argc, argv); |
---|
| 398 | gui->printHelp(); |
---|
| 399 | delete gui; |
---|
| 400 | } |
---|
[4135] | 401 | PRINT(0)(" -b|--benchmark:\t\tstarts the orxonox benchmark\n"); |
---|
| 402 | PRINT(0)(" -h|--help:\t\t\tshows this help\n"); |
---|
[3648] | 403 | } |
---|
| 404 | |
---|
[3649] | 405 | |
---|
[4766] | 406 | |
---|
| 407 | /** |
---|
| 408 | * starts orxonox |
---|
| 409 | * @param argc parameters count given to orxonox |
---|
| 410 | * @param argv parameters given to orxonox |
---|
| 411 | */ |
---|
[3648] | 412 | int startOrxonox(int argc, char** argv) |
---|
| 413 | { |
---|
[4830] | 414 | // checking for existence of the configuration-files, or if the lock file is still used |
---|
[5424] | 415 | if (showGui || (!ResourceManager::isFile("./orxonox.conf") && |
---|
| 416 | !ResourceManager::isFile(DEFAULT_CONFIG_FILE)) |
---|
| 417 | #if DEBUG < 3 // developers do not need to see the GUI, when orxonox fails |
---|
[4981] | 418 | || ResourceManager::isFile(DEFAULT_LOCK_FILE) |
---|
| 419 | #endif |
---|
| 420 | ) |
---|
[4032] | 421 | { |
---|
[4766] | 422 | if (ResourceManager::isFile(DEFAULT_LOCK_FILE)) |
---|
| 423 | ResourceManager::deleteFile(DEFAULT_LOCK_FILE); |
---|
[4556] | 424 | |
---|
[4132] | 425 | // starting the GUI |
---|
[4056] | 426 | Gui* gui = new Gui(argc, argv); |
---|
[4132] | 427 | gui->startGui(); |
---|
| 428 | |
---|
[4054] | 429 | if (! gui->startOrxonox) |
---|
[4556] | 430 | return 0; |
---|
| 431 | |
---|
[4054] | 432 | delete gui; |
---|
[4032] | 433 | } |
---|
[4556] | 434 | |
---|
[4032] | 435 | PRINT(0)(">>> Starting Orxonox <<<\n"); |
---|
[4033] | 436 | |
---|
[4766] | 437 | ResourceManager::touchFile(DEFAULT_LOCK_FILE); |
---|
[4033] | 438 | |
---|
[1850] | 439 | Orxonox *orx = Orxonox::getInstance(); |
---|
[4556] | 440 | |
---|
[5488] | 441 | if(orx->init(argc, argv) == -1) |
---|
[2636] | 442 | { |
---|
[4032] | 443 | PRINTF(1)("! Orxonox initialization failed\n"); |
---|
[2636] | 444 | return -1; |
---|
| 445 | } |
---|
[4556] | 446 | |
---|
[5018] | 447 | printf("finished inizialisation\n"); |
---|
[2636] | 448 | orx->start(); |
---|
[4556] | 449 | |
---|
[3676] | 450 | delete orx; |
---|
[4033] | 451 | ResourceManager::deleteFile("~/.orxonox/orxonox.lock"); |
---|
[1803] | 452 | } |
---|