game_controller_abstract.cpp

Go to the documentation of this file.
00001 /* $Id: game_controller_abstract.cpp 52869 2012-02-03 20:18:33Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2011 - 2012 by Lukasz Dobrogowski <lukasz.dobrogowski@gmail.com>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #include "game_controller_abstract.hpp"
00017 
00018 #include "foreach.hpp"
00019 #include "game_display.hpp"
00020 #include "gettext.hpp"
00021 #include "hotkeys.hpp"
00022 #include "language.hpp"
00023 #include "preferences_display.hpp"
00024 
00025 #include "preferences.hpp"
00026 
00027 #include <iostream>
00028 
00029 game_controller_abstract::game_controller_abstract(const commandline_options &cmdline_opts) :
00030     cmdline_opts_(cmdline_opts),
00031     disp_(NULL),
00032     video_()
00033 {
00034 
00035 }
00036 
00037 game_display& game_controller_abstract::disp()
00038 {
00039     if(disp_.get() == NULL) {
00040         if(get_video_surface() == NULL) {
00041             throw CVideo::error();
00042         }
00043         disp_.assign(game_display::create_dummy_display(video_));
00044     }
00045     return *disp_.get();
00046 }
00047 
00048 bool game_controller_abstract::init_joystick()
00049 {
00050     if (!preferences::joystick_support_enabled())
00051         return false;
00052 
00053     if(SDL_WasInit(SDL_INIT_JOYSTICK) == 0)
00054         if(SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1)
00055             return false;
00056 
00057     int joysticks = SDL_NumJoysticks();
00058     if (joysticks == 0) return false;
00059 
00060     SDL_JoystickEventState(SDL_ENABLE);
00061 
00062     SDL_Joystick* joystick;
00063 
00064     bool joystick_found = false;
00065     for (int i = 0; i<joysticks; i++)  {
00066 
00067         joystick = SDL_JoystickOpen(i);
00068 
00069         if (joystick)
00070             joystick_found = true;
00071     }
00072     return joystick_found;
00073 }
00074 
00075 bool game_controller_abstract::init_language()
00076 {
00077     if(!::load_language_list())
00078         return false;
00079 
00080     language_def locale;
00081     if(cmdline_opts_.language) {
00082         std::vector<language_def> langs = get_languages();
00083         foreach(const language_def & def, langs) {
00084             if(def.localename == *cmdline_opts_.language) {
00085                 locale = def;
00086                 break;
00087             }
00088         }
00089         if(locale.localename.empty()) {
00090             std::cerr << "Language symbol '" << *cmdline_opts_.language << "' not found.\n";
00091             return false;
00092         }
00093     } else {
00094         locale = get_locale();
00095     }
00096 	::set_language(locale);
00097 
00098     if(!cmdline_opts_.nogui) {
00099         std::string wm_title_string = _("The Battle for Wesnoth");
00100         wm_title_string += " - " + game_config::revision;
00101         SDL_WM_SetCaption(wm_title_string.c_str(), NULL);
00102     }
00103 
00104     hotkey::load_descriptions();
00105 
00106     return true;
00107 }
00108 
00109 bool game_controller_abstract::init_video()
00110 {
00111     if(cmdline_opts_.nogui) {
00112         if( !(cmdline_opts_.multiplayer || cmdline_opts_.screenshot) ) {
00113             std::cerr << "--nogui flag is only valid with --multiplayer flag or --screenshot flag\n";
00114             return false;
00115         }
00116         video_.make_fake();
00117         game_config::no_delay = true;
00118         return true;
00119     }
00120 
00121 #if !(defined(__APPLE__))
00122     surface icon(image::get_image("game-icon.png", image::UNSCALED));
00123     if(icon != NULL) {
00124         ///must be called after SDL_Init() and before setting video mode
00125         ::SDL_WM_SetIcon(icon,NULL);
00126     }
00127 #endif
00128 
00129     std::pair<int,int> resolution;
00130     int bpp = 0;
00131     int video_flags = 0;
00132 
00133     bool found_matching = preferences::detect_video_settings(video_, resolution, bpp, video_flags);
00134 
00135     if (cmdline_opts_.bpp) {
00136         bpp = *cmdline_opts_.bpp;
00137     } else if (cmdline_opts_.screenshot) {
00138         bpp = 32;
00139     }
00140 
00141     if(!found_matching) {
00142         std::cerr << "Video mode " << resolution.first << 'x'
00143             << resolution.second << 'x' << bpp
00144             << " is not supported.\n";
00145 
00146         if ((video_flags & FULL_SCREEN)) {
00147             std::cerr << "Try running the program with the --windowed option "
00148                 << "using a " << bpp << "bpp setting for your display adapter.\n";
00149         } else {
00150             std::cerr << "Try running the program with the --fullscreen option.\n";
00151         }
00152 
00153         return false;
00154     }
00155 
00156     std::cerr << "setting mode to " << resolution.first << "x" << resolution.second << "x" << bpp << "\n";
00157     const int res = video_.setMode(resolution.first,resolution.second,bpp,video_flags);
00158     video_.setBpp(bpp);
00159     if(res == 0) {
00160         std::cerr << "required video mode, " << resolution.first << "x"
00161                   << resolution.second << "x" << bpp << " is not supported\n";
00162         return false;
00163     }
00164 
00165     return true;
00166 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:02:52 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs