diff --git a/include/savedata.h b/include/savedata.h index db91734..94d1045 100644 --- a/include/savedata.h +++ b/include/savedata.h @@ -23,6 +23,7 @@ void json_load_page(struct page *page, cJSON *vars, bool call_dtors); int save_json(const char *filename, cJSON *json); +cJSON *load_json(const char *filename); int save_globals(const char *keyname, const char *filename, const char *group_name, int *n); int load_globals(const char *keyname, const char *filename, const char *group_name, int *n); int delete_save_file(const char *filename); diff --git a/src/hll/SystemService.c b/src/hll/SystemService.c index c9fbca8..b3e86c9 100644 --- a/src/hll/SystemService.c +++ b/src/hll/SystemService.c @@ -20,9 +20,11 @@ #include "system4/ain.h" #include "system4/string.h" +#include "cJSON.h" #include "input.h" #include "gfx/gfx.h" #include "mixer.h" +#include "savedata.h" #include "vm.h" #include "xsystem4.h" #include "hll.h" @@ -140,6 +142,27 @@ enum window_settings_id { WINDOW_SETTINGS_CLOSE_GAME_CONFIRM = 6, }; +static void save_window_settings(void) +{ + cJSON *root = cJSON_CreateObject(); + cJSON_AddBoolToObject(root, "wait_vsync", window_settings.wait_vsync); + save_json("WindowSetting.json", root); + cJSON_Delete(root); +} + +static void load_window_settings(void) +{ + cJSON *root = load_json("WindowSetting.json"); + if (!root) + return; + cJSON *v; + if ((v = cJSON_GetObjectItem(root, "wait_vsync"))) { + window_settings.wait_vsync = cJSON_IsTrue(v); + gfx_set_wait_vsync(window_settings.wait_vsync); + } + cJSON_Delete(root); +} + static bool SystemService_SetWindowSetting(int type, int value) { switch (type) { @@ -169,6 +192,7 @@ static bool SystemService_SetWindowSetting(int type, int value) WARNING("Invalid window setting type: %d", type); return false; } + save_window_settings(); return true; } @@ -275,8 +299,14 @@ static void SystemService_Test(struct string **text) static void SystemService_PreLink(void); +static void SystemService_ModuleInit(void) +{ + load_window_settings(); +} + HLL_LIBRARY(SystemService, HLL_EXPORT(_PreLink, SystemService_PreLink), + HLL_EXPORT(_ModuleInit, SystemService_ModuleInit), HLL_EXPORT(GetMixerNumof, mixer_get_numof), HLL_EXPORT(GetMixerName, SystemService_GetMixerName), HLL_EXPORT(GetMixerVolume, mixer_get_volume), diff --git a/src/savedata.c b/src/savedata.c index ccfd746..a496e32 100644 --- a/src/savedata.c +++ b/src/savedata.c @@ -67,7 +67,18 @@ int save_json(const char *filename, cJSON *json) } free(str); return 1; +} +cJSON *load_json(const char *filename) +{ + char *path = savedir_path(filename); + char *json = file_read(path, NULL); + free(path); + if (!json) + return NULL; + cJSON *root = cJSON_Parse(json); + free(json); + return root; } static int get_group_index(const char *name) diff --git a/src/video.c b/src/video.c index 1f0f210..7983c55 100644 --- a/src/video.c +++ b/src/video.c @@ -69,6 +69,7 @@ static struct texture *view = &main_surface; static GLint max_texture_size; static SDL_Color clear_color = { 0, 0, 0, 255 }; static float frame_rate; +static bool wait_vsync = false; static GLchar *read_shader_file(const char *path) { @@ -267,7 +268,7 @@ int gfx_init(void) ERROR("glewInit failed"); #endif - SDL_GL_SetSwapInterval(0); + SDL_GL_SetSwapInterval(wait_vsync ? 1 : 0); gl_initialize(); gfx_draw_init(); gfx_set_window_logical_size(config.view_width, config.view_height); @@ -353,7 +354,9 @@ void gfx_update_screen_scale(void) void gfx_set_wait_vsync(bool wait) { - SDL_GL_SetSwapInterval(wait); + wait_vsync = wait; + if (gfx_initialized) + SDL_GL_SetSwapInterval(wait ? 1 : 0); } void gfx_set_clear_color(int r, int g, int b, int a)