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