Implement FullScreen and FPS opts
This commit is contained in:
65
game/main.c
65
game/main.c
@@ -18,7 +18,7 @@
|
||||
#include "sounds.h"
|
||||
|
||||
// Constants
|
||||
const char *SETTINGS_PATH = "settings.ini";
|
||||
const char *GAME_DATA_SAVE_PATH = "game_data.ini";
|
||||
// Constants end
|
||||
|
||||
ECS_COMPONENT_DECLARE(Game);
|
||||
@@ -99,7 +99,7 @@ int cmpDrawData(const void *a, const void *b) {
|
||||
}
|
||||
|
||||
|
||||
bool serializeOptions(const char *path, const Options *opts) {
|
||||
bool serializeGameData(const char *path, const GameData *gameData) {
|
||||
|
||||
#ifdef EMSCRIPTEN
|
||||
char buf[4096];
|
||||
@@ -109,7 +109,7 @@ bool serializeOptions(const char *path, const Options *opts) {
|
||||
bzLogInfo("write path: %s", path);
|
||||
FILE *f = fopen(path, "w");
|
||||
if (!f) return false;
|
||||
size_t numWritten = fwrite(opts, sizeof(*opts), 1, f);
|
||||
size_t numWritten = fwrite(gameData, sizeof(*gameData), 1, f);
|
||||
fclose(f);
|
||||
#ifdef EMSCRIPTEN
|
||||
// Sync FS
|
||||
@@ -122,7 +122,7 @@ bool serializeOptions(const char *path, const Options *opts) {
|
||||
return numWritten == 1;
|
||||
}
|
||||
|
||||
bool deserializeOptions(const char *path, Options *optsOut) {
|
||||
bool deserializeGameData(const char *path, GameData *gameData) {
|
||||
#ifdef EMSCRIPTEN
|
||||
char buf[4096];
|
||||
snprintf(buf, sizeof(buf), "%s%s", "/game/", path);
|
||||
@@ -134,15 +134,29 @@ bool deserializeOptions(const char *path, Options *optsOut) {
|
||||
bzLogInfo("start reading");
|
||||
if (!f) return false;
|
||||
bzLogInfo("reading");
|
||||
Options opts;
|
||||
size_t numRead = fread(&opts, sizeof(opts), 1, f);
|
||||
GameData data;
|
||||
size_t numRead = fread(&data, sizeof(data), 1, f);
|
||||
fclose(f);
|
||||
if (numRead == 1) {
|
||||
*optsOut = opts;
|
||||
*gameData = data;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
void applyOptions(const Options *options) {
|
||||
SoundState *sounds = ecs_singleton_get_mut(ECS, SoundState);
|
||||
soundsApplyVolume(sounds, options->master, options->music, options->sound);
|
||||
|
||||
bool isFullscreen = IsWindowFullscreen();
|
||||
bool wantFullscreen = options->fullscreen;
|
||||
if (isFullscreen != wantFullscreen)
|
||||
ToggleFullscreen();
|
||||
|
||||
if (options->limitFps)
|
||||
SetTargetFPS(60);
|
||||
else
|
||||
SetTargetFPS(0);
|
||||
}
|
||||
|
||||
bool init(void *userData) {
|
||||
// Center window
|
||||
@@ -333,7 +347,7 @@ void deinit(void *userData) {
|
||||
InputState *input = ecs_singleton_get_mut(ECS, InputState);
|
||||
SoundState *sounds = ecs_singleton_get_mut(ECS, SoundState);
|
||||
|
||||
serializeOptions(SETTINGS_PATH, &game->options);
|
||||
serializeGameData(GAME_DATA_SAVE_PATH, &game->gameData);
|
||||
|
||||
unloadMap(game);
|
||||
|
||||
@@ -379,25 +393,23 @@ void update(float dt, void *userData) {
|
||||
|
||||
char titleBuf[32];
|
||||
snprintf(titleBuf, sizeof(titleBuf), "FPS: %d | %.2f ms", GetFPS(), GetFrameTime() * 1000);
|
||||
//SetWindowTitle(titleBuf);
|
||||
SetWindowTitle(titleBuf);
|
||||
|
||||
Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||
InputState *input = ecs_singleton_get_mut(ECS, InputState);
|
||||
|
||||
{
|
||||
static bool optsLoaded = false;
|
||||
if (!optsLoaded) {
|
||||
static bool gameDataLoaded = false;
|
||||
if (!gameDataLoaded) {
|
||||
// Load settings
|
||||
const char *path = SETTINGS_PATH;
|
||||
Options opts;
|
||||
if (deserializeOptions(path, &opts)) {
|
||||
game->options = opts;
|
||||
} else {
|
||||
game->options = getDefaultOptions();
|
||||
const char *path = GAME_DATA_SAVE_PATH;
|
||||
GameData gameData = getDefaultGameData();
|
||||
if (deserializeGameData(path, &gameData)) {
|
||||
bzLogWarning("Failed to read game data: %s.", path);
|
||||
}
|
||||
SoundState *sounds = ecs_singleton_get_mut(ECS, SoundState);
|
||||
soundsApplyVolume(sounds, opts.master, opts.music, opts.sound);
|
||||
optsLoaded = true;
|
||||
game->gameData = gameData;
|
||||
applyOptions(&gameData.options);
|
||||
gameDataLoaded = true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -719,9 +731,16 @@ void imguiRender(float dt, void *userData) {
|
||||
}
|
||||
if (igCollapsingHeader_TreeNodeFlags("Resources", 0)) {
|
||||
PlayerResources resources = game->playerResources[game->player];
|
||||
igText("Wood: %lld", resources.wood);
|
||||
igText("Food: %lld", resources.food);
|
||||
igText("Gold: %lld", resources.gold);
|
||||
i32 wood = resources.wood;
|
||||
i32 food = resources.food;
|
||||
i32 gold = resources.gold;
|
||||
igSliderInt("Wood: ", &wood, 0, 10000, "%d", 0);
|
||||
igSliderInt("Food: ", &food, 0, 10000, "%d", 0);
|
||||
igSliderInt("Gold: ", &gold, 0, 10000, "%d", 0);
|
||||
resources.wood = wood;
|
||||
resources.food = food;
|
||||
resources.gold = gold;
|
||||
game->playerResources[game->player] = resources;
|
||||
igText("Pop: %lld", resources.pop);
|
||||
igText("Pop Capacity: %lld", resources.pop);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user