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