| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file setup.cpp |
| 5 | * |
| 6 | * Part of the OpenJazz project |
| 7 | * |
| 8 | * @par History: |
| 9 | * - 23rd of August 2005: Created main.c and menu.c |
| 10 | * - 3rd of February 2009: Renamed main.c to main.cpp and menu.c to menu.cpp |
| 11 | * - 18th July 2009: Created menusetup.cpp from parts of menu.cpp |
| 12 | * - 26th July 2009: Renamed menusetup.cpp to setupmenu.cpp |
| 13 | * - 21st July 2013: Created setup.cpp from parts of main.cpp and setupmenu.cpp |
| 14 | * |
| 15 | * @par Licence: |
| 16 | * Copyright (c) 2005-2017 AJ Thomson |
| 17 | * Copyright (c) 2015-2026 Carsten Teibes |
| 18 | * |
| 19 | * OpenJazz is distributed under the terms of |
| 20 | * the GNU General Public License, version 2.0 |
| 21 | * |
| 22 | * @par Description: |
| 23 | * Deals with the running of setup menus. |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | |
| 28 | #include "io/controls.h" |
| 29 | #include "io/file.h" |
| 30 | #include "io/gfx/video.h" |
| 31 | #include "io/sound.h" |
| 32 | #include "player/player.h" |
| 33 | #include "setup.h" |
| 34 | #include "util.h" |
| 35 | #include "io/log.h" |
| 36 | |
| 37 | #define CONFIG_FILE "openjazz.cfg" |
| 38 | |
| 39 | |
| 40 | /** |
| 41 | * Create default setup |
| 42 | */ |
| 43 | Setup::Setup () { |
| 44 | |
| 45 | // Create the player's name |
| 46 | characterName = createEditableString(CHAR_NAME); |
| 47 | |
| 48 | // Assign the player's colour |
| 49 | characterCols[0] = CHAR_FUR; |
| 50 | characterCols[1] = CHAR_BAND; |
| 51 | characterCols[2] = CHAR_GUN; |
| 52 | characterCols[3] = CHAR_WBAND; |
| 53 | |
| 54 | // defaults |
| 55 | manyBirds = false; |
| 56 | leaveUnneeded = true; |
| 57 | slowMotion = false; |
| 58 | hudStyle = hudType::Classic; |
| 59 | |
| 60 | } |
| 61 | |
| 62 | |
| 63 | /** |
| 64 | * Delete the setup data |
| 65 | */ |
| 66 | Setup::~Setup () { |
| 67 | |
| 68 | delete[] characterName; |
| 69 | |
| 70 | } |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * Load settings from config file. |
| 75 | */ |
| 76 | SetupOptions Setup::load () { |
| 77 | |
| 78 | File* file; |
| 79 | SetupOptions cfg = { false, 0, 0, false, 0, scalerType::None }; |
| 80 | #ifdef FULLSCREEN_ONLY |
| 81 | cfg.fullScreen = true; |
| 82 | #endif |
| 83 | |
| 84 | // Open config file |
| 85 | |
| 86 | try { |
| 87 | |
| 88 | file = new File(CONFIG_FILE, PATH_TYPE_CONFIG, false); |
| 89 | |
| 90 | } catch (int e) { |
| 91 | |
| 92 | LOG_INFO("Configuration file not found. Using defaults."); |
| 93 | |
| 94 | return cfg; |
| 95 | |
| 96 | } |
| 97 | |
| 98 | // Check that the config file has the correct version |
| 99 | if (file->loadChar() != 7) { |
| 100 | |
| 101 | LOG_WARN("Valid configuration file not found."); |
| 102 | delete file; |
| 103 | |
| 104 | return cfg; |
| 105 | |
| 106 | } |
| 107 | |
| 108 | // Read video settings |
| 109 | cfg.videoWidth = file->loadShort(MAX_SCREEN_WIDTH); |
| 110 | cfg.videoHeight = file->loadShort(MAX_SCREEN_HEIGHT); |
| 111 | |
| 112 | #ifdef FULLSCREEN_ONLY |
| 113 | file->loadChar(); // skip |
| 114 | #else |
| 115 | cfg.fullScreen = file->loadChar(); |
| 116 | #endif |
| 117 | |
| 118 | unsigned int scaleOpt = file->loadChar(); |
| 119 | cfg.videoScale = CLAMP(scaleOpt, MIN_SCALE, MAX_SCALE); |
| 120 | |
| 121 | scaleOpt = file->loadChar(); |
| 122 | cfg.scaleMethod = static_cast<scalerType>(CLAMP(scaleOpt, +scalerType::None, +scalerType::hqx)); |
| 123 | |
| 124 | cfg.valid = true; |
| 125 | |
| 126 | // Read controls |
| 127 | for (int i = 0; i < CONTROLS - 4; i++) |
| 128 | controls.setKey(i, file->loadInt()); |
| 129 | |
| 130 | for (int i = 0; i < CONTROLS; i++) |
| 131 | controls.setButton(i, file->loadInt()); |
| 132 | |
| 133 | for (int i = 0; i < CONTROLS; i++) { |
| 134 | |
| 135 | int a, d; |
| 136 | |
| 137 | a = file->loadInt(); |
| 138 | d = file->loadInt(); |
| 139 | controls.setAxis(i, a, d); |
| 140 | |
| 141 | } |
| 142 | |
| 143 | for (int i = 0; i < CONTROLS; i++) { |
| 144 | |
| 145 | int h, d; |
| 146 | |
| 147 | h = file->loadInt(); |
| 148 | d = file->loadInt(); |
| 149 | controls.setHat(i, h, d); |
| 150 | |
| 151 | } |
| 152 | |
| 153 | // Read the player's name |
| 154 | for (int i = 0; i < STRING_LENGTH; i++) |
| 155 | setup.characterName[i] = file->loadChar(); |
| 156 | |
| 157 | setup.characterName[STRING_LENGTH] = 0; |
| 158 | |
| 159 | // Read the player's colours |
| 160 | setup.characterCols[0] = file->loadChar(); |
| 161 | setup.characterCols[1] = file->loadChar(); |
| 162 | setup.characterCols[2] = file->loadChar(); |
| 163 | setup.characterCols[3] = file->loadChar(); |
| 164 | |
| 165 | // Read the music and sound effect volume |
| 166 | setMusicVolume(file->loadChar()); |
| 167 | setSoundVolume(file->loadChar()); |
| 168 | |
| 169 | // Read gameplay options |
| 170 | int opt = file->loadChar(); |
| 171 | setup.manyBirds = ((opt & 1) != 0); |
| 172 | setup.leaveUnneeded = ((opt & 2) != 0); |
| 173 | setup.slowMotion = ((opt & 4) != 0); |
| 174 | if((opt & 8) != 0) { |
| 175 | setup.hudStyle = hudType::FPS; |
| 176 | } else { |
| 177 | setup.hudStyle = hudType::Classic; |
| 178 | } |
| 179 | |
| 180 | delete file; |
| 181 | |
| 182 | return cfg; |
| 183 | |
| 184 | } |
| 185 | |
| 186 | |
| 187 | /** |
| 188 | * Save settings to config file. |
| 189 | */ |
| 190 | void Setup::save () { |
| 191 | |
| 192 | File *file; |
| 193 | int count; |
| 194 | |
| 195 | // Open config file |
| 196 | try { |
| 197 | |
| 198 | file = new File(CONFIG_FILE, PATH_TYPE_CONFIG, true); |
| 199 | |
| 200 | } catch (int e) { |
| 201 | |
| 202 | file = NULL; |
| 203 | |
| 204 | } |
| 205 | |
| 206 | // Check that the config file was opened |
| 207 | if (!file) { |
| 208 | |
| 209 | LOG_ERROR("Could not write configuration file: File could not be opened."); |
| 210 | |
| 211 | return; |
| 212 | |
| 213 | } |
| 214 | |
| 215 | |
| 216 | // Write the version number |
| 217 | file->storeChar(7); |
| 218 | |
| 219 | // Write video settings |
| 220 | file->storeShort(video.getWidth()); |
| 221 | file->storeShort(video.getHeight()); |
| 222 | |
| 223 | #ifdef FULLSCREEN_ONLY |
| 224 | file->storeChar(1); |
| 225 | #else |
| 226 | file->storeChar(video.isFullscreen()? 1: 0); |
| 227 | #endif |
| 228 | |
| 229 | file->storeChar(video.getScaleFactor()); |
| 230 | file->storeChar(+video.getScaleMethod()); |
| 231 | |
| 232 | // Write controls |
| 233 | for (count = 0; count < CONTROLS - 4; count++) |
| 234 | file->storeInt(controls.getKey(count)); |
| 235 | |
| 236 | for (count = 0; count < CONTROLS; count++) |
| 237 | file->storeInt(controls.getButton(count)); |
| 238 | |
| 239 | for (count = 0; count < CONTROLS; count++) { |
| 240 | |
| 241 | file->storeInt(controls.getAxis(count)); |
| 242 | file->storeInt(controls.getAxisDirection(count)); |
| 243 | |
| 244 | } |
| 245 | |
| 246 | for (count = 0; count < CONTROLS; count++) { |
| 247 | |
| 248 | file->storeInt(controls.getHat(count)); |
| 249 | file->storeInt(controls.getHatDirection(count)); |
| 250 | |
| 251 | } |
| 252 | |
| 253 | // Write the player's name |
| 254 | for (count = 0; count < STRING_LENGTH; count++) |
| 255 | file->storeChar(setup.characterName[count]); |
| 256 | |
| 257 | // Write the player's colour |
| 258 | file->storeChar(setup.characterCols[0]); |
| 259 | file->storeChar(setup.characterCols[1]); |
| 260 | file->storeChar(setup.characterCols[2]); |
| 261 | file->storeChar(setup.characterCols[3]); |
| 262 | |
| 263 | // Write the music and sound effect volume |
| 264 | file->storeChar(getMusicVolume()); |
| 265 | file->storeChar(getSoundVolume()); |
| 266 | |
| 267 | // Write gameplay options |
| 268 | |
| 269 | count = 0; |
| 270 | |
| 271 | if (setup.manyBirds) count |= 1; |
| 272 | if (setup.leaveUnneeded) count |= 2; |
| 273 | if (setup.slowMotion) count |= 4; |
| 274 | if (setup.hudStyle == hudType::FPS) count |= 8; |
| 275 | |
| 276 | file->storeChar(count); |
| 277 | |
| 278 | delete file; |
| 279 | |
| 280 | } |