| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (C) 2005-2010 by the FIFE team * |
|---|
| 3 | * http://www.fifengine.net * |
|---|
| 4 | * This file is part of FIFE. * |
|---|
| 5 | * * |
|---|
| 6 | * FIFE is free software; you can redistribute it and/or * |
|---|
| 7 | * modify it under the terms of the GNU Lesser General Public * |
|---|
| 8 | * License as published by the Free Software Foundation; either * |
|---|
| 9 | * version 2.1 of the License, or (at your option) any later version. * |
|---|
| 10 | * * |
|---|
| 11 | * This library is distributed in the hope that it will be useful, * |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
|---|
| 14 | * Lesser General Public License for more details. * |
|---|
| 15 | * * |
|---|
| 16 | * You should have received a copy of the GNU Lesser General Public * |
|---|
| 17 | * License along with this library; if not, write to the * |
|---|
| 18 | * Free Software Foundation, Inc., * |
|---|
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * |
|---|
| 20 | ***************************************************************************/ |
|---|
| 21 | |
|---|
| 22 | // Standard C++ library includes |
|---|
| 23 | #include <algorithm> |
|---|
| 24 | #include <string> |
|---|
| 25 | |
|---|
| 26 | // 3rd party library includes |
|---|
| 27 | #include <SDL.h> |
|---|
| 28 | |
|---|
| 29 | // FIFE includes |
|---|
| 30 | // These includes are split up in two parts, separated by one empty line |
|---|
| 31 | // First block: files included from the FIFE root src directory |
|---|
| 32 | // Second block: files included from the same folder |
|---|
| 33 | #include "util/base/exception.h" |
|---|
| 34 | |
|---|
| 35 | #include "enginesettings.h" |
|---|
| 36 | |
|---|
| 37 | namespace FIFE { |
|---|
| 38 | const float MAXIMUM_VOLUME = 10.0; |
|---|
| 39 | |
|---|
| 40 | EngineSettings::EngineSettings(): |
|---|
| 41 | m_bitsperpixel(0), |
|---|
| 42 | m_fullscreen(false), |
|---|
| 43 | m_initialvolume(MAXIMUM_VOLUME / 2), |
|---|
| 44 | m_renderbackend("SDL"), |
|---|
| 45 | m_sdlremovefakealpha(false), |
|---|
| 46 | m_screenwidth(800), |
|---|
| 47 | m_screenheight(600), |
|---|
| 48 | m_windowtitle("FIFE"), |
|---|
| 49 | m_windowicon(""), |
|---|
| 50 | m_defaultfontpath(""), |
|---|
| 51 | m_defaultfontsize(8), |
|---|
| 52 | m_defaultfontglyphs(""), |
|---|
| 53 | m_lighting(0), |
|---|
| 54 | m_iscolorkeyenabled(false){ |
|---|
| 55 | m_colorkey.r = 255; |
|---|
| 56 | m_colorkey.g = 0; |
|---|
| 57 | m_colorkey.b = 255; |
|---|
| 58 | |
|---|
| 59 | #if defined( __unix__ ) |
|---|
| 60 | m_videodriver = "x11"; |
|---|
| 61 | #elif defined( WIN32 ) |
|---|
| 62 | m_videodriver = "windib"; |
|---|
| 63 | #elif defined( __APPLE_CC__ ) |
|---|
| 64 | m_videodriver = "x11"; |
|---|
| 65 | #else |
|---|
| 66 | m_videodriver = ""; |
|---|
| 67 | #endif |
|---|
| 68 | |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | EngineSettings::~EngineSettings() { |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | void EngineSettings::validate() const { |
|---|
| 75 | if (m_defaultfontpath == "") { |
|---|
| 76 | throw NotSet("Path for default font is not set"); |
|---|
| 77 | } |
|---|
| 78 | std::string::size_type loc = m_defaultfontpath.find(".ttf", 0); |
|---|
| 79 | if ((loc == std::string::npos) && (m_defaultfontglyphs == "")) { |
|---|
| 80 | throw NotSet("Glyphs for default font are not set"); |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | std::vector<std::pair<uint16_t, uint16_t> > EngineSettings::getPossibleResolutions() const { |
|---|
| 85 | SDL_Rect **modes = SDL_ListModes(NULL, ((getRenderBackend() != "SDL") ? (SDL_OPENGL | SDL_HWPALETTE | SDL_HWACCEL) : 0) | (isFullScreen() ? SDL_FULLSCREEN : 0)); |
|---|
| 86 | if(modes == (SDL_Rect **)0) |
|---|
| 87 | throw NotFound("No VideoMode Found"); |
|---|
| 88 | |
|---|
| 89 | std::vector<std::pair<uint16_t, uint16_t> > result; |
|---|
| 90 | if(modes != (SDL_Rect **)-1) |
|---|
| 91 | for(unsigned int i = 0; modes[i]; ++i) |
|---|
| 92 | result.push_back(std::pair<uint16_t, uint16_t>(modes[i]->w, modes[i]->h)); |
|---|
| 93 | return result; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | void EngineSettings::setBitsPerPixel(uint16_t bitsperpixel) { |
|---|
| 97 | std::vector<uint16_t> pv = getPossibleBitsPerPixel(); |
|---|
| 98 | std::vector<uint16_t>::iterator i = std::find(pv.begin(), pv.end(), bitsperpixel); |
|---|
| 99 | if (i != pv.end()) { |
|---|
| 100 | m_bitsperpixel = bitsperpixel; |
|---|
| 101 | return; |
|---|
| 102 | } |
|---|
| 103 | throw NotSupported("Given bits per pixel value is not supported"); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | std::vector<uint16_t> EngineSettings::getPossibleBitsPerPixel() const { |
|---|
| 107 | std::vector<uint16_t> tmp; |
|---|
| 108 | tmp.push_back(0); |
|---|
| 109 | tmp.push_back(16); |
|---|
| 110 | tmp.push_back(24); |
|---|
| 111 | tmp.push_back(32); |
|---|
| 112 | return tmp; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | void EngineSettings::setInitialVolume(float volume) { |
|---|
| 116 | if (volume > getMaxVolume()) { |
|---|
| 117 | throw NotSupported("Given volume exceeds maximum volume"); |
|---|
| 118 | } |
|---|
| 119 | if (volume < 0) { |
|---|
| 120 | throw NotSupported("Given volume is below 0"); |
|---|
| 121 | } |
|---|
| 122 | m_initialvolume = volume; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | float EngineSettings::getMaxVolume() const { |
|---|
| 126 | return MAXIMUM_VOLUME; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | void EngineSettings::setRenderBackend(const std::string& renderbackend) { |
|---|
| 130 | std::vector<std::string> pv = getPossibleRenderBackends(); |
|---|
| 131 | std::vector<std::string>::iterator i = std::find(pv.begin(), pv.end(), renderbackend); |
|---|
| 132 | if (i != pv.end()) { |
|---|
| 133 | m_renderbackend = renderbackend; |
|---|
| 134 | return; |
|---|
| 135 | } |
|---|
| 136 | throw NotSupported("Given render backend is not supported"); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | std::vector<std::string> EngineSettings::getPossibleRenderBackends() { |
|---|
| 140 | std::vector<std::string> tmp; |
|---|
| 141 | tmp.push_back("SDL"); |
|---|
| 142 | tmp.push_back("OpenGL"); |
|---|
| 143 | return tmp; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | void EngineSettings::setSDLRemoveFakeAlpha(bool sdlremovefakealpha) { |
|---|
| 147 | m_sdlremovefakealpha = sdlremovefakealpha; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | void EngineSettings::setScreenWidth(uint16_t screenwidth) { |
|---|
| 151 | m_screenwidth = screenwidth; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | void EngineSettings::setScreenHeight(uint16_t screenheight) { |
|---|
| 155 | m_screenheight = screenheight; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | void EngineSettings::setDefaultFontPath(const std::string& defaultfontpath) { |
|---|
| 159 | m_defaultfontpath = defaultfontpath; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | void EngineSettings::setDefaultFontSize(uint16_t defaultfontsize) { |
|---|
| 163 | m_defaultfontsize = defaultfontsize; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | void EngineSettings::setDefaultFontGlyphs(const std::string& defaultfontglyphs) { |
|---|
| 167 | m_defaultfontglyphs = defaultfontglyphs; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | void EngineSettings::setWindowTitle(const std::string& title) { |
|---|
| 171 | m_windowtitle = title; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | void EngineSettings::setWindowIcon(const std::string& icon) { |
|---|
| 175 | m_windowicon = icon; |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | void EngineSettings::setColorKeyEnabled(bool colorkeyenable) { |
|---|
| 179 | m_iscolorkeyenabled = colorkeyenable; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | bool EngineSettings::isColorKeyEnabled() const { |
|---|
| 183 | return m_iscolorkeyenabled; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | void EngineSettings::setColorKey(uint8_t r, uint8_t g, uint8_t b) { |
|---|
| 187 | m_colorkey.r = r; |
|---|
| 188 | m_colorkey.g = g; |
|---|
| 189 | m_colorkey.b = b; |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | const SDL_Color& EngineSettings::getColorKey() const { |
|---|
| 193 | return m_colorkey; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | void EngineSettings::setVideoDriver(const std::string& driver) { |
|---|
| 197 | m_videodriver = driver; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | const std::string& EngineSettings::getVideoDriver() const { |
|---|
| 201 | return m_videodriver; |
|---|
| 202 | } |
|---|
| 203 | void EngineSettings::setLightingModel(unsigned int lighting) { |
|---|
| 204 | if (lighting <= 2) { |
|---|
| 205 | m_lighting = lighting; |
|---|
| 206 | return; |
|---|
| 207 | } |
|---|
| 208 | throw NotSupported("Given light model is not supported"); |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | } |
|---|
| 212 | |
|---|