main_options.cpp

Go to the documentation of this file.
00001 
00002 //            Copyright (C) 2004-2007 by The Allacrost Project
00003 //                         All Rights Reserved
00004 //
00005 // This code is licensed under the GNU GPL version 2. It is free software
00006 // and you may modify it and/or redistribute it under the terms of this license.
00007 // See http://www.gnu.org/copyleft/gpl.html for details.
00009 
00016 #include "utils.h"
00017 #include "defs.h"
00018 #include "audio.h"
00019 #include "video.h"
00020 #include "script.h"
00021 #include "mode_manager.h"
00022 #include "input.h"
00023 #include "system.h"
00024 #include "global.h"
00025 #include "main_options.h"
00026 
00027 using namespace std;
00028 using namespace hoa_utils;
00029 
00030 namespace hoa_main {
00031 
00032 bool ParseProgramOptions(int32 &return_code, int32 argc, char **argv) {
00033   // Convert the argument list to a vector of strings for convenience
00034   vector<string> options(argv, argv + argc);
00035   return_code = 0;
00036 
00037   for (uint32 i = 1; i < options.size(); i++) {
00038     if (options[i] == "-c" || options[i] == "--check") {
00039       if (CheckFiles() == true) {
00040         return_code = 0;
00041       }
00042       else {
00043         return_code = 1;
00044       }
00045       return false;
00046     }
00047     else if (options[i] == "-d" || options[i] == "--debug") {
00048       if ((i + 1) >= options.size()) {
00049         cerr << "Option " << options[i] << " requires an argument." << endl;
00050         PrintUsage();
00051         return_code = 1;
00052         return false;
00053       }
00054       if (EnableDebugging(options[i + 1]) == false) {
00055         return_code = 1;
00056         return false;
00057       }
00058       i++;
00059     }
00060     else if (options[i] == "-h" || options[i] == "--help") {
00061       PrintUsage();
00062       return_code = 0;
00063       return false;
00064     }
00065     else if (options[i] == "-i" || options[i] == "--info") {
00066       if (PrintSystemInformation() == true) {
00067         return_code = 0;
00068       }
00069       else {
00070         return_code = 1;
00071       }
00072       return false;
00073     }
00074     else if (options[i] == "-r" || options[i] == "--reset") {
00075       if (ResetSettings() == true) {
00076         return_code = 0;
00077       }
00078       else {
00079         return_code = 1;
00080       }
00081       return_code = 0;
00082       return false;
00083     }
00084     else if (options[i] == "-t" || options[i] == "--test") {
00085       if ((i + 1) >= options.size()) {
00086         cerr << "Option " << options[i] << " requires an argument." << endl;
00087         PrintUsage();
00088         return_code = 1;
00089         return false;
00090       }
00091       // TODO: convert options[i+1] from a string to a number and call test code
00092 //      if (DEBUG_TestCode(options[i + 1]) == false) {
00093 //        return_code = 1;
00094 //        return false;
00095 //      }
00096       i++;
00097     }
00098     else if (options[i] == "-v" || options[i] == "--version") {
00099       if (IsLatestVersion())
00100         cout << "This is the latest version of Allacrost" << endl;
00101       else
00102         cout << "A newer version of Allacrost (" << GetLatestVersion() << ") is available!" << endl;
00103       return false;
00104     }
00105     else {
00106       cerr << "Unrecognized option: " << options[i] << endl;
00107       PrintUsage();
00108       return_code = 1;
00109       return false;
00110     }
00111   }
00112 
00113   return true;
00114 } // bool ParseProgramOptions(int32_t &return_code, int32_t argc, char **argv)
00115 
00116 
00117 
00118 // Prints out the usage options (arguments) for running the program (work in progress)
00119 void PrintUsage() {
00120   cout << "usage: allacrost [options]" << endl;
00121   cout << "  --check/-c        :: checks all files for integrity" << endl;
00122   cout << "  --debug/-d <args> :: enables debug statements in specifed sections of the" << endl;
00123   cout << "                       program, where <args> can be:" << endl;
00124   cout << "                       all, audio, battle, boot, data, global, input," << endl;
00125   cout << "                       map, mode_manager, pause, quit, scene, system" << endl;
00126   cout << "                       utils, video" << endl;
00127   cout << "  --help/-h         :: prints this help menu" << endl;
00128   cout << "  --info/-i         :: prints information about the user's system" << endl;
00129   cout << "  --reset/-r        :: resets game configuration to use default settings" << endl;
00130   cout << "  --test/-t <arg>   :: executes requested testing code and then exits" << endl;
00131   cout << "  --version/-v      :: checks for newer versions of Allacrost online" << endl;
00132 }
00133 
00134 
00135 // EnableDebugging enables various debugging print statements in different parts of the game
00136 bool EnableDebugging(string vars) {
00137   if (vars.empty()) {
00138     cerr << "ERROR: debug specifier string is empty" << endl;
00139     return false;
00140   }
00141 
00142   // A vector of all the debug arguments
00143   vector<string> args;
00144 
00145   // Find the first non-whitespace character
00146   uint32 sbegin = 0;
00147   while (vars[sbegin] == ' ' || vars[sbegin] == '\t') {
00148     sbegin++;
00149     if (sbegin >= vars.size()) {
00150       cerr << "ERROR: no white-space characters in debug specifier string" << endl;
00151       return false;
00152     }
00153   }
00154 
00155   // Parse the vars string on white-space characters and fill the args vector
00156   // TODO: this loop needs to be made more robust to errors
00157   for (uint32 i = sbegin; i < vars.size(); i++) {
00158     if (vars[i] == ' ' || vars[i] == '\t') {
00159       args.push_back(vars.substr(sbegin, i - sbegin));
00160       sbegin = i + 1;
00161     }
00162   }
00163   args.push_back(vars.substr(sbegin, vars.size() - sbegin));
00164 
00165   // Enable all specified debug variables
00166   for (uint32 i = 0; i < args.size(); i++) {
00167     if (args[i] == "all") {
00168       // This causes every call to SDL_SetError to also print an error message on stderr.
00169       // NOTE: commented out because apparently SDL_putenv is not yet an available function on some systems
00170       // SDL_putenv("SDL_DEBUG=1");
00171 
00172       hoa_audio::AUDIO_DEBUG                  = true;
00173       hoa_battle::BATTLE_DEBUG                = true;
00174       hoa_boot::BOOT_DEBUG                    = true;
00175       hoa_script::SCRIPT_DEBUG                = true;
00176       hoa_mode_manager::MODE_MANAGER_DEBUG    = true;
00177       hoa_input::INPUT_DEBUG                  = true;
00178       hoa_system::SYSTEM_DEBUG                = true;
00179       hoa_global::GLOBAL_DEBUG                = true;
00180       hoa_map::MAP_DEBUG                      = true;
00181       hoa_menu::MENU_DEBUG                    = true;
00182       hoa_pause::PAUSE_DEBUG                  = true;
00183       hoa_quit::QUIT_DEBUG                    = true;
00184       hoa_shop::SHOP_DEBUG                    = true;
00185       hoa_scene::SCENE_DEBUG                  = true;
00186       hoa_utils::UTILS_DEBUG                  = true;
00187       hoa_video::VIDEO_DEBUG                  = true;
00188     }
00189     else if (args[i] == "audio") {
00190       hoa_audio::AUDIO_DEBUG = true;
00191     }
00192     else if (args[i] == "battle") {
00193       hoa_battle::BATTLE_DEBUG = true;
00194     }
00195     else if (args[i] == "boot") {
00196       hoa_boot::BOOT_DEBUG = true;
00197     }
00198     else if (args[i] == "data") {
00199       hoa_script::SCRIPT_DEBUG = true;
00200     }
00201     else if (args[i] == "mode_manager") {
00202       hoa_mode_manager::MODE_MANAGER_DEBUG = true;
00203     }
00204     else if (args[i] == "input") {
00205       hoa_input::INPUT_DEBUG = true;
00206     }
00207     else if (args[i] == "system") {
00208       hoa_system::SYSTEM_DEBUG = true;
00209     }
00210     else if (args[i] == "global") {
00211       hoa_global::GLOBAL_DEBUG = true;
00212     }
00213     else if (args[i] == "map") {
00214       hoa_map::MAP_DEBUG = true;
00215     }
00216     else if (args[i] == "menu") {
00217       hoa_menu::MENU_DEBUG = true;
00218     }
00219     else if (args[i] == "pause") {
00220       hoa_pause::PAUSE_DEBUG = true;
00221     }
00222     else if (args[i] == "quit") {
00223       hoa_quit::QUIT_DEBUG = true;
00224     }
00225     else if (args[i] == "scene") {
00226       hoa_scene::SCENE_DEBUG = true;
00227     }
00228     else if (args[i] == "shop") {
00229       hoa_shop::SHOP_DEBUG = true;
00230     }
00231     else if (args[i] == "utils") {
00232       hoa_utils::UTILS_DEBUG = true;
00233     }
00234     else if (args[i] == "video") {
00235       hoa_video::VIDEO_DEBUG = true;
00236     }
00237     else {
00238       cerr << "ERROR: invalid debug argument: " << args[i] << endl;
00239       return false;
00240     }
00241   } // for (uint32 i = 0; i < args.size(); i++)
00242 
00243   return true;
00244 } // bool EnableDebugging(string vars)
00245 
00246 
00247 // Prints version numbers for SDL libraries, video rendering information, and other info
00248 //  about the user's system (work in progress)
00249 bool PrintSystemInformation() {
00250   printf("\n===== System Information\n");
00251 
00252   // Initialize SDL and its subsystems and make sure it shutdowns properly on exit
00253   if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) != 0) {
00254     cerr << "ERROR: Unable to initialize SDL: " << SDL_GetError() << endl;
00255     return false;
00256   }
00257   atexit(SDL_Quit);
00258 
00259   printf("SDL version (compiled):  %d.%d.%d\n", SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL);
00260   printf("SDL version (linked):    %d.%d.%d\n", SDL_Linked_Version()->major, SDL_Linked_Version()->minor, SDL_Linked_Version()->patch);
00261 
00262   SDL_Joystick *js_test;
00263   int32 js_num = SDL_NumJoysticks();
00264   printf("Number of joysticks found:  %d\n", js_num);
00265   for (int32 i = 0; i < js_num; i++) { // Print out information about each joystick
00266     printf("  Joystick #%d\n", i);
00267     printf("    Joystick Name: %s\n", SDL_JoystickName(i));
00268     js_test = SDL_JoystickOpen(i);
00269     if (js_test == NULL)
00270       printf("    ERROR: SDL was unable to open joystick #%d!\n", i);
00271     else {
00272       printf("    Number Axes: %d\n", SDL_JoystickNumAxes(js_test));
00273       printf("    Number Buttons: %d\n", SDL_JoystickNumButtons(js_test));
00274       printf("    Number Trackballs: %d\n", SDL_JoystickNumBalls(js_test));
00275       printf("    Number Hat Switches: %d\n", SDL_JoystickNumHats(js_test));
00276       SDL_JoystickClose(js_test);
00277     }
00278   }
00279 
00280   printf("\n===== Video Information\n");
00281 
00282   // TODO: This code should be re-located to a function (DEBUG_PrintInfo()) in the video engine
00283 //  hoa_video::VideoManager = hoa_video::GameVideo::SingletonCreate();
00284 //  if (hoa_video::VideoManager->SingletonInitialize() == false) {
00285 //    cerr << "ERROR: unable to initialize the VideoManager" << endl;
00286 //    return false;
00287 //  }
00288 //  else {
00289 //    hoa_video::VideoManager->DEBUG_PrintInfo();
00290 //  }
00291 //  hoa_video::GameVideo::SingletonDestroy();
00292 
00293   // TODO: print the OpenGL version number here
00294 
00295   printf("SDL_ttf version (compiled): %d.%d.%d\n", SDL_TTF_MAJOR_VERSION, SDL_TTF_MINOR_VERSION, SDL_TTF_PATCHLEVEL);
00296   // printf("SDL_ttf version (linked):   %d.%d.%d\n", Ttf_Linked_Version()->major, Ttf_Linked_Version()->minor, Ttf_Linked_Version()->patch);
00297 
00298   char video_driver[80];
00299   SDL_VideoDriverName(video_driver, 80);
00300   printf("Name of video driver: %s\n", video_driver);
00301 
00302   const SDL_VideoInfo *user_video;
00303   user_video = SDL_GetVideoInfo(); // Get information about the user's video system
00304   cout << "  Best available video mode" << endl;
00305   cout << "    Creates hardware surfaces: ";
00306   if (user_video->hw_available == 1) cout << "yes\n";
00307   else cout << "no\n";
00308   cout << "    Has window manager available: ";
00309   if (user_video->wm_available == 1) cout << "yes\n";
00310   else cout << "no\n";
00311   cout << "    Hardware to hardware blits accelerated: ";
00312   if (user_video->blit_hw == 1) cout << "yes\n";
00313   else cout << "no\n";
00314   cout << "    Hardware to hardware colorkey blits accelerated: ";
00315   if (user_video->blit_hw_CC == 1) cout << "yes\n";
00316   else cout << "no\n";
00317   cout << "    Hardware to hardware alpha blits accelerated: ";
00318   if (user_video->blit_hw_A == 1) cout << "yes\n";
00319   else cout << "no\n";
00320   cout << "    Software to hardware blits acceleerated: ";
00321   if (user_video->blit_sw == 1) cout << "yes\n";
00322   else cout << "no\n";
00323   cout << "    Software to hardware colorkey blits accelerated: ";
00324   if (user_video->blit_sw_CC == 1) cout << "yes\n";
00325   else cout << "no\n";
00326   cout << "    Software to hardware alpha blits accelerated: ";
00327   if (user_video->blit_sw_A == 1) cout << "yes\n";
00328   else cout << "no\n";
00329   cout << "    Color fills accelerated: ";
00330   if (user_video->blit_fill == 1) cout << "yes\n";
00331   else cout << "no\n";
00332   cout << "    Total video memory: " << user_video->video_mem << " kilobytes" << endl;
00333   // cout << "    Best pixel format: " << user_video->vfmt << endl;
00334 
00335   printf("\n===== Audio Information\n");
00336 
00337   hoa_audio::AudioManager = hoa_audio::GameAudio::SingletonCreate();
00338   if (hoa_audio::AudioManager->SingletonInitialize() == false) {
00339     cerr << "ERROR: unable to initialize the AudioManager" << endl;
00340     return false;
00341   }
00342   else {
00343     hoa_audio::AudioManager->DEBUG_PrintInfo();
00344   }
00345   hoa_audio::GameAudio::SingletonDestroy();
00346 
00347   printf("\n");
00348 
00349   return true;
00350 } // bool PrintSystemInformation()
00351 
00352 
00353 // Resets the game settings to default.
00354 bool ResetSettings() {
00355   cerr << "This option is not yet implemented." << endl;
00356 
00357   // And then if they say yes, we overwrite the user prefs file with the default prefs file
00358   return false;
00359 } // bool ResetSettings()
00360 
00361 
00362 
00363 // NOTE: the following function contains operating system dependant code
00364 // Prints any bad file checks (work in progress)
00365 bool CheckFiles() {
00366   cout << "This option is not yet implemented." << endl;
00367   return false;
00368 } // bool CheckFiles()
00369 
00370 
00371 
00372 void DEBUG_TestCode(uint32 code) {
00373   // Default test message
00374   if (code == 0) {
00375     cout << "0: This is not a real test code id, it just prints this message" << endl;
00376   }
00377 
00378   // NOTE: All code cases require a comment describing what the test involves
00379 //  else if (code == 1) {
00380 //    do_something
00381 //  }
00382 
00383 
00384   else {
00385     cerr << "MAIN ERROR: no definition for test code id number: " << code << endl;
00386   }
00387 } // void DEBUG_TestCode(uint32 code)
00388 
00389 } // namespace hoa_main

Generated on Fri Jul 6 23:11:17 2007 for Hero of Allacrost by  doxygen 1.5.1