mirror of
https://github.com/nunuhara/xsystem4.git
synced 2026-10-01 11:28:01 +03:00
SystemService: Persist "wait vsync" setting to disk
This setting has a significant impact on system load, so users may want xsystem4 to remember their preference. This implementation uses a JSON format, while the AliceSoft's implementation saves it to a binary file (WindowSetting.sav). This only records the wait_vsync setting, since the other fields in window_settings are currently no-op.
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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)
|
||||
|
||||
+5
-2
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user