config.cpp (14770B)
1/* 2 * Gearboy - Nintendo Game Boy Emulator 3 * Copyright (C) 2012 Ignacio Sanchez 4 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * any later version. 9 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see http://www.gnu.org/licenses/ 17 * 18 */ 19 20#include <SDL.h> 21#include "../../src/gearboy.h" 22#define MINI_CASE_SENSITIVE 23#include "mINI/ini.h" 24 25#define CONFIG_IMPORT 26#include "config.h" 27 28static bool check_portable(void); 29static int read_int(const char* group, const char* key, int default_value); 30static void write_int(const char* group, const char* key, int integer); 31static float read_float(const char* group, const char* key, float default_value); 32static void write_float(const char* group, const char* key, float value); 33static bool read_bool(const char* group, const char* key, bool default_value); 34static void write_bool(const char* group, const char* key, bool boolean); 35static std::string read_string(const char* group, const char* key); 36static void write_string(const char* group, const char* key, std::string value); 37 38void config_init(void) 39{ 40 if (check_portable()) 41 config_root_path = SDL_GetBasePath(); 42 else 43 config_root_path = SDL_GetPrefPath("Geardome", GEARBOY_TITLE); 44 45 strcpy(config_emu_file_path, config_root_path); 46 strcat(config_emu_file_path, "config.ini"); 47 48 strcpy(config_imgui_file_path, config_root_path); 49 strcat(config_imgui_file_path, "imgui.ini"); 50 51 config_ini_file = new mINI::INIFile(config_emu_file_path); 52} 53 54void config_destroy(void) 55{ 56 SafeDelete(config_ini_file) 57 SDL_free(config_root_path); 58} 59 60void config_read(void) 61{ 62 if (!config_ini_file->read(config_ini_data)) 63 { 64 Log("Unable to load settings from %s", config_emu_file_path); 65 return; 66 } 67 68 Log("Loading settings from %s", config_emu_file_path); 69 70 config_debug.debug = read_bool("Debug", "Debug", false); 71 config_debug.show_audio = read_bool("Debug", "Audio", false); 72 config_debug.show_disassembler = read_bool("Debug", "Disassembler", true); 73 config_debug.show_gameboy = read_bool("Debug", "GameBoy", true); 74 config_debug.show_iomap = read_bool("Debug", "IOMap", false); 75 config_debug.show_memory = read_bool("Debug", "Memory", true); 76 config_debug.show_processor = read_bool("Debug", "Processor", true); 77 config_debug.show_video = read_bool("Debug", "Video", false); 78 config_debug.font_size = read_int("Debug", "FontSize", 0); 79 80 config_emulator.ffwd_speed = read_int("Emulator", "FFWD", 1); 81 config_emulator.save_slot = read_int("Emulator", "SaveSlot", 0); 82 config_emulator.start_paused = read_bool("Emulator", "StartPaused", false); 83 config_emulator.force_dmg = read_bool("Emulator", "ForceDMG", false); 84 config_emulator.force_gba = read_bool("Emulator", "ForceGBA", false); 85 config_emulator.mbc = read_int("Emulator", "MBC", 0); 86 config_emulator.dmg_bootrom = read_bool("Emulator", "DMGBootrom", false); 87 config_emulator.dmg_bootrom_path = read_string("Emulator", "DMGBootromPath"); 88 config_emulator.gbc_bootrom = read_bool("Emulator", "GBCBootrom", false); 89 config_emulator.gbc_bootrom_path = read_string("Emulator", "GBCBootromPath"); 90 config_emulator.savefiles_dir_option = read_int("Emulator", "SaveFilesDirOption", 0); 91 config_emulator.savefiles_path = read_string("Emulator", "SaveFilesPath"); 92 config_emulator.savestates_dir_option = read_int("Emulator", "SaveStatesDirOption", 0); 93 config_emulator.savestates_path = read_string("Emulator", "SaveStatesPath"); 94 95 if (config_emulator.savefiles_path.empty()) 96 { 97 config_emulator.savefiles_path = config_root_path; 98 } 99 if (config_emulator.savestates_path.empty()) 100 { 101 config_emulator.savestates_path = config_root_path; 102 } 103 104 for (int i = 0; i < config_max_recent_roms; i++) 105 { 106 std::string item = "RecentROM" + std::to_string(i); 107 config_emulator.recent_roms[i] = read_string("Emulator", item.c_str()); 108 } 109 110 config_video.scale = read_int("Video", "Scale", 0); 111 config_video.ratio = read_int("Video", "AspectRatio", 0); 112 config_video.fps = read_bool("Video", "FPS", false); 113 config_video.bilinear = read_bool("Video", "Bilinear", false); 114 config_video.mix_frames = read_bool("Video", "MixFrames", true); 115 config_video.mix_frames_intensity = read_float("Video", "MixFramesIntensity", 0.50f); 116 config_video.matrix = read_bool("Video", "Matrix", true); 117 config_video.matrix_intensity = read_float("Video", "MatrixIntensity", 0.30f); 118 config_video.palette = read_int("Video", "Palette", 0); 119 for (int i = 0; i < config_max_custom_palettes; i++) 120 { 121 for (int c = 0; c < 4; c++) 122 { 123 char pal_label_r[32]; 124 char pal_label_g[32]; 125 char pal_label_b[32]; 126 sprintf(pal_label_r, "CustomPalette%i%iR", i, c); 127 sprintf(pal_label_g, "CustomPalette%i%iG", i, c); 128 sprintf(pal_label_b, "CustomPalette%i%iB", i, c); 129 config_video.color[i][c].red = read_int("Video", pal_label_r, config_video.color[i][c].red); 130 config_video.color[i][c].green = read_int("Video", pal_label_g, config_video.color[i][c].green); 131 config_video.color[i][c].blue = read_int("Video", pal_label_b, config_video.color[i][c].blue); 132 } 133 } 134 config_video.sync = read_bool("Video", "Sync", true); 135 config_video.color_correction = read_bool("Video", "ColorCorrection", true); 136 137 config_audio.enable = read_bool("Audio", "Enable", true); 138 config_audio.sync = read_bool("Audio", "Sync", true); 139 140 config_input.key_left = (SDL_Scancode)read_int("Input", "KeyLeft", SDL_SCANCODE_LEFT); 141 config_input.key_right = (SDL_Scancode)read_int("Input", "KeyRight", SDL_SCANCODE_RIGHT); 142 config_input.key_up = (SDL_Scancode)read_int("Input", "KeyUp", SDL_SCANCODE_UP); 143 config_input.key_down = (SDL_Scancode)read_int("Input", "KeyDown", SDL_SCANCODE_DOWN); 144 config_input.key_a = (SDL_Scancode)read_int("Input", "KeyA", SDL_SCANCODE_A); 145 config_input.key_b = (SDL_Scancode)read_int("Input", "KeyB", SDL_SCANCODE_S); 146 config_input.key_start = (SDL_Scancode)read_int("Input", "KeyStart", SDL_SCANCODE_RETURN); 147 config_input.key_select = (SDL_Scancode)read_int("Input", "KeySelect", SDL_SCANCODE_SPACE); 148 149 config_input.gamepad = read_bool("Input", "Gamepad", true); 150 config_input.gamepad_directional = read_int("Input", "GamepadDirectional", 0); 151 config_input.gamepad_invert_x_axis = read_bool("Input", "GamepadInvertX", false); 152 config_input.gamepad_invert_y_axis = read_bool("Input", "GamepadInvertY", false); 153 config_input.gamepad_a = read_int("Input", "GamepadA", SDL_CONTROLLER_BUTTON_B); 154 config_input.gamepad_b = read_int("Input", "GamepadB", SDL_CONTROLLER_BUTTON_A); 155 config_input.gamepad_start = read_int("Input", "GamepadStart", SDL_CONTROLLER_BUTTON_START); 156 config_input.gamepad_select = read_int("Input", "GamepadSelect", SDL_CONTROLLER_BUTTON_BACK); 157 config_input.gamepad_x_axis = read_int("Input", "GamepadX", SDL_CONTROLLER_AXIS_LEFTX); 158 config_input.gamepad_y_axis = read_int("Input", "GamepadY", SDL_CONTROLLER_AXIS_LEFTY); 159 160 Log("Settings loaded"); 161} 162 163void config_write(void) 164{ 165 Log("Saving settings to %s", config_emu_file_path); 166 167 write_bool("Debug", "Debug", config_debug.debug); 168 write_bool("Debug", "Audio", config_debug.show_audio); 169 write_bool("Debug", "Disassembler", config_debug.show_disassembler); 170 write_bool("Debug", "GameBoy", config_debug.show_gameboy); 171 write_bool("Debug", "IOMap", config_debug.show_iomap); 172 write_bool("Debug", "Memory", config_debug.show_memory); 173 write_bool("Debug", "Processor", config_debug.show_processor); 174 write_bool("Debug", "Video", config_debug.show_video); 175 write_int("Debug", "FontSize", config_debug.font_size); 176 177 write_int("Emulator", "FFWD", config_emulator.ffwd_speed); 178 write_int("Emulator", "SaveSlot", config_emulator.save_slot); 179 write_bool("Emulator", "StartPaused", config_emulator.start_paused); 180 write_bool("Emulator", "ForceDMG", config_emulator.force_dmg); 181 write_bool("Emulator", "ForceGBA", config_emulator.force_gba); 182 write_int("Emulator", "MBC", config_emulator.mbc); 183 write_bool("Emulator", "DMGBootrom", config_emulator.dmg_bootrom); 184 write_string("Emulator", "DMGBootromPath", config_emulator.dmg_bootrom_path); 185 write_bool("Emulator", "GBCBootrom", config_emulator.gbc_bootrom); 186 write_string("Emulator", "GBCBootromPath", config_emulator.gbc_bootrom_path); 187 write_int("Emulator", "SaveFilesDirOption", config_emulator.savefiles_dir_option); 188 write_string("Emulator", "SaveFilesPath", config_emulator.savefiles_path); 189 write_int("Emulator", "SaveStatesDirOption", config_emulator.savestates_dir_option); 190 write_string("Emulator", "SaveStatesPath", config_emulator.savestates_path); 191 192 for (int i = 0; i < config_max_recent_roms; i++) 193 { 194 std::string item = "RecentROM" + std::to_string(i); 195 write_string("Emulator", item.c_str(), config_emulator.recent_roms[i]); 196 } 197 198 write_int("Video", "Scale", config_video.scale); 199 write_int("Video", "AspectRatio", config_video.ratio); 200 write_bool("Video", "FPS", config_video.fps); 201 write_bool("Video", "Bilinear", config_video.bilinear); 202 write_bool("Video", "MixFrames", config_video.mix_frames); 203 write_float("Video", "MixFramesIntensity", config_video.mix_frames_intensity); 204 write_bool("Video", "Matrix", config_video.matrix); 205 write_float("Video", "MatrixIntensity", config_video.matrix_intensity); 206 write_int("Video", "Palette", config_video.palette); 207 for (int i = 0; i < config_max_custom_palettes; i++) 208 { 209 for (int c = 0; c < 4; c++) 210 { 211 char pal_label_r[32]; 212 char pal_label_g[32]; 213 char pal_label_b[32]; 214 sprintf(pal_label_r, "CustomPalette%i%iR", i, c); 215 sprintf(pal_label_g, "CustomPalette%i%iG", i, c); 216 sprintf(pal_label_b, "CustomPalette%i%iB", i, c); 217 write_int("Video", pal_label_r, config_video.color[i][c].red); 218 write_int("Video", pal_label_g, config_video.color[i][c].green); 219 write_int("Video", pal_label_b, config_video.color[i][c].blue); 220 } 221 } 222 write_bool("Video", "Sync", config_video.sync); 223 write_bool("Video", "ColorCorrection", config_video.color_correction); 224 225 write_bool("Audio", "Enable", config_audio.enable); 226 write_bool("Audio", "Sync", config_audio.sync); 227 228 write_int("Input", "KeyLeft", config_input.key_left); 229 write_int("Input", "KeyRight", config_input.key_right); 230 write_int("Input", "KeyUp", config_input.key_up); 231 write_int("Input", "KeyDown", config_input.key_down); 232 write_int("Input", "KeyA", config_input.key_a); 233 write_int("Input", "KeyB", config_input.key_b); 234 write_int("Input", "KeyStart", config_input.key_start); 235 write_int("Input", "KeySelect", config_input.key_select); 236 237 write_bool("Input", "Gamepad", config_input.gamepad); 238 write_int("Input", "GamepadDirectional", config_input.gamepad_directional); 239 write_bool("Input", "GamepadInvertX", config_input.gamepad_invert_x_axis); 240 write_bool("Input", "GamepadInvertY", config_input.gamepad_invert_y_axis); 241 write_int("Input", "GamepadA", config_input.gamepad_a); 242 write_int("Input", "GamepadB", config_input.gamepad_b); 243 write_int("Input", "GamepadStart", config_input.gamepad_start); 244 write_int("Input", "GamepadSelect", config_input.gamepad_select); 245 write_int("Input", "GamepadX", config_input.gamepad_x_axis); 246 write_int("Input", "GamepadY", config_input.gamepad_y_axis); 247 248 if (config_ini_file->write(config_ini_data, true)) 249 { 250 Log("Settings saved"); 251 } 252} 253 254static bool check_portable(void) 255{ 256 char* base_path; 257 char portable_file_path[260]; 258 259 base_path = SDL_GetBasePath(); 260 261 strcpy(portable_file_path, base_path); 262 strcat(portable_file_path, "portable.ini"); 263 264 FILE* file = fopen(portable_file_path, "r"); 265 266 if (IsValidPointer(file)) 267 { 268 fclose(file); 269 return true; 270 } 271 272 return false; 273} 274 275static int read_int(const char* group, const char* key, int default_value) 276{ 277 int ret = 0; 278 279 std::string value = config_ini_data[group][key]; 280 281 if(value.empty()) 282 ret = default_value; 283 else 284 ret = std::stoi(value); 285 286 Log("Load setting: [%s][%s]=%d", group, key, ret); 287 return ret; 288} 289 290static void write_int(const char* group, const char* key, int integer) 291{ 292 std::string value = std::to_string(integer); 293 config_ini_data[group][key] = value; 294 Log("Save setting: [%s][%s]=%s", group, key, value.c_str()); 295} 296 297static float read_float(const char* group, const char* key, float default_value) 298{ 299 float ret = 0.0f; 300 301 std::string value = config_ini_data[group][key]; 302 303 if(value.empty()) 304 ret = default_value; 305 else 306 ret = strtof(value.c_str(), NULL); 307 308 Log("Load setting: [%s][%s]=%.2f", group, key, ret); 309 return ret; 310} 311 312static void write_float(const char* group, const char* key, float value) 313{ 314 std::string value_str = std::to_string(value); 315 config_ini_data[group][key] = value_str; 316 Log("Save setting: [%s][%s]=%s", group, key, value_str.c_str()); 317} 318 319static bool read_bool(const char* group, const char* key, bool default_value) 320{ 321 bool ret; 322 323 std::string value = config_ini_data[group][key]; 324 325 if(value.empty()) 326 ret = default_value; 327 else 328 std::istringstream(value) >> std::boolalpha >> ret; 329 330 Log("Load setting: [%s][%s]=%s", group, key, ret ? "true" : "false"); 331 return ret; 332} 333 334static void write_bool(const char* group, const char* key, bool boolean) 335{ 336 std::stringstream converter; 337 converter << std::boolalpha << boolean; 338 std::string value; 339 value = converter.str(); 340 config_ini_data[group][key] = value; 341 Log("Save setting: [%s][%s]=%s", group, key, value.c_str()); 342} 343 344static std::string read_string(const char* group, const char* key) 345{ 346 std::string ret = config_ini_data[group][key]; 347 Log("Load setting: [%s][%s]=%s", group, key, ret.c_str()); 348 return ret; 349} 350 351static void write_string(const char* group, const char* key, std::string value) 352{ 353 config_ini_data[group][key] = value; 354 Log("Save setting: [%s][%s]=%s", group, key, value.c_str()); 355}