// $Id: main.cc,v 1.6 2006/08/23 22:43:17 matthew Exp $ // Fish Supper // Copyright (C) 2006 Matthew Clarke // // 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 // of the License, 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. #include "SDL.h" #include "SDL_mixer.h" #include #include #include "enums.h" #include "gamemanager.h" #include "fontrepository.h" #include #include #include #include #ifdef __unix__ #include #include #endif // ************************ // *** GLOBAL VARIABLES *** // ************************ bool test_level = false; char * test_file; bool joystick_available = false; char * fs_dir; FS::FontRepository * font_rep; // ************************ // *** STATIC VARIABLES *** // ************************ static bool fullscreen = false; // *************************** // *** FUNCTION PROTOTYPES *** // *************************** void set_fs_dir(); void print_version(); void print_help(); void process_command_line_args(int argc, char * argv[]); // ************** // *** main() *** // ************** int main(int argc, char * argv[]) { // This ensures that everything is cleaned up on exit. // N.B. Functions are called in reverse order. atexit(SDL_Quit); atexit(Mix_CloseAudio); process_command_line_args(argc, argv); // initialize SDL - need video, audio and joystick subsystems if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_JOYSTICK) < 0 ) { std::cerr << "Can't initialize SDL: " << SDL_GetError() << std::endl; exit(EXIT_FAILURE); } // if // set up audio (via SDL mixer) if ( Mix_OpenAudio(22050, AUDIO_S16SYS, 2, 1024) < 0 ) { std::cerr << "Can't open audio: " << Mix_GetError() << std::endl; exit(EXIT_FAILURE); } // if // set up joystick SDL_Joystick * js = NULL; if ( SDL_NumJoysticks() > 0 ) { if ( (js = SDL_JoystickOpen(0)) ) { joystick_available = true; std::cerr << "Joystick available: " << SDL_JoystickName(0) << std::endl; } // if } // if // Get an SDL_Surface. SDL_Surface * screen = NULL; Uint32 flags = (SDL_SWSURFACE | SDL_ANYFORMAT | ((fullscreen) ? SDL_FULLSCREEN : 0)); if ( (screen = SDL_SetVideoMode(FS::SCREEN_WIDTH, FS::SCREEN_HEIGHT, FS::BPP, flags)) == NULL ) { std::cerr << "Can't get an SDL surface: " << SDL_GetError() << std::endl; exit(EXIT_FAILURE); } // if // set window/icon title SDL_WM_SetCaption( PACKAGE_STRING, PACKAGE_NAME ); // hide mouse cursor SDL_ShowCursor(SDL_DISABLE); // seed random-number generator srand(time(0)); set_fs_dir(); // Set up FontRepository object. This is needed by many classes under // control of GameManager. font_rep = new FS::FontRepository(); // Pass control to GameManager. FS::GameManager gm(screen); gm.start_loop(); delete font_rep; delete [] fs_dir; SDL_JoystickClose(js); Mix_CloseAudio(); SDL_Quit(); return 0; } // main() // **************************** // *** FUNCTION DEFINITIONS *** // **************************** // ************************************************** void set_fs_dir() { bool ok = false; #ifdef __unix__ char * home_dir; std::cerr << "Checking for $HOME... "; if ( (home_dir = getenv("HOME")) ) { std::cerr << home_dir << std::endl; fs_dir = new char[ strlen(home_dir) + strlen("/.fishsupper") + 1 ]; strcpy(fs_dir, home_dir); strcat(fs_dir, "/.fishsupper"); std::cerr << "Creating directory " << fs_dir << "... "; if ( !mkdir(fs_dir, (S_IRWXU|S_IRWXG|S_IRWXO)) ) { std::cerr << "OK\n"; // Write config.txt and table.txt in new dotdir. std::ofstream fout; std::string my_file; my_file = fs_dir; my_file.append("/config.txt"); std::cerr << "Writing file " << my_file << "... "; fout.open( my_file.c_str() ); if ( fout.is_open() ) { fout << "joystick_selected=0" << std::endl << "sound=1" << std::endl << "fullscreen=0" << std::endl << "furthest_level=1" << std::endl; fout.close(); std::cerr << "OK\n"; my_file = fs_dir; my_file.append("/table.txt"); std::cerr << "Writing file " << my_file << "... "; fout.open( my_file.c_str() ); if ( fout.is_open() ) { fout << "Bertie=4000" << std::endl << "Agatha=3000" << std::endl << "Oofy=2000" << std::endl << "Madelaine=1000" << std::endl << "Spode=500" << std::endl; fout.close(); std::cerr << "OK\n"; ok = true; } else { // couldn't write table.txt std::cerr << "failed\n"; delete [] fs_dir; } // if ... else } else { // couldn't write config.txt std::cerr << "failed\n"; delete [] fs_dir; } // if ... else } else { // couldn't create dotdir // Is this because it already exists or has an error occurred? switch (errno) { case EEXIST: std::cerr << "exists\n"; ok = true; break; default: std::cerr << "failed\n"; delete [] fs_dir; break; } // switch } // if ... else } else { // couldn't find $HOME std::cerr << "failed\n"; } // if ... else #endif if (!ok) { fs_dir = new char[ strlen(".") ]; strcpy(fs_dir, "."); } // if ... else } // set_fs_dir() // ************************************************** void print_version() { std::cerr << PACKAGE_STRING "\n" "Copyright (C) 2006 Matthew Clarke\n" "This is free software; see the source for copying conditions. " "There is NO\n" "warranty; not even for MERCHANTABILITY or FITNESS FOR A " "PARTICULAR PURPOSE.\n\n"; } // print_version() // ************************************************** void print_help() { std::cerr << "Usage: fishsupper [options]\n" "Options:\n" " --fullscreen Start game in fullscreen mode\n" " --help Display this message and quit\n" " --testlevel=FILE Play level called 'FILE' repeatedly\n" " --version Display version number and quit\n\n"; } // print_help() // ************************************************** void process_command_line_args(int argc, char * argv[]) { for (int i = 1; i < argc; ++i) { if (!strcmp(argv[i], "--help")) { print_help(); exit(EXIT_SUCCESS); } else if (!strcmp(argv[i], "--version")) { print_version(); exit(EXIT_SUCCESS); } // Also check that arg is > 12 (i.e. that at least one character follows '='). // FIXME: error checking and print message to screen? else if ( (!strncmp(argv[i], "--testlevel=", 12)) && (strlen(argv[i]) > 12) ) { test_level = true; test_file = &(argv[i][12]); } else if (!strcmp(argv[i], "--fullscreen")) { fullscreen = true; } // if ... else } // for } // process_command_line_args() // **************************************************