| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (C) 2005-2013 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 | #include "util/log/logger.h" |
|---|
| 35 | |
|---|
| 36 | #include "enginesettings.h" |
|---|
| 37 | |
|---|
| 38 | namespace FIFE { |
|---|
| 39 | /** Logger to use for this source file. |
|---|
| 40 | * @relates Logger |
|---|
| 41 | */ |
|---|
| 42 | static Logger _log(LM_CONTROLLER); |
|---|
| 43 | |
|---|
| 44 | const float MAXIMUM_VOLUME = 10.0; |
|---|
| 45 | |
|---|
| 46 | EngineSettings::EngineSettings(): |
|---|
| 47 | m_bitsperpixel(0), |
|---|
| 48 | m_fullscreen(false), |
|---|
| 49 | m_initialvolume(MAXIMUM_VOLUME / 2), |
|---|
| 50 | m_renderbackend("SDL"), |
|---|
| 51 | m_sdlremovefakealpha(false), |
|---|
| 52 | m_oglcompressimages(false), |
|---|
| 53 | m_ogluseframebuffer(true), |
|---|
| 54 | m_oglusenpot(true), |
|---|
| 55 | m_screenwidth(800), |
|---|
| 56 | m_screenheight(600), |
|---|
| 57 | m_windowtitle("FIFE"), |
|---|
| 58 | m_windowicon(""), |
|---|
| 59 | m_defaultfontpath("fonts/FreeSans.ttf"), |
|---|
| 60 | m_defaultfontsize(8), |
|---|
| 61 | m_defaultfontglyphs("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&`'*#=[]\\\""), |
|---|
| 62 | m_iscolorkeyenabled(false), |
|---|
| 63 | m_lighting(0), |
|---|
| 64 | m_isframelimit(false), |
|---|
| 65 | m_framelimit(60), |
|---|
| 66 | m_mousesensitivity(0.0), |
|---|
| 67 | m_mouseacceleration(false) { |
|---|
| 68 | m_colorkey.r = 255; |
|---|
| 69 | m_colorkey.g = 0; |
|---|
| 70 | m_colorkey.b = 255; |
|---|
| 71 | |
|---|
| 72 | #if defined( __unix__ ) |
|---|
| 73 | m_videodriver = "x11"; |
|---|
| 74 | #elif defined( WIN32 ) |
|---|
| 75 | m_videodriver = "windib"; |
|---|
| 76 | #elif defined( __APPLE_CC__ ) |
|---|
| 77 | m_videodriver = "x11"; |
|---|
| 78 | #else |
|---|
| 79 | m_videodriver = ""; |
|---|
| 80 | #endif |
|---|
| 81 | |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | EngineSettings::~EngineSettings() { |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | void EngineSettings::setBitsPerPixel(uint8_t bitsperpixel) { |
|---|
| 88 | std::vector<uint8_t> pv = getPossibleBitsPerPixel(); |
|---|
| 89 | std::vector<uint8_t>::iterator i = std::find(pv.begin(), pv.end(), bitsperpixel); |
|---|
| 90 | if (i != pv.end()) { |
|---|
| 91 | m_bitsperpixel = bitsperpixel; |
|---|
| 92 | return; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | FL_WARN(_log, LMsg("EngineSettings::setBitsPerPixel() - ") |
|---|
| 96 | << " Tried to set screen bpp to an unsupporded value of " << bitsperpixel << |
|---|
| 97 | ". Setting bpp to use the default value of 0 (the current screen bpp)"); |
|---|
| 98 | |
|---|
| 99 | m_bitsperpixel = 0; //default value |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | std::vector<uint8_t> EngineSettings::getPossibleBitsPerPixel() const { |
|---|
| 103 | std::vector<uint8_t> tmp; |
|---|
| 104 | tmp.push_back(0); |
|---|
| 105 | tmp.push_back(16); |
|---|
| 106 | tmp.push_back(24); |
|---|
| 107 | tmp.push_back(32); |
|---|
| 108 | return tmp; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | void EngineSettings::setInitialVolume(float volume) { |
|---|
| 112 | if (volume > getMaxVolume() || volume < 0) { |
|---|
| 113 | FL_WARN(_log, LMsg("EngineSettings::setInitialVolume() - ") |
|---|
| 114 | << " Tried to set initial volume to an unsupporded value of " << volume << |
|---|
| 115 | ". Setting volume to the default value of 5 (minumum is 0, maximum is 10)"); |
|---|
| 116 | |
|---|
| 117 | m_initialvolume = 5.0; |
|---|
| 118 | return; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | m_initialvolume = volume; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | float EngineSettings::getMaxVolume() const { |
|---|
| 125 | return MAXIMUM_VOLUME; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | void EngineSettings::setRenderBackend(const std::string& renderbackend) { |
|---|
| 129 | std::vector<std::string> pv = getPossibleRenderBackends(); |
|---|
| 130 | std::vector<std::string>::iterator i = std::find(pv.begin(), pv.end(), renderbackend); |
|---|
| 131 | if (i != pv.end()) { |
|---|
| 132 | m_renderbackend = renderbackend; |
|---|
| 133 | return; |
|---|
| 134 | } |
|---|
| 135 | FL_WARN(_log, LMsg("EngineSettings::setRenderBackend() - ") |
|---|
| 136 | << renderbackend << " is not a valid render backend " << |
|---|
| 137 | ". Setting the render backend to the default value of \"SDL\"."); |
|---|
| 138 | |
|---|
| 139 | m_renderbackend = "SDL"; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | std::vector<std::string> EngineSettings::getPossibleRenderBackends() { |
|---|
| 143 | std::vector<std::string> tmp; |
|---|
| 144 | tmp.push_back("SDL"); |
|---|
| 145 | tmp.push_back("OpenGL"); |
|---|
| 146 | tmp.push_back("OpenGLe"); |
|---|
| 147 | return tmp; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | void EngineSettings::setSDLRemoveFakeAlpha(bool sdlremovefakealpha) { |
|---|
| 151 | m_sdlremovefakealpha = sdlremovefakealpha; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | void EngineSettings::setGLCompressImages(bool oglcompressimages) { |
|---|
| 155 | m_oglcompressimages = oglcompressimages; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | void EngineSettings::setGLUseFramebuffer(bool ogluseframebuffer) { |
|---|
| 159 | m_ogluseframebuffer = ogluseframebuffer; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | void EngineSettings::setGLUseNPOT(bool oglusenpot) { |
|---|
| 163 | m_oglusenpot = oglusenpot; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | void EngineSettings::setScreenWidth(uint16_t screenwidth) { |
|---|
| 167 | m_screenwidth = screenwidth; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | void EngineSettings::setScreenHeight(uint16_t screenheight) { |
|---|
| 171 | m_screenheight = screenheight; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | void EngineSettings::setDefaultFontPath(const std::string& defaultfontpath) { |
|---|
| 175 | m_defaultfontpath = defaultfontpath; |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | void EngineSettings::setDefaultFontSize(uint16_t defaultfontsize) { |
|---|
| 179 | m_defaultfontsize = defaultfontsize; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | void EngineSettings::setDefaultFontGlyphs(const std::string& defaultfontglyphs) { |
|---|
| 183 | m_defaultfontglyphs = defaultfontglyphs; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | void EngineSettings::setWindowTitle(const std::string& title) { |
|---|
| 187 | m_windowtitle = title; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | void EngineSettings::setWindowIcon(const std::string& icon) { |
|---|
| 191 | m_windowicon = icon; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | void EngineSettings::setColorKeyEnabled(bool colorkeyenable) { |
|---|
| 195 | m_iscolorkeyenabled = colorkeyenable; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | bool EngineSettings::isColorKeyEnabled() const { |
|---|
| 199 | return m_iscolorkeyenabled; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | void EngineSettings::setColorKey(uint8_t r, uint8_t g, uint8_t b) { |
|---|
| 203 | m_colorkey.r = r; |
|---|
| 204 | m_colorkey.g = g; |
|---|
| 205 | m_colorkey.b = b; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | const SDL_Color& EngineSettings::getColorKey() const { |
|---|
| 209 | return m_colorkey; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | void EngineSettings::setVideoDriver(const std::string& driver) { |
|---|
| 213 | //TODO: validate the video driver |
|---|
| 214 | m_videodriver = driver; |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | const std::string& EngineSettings::getVideoDriver() const { |
|---|
| 218 | return m_videodriver; |
|---|
| 219 | } |
|---|
| 220 | void EngineSettings::setLightingModel(uint32_t lighting) { |
|---|
| 221 | if (lighting <= 2) { |
|---|
| 222 | m_lighting = lighting; |
|---|
| 223 | return; |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | FL_WARN(_log, LMsg("EngineSettings::setLightingModel() - ") |
|---|
| 227 | << lighting << " is not a valid lighting model." << |
|---|
| 228 | ". Setting the lighting model to the default value of 0 (off)"); |
|---|
| 229 | |
|---|
| 230 | m_lighting = 0; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | void EngineSettings::setFrameLimitEnabled(bool limited) { |
|---|
| 234 | m_isframelimit = limited; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | bool EngineSettings::isFrameLimitEnabled() const { |
|---|
| 238 | return m_isframelimit; |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | void EngineSettings::setFrameLimit(uint16_t framelimit) { |
|---|
| 242 | m_framelimit = framelimit; |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | uint16_t EngineSettings::getFrameLimit() const { |
|---|
| 246 | return m_framelimit; |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | void EngineSettings::setMouseSensitivity(float sens) { |
|---|
| 250 | m_mousesensitivity = sens; |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | float EngineSettings::getMouseSensitivity() const { |
|---|
| 254 | return m_mousesensitivity; |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | void EngineSettings::setMouseAccelerationEnabled(bool acceleration) { |
|---|
| 258 | m_mouseacceleration = acceleration; |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | bool EngineSettings::isMouseAccelerationEnabled() const { |
|---|
| 262 | return m_mouseacceleration; |
|---|
| 263 | } |
|---|
| 264 | } |
|---|
| 265 | |
|---|