From 8e348a8cd194e144672df628a56d01cf9ec0f756 Mon Sep 17 00:00:00 2001 From: ManDude <7569514+ManDude@users.noreply.github.com> Date: Mon, 16 Aug 2021 15:43:12 +0100 Subject: [PATCH] save & load to file! --- .gitignore | 4 +++ game/common/file_paths.h | 12 ++++++++ game/graphics/display.cpp | 2 +- game/graphics/gfx.cpp | 65 +++++++++++++++++++++++++++++++-------- game/graphics/gfx.h | 15 +++++---- 5 files changed, 78 insertions(+), 20 deletions(-) create mode 100644 game/common/file_paths.h diff --git a/.gitignore b/.gitignore index cd4b3d4758..788ef422e1 100644 --- a/.gitignore +++ b/.gitignore @@ -16,9 +16,13 @@ logs/* .#*# *.pyc +# output from our tools *.bin *.log *.p2s savestate-out/ failures/ ee-results.json + +# game stuff +game_config/* diff --git a/game/common/file_paths.h b/game/common/file_paths.h new file mode 100644 index 0000000000..a4bdb56571 --- /dev/null +++ b/game/common/file_paths.h @@ -0,0 +1,12 @@ +#pragma once + +/*! + * @file file_paths.h + * File names and directories for user config files. + */ + +#include + +static const std::string GAME_CONFIG_DIR_NAME = "game_config"; + +static const std::string SETTINGS_GFX_FILE_NAME = "SETTINGS_GFX.CFG"; diff --git a/game/graphics/display.cpp b/game/graphics/display.cpp index 1d0b50274d..d5a3363a04 100644 --- a/game/graphics/display.cpp +++ b/game/graphics/display.cpp @@ -106,7 +106,7 @@ int InitMainDisplay(int width, int height, const char* title, GfxSettings& setti return 1; } - auto display = settings.renderer->make_main_display(width, height, title, settings); + auto display = Gfx::GetRenderer(settings.renderer)->make_main_display(width, height, title, settings); if (display == NULL) { lg::error("Failed to make main display."); return 1; diff --git a/game/graphics/gfx.cpp b/game/graphics/gfx.cpp index d2d8ea9f24..2a3333ba01 100644 --- a/game/graphics/gfx.cpp +++ b/game/graphics/gfx.cpp @@ -3,15 +3,19 @@ * Graphics component for the runtime. Abstraction layer for the main graphics routines. */ +#include #include +#include #include "gfx.h" #include "display.h" #include "pipelines/opengl.h" -#include "game/kernel/kscheme.h" #include "common/symbols.h" #include "common/log/log.h" +#include "common/util/FileUtil.h" +#include "game/common/file_paths.h" +#include "game/kernel/kscheme.h" #include "game/runtime.h" #include "game/system/newpad.h" @@ -24,7 +28,7 @@ void InitSettings(GfxSettings& settings) { settings.version = GfxSettings::CURRENT_VERSION; // use opengl by default for now - settings.renderer = Gfx::GetRenderer(GfxPipeline::OpenGL); // Gfx::renderers[0]; + settings.renderer = GfxPipeline::OpenGL; // Gfx::renderers[0]; // 1 screen update per frame settings.vsync = 1; @@ -47,6 +51,33 @@ namespace Gfx { GfxSettings g_settings; // const std::vector renderers = {&moduleOpenGL}; +void LoadSettings() { + const auto filename = file_util::get_file_path({GAME_CONFIG_DIR_NAME, SETTINGS_GFX_FILE_NAME}); + if (std::filesystem::exists(filename)) { + FILE* fp = fopen(filename.c_str(), "rb"); + u64 version; + fread(&version, sizeof(u64), 1, fp); + if (version == GfxSettings::CURRENT_VERSION) { + fseek(fp, 0, SEEK_SET); + fread(&g_settings, sizeof(GfxSettings), 1, fp); + lg::info("Loaded gfx settings."); + } else { + // TODO upgrade func + lg::info("Detected gfx settings from old version. Ignoring."); + } + fclose(fp); + } +} + +void SaveSettings() { + const auto filename = file_util::get_file_path({GAME_CONFIG_DIR_NAME, SETTINGS_GFX_FILE_NAME}); + file_util::create_dir_if_needed(file_util::get_file_path({GAME_CONFIG_DIR_NAME})); + FILE* fp = fopen(filename.c_str(), "wb"); + fwrite(&g_settings, sizeof(GfxSettings), 1, fp); + fclose(fp); + lg::info("Saved gfx settings."); +} + const GfxRendererModule* GetRenderer(GfxPipeline pipeline) { switch (pipeline) { case GfxPipeline::Invalid: @@ -61,6 +92,10 @@ const GfxRendererModule* GetRenderer(GfxPipeline pipeline) { } } +const GfxRendererModule* GetCurrentRenderer() { + return GetRenderer(g_settings.renderer); +} + u32 Init() { lg::info("GFX Init"); // initialize settings @@ -68,7 +103,9 @@ u32 Init() { // guarantee we have no keys detected by pad Pad::ForceClearKeys(); - if (g_settings.renderer->init(g_settings)) { + LoadSettings(); + + if (GetCurrentRenderer()->init(g_settings)) { lg::error("Gfx::Init error"); return 1; } @@ -99,38 +136,38 @@ void Loop(std::function f) { u32 Exit() { lg::info("GFX Exit"); Display::KillMainDisplay(); - g_settings.renderer->exit(); + GetCurrentRenderer()->exit(); return 0; } u32 vsync() { - return g_settings.renderer->vsync(); + return GetCurrentRenderer()->vsync(); } u32 sync_path() { - return g_settings.renderer->sync_path(); + return GetCurrentRenderer()->sync_path(); } void send_chain(const void* data, u32 offset) { - if (g_settings.renderer) { - g_settings.renderer->send_chain(data, offset); + if (GetCurrentRenderer()) { + GetCurrentRenderer()->send_chain(data, offset); } } void texture_upload_now(const u8* tpage, int mode, u32 s7_ptr) { - if (g_settings.renderer) { - g_settings.renderer->texture_upload_now(tpage, mode, s7_ptr); + if (GetCurrentRenderer()) { + GetCurrentRenderer()->texture_upload_now(tpage, mode, s7_ptr); } } void texture_relocate(u32 destination, u32 source, u32 format) { - if (g_settings.renderer) { - g_settings.renderer->texture_relocate(destination, source, format); + if (GetCurrentRenderer()) { + GetCurrentRenderer()->texture_relocate(destination, source, format); } } void poll_events() { - g_settings.renderer->poll_events(); + GetCurrentRenderer()->poll_events(); } void input_mode_set(u32 enable) { @@ -147,6 +184,8 @@ void input_mode_save() { } else if (Pad::input_mode_get() == (u64)Pad::InputModeStatus::Disabled) { g_settings.pad_mapping_info_backup = g_settings.pad_mapping_info; // copy to backup g_settings.pad_mapping_info = Pad::g_input_mode_mapping; // set current mapping + + SaveSettings(); } } diff --git a/game/graphics/gfx.h b/game/graphics/gfx.h index 207ceda8c7..dddff5445c 100644 --- a/game/graphics/gfx.h +++ b/game/graphics/gfx.h @@ -42,16 +42,19 @@ struct GfxRendererModule { struct GfxSettings { // current version of the settings. this should be set up so that newer versions are always higher // than older versions - static constexpr u64 CURRENT_VERSION = 0x0000'0000'0003'0001; + // increment this whenever you change this struct. + // there's probably a smarter way to do this (automatically deduce size etc.) + static constexpr u64 CURRENT_VERSION = 0x0000'0000'0004'0001; - u64 version; // the version of this settings struct - int vsync; // (temp) number of screen update per frame - bool debug; // graphics debugging - - const GfxRendererModule* renderer; // which rendering pipeline to use. + u64 version; // the version of this settings struct. MUST ALWAYS BE THE FIRST THING! Pad::MappingInfo pad_mapping_info; // button mapping Pad::MappingInfo pad_mapping_info_backup; // button mapping backup (see newpad.h) + + int vsync; // (temp) number of screen update per frame + bool debug; // graphics debugging + + GfxPipeline renderer; // which rendering pipeline to use. }; namespace Gfx {