/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ### File Specific: main-programmer: Patrick Boenzli co-programmer: Christian Meyer co-programmer: Benjamin Grauer: injected ResourceManager/GraphicsEngine/GUI */ #include "orxonox.h" #include "gui.h" #include "world.h" #include "data_tank.h" #include "command_node.h" #include "ini_parser.h" #include "game_loader.h" #include "graphics_engine.h" #include "sound_engine.h" #include "resource_manager.h" #include "text_engine.h" #include "factory.h" #include "benchmark.h" #include int verbose = 4; using namespace std; /** \brief create a new Orxonox In this funcitons only global values are set. The game will not be started here. */ Orxonox::Orxonox () { this->pause = false; this->world = NULL; this->resources = NULL; this->localinput = NULL; this->argc = 0; this->argv = NULL; } /** \brief remove Orxonox from memory */ Orxonox::~Orxonox () { int i =0; Orxonox::singletonRef = NULL; if( world != NULL) delete world; if( localinput != NULL) delete localinput; if( resources != NULL) delete resources; delete GraphicsEngine::getInstance(); // deleting the Graphics delete TextEngine::getInstance(); delete SoundEngine::getInstance(); delete ResourceManager::getInstance(); // deletes the Resource Manager } /** \brief this is a singleton class to prevent duplicates */ Orxonox* Orxonox::singletonRef = 0; /** \returns reference or new Object of Orxonox if not existent. */ Orxonox* Orxonox::getInstance (void) { if (singletonRef == NULL) singletonRef = new Orxonox(); return singletonRef; } /** \brief this finds the config file Since the config file varies from user to user and since one may want to specify different config files for certain occasions or platforms this function finds the right config file for every occasion and stores it's path and name into configfilename */ void Orxonox::getConfigFile (int argc, char** argv) { strcpy (configfilename, "~/.orxonox/orxonox.conf"); } /** \brief initialize Orxonox with command line */ int Orxonox::init (int argc, char** argv) { this->argc = argc; this->argv = argv; // parse command line // config file getConfigFile (argc, argv); SDL_Init (SDL_INIT_TIMER); // initialize everything printf("> Initializing resources\n"); if( initResources () == -1) return -1; if( initVideo() == -1) return -1; if( initSound() == -1) return -1; PRINT(3)("> Initializing input\n"); if( initInput() == -1) return -1; PRINT(3)("> Initializing networking\n"); if( initNetworking () == -1) return -1; //printf("> Initializing world\n"); //if( init_world () == -1) return -1; PB: world will be initialized when started return 0; } /** \brief initializes SDL and OpenGL */ int Orxonox::initVideo() { PRINTF(3)("> Initializing video\n"); GraphicsEngine::getInstance(); return 0; } /** \brief initializes the sound engine */ int Orxonox::initSound() { PRINT(3)("> Initializing sound\n"); // SDL_Init(SDL_INIT_AUDIO); SoundEngine::getInstance()->initAudio(); return 0; } /** \brief initializes input functions */ int Orxonox::initInput() { // create localinput localinput = new CommandNode(configfilename); return 0; } /** \brief initializes network system */ int Orxonox::initNetworking() { printf("Not yet implemented\n"); return 0; } /** \brief initializes and loads resource files */ int Orxonox::initResources() { PRINT(3)("initializing ResourceManager\n"); resourceManager = ResourceManager::getInstance(); // create parser IniParser parser (DEFAULT_CONFIG_FILE); if( parser.getSection (CONFIG_SECTION_DATA) == -1) { PRINTF(1)("Could not find Section %s in %s\n", CONFIG_SECTION_DATA, DEFAULT_CONFIG_FILE); return -1; } char namebuf[256]; char valuebuf[256]; memset (namebuf, 0, 256); memset (valuebuf, 0, 256); while( parser.nextVar (namebuf, valuebuf) != -1) { if (!strcmp(namebuf, CONFIG_NAME_DATADIR)) { // printf("Not yet implemented\n"); if (!resourceManager->setDataDir(valuebuf)) { PRINTF(1)("Data Could not be located\n"); exit(-1); } } memset (namebuf, 0, 256); memset (valuebuf, 0, 256); } if (!resourceManager->checkDataDir(DEFAULT_DATA_DIR_CHECKFILE)) { PRINTF(1)("The DataDirectory %s could not be verified\nPlease Change in File %s Section %s Entry %s to a suitable value\n", resourceManager->getDataDir(), DEFAULT_CONFIG_FILE, CONFIG_SECTION_DATA, CONFIG_NAME_DATADIR); exit(-1); } PRINT(3)("initializing TextEngine\n"); TextEngine::getInstance(); return 0; } /** \brief initializes the world */ int Orxonox::initWorld() { //world = new World(); // TO DO: replace this with a menu/intro //world->load_debug_level(); return 0; } /** \brief starts the orxonox game or menu here is the central orxonox state manager. There are currently two states - menu - game-play both states manage their states themselfs again. */ void Orxonox::start() { this->gameLoader = GameLoader::getInstance(); this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc"); // this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0); this->gameLoader->init(); this->gameLoader->start(); } /** \brief exits Orxonox */ void Orxonox::quitGame() { bQuitOrxonox = true; } /** \brief handles sprecial events from localinput \param event: an event not handled by the CommandNode */ void Orxonox::eventHandler(SDL_Event* event) { // Handle special events such as reshape, quit, focus changes switch (event->type) { case SDL_VIDEORESIZE: GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance(); tmpGEngine->resolutionChanged(&event->resize); break; } } /** \brief handle keyboard commands that are not meant for WorldEntities \param cmd: the command to handle \return true if the command was handled by the system or false if it may be passed to the WorldEntities */ bool Orxonox::systemCommand(Command* cmd) { /* if( !strcmp( cmd->cmd, "quit")) { if( !cmd->bUp) this->gameLoader->stop(); return true; } return false; */ return false; } /** \brief retrieve a pointer to the local CommandNode \return a pointer to localinput */ CommandNode* Orxonox::getLocalInput() { return localinput; } /** \brief retrieve a pointer to the local World \return a pointer to world */ World* Orxonox::getWorld() { return world; } /** \return The reference of the SDL-screen of orxonox */ SDL_Surface* Orxonox::getScreen () { return this->screen; } bool showGui = false; /** \brief main function here the journey begins */ int main(int argc, char** argv) { // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark. int i; for(i = 1; i < argc; ++i) { if(! strcmp( "--help", argv[i]) || !strcmp("-h", argv[i])) return startHelp(argc, argv); else if(!strcmp( "--benchmark", argv[i]) || !strcmp("-b", argv[i])) return startBenchmarks(); else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true; // else PRINTF(2)("Orxonox does not understand the arguments %s\n", argv[i]); } return startOrxonox(argc, argv); } int startHelp(int argc, char** argv) { PRINT(0)("orxonox: starts the orxonox game - rules\n"); PRINT(0)("usage: orxonox [arg [arg...]]\n\n"); PRINT(0)("valid options:\n"); { Gui* gui = new Gui(argc, argv); gui->printHelp(); delete gui; } PRINT(0)(" -b|--benchmark:\t\tstarts the orxonox benchmark\n"); PRINT(0)(" -h|--help:\t\t\tshows this help\n"); } int startOrxonox(int argc, char** argv) { // checking for existence of the configuration-files if (showGui || !ResourceManager::isFile("~/.orxonox/orxonox.conf") || ResourceManager::isFile("~/.orxonox/orxonox.lock")) { if (ResourceManager::isFile("~/.orxonox/orxonox.lock")) ResourceManager::deleteFile("~/.orxonox/orxonox.lock"); // starting the GUI Gui* gui = new Gui(argc, argv); gui->startGui(); if (! gui->startOrxonox) return 0; delete gui; } PRINT(0)(">>> Starting Orxonox <<<\n"); ResourceManager::touchFile("~/.orxonox/orxonox.lock"); Orxonox *orx = Orxonox::getInstance(); if((*orx).init(argc, argv) == -1) { PRINTF(1)("! Orxonox initialization failed\n"); return -1; } orx->start(); delete orx; ResourceManager::deleteFile("~/.orxonox/orxonox.lock"); }