save & load to file!

This commit is contained in:
ManDude 2021-08-16 15:43:12 +01:00
parent 093526721c
commit 8e348a8cd1
5 changed files with 78 additions and 20 deletions

4
.gitignore vendored
View file

@ -16,9 +16,13 @@ logs/*
.#*# .#*#
*.pyc *.pyc
# output from our tools
*.bin *.bin
*.log *.log
*.p2s *.p2s
savestate-out/ savestate-out/
failures/ failures/
ee-results.json ee-results.json
# game stuff
game_config/*

12
game/common/file_paths.h Normal file
View file

@ -0,0 +1,12 @@
#pragma once
/*!
* @file file_paths.h
* File names and directories for user config files.
*/
#include <string>
static const std::string GAME_CONFIG_DIR_NAME = "game_config";
static const std::string SETTINGS_GFX_FILE_NAME = "SETTINGS_GFX.CFG";

View file

@ -106,7 +106,7 @@ int InitMainDisplay(int width, int height, const char* title, GfxSettings& setti
return 1; 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) { if (display == NULL) {
lg::error("Failed to make main display."); lg::error("Failed to make main display.");
return 1; return 1;

View file

@ -3,15 +3,19 @@
* Graphics component for the runtime. Abstraction layer for the main graphics routines. * Graphics component for the runtime. Abstraction layer for the main graphics routines.
*/ */
#include <cstdio>
#include <functional> #include <functional>
#include <filesystem>
#include "gfx.h" #include "gfx.h"
#include "display.h" #include "display.h"
#include "pipelines/opengl.h" #include "pipelines/opengl.h"
#include "game/kernel/kscheme.h"
#include "common/symbols.h" #include "common/symbols.h"
#include "common/log/log.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/runtime.h"
#include "game/system/newpad.h" #include "game/system/newpad.h"
@ -24,7 +28,7 @@ void InitSettings(GfxSettings& settings) {
settings.version = GfxSettings::CURRENT_VERSION; settings.version = GfxSettings::CURRENT_VERSION;
// use opengl by default for now // 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 // 1 screen update per frame
settings.vsync = 1; settings.vsync = 1;
@ -47,6 +51,33 @@ namespace Gfx {
GfxSettings g_settings; GfxSettings g_settings;
// const std::vector<const GfxRendererModule*> renderers = {&moduleOpenGL}; // const std::vector<const GfxRendererModule*> 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) { const GfxRendererModule* GetRenderer(GfxPipeline pipeline) {
switch (pipeline) { switch (pipeline) {
case GfxPipeline::Invalid: case GfxPipeline::Invalid:
@ -61,6 +92,10 @@ const GfxRendererModule* GetRenderer(GfxPipeline pipeline) {
} }
} }
const GfxRendererModule* GetCurrentRenderer() {
return GetRenderer(g_settings.renderer);
}
u32 Init() { u32 Init() {
lg::info("GFX Init"); lg::info("GFX Init");
// initialize settings // initialize settings
@ -68,7 +103,9 @@ u32 Init() {
// guarantee we have no keys detected by pad // guarantee we have no keys detected by pad
Pad::ForceClearKeys(); Pad::ForceClearKeys();
if (g_settings.renderer->init(g_settings)) { LoadSettings();
if (GetCurrentRenderer()->init(g_settings)) {
lg::error("Gfx::Init error"); lg::error("Gfx::Init error");
return 1; return 1;
} }
@ -99,38 +136,38 @@ void Loop(std::function<bool()> f) {
u32 Exit() { u32 Exit() {
lg::info("GFX Exit"); lg::info("GFX Exit");
Display::KillMainDisplay(); Display::KillMainDisplay();
g_settings.renderer->exit(); GetCurrentRenderer()->exit();
return 0; return 0;
} }
u32 vsync() { u32 vsync() {
return g_settings.renderer->vsync(); return GetCurrentRenderer()->vsync();
} }
u32 sync_path() { u32 sync_path() {
return g_settings.renderer->sync_path(); return GetCurrentRenderer()->sync_path();
} }
void send_chain(const void* data, u32 offset) { void send_chain(const void* data, u32 offset) {
if (g_settings.renderer) { if (GetCurrentRenderer()) {
g_settings.renderer->send_chain(data, offset); GetCurrentRenderer()->send_chain(data, offset);
} }
} }
void texture_upload_now(const u8* tpage, int mode, u32 s7_ptr) { void texture_upload_now(const u8* tpage, int mode, u32 s7_ptr) {
if (g_settings.renderer) { if (GetCurrentRenderer()) {
g_settings.renderer->texture_upload_now(tpage, mode, s7_ptr); GetCurrentRenderer()->texture_upload_now(tpage, mode, s7_ptr);
} }
} }
void texture_relocate(u32 destination, u32 source, u32 format) { void texture_relocate(u32 destination, u32 source, u32 format) {
if (g_settings.renderer) { if (GetCurrentRenderer()) {
g_settings.renderer->texture_relocate(destination, source, format); GetCurrentRenderer()->texture_relocate(destination, source, format);
} }
} }
void poll_events() { void poll_events() {
g_settings.renderer->poll_events(); GetCurrentRenderer()->poll_events();
} }
void input_mode_set(u32 enable) { void input_mode_set(u32 enable) {
@ -147,6 +184,8 @@ void input_mode_save() {
} else if (Pad::input_mode_get() == (u64)Pad::InputModeStatus::Disabled) { } 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_backup = g_settings.pad_mapping_info; // copy to backup
g_settings.pad_mapping_info = Pad::g_input_mode_mapping; // set current mapping g_settings.pad_mapping_info = Pad::g_input_mode_mapping; // set current mapping
SaveSettings();
} }
} }

View file

@ -42,16 +42,19 @@ struct GfxRendererModule {
struct GfxSettings { struct GfxSettings {
// current version of the settings. this should be set up so that newer versions are always higher // current version of the settings. this should be set up so that newer versions are always higher
// than older versions // 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 u64 version; // the version of this settings struct. MUST ALWAYS BE THE FIRST THING!
int vsync; // (temp) number of screen update per frame
bool debug; // graphics debugging
const GfxRendererModule* renderer; // which rendering pipeline to use.
Pad::MappingInfo pad_mapping_info; // button mapping Pad::MappingInfo pad_mapping_info; // button mapping
Pad::MappingInfo pad_mapping_info_backup; // button mapping backup (see newpad.h) 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 { namespace Gfx {