Add options
This commit is contained in:
@@ -19,8 +19,29 @@ typedef struct DrawData {
|
|||||||
f32 rotation;
|
f32 rotation;
|
||||||
} DrawData;
|
} DrawData;
|
||||||
|
|
||||||
|
typedef struct Options {
|
||||||
|
// Video
|
||||||
|
bool fullscreen;
|
||||||
|
bool vsync;
|
||||||
|
// Audio
|
||||||
|
f32 master;
|
||||||
|
f32 music;
|
||||||
|
f32 sound;
|
||||||
|
} Options;
|
||||||
|
|
||||||
|
static Options getDefaultOptions() {
|
||||||
|
return (Options) {
|
||||||
|
.fullscreen = false,
|
||||||
|
.vsync = false,
|
||||||
|
.master = 5.0f,
|
||||||
|
.music = 5.0f,
|
||||||
|
.sound = 5.0f,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
typedef struct Game {
|
typedef struct Game {
|
||||||
GameScreen screen;
|
GameScreen screen;
|
||||||
|
GameScreen screenPrevFrame;
|
||||||
Camera2D camera;
|
Camera2D camera;
|
||||||
BzTileset tileset;
|
BzTileset tileset;
|
||||||
BzTileMap map;
|
BzTileMap map;
|
||||||
@@ -29,6 +50,8 @@ typedef struct Game {
|
|||||||
|
|
||||||
Font font;
|
Font font;
|
||||||
|
|
||||||
|
Options options;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
i64 wood;
|
i64 wood;
|
||||||
i64 iron;
|
i64 iron;
|
||||||
|
|||||||
60
game/main.c
60
game/main.c
@@ -144,6 +144,24 @@ int cmpDrawData(const void *a, const void *b) {
|
|||||||
return cmpVal;
|
return cmpVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool serializeOptions(const char *path, const Options *opts) {
|
||||||
|
FILE *f = fopen(path, "w");
|
||||||
|
size_t numWritten = fwrite(opts, sizeof(*opts), 1, f);
|
||||||
|
fclose(f);
|
||||||
|
return numWritten == 1;
|
||||||
|
}
|
||||||
|
bool deserializeOptions(const char *path, Options *optsOut) {
|
||||||
|
FILE *f = fopen(path, "r");
|
||||||
|
Options opts;
|
||||||
|
size_t numRead = fread(&opts, sizeof(opts), 1, f);
|
||||||
|
fclose(f);
|
||||||
|
if (numRead == 1) {
|
||||||
|
*optsOut = opts;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
bool init(void *userData) {
|
bool init(void *userData) {
|
||||||
// Center window
|
// Center window
|
||||||
int monitor = GetCurrentMonitor();
|
int monitor = GetCurrentMonitor();
|
||||||
@@ -303,6 +321,18 @@ bool init(void *userData) {
|
|||||||
game->debug.drawPath = true;
|
game->debug.drawPath = true;
|
||||||
game->debug.inspecting = bzArrayCreate(ecs_entity_t, 10);
|
game->debug.inspecting = bzArrayCreate(ecs_entity_t, 10);
|
||||||
|
|
||||||
|
|
||||||
|
// Load settings
|
||||||
|
const char *workDir = GetApplicationDirectory();
|
||||||
|
char buf[FILENAME_MAX];
|
||||||
|
snprintf(buf, sizeof(buf), "%s%s", workDir, "settings");
|
||||||
|
Options opts;
|
||||||
|
if (deserializeOptions(buf, &opts)) {
|
||||||
|
game->options = opts;
|
||||||
|
} else {
|
||||||
|
game->options = getDefaultOptions();
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
void deinit(void *userData) {
|
void deinit(void *userData) {
|
||||||
@@ -311,6 +341,11 @@ void deinit(void *userData) {
|
|||||||
InputState *input = ecs_singleton_get_mut(ECS, InputState);
|
InputState *input = ecs_singleton_get_mut(ECS, InputState);
|
||||||
SoundState *sounds = ecs_singleton_get_mut(ECS, SoundState);
|
SoundState *sounds = ecs_singleton_get_mut(ECS, SoundState);
|
||||||
|
|
||||||
|
const char *workDir = GetApplicationDirectory();
|
||||||
|
char buf[FILENAME_MAX];
|
||||||
|
snprintf(buf, sizeof(buf), "%s%s", workDir, "settings");
|
||||||
|
serializeOptions(buf, &game->options);
|
||||||
|
|
||||||
unloadMap(game);
|
unloadMap(game);
|
||||||
|
|
||||||
// Destroy queries
|
// Destroy queries
|
||||||
@@ -588,6 +623,7 @@ static void renderMainMenu(Game *game, float dt) {
|
|||||||
loadMap(game, "assets/maps/pathing_test.tmj");
|
loadMap(game, "assets/maps/pathing_test.tmj");
|
||||||
}
|
}
|
||||||
if (uiMainMenuButton("Settings")) {
|
if (uiMainMenuButton("Settings")) {
|
||||||
|
game->screenPrevFrame = game->screen;
|
||||||
game->screen = SCREEN_SETTINGS;
|
game->screen = SCREEN_SETTINGS;
|
||||||
}
|
}
|
||||||
if (uiMainMenuButton("Exit")) {
|
if (uiMainMenuButton("Exit")) {
|
||||||
@@ -616,19 +652,17 @@ static void renderSettings(Game *game, float dt) {
|
|||||||
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
.type = BZ_UI_LAYOUT_FLEX_BOX,
|
||||||
.flags = BZ_UI_FLEX_DIR_COLUMN | BZ_UI_FLEX_JUSTIFY_CENTER | BZ_UI_FLEX_ALIGN_CENTER
|
.flags = BZ_UI_FLEX_DIR_COLUMN | BZ_UI_FLEX_JUSTIFY_CENTER | BZ_UI_FLEX_ALIGN_CENTER
|
||||||
});
|
});
|
||||||
static bool fullscreen = false;
|
static Options opts;
|
||||||
static bool vsync = false;
|
if (game->screenPrevFrame != SCREEN_SETTINGS)
|
||||||
|
opts = game->options;
|
||||||
uiSettingsLabel("Video");
|
uiSettingsLabel("Video");
|
||||||
uiSettingsCheckbox("Fullscreen", &fullscreen);
|
uiSettingsCheckbox("Fullscreen", &opts.fullscreen);
|
||||||
uiSettingsCheckbox("V-Sync", &vsync);
|
uiSettingsCheckbox("V-Sync", &opts.vsync);
|
||||||
|
|
||||||
static f32 master = 50.0f;
|
|
||||||
static f32 music = 50.0f;
|
|
||||||
static f32 sound = 50.0f;
|
|
||||||
uiSettingsLabel("Audio");
|
uiSettingsLabel("Audio");
|
||||||
uiSettingsSlider("Master: ", &master);
|
uiSettingsSlider("Master: ", &opts.master);
|
||||||
uiSettingsSlider("Music: ", &music);
|
uiSettingsSlider("Music: ", &opts.music);
|
||||||
uiSettingsSlider("Sound: ", &sound);
|
uiSettingsSlider("Sound: ", &opts.sound);
|
||||||
|
|
||||||
bzUIPopParent(UI);
|
bzUIPopParent(UI);
|
||||||
bzUIPushDiv(UI, (BzUISize) {BZ_UI_SIZE_REL_PARENT, 0.8f},
|
bzUIPushDiv(UI, (BzUISize) {BZ_UI_SIZE_REL_PARENT, 0.8f},
|
||||||
@@ -642,9 +676,10 @@ static void renderSettings(Game *game, float dt) {
|
|||||||
game->screen = SCREEN_MAIN_MENU;
|
game->screen = SCREEN_MAIN_MENU;
|
||||||
}
|
}
|
||||||
if (uiSettingsButton("Reset")) {
|
if (uiSettingsButton("Reset")) {
|
||||||
|
opts = game->options;
|
||||||
}
|
}
|
||||||
if (uiSettingsButton("Apply")) {
|
if (uiSettingsButton("Apply")) {
|
||||||
|
game->options = opts;
|
||||||
game->screen = SCREEN_MAIN_MENU;
|
game->screen = SCREEN_MAIN_MENU;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -678,10 +713,9 @@ void render(float dt, void *userData) {
|
|||||||
renderGame(game, dt);
|
renderGame(game, dt);
|
||||||
drawOverScreen(shadow);
|
drawOverScreen(shadow);
|
||||||
renderSettings(game, dt);
|
renderSettings(game, dt);
|
||||||
|
game->screenPrevFrame = SCREEN_SETTINGS;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void igInspectComp(const char *label, ecs_entity_t entity, ecs_entity_t comp, ImGuiCompFn fn) {
|
void igInspectComp(const char *label, ecs_entity_t entity, ecs_entity_t comp, ImGuiCompFn fn) {
|
||||||
|
|||||||
Reference in New Issue
Block a user