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