home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 March / ENTER.ISO / files / fwp-0.0.6-win32-installer.exe / System.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-01-22  |  11.8 KB  |  422 lines

  1. #include "System.h"
  2.  
  3. #include "log.h"
  4. #include "SDL.h"
  5. #include "ConsoleInfo.h"
  6. #include "Display.h"
  7. #include "Renderer.h"
  8. #include "Sound.h"
  9. #include "Gui.h"
  10. #include "Input.h"
  11. #include "Game.h"
  12. #include "Network.h"
  13. #include "version.h"
  14.  
  15. #include <stdlib.h>
  16.  
  17. bool System::init(){
  18.     beginLog();
  19.  
  20.  
  21.     log("\n");
  22.     log("****************************\n");
  23.     log("*** Initializing System ***\n");
  24.     log("****************************\n");
  25.     log("\n");
  26.  
  27.  
  28.     if( SDL_Init( SDL_INIT_VIDEO ) < 0 ){    // THIS MUST BE DONE BEFORE ALL OTHER THINGS!!
  29.         error("(in initSDL()): Failed initializing SDL Video : %s.\n\n", SDL_GetError());
  30.         return false;
  31.     }
  32.  
  33.     if(!ConsoleInfo::init())        // THIS MUST BE DONE SECOND!!
  34.         return false;
  35.  
  36.     if(!registerCVarsAndCCmds())
  37.         return false;
  38.  
  39.     if(!readSystemConfig())
  40.         log("Heul doch!\n");//return false;
  41.  
  42.     if(!Display::init())
  43.         return false;
  44.  
  45.     if(!Renderer::init())
  46.         return false;
  47.  
  48.     if(!ConsoleInfo::initFrontEnd())
  49.         return false;
  50.  
  51.     if(!Sound::init())
  52.         return false;
  53.  
  54.     if(!Gui::init())
  55.         return false;
  56.     
  57.     Gui::loadingMenu->open();
  58.  
  59.     if(!Network::init())
  60.         return false;
  61.  
  62.     if(!Input::init())
  63.         return false;
  64.  
  65.     if(!readKeybindings())
  66.         return false;
  67.     
  68.     if(!readAutoexec())
  69.         return false;
  70.  
  71.     Gui::loadingMenu->close();
  72.  
  73.  
  74.     return true;
  75. }
  76.  
  77.  
  78. /*!
  79. This function quits the game with nearly no shutdown. This one should be used in case of bad errors.
  80. It just calls #endLog() and SDL_Quit().
  81. */
  82. void System::errorQuit(){
  83.     log("\n###### ERROR QUIT #######\n");
  84.     endLog();
  85.  
  86.     SDL_Quit();
  87.     exit(-1);
  88. }
  89.  
  90. /*!
  91. This function quits the game, after shutting down all subsystems.
  92. This one brings the system down CAREFULLY and so it should be used whenever the uses chooses to quit.
  93. \see #errorQuit()
  94. */
  95. void System::normalQuit(){
  96.     log("\n####### NORMAL QUIT ########\n");
  97.  
  98.     if(Game::wasInit())
  99.         Game::shutdown();
  100.  
  101.     if(Gui::wasHudInit())
  102.         Gui::shutdownHud();
  103.  
  104.     if(Renderer::wasParticleSystemInit())
  105.         Renderer::shutdownParticleSystem();
  106.  
  107.     if(Gui::wasInit())
  108.         Gui::shutdown();
  109.  
  110.     writeKeybindings();
  111.  
  112.     if(Input::wasInit())
  113.         Input::shutdown();
  114.  
  115.     if(Network::wasInit())
  116.         Network::shutdown();
  117.  
  118.     if(Sound::wasInit())
  119.         Sound::shutdown();
  120.  
  121.     if(ConsoleInfo::wasFrontEndInit())
  122.         ConsoleInfo::shutdownFrontEnd();
  123.  
  124.     if(Renderer::wasInit())
  125.         Renderer::shutdown();
  126.  
  127.     if(Display::wasInit())
  128.         Display::shutdown();
  129.  
  130.     writeSystemConfig();
  131.  
  132.     unregisterCVarsAndCCmds();
  133.  
  134.     if(ConsoleInfo::wasInit())
  135.         ConsoleInfo::shutdown();
  136.  
  137.     SDL_Quit();
  138.     log("### THE EAGLE HAS LANDED ###\n");
  139.     endLog();
  140.     // THINKABOUTME: hier SDLQuit?
  141.     exit(0);
  142. }
  143.  
  144.  
  145. /*!
  146. This functions creates all game specific console variables and commands and registers them to the console. This requires that the console has been initialized.
  147. This function should be executed as soon as possible in the initialization process, because most other init-functions will use this console variables.
  148. \return \c true on success \c false on errors (most likely the console wasn't initialized).
  149. */
  150. bool System::registerCVarsAndCCmds(){
  151.     if(!ConsoleInfo::wasInit()){
  152.         error("(in createAndRegisterCVars()): Console not initialized.\n\n");
  153.         return false;
  154.     }
  155.  
  156. //    int i;
  157.  
  158.     log("\n");
  159.     log("**************************************\n");
  160.     log("*** Registering FWP-specific CVars ***\n");
  161.     log("**************************************\n");
  162.     log("\n");
  163.  
  164.  
  165.     ConsoleInfo::registerCVarsAndCCmds();
  166.     Display::registerCVarsAndCCmds();
  167.     Renderer::registerCVarsAndCCmds();
  168.     Gui::registerCVarsAndCCmds();
  169.     Sound::registerCVarsAndCCmds();
  170.     Network::registerCVarsAndCCmds();
  171.     Input::registerCVarsAndCCmds();
  172.     Game::registerCVarsAndCCmds();
  173.  
  174.  
  175. //    console->registerCVar(new CVarInt("debugAllzweck", &debugAllzweck, true));
  176.  
  177.     console->registerCCmd(new CCmdSystemQuit());
  178.  
  179.  
  180.  
  181.  
  182.  
  183.     return true;
  184. }
  185.  
  186. /*!
  187. This functions unregisters and deletes all game specific console variables and commands.
  188. This function should be executed right before the game quits or in a console restart process.
  189. \return \c true on success \c false on errors (most likely the console wasn't initialized).
  190. */
  191. bool System::unregisterCVarsAndCCmds(){
  192.     log("\n");
  193.     log("========================================\n");
  194.     log("=== Unregistering FWP-specific CVars ===\n");
  195.     log("========================================\n");
  196.     log("\n");
  197.  
  198.     if(!ConsoleInfo::wasInit()){
  199.         error("(in deleteAndUnregisterCVars()): Console not initialized.\n\n");
  200.         return false;
  201.     }
  202.  
  203.     Display::unregisterCVarsAndCCmds();
  204.     Renderer::unregisterCVarsAndCCmds();
  205.     ConsoleInfo::unregisterCVarsAndCCmds();
  206.     Gui::unregisterCVarsAndCCmds();
  207.     Sound::unregisterCVarsAndCCmds();
  208.     Network::unregisterCVarsAndCCmds();
  209.     Input::unregisterCVarsAndCCmds();
  210.     Game::unregisterCVarsAndCCmds();
  211.  
  212.  
  213.     return true;
  214. }
  215.  
  216. /*!
  217. This functions reads the \c sysinit.config file in the \c configs directory, which contains all settings for the game.
  218. The Console must be initialized and all neccessary cvars and ccmds should be registered (see #createAndRegisterCVars()).
  219. If the file doesn't exist the function will set all values to their default and return false.
  220. This should be executed early in the initialization process, because most other init-functions will read out the cvars that are set here to do their init-stuff.
  221. \warning Since \c sysinit.config is overwritten by #writeSysinit(), autoexec.config should be used for additional manual editing.
  222. \return \c true on success \c false on errors (file not found ot not parsable, ...)
  223. */
  224. bool System::readSystemConfig(){
  225.     log("\n");
  226.     log("*****************************\n");
  227.     log("*** Reading system.config ***\n");
  228.     log("*****************************\n");
  229.     log("\n");
  230.  
  231.     if(!console->parse("execFile configs/system.config")){
  232.         error("(in readSysinit()): Couldn't execute 'configs/system.config'. Using default values.\n\n");
  233.         return false;
  234.     }else{
  235.         log("system.config read.\n");
  236.         return true;
  237.     }
  238.     
  239. }
  240.  
  241. /*!
  242. This functions reads the \c keybindings.config file in the \c configs directory, which contains all key bindings (control settings) for the game.
  243. The Console must be initialized and all neccessary cvars and ccmds should be registered (especially the \c bind ccmd!! See #createAndRegisterCVars()).
  244. Also the input supsysten should be initialized (#initInput()).
  245. If the file doesn't exist the function will set all values to their default and return false.
  246. \warning Since \c keybindigs.config is overwritten by #writeKeybindings(), autoexec.config should be used for manual editing.
  247. \return \c true on success \c false on errors (file not found ot not parsable, ...)
  248.  
  249. */
  250. bool System::readKeybindings(){
  251.     log("\n");
  252.     log("**********************************\n");
  253.     log("*** Reading keybindings.config ***\n");
  254.     log("**********************************\n");
  255.     log("\n");
  256.  
  257.  
  258.     if(!console->parse("execFile configs/keybindings.config")){
  259.         error("(in readKeybindings()): Couldn't execute 'configs/keybindings.config'. Using default bindings.\n\n");
  260.         // TODO: bind default keys !!!
  261.         return false;
  262.     }else{
  263.         log("Keybindings read.\n");
  264.         return true;
  265.     }
  266. }
  267.  
  268. /*!
  269. This functions reads the \c autoexec.config file in the \c configs directory. The purpose of this file is to allow expirienced users to write their own console scripts and have them executed automatically on every startup.
  270. If the file doesn't exist the function will return false (which isn't a big deal here).
  271. \return \c true on success \c false on errors (file not found ot not parsable, ...)
  272. */
  273. bool System::readAutoexec(){
  274.     log("\n");
  275.     log("*******************************\n");
  276.     log("*** Reading autoexec.config ***\n");
  277.     log("*******************************\n");
  278.     log("\n");
  279.  
  280.  
  281.     if(!console->parse("execFile configs/autoexec.config")){
  282.         warn("(in readAutoexec()): Couldn't execute 'configs/autoexec.config'.\n\n");
  283.         return false;
  284.     }else{
  285.         log("Autoexec read.\n");
  286.         return true;
  287.     }
  288. }
  289.  
  290. /*!
  291. This functions writes the \c sysinit.config file in the \c configs directory, which contains all settings for the game. Should be executed right before the game quits to save all settings.
  292. \return \c true on success \c false on errors (file could not be opened, ...)
  293. */
  294. bool System::writeSystemConfig(){
  295.     int i;
  296.  
  297.     log("\n");
  298.     log("==============================\n");
  299.     log("=== Writing sysinit.config ===\n");
  300.     log("==============================\n");
  301.     log("\n");
  302.  
  303.     FILE* f = fopen("configs/system.config", "wt");
  304.     if( f == NULL ){
  305.         warn("(in writeSystemConfig()): Couldn't open 'configs/system.config'.\n\n");
  306.         return false;
  307.     }
  308.  
  309.     fprintf(f, "////////////////////////////////////////////////////////////////////////////////////////\n");
  310.     fprintf(f, "// FWP system configuration - generated by FWP v%s for %s.\n", FWP_VERSION_STRING, FWP_OS_STRING);
  311.     fprintf(f, "// Note: FWP will overwrite this file! Place your personal console-scripts, keybindings, etc in 'autoexec.config'.\n");
  312.     fprintf(f, "//\n");
  313.     fprintf(f, "\n");
  314.  
  315.     fprintf(f, "// CVARS ////////////////////////////////////////////////////////////\n");
  316.     for(i = 0; i < CON_MAX_CVARS; i++){
  317.         CVar* cvar = console->cVars[i];
  318.         if( cvar != NULL ){
  319.             char buff[256];
  320.             if(    cvar->flags & CON_FLAG_SYSTEM ){
  321.                 fprintf(f, "%s \"%s\"\n", cvar->name, cvar->getValStr(buff));
  322.             }
  323.         }
  324.     }
  325.     fprintf(f, "\n");
  326. /*
  327.     fprintf(f, "// ALIASES ////////////////////////////////////////////////////////////\n");
  328.     for(i = 0; i < CON_MAX_ALIASES; i++){
  329.         CAlias* alias = console->aliases[i];
  330.         if( alias != NULL ){
  331. //            char buff[256];
  332.             fprintf(f, "alias %s \"%s\"\n", alias->name, alias->string);
  333.         }
  334.     }
  335. */    
  336.     
  337.     fclose(f);
  338.  
  339.     log("Wrote file 'configs/system.config'.");
  340.     log("\n");
  341.  
  342.     return false;
  343. }
  344.  
  345. /*!
  346. This functions writes the \c keybindigs.config file in the \c configs directory. Should be executed right before the game quits to save all settings.
  347. \return \c true on success \c false on errors (file could not be opened, ...)
  348. */
  349. bool System::writeKeybindings(){
  350.     int i;
  351.  
  352.     log("\n");
  353.     log("===================================\n");
  354.     log("=== Writing keybindings.config ===\n");
  355.     log("==================================\n");
  356.     log("\n");
  357.  
  358.     FILE* f = fopen("configs/keybindings.config", "wt");
  359.     if( f == NULL ){
  360.         warn("(in writeKeybindings()): Couldn't open 'configs/keybindings.config'.\n\n");
  361.         return false;
  362.     }
  363.  
  364.     fprintf(f, "////////////////////////////////////////////////////////////////////////////////////////\n");
  365.     fprintf(f, "// FWP keybinding configuration - generated by FWP v%s for %s.\n", FWP_VERSION_STRING, FWP_OS_STRING);
  366.     fprintf(f, "// Note: FWP will overwrite this file! Place your personal console-scripts, keybindings, etc in 'autoexec.config'.\n");
  367.     fprintf(f, "//\n");
  368.     fprintf(f, "\n");
  369.  
  370.     for(i=0;i<SDLK_LAST;i++){
  371.         if( Input::keyMap[i].action != NO_ACTION ){
  372.             fprintf(f, "input.bind %s %s\n", Input::getKeyName(i), Input::getActionName(Input::keyMap[i].action) );
  373.         }else if( Input::keyMap[i].consoleString != NULL ){
  374.             fprintf(f, "input.bind %s \"%s\"\n", Input::getKeyName(i), Input::keyMap[i].consoleString );
  375.         }
  376.     }
  377.     fprintf(f, "\n");
  378.  
  379.     for(i=0;i<NUM_MOUSE_BUTTONS;i++){
  380.         if( Input::mouse.buttons[i].action != NO_ACTION ){
  381.             fprintf(f, "input.bind mouse%i %s\n", i, Input::getActionName(Input::mouse.buttons[i].action) );
  382.         }else if( Input::mouse.buttons[i].consoleString != NULL ){
  383.             fprintf(f, "input.bind mouse%i \"%s\"\n", i, Input::mouse.buttons[i].consoleString );
  384.         }
  385.     }
  386.     fprintf(f, "\n");
  387.  
  388.     fclose(f);
  389.  
  390.     log("Wrote file 'configs/keybindings.config'.");
  391.     log("\n");
  392.  
  393.     return true;
  394. }
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401. CCmdSystemQuit::CCmdSystemQuit():CCmd("system.quit"){
  402.     usageStr="system.quit";
  403.     infoStr="quits fwp";
  404. }
  405.  
  406. CCmdSystemQuit::~CCmdSystemQuit(){
  407.     if(console!=NULL)
  408.         console->unregisterCCmd(this);
  409. }
  410.  
  411. bool CCmdSystemQuit::exec(int argc, char* argv[]){
  412.     if(argc==0){
  413.         log("exiting...\n");
  414.         System::normalQuit();
  415.         return true;    // never reached
  416.     }else{
  417.         console->print("usage: %s\n", usageStr);
  418.         return false;
  419.     }
  420. }
  421.  
  422.