move game settings into their own file
This commit is contained in:
parent
91a4a2079b
commit
7fa5294e02
134
src/main.cpp
134
src/main.cpp
@ -22,6 +22,9 @@
|
||||
|
||||
using namespace glm;
|
||||
|
||||
#define ORG ("ammerhai")
|
||||
#define APP ("mikemon")
|
||||
|
||||
#define ASSETS_PATH "./assets/"
|
||||
|
||||
#define NEAR_PLANE (0.01f)
|
||||
@ -109,7 +112,6 @@ static bool show_demo_window;
|
||||
static bool show_tile_picker;
|
||||
static bool show_settings;
|
||||
|
||||
|
||||
#define log_error(...) SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
||||
|
||||
float remap(float in_a, float in_b, float out_a, float out_b, float v) {
|
||||
@ -2413,6 +2415,89 @@ static void render(WGPUTexture surface_texture) {
|
||||
wgpuTextureViewRelease(surface_texture_view);
|
||||
}
|
||||
|
||||
void save_settings() {
|
||||
SDL_Storage *user_storage = SDL_OpenUserStorage(ORG, APP, 0);
|
||||
if (!user_storage) return;
|
||||
defer(SDL_CloseStorage(user_storage));
|
||||
|
||||
SDL_IOStream *io = SDL_IOFromDynamicMem();
|
||||
SDL_IOprintf(io, "[Audio]\n");
|
||||
SDL_IOprintf(io, "Master=%.0f\n", volume_master);
|
||||
SDL_IOprintf(io, "Music=%.0f\n", volume_music);
|
||||
SDL_IOprintf(io, "SFX=%.0f\n", volume_sfx);
|
||||
|
||||
SDL_PropertiesID properties = SDL_GetIOProperties(io);
|
||||
void *data = SDL_GetPointerProperty(properties, SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, NULL);
|
||||
|
||||
Sint64 size = SDL_GetIOSize(io);
|
||||
SDL_WriteStorageFile(user_storage, "settings.ini", data, size);
|
||||
|
||||
SDL_CloseIO(io);
|
||||
}
|
||||
|
||||
void load_settings() {
|
||||
SDL_Storage *user_storage = SDL_OpenUserStorage(ORG, APP, 0);
|
||||
if (!user_storage) return;
|
||||
defer(SDL_CloseStorage(user_storage));
|
||||
|
||||
Uint64 file_size = 0;
|
||||
if (SDL_GetStorageFileSize(user_storage, "settings.ini", &file_size)) {
|
||||
char *file_data = (char *)SDL_calloc(1, file_size + 1);
|
||||
defer(SDL_free(file_data));
|
||||
|
||||
if (!SDL_ReadStorageFile(user_storage, "settings.ini", file_data, file_size))
|
||||
return;
|
||||
|
||||
Settings_Category current_category = SETTINGS_UNKNOWN;
|
||||
|
||||
char *line_end = NULL;
|
||||
for (char *line = file_data; line < file_data + file_size; line = line_end + 1) {
|
||||
while (line[0] == '\n' || line[0] == '\r') line++;
|
||||
line_end = line;
|
||||
while(line_end < file_data + file_size && line_end[0] != '\n' && line_end[0] != '\r') line_end++;
|
||||
line_end[0] = 0;
|
||||
|
||||
if (line[0] == '[' && line_end > line && line_end[-1] == ']') {
|
||||
line_end[-1] = 0;
|
||||
|
||||
const char *category_name = line + 1;
|
||||
if (SDL_strcasecmp(category_name, "AUDIO") == 0) {
|
||||
current_category = SETTINGS_AUDIO;
|
||||
} else {
|
||||
SDL_Log("Unknown category '%s' in settings.ini.", category_name);
|
||||
}
|
||||
} else {
|
||||
switch (current_category) {
|
||||
case SETTINGS_UNKNOWN: {
|
||||
SDL_Log("Unknown value '%s' outside of any category in settings.ini.", line);
|
||||
} break;
|
||||
|
||||
case SETTINGS_AUDIO: {
|
||||
if (SDL_strncasecmp(line, "Master", sizeof("Master") - 1) == 0) {
|
||||
line = line + sizeof("Master") - 1;
|
||||
SDL_sscanf(line, "=%f", &volume_master);
|
||||
} else if (SDL_strncasecmp(line, "Music", sizeof("Music") - 1) == 0) {
|
||||
line = line + sizeof("Music") - 1;
|
||||
SDL_sscanf(line, "=%f", &volume_music);
|
||||
} else if (SDL_strncasecmp(line, "SFX", sizeof("SFX") - 1) == 0) {
|
||||
line = line + sizeof("SFX") - 1;
|
||||
SDL_sscanf(line, "=%f", &volume_sfx);
|
||||
} else {
|
||||
SDL_Log("Unknown value '%s' in category 'AUDIO' in settings.ini.", line);
|
||||
}
|
||||
} break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MIX_SetMixerGain(mixer, volume_master / 100.0f);
|
||||
MIX_SetTrackGain(music_track, volume_music / 100.0f);
|
||||
MIX_SetTrackGain(sfx_track, volume_sfx / 100.0f);
|
||||
} else {
|
||||
SDL_Log("No settings found. Using default values.");
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
setup_memory_functions();
|
||||
setup_working_directory();
|
||||
@ -2467,6 +2552,9 @@ int main(int argc, char **argv) {
|
||||
if (!music_setting_off_piano) {
|
||||
log_error("Failed to load music setting_off_piano.opus. Ignoring.");
|
||||
}
|
||||
|
||||
load_settings();
|
||||
save_settings();
|
||||
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGuiContext *imgui_context = ImGui::CreateContext();
|
||||
@ -2475,6 +2563,18 @@ int main(int argc, char **argv) {
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
|
||||
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
|
||||
|
||||
char *storage_path = SDL_GetPrefPath(ORG, APP);
|
||||
|
||||
size_t imgui_ini_path_len = SDL_strlen(storage_path) + SDL_strlen("imgui.ini") + 1;
|
||||
char *imgui_ini_path = (char *)SDL_calloc(imgui_ini_path_len, 1);
|
||||
|
||||
SDL_strlcat(imgui_ini_path, storage_path, imgui_ini_path_len);
|
||||
SDL_strlcat(imgui_ini_path, "imgui.ini", imgui_ini_path_len);
|
||||
|
||||
SDL_free(storage_path);
|
||||
|
||||
io.IniFilename = imgui_ini_path;
|
||||
|
||||
io.Fonts->AddFontDefaultVector();
|
||||
|
||||
ImGui::StyleColorsDark();
|
||||
@ -2530,36 +2630,6 @@ int main(int argc, char **argv) {
|
||||
};
|
||||
ImGui::AddSettingsHandler(&time_tints_settings_handler);
|
||||
|
||||
ImGuiSettingsHandler settings_handler = {};
|
||||
settings_handler.TypeName = "Settings";
|
||||
settings_handler.TypeHash = ImHashStr("Settings");
|
||||
settings_handler.ReadOpenFn = [](ImGuiContext *context, ImGuiSettingsHandler *handler, const char *name) -> void * {
|
||||
if (strcmp(name, "Audio") == 0)
|
||||
return (void *)SETTINGS_AUDIO;
|
||||
|
||||
return (void *)SETTINGS_UNKNOWN;
|
||||
};
|
||||
settings_handler.ReadLineFn = [](ImGuiContext *context, ImGuiSettingsHandler *handler, void *entry, const char *line) {
|
||||
if (entry == (void *)SETTINGS_AUDIO) {
|
||||
if (SDL_sscanf(line, "Master=%f", &volume_master) == 1) {
|
||||
MIX_SetMixerGain(mixer, volume_master / 100.0f);
|
||||
}
|
||||
if (SDL_sscanf(line, "Music=%f", &volume_music) == 1)
|
||||
MIX_SetTrackGain(music_track, volume_music / 100.0f);
|
||||
if (SDL_sscanf(line, "SFX=%f", &volume_sfx) == 1)
|
||||
MIX_SetTrackGain(sfx_track, volume_sfx / 100.0f);
|
||||
return;
|
||||
}
|
||||
};
|
||||
settings_handler.WriteAllFn = [](ImGuiContext *context, ImGuiSettingsHandler *handler, ImGuiTextBuffer *buffer) {
|
||||
buffer->append("[Settings][Audio]\n");
|
||||
buffer->appendf("Master=%.0f\n", volume_master);
|
||||
buffer->appendf("Music=%.0f\n", volume_music);
|
||||
buffer->appendf("SFX=%.0f\n", volume_sfx);
|
||||
buffer->append("\n");
|
||||
};
|
||||
ImGui::AddSettingsHandler(&settings_handler);
|
||||
|
||||
SDL_GetWindowSizeInPixels(window, &window_size.x, &window_size.y);
|
||||
|
||||
while (Running) {
|
||||
@ -2600,6 +2670,8 @@ int main(int argc, char **argv) {
|
||||
FrameMark;
|
||||
}
|
||||
|
||||
save_settings();
|
||||
|
||||
ImGui_ImplWGPU_Shutdown();
|
||||
ImGui_ImplSDL3_Shutdown();
|
||||
ImGui::DestroyContext();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user