| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2012 Jussi Lind <[email protected]> |
| 3 | // |
| 4 | // Dust Racing 2D is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // Dust Racing 2D is distributed in the hope that it will be useful, |
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | // GNU General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>. |
| 15 | |
| 16 | #include "settings.hpp" |
| 17 | #include "track.hpp" |
| 18 | #include "trackdata.hpp" |
| 19 | #include <QSettings> |
| 20 | #include <cassert> |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | static constexpr auto SETTINGS_GROUP_CONFIG = "Config"; |
| 25 | |
| 26 | } // namespace |
| 27 | |
| 28 | Settings * Settings::m_instance = nullptr; |
| 29 | |
| 30 | QString Settings::difficultyKey() |
| 31 | { |
| 32 | return "difficulty"; |
| 33 | } |
| 34 | |
| 35 | QString Settings::fpsKey() |
| 36 | { |
| 37 | return "fps"; |
| 38 | } |
| 39 | |
| 40 | QString Settings::lapCountKey() |
| 41 | { |
| 42 | return "lapCount"; |
| 43 | } |
| 44 | |
| 45 | QString Settings::soundsKey() |
| 46 | { |
| 47 | return "sounds"; |
| 48 | } |
| 49 | |
| 50 | QString Settings::screenKey() |
| 51 | { |
| 52 | return "screen"; |
| 53 | } |
| 54 | |
| 55 | QString Settings::vsyncKey() |
| 56 | { |
| 57 | return "vsync"; |
| 58 | } |
| 59 | |
| 60 | Settings::Settings() |
| 61 | { |
| 62 | assert(!Settings::m_instance); |
| 63 | Settings::m_instance = this; |
| 64 | |
| 65 | m_actionToStringMap[InputHandler::Action::Up] = "IA_UP"; |
| 66 | m_actionToStringMap[InputHandler::Action::Down] = "IA_DOWN"; |
| 67 | m_actionToStringMap[InputHandler::Action::Left] = "IA_LEFT"; |
| 68 | m_actionToStringMap[InputHandler::Action::Right] = "IA_RIGHT"; |
| 69 | } |
| 70 | |
| 71 | Settings & Settings::instance() |
| 72 | { |
| 73 | assert(Settings::m_instance); |
| 74 | return *Settings::m_instance; |
| 75 | } |
| 76 | |
| 77 | void Settings::saveResolution(int hRes, int vRes, bool fullScreen) |
| 78 | { |
| 79 | QSettings settings; |
| 80 | |
| 81 | settings.beginGroup(SETTINGS_GROUP_CONFIG); |
| 82 | settings.setValue("hRes", hRes); |
| 83 | settings.setValue("vRes", vRes); |
| 84 | settings.setValue("fullScreen", fullScreen); |
| 85 | settings.endGroup(); |
| 86 | } |
| 87 | |
| 88 | void Settings::loadResolution(int & hRes, int & vRes, bool & fullScreen) |
| 89 | { |
| 90 | QSettings settings; |
| 91 | |
| 92 | settings.beginGroup(SETTINGS_GROUP_CONFIG); |
| 93 | fullScreen = settings.value("fullScreen", true).toBool(); |
| 94 | hRes = settings.value("hRes", 0).toInt(); |
| 95 | vRes = settings.value("vRes", 0).toInt(); |
| 96 | settings.endGroup(); |
| 97 | } |
| 98 | |
| 99 | void Settings::saveVSync(int value) |
| 100 | { |
| 101 | saveValue(Settings::vsyncKey(), value); |
| 102 | } |
| 103 | |
| 104 | int Settings::loadVSync() |
| 105 | { |
| 106 | return loadValue(Settings::vsyncKey(), 1); // On by default |
| 107 | } |
| 108 | |
| 109 | void Settings::saveValue(QString key, int value) |
| 110 | { |
| 111 | QSettings settings; |
| 112 | |
| 113 | settings.beginGroup(SETTINGS_GROUP_CONFIG); |
| 114 | settings.setValue(key, value); |
| 115 | settings.endGroup(); |
| 116 | } |
| 117 | |
| 118 | int Settings::loadValue(QString key, int defaultValue) |
| 119 | { |
| 120 | QSettings settings; |
| 121 | |
| 122 | int value = 0; |
| 123 | settings.beginGroup(SETTINGS_GROUP_CONFIG); |
| 124 | value = settings.value(key, defaultValue).toInt(); |
| 125 | settings.endGroup(); |
| 126 | |
| 127 | return value; |
| 128 | } |
| 129 | |
| 130 | QString Settings::combineActionAndPlayer(int player, InputHandler::Action action) |
| 131 | { |
| 132 | return QString("%2_%1").arg(player).arg(m_actionToStringMap[action]); |
| 133 | } |
| 134 | |
| 135 | void Settings::saveKeyMapping(int player, InputHandler::Action action, int key) |
| 136 | { |
| 137 | QSettings settings; |
| 138 | |
| 139 | settings.beginGroup(SETTINGS_GROUP_CONFIG); |
| 140 | settings.setValue(combineActionAndPlayer(player, action), key); |
| 141 | settings.endGroup(); |
| 142 | } |
| 143 | |
| 144 | int Settings::loadKeyMapping(int player, InputHandler::Action action) |
| 145 | { |
| 146 | QSettings settings; |
| 147 | |
| 148 | int key = 0; |
| 149 | settings.beginGroup(SETTINGS_GROUP_CONFIG); |
| 150 | key = settings.value(combineActionAndPlayer(player, action), 0).toInt(); |
| 151 | settings.endGroup(); |
| 152 | |
| 153 | return key; |
| 154 | } |
| 155 | |
| 156 | void Settings::saveDifficulty(DifficultyProfile::Difficulty difficulty) |
| 157 | { |
| 158 | QSettings settings; |
| 159 | |
| 160 | settings.beginGroup(SETTINGS_GROUP_CONFIG); |
| 161 | settings.setValue(difficultyKey(), static_cast<int>(difficulty)); |
| 162 | settings.endGroup(); |
| 163 | } |
| 164 | |
| 165 | DifficultyProfile::Difficulty Settings::loadDifficulty() const |
| 166 | { |
| 167 | QSettings settings; |
| 168 | |
| 169 | DifficultyProfile::Difficulty difficulty = DifficultyProfile::Difficulty::Medium; |
| 170 | settings.beginGroup(SETTINGS_GROUP_CONFIG); |
| 171 | difficulty = static_cast<DifficultyProfile::Difficulty>(settings.value(difficultyKey(), 0).toInt()); |
| 172 | settings.endGroup(); |
| 173 | |
| 174 | return difficulty; |
| 175 | } |