1
0
Fork 0
mirror of https://github.com/k4zmu2a/SpaceCadetPinball.git synced 2024-11-24 01:40:18 +01:00

Added user settings persistence.

Using ImGui .ini writer.
This commit is contained in:
Muzychenko Andrey 2021-09-16 10:57:46 +03:00
parent af5a70785e
commit 8bae7a5b05
12 changed files with 158 additions and 227 deletions

View file

@ -5,7 +5,7 @@
#include "pb.h"
int Sound::num_channels;
unsigned int Sound::enabled_flag;
unsigned int Sound::enabled_flag = -1;
int Sound::Init(int voices)
{
@ -13,14 +13,13 @@ int Sound::Init(int voices)
if (voices > 8)
channelCount = 8;
num_channels = channelCount;
enabled_flag = -1;
return Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024);
}
void Sound::Enable(int channelFrom, int channelTo, int enableFlag)
{
enabled_flag = enableFlag;
enabled_flag = enableFlag ? -1 : 0;
}
void Sound::Activate()

View file

@ -37,7 +37,7 @@ int main(int argc, char* argv[])
auto xx = sizeof(datFileHeader);
strncpy(winmain::DatFileName, "PINBALL.DAT", sizeof winmain::DatFileName);
winmain::DatFileName = "PINBALL.DAT";
pb::init();
auto datFile = pb::record_table;

View file

@ -11,98 +11,62 @@ char high_score::default_name[32]{};
high_score_struct* high_score::dlg_hst;
bool high_score::ShowDialog = false;
winhelp_entry high_score::help[21]
{
winhelp_entry{0x70, 0x3E9},
winhelp_entry{0x191, 0x3EB},
winhelp_entry{0x1F5, 0x3EB},
winhelp_entry{0x259, 0x3EB},
winhelp_entry{0x192, 0x3EB},
winhelp_entry{0x193, 0x3EB},
winhelp_entry{0x194, 0x3EB},
winhelp_entry{0x195, 0x3EB},
winhelp_entry{0x1F6, 0x3EB},
winhelp_entry{0x1F7, 0x3EB},
winhelp_entry{0x1F8, 0x3EB},
winhelp_entry{0x1F9, 0x3EB},
winhelp_entry{0x2BD, 0x3EB},
winhelp_entry{0x2BE, 0x3EB},
winhelp_entry{0x2BF, 0x3EB},
winhelp_entry{0x2C0, 0x3EB},
winhelp_entry{0x2C1, 0x3EB},
winhelp_entry{0x2C2, 0x3EB},
winhelp_entry{0x2C3, 0x3EB},
winhelp_entry{0x2C4, 0x3EB},
winhelp_entry{0, 0},
};
int high_score::read(high_score_struct* table, int* ptrToSmth)
int high_score::read(high_score_struct* table)
{
char Buffer[20];
int scoreSum = 0;
int checkSum = 0;
clear_table(table);
char* buf1 = memory::allocate(300u);
if (!buf1)
return 1;
char* buf2 = memory::allocate(300u);
auto optPath = pinball::get_rc_string(166, 0);
for (auto position = 0; position < 5; ++position)
{
auto tablePtr = &table[position];
snprintf(Buffer, sizeof Buffer, "%d", position);
strcat(Buffer, ".Name");
options::get_string(optPath, Buffer, buf1, "", 32);
buf1[32] = 0;
strncpy(tablePtr->Name, buf1, sizeof tablePtr->Name);
auto name = options::get_string(Buffer, "");
strncpy(tablePtr->Name, name.c_str(), sizeof tablePtr->Name);
snprintf(Buffer, sizeof Buffer, "%d", position);
strcat(Buffer, ".Score");
options::get_string(optPath, Buffer, buf1, "", 300);
tablePtr->Score = atol(buf1);
for (int i = (int)strlen(tablePtr->Name); --i >= 0; scoreSum += tablePtr->Name[i])
tablePtr->Score = options::get_int(Buffer, tablePtr->Score);
for (int i = static_cast<int>(strlen(tablePtr->Name)); --i >= 0; checkSum += tablePtr->Name[i])
{
}
scoreSum += tablePtr->Score;
checkSum += tablePtr->Score;
}
scramble_number_string(scoreSum, buf1);
options::get_string(optPath, "Verification", buf2, "", 300);
if (strcmp(buf1, buf2) != 0)
auto verification = options::get_int("Verification", 7);
if (checkSum != verification)
clear_table(table);
memory::free(buf1);
memory::free(buf2);
return 0;
}
int high_score::write(high_score_struct* table, int* ptrToSmth)
int high_score::write(high_score_struct* table)
{
char Buffer[20];
high_score_struct* tablePtr = table;
int scoreSum = 0;
char* buf = memory::allocate(300u);
if (!buf)
return 1;
const char* optPath = pinball::get_rc_string(166, 0);
int checkSum = 0;
for (auto position = 0; position < 5; ++position)
{
auto tablePtr = &table[position];
snprintf(Buffer, sizeof Buffer, "%d", position);
strcat(Buffer, ".Name");
options::set_string(optPath, Buffer, tablePtr->Name);
options::set_string(Buffer, tablePtr->Name);
snprintf(Buffer, sizeof Buffer, "%d", position);
strcat(Buffer, ".Score");
snprintf(buf, 300, "%d", tablePtr->Score);
options::set_string(optPath, Buffer, buf);
for (int i = (int)strlen(tablePtr->Name); --i >= 0; scoreSum += tablePtr->Name[i])
options::set_int(Buffer, tablePtr->Score);
for (int i = static_cast<int>(strlen(tablePtr->Name)); --i >= 0; checkSum += tablePtr->Name[i])
{
}
scoreSum += tablePtr->Score;
++position;
++tablePtr;
checkSum += tablePtr->Score;
}
scramble_number_string(scoreSum, buf);
options::set_string(optPath, "Verification", buf);
memory::free(buf);
options::set_int("Verification", checkSum);
return 0;
}
@ -155,11 +119,6 @@ int high_score::place_new_score_into(high_score_struct* table, int score, LPSTR
return position;
}
void high_score::scramble_number_string(int Value, char* Buffer)
{
snprintf(Buffer, 300, "%d", Value);
}
void high_score::show_high_score_dialog(high_score_struct* table)
{
dlg_enter_name = 0;
@ -183,7 +142,7 @@ void high_score::RenderHighScoreDialog()
if (ShowDialog == true)
{
ShowDialog = false;
if (dlg_position == -1)
if (dlg_position == -1)
{
dlg_enter_name = 0;
return;

View file

@ -11,12 +11,11 @@ struct high_score_struct
class high_score
{
public:
static int read(high_score_struct* table, int* ptrToSmth);
static int write(high_score_struct* table, int* ptrToSmth);
static int read(high_score_struct* table);
static int write(high_score_struct* table);
static void clear_table(high_score_struct* table);
static int get_score_position(high_score_struct* table, int score);
static int place_new_score_into(high_score_struct* table, int score, LPSTR scoreStr, int position);
static void scramble_number_string(int Value, char* Buffer);
static void show_high_score_dialog(high_score_struct* table);
static void show_and_set_high_score_dialog(high_score_struct* table, int score, int pos, LPCSTR defaultName);
@ -27,6 +26,5 @@ private :
static int dlg_position;
static char default_name[32];
static high_score_struct* dlg_hst;
static winhelp_entry help[21];
static bool ShowDialog;
};

View file

@ -9,32 +9,8 @@
#include "Sound.h"
#include "winmain.h"
LPCSTR options::OptionsRegPath;
LPSTR options::OptionsRegPathCur;
optionsStruct options::Options{};
winhelp_entry options::keymap_help[18]
{
winhelp_entry{0x1F5, 0x3EA},
winhelp_entry{0x191, 0x3EC},
winhelp_entry{0x192, 0x3ED},
winhelp_entry{0x193, 0x3F1},
winhelp_entry{0x194, 0x3EE},
winhelp_entry{0x195, 0x3EF},
winhelp_entry{0x196, 0x3F0},
winhelp_entry{0x385, 0x3EC},
winhelp_entry{0x386, 0x3ED},
winhelp_entry{0x387, 0x3F1},
winhelp_entry{0x388, 0x3EE},
winhelp_entry{0x389, 0x3EF},
winhelp_entry{0x38A, 0x3F0},
winhelp_entry{0x38C, -1},
winhelp_entry{0x38D, -1},
winhelp_entry{0x321, -1},
winhelp_entry{0x322, -1},
winhelp_entry{0, 0},
};
short options::vk_list[28]
{
-32703,
@ -67,12 +43,30 @@ short options::vk_list[28]
-1
};
std::map<std::string, std::string> options::settings{};
void options::init()
{
auto imContext = ImGui::GetCurrentContext();
ImGuiSettingsHandler ini_handler;
ini_handler.TypeName = "Pinball";
ini_handler.TypeHash = ImHashStr(ini_handler.TypeName);
ini_handler.ReadOpenFn = MyUserData_ReadOpen;
ini_handler.ReadLineFn = MyUserData_ReadLine;
ini_handler.WriteAllFn = MyUserData_WriteAll;
imContext->SettingsHandlers.push_back(ini_handler);
// Settings are loaded from disk on the first frame
if (!imContext->SettingsLoaded)
{
ImGui::NewFrame();
ImGui::EndFrame();
}
Options.Sounds = 1;
Options.Music = 0;
Options.FullScreen = 0;
Options.Average = 5;
Options.LeftFlipperKeyDft = SDLK_z;
Options.RightFlipperKeyDft = SDLK_SLASH;
Options.PlungerKeyDft = SDLK_SPACE;
@ -93,19 +87,17 @@ void options::init()
Options.Players = 1;
Options.BottomTableBumpKey = Options.BottomTableBumpKeyDft;
Options.UniformScaling = true;
/*Options.Sounds = get_int(nullptr, "Sounds", Options.Sounds);
Options.Music = get_int(nullptr, "Music", Options.Music);
Options.Average = get_int(nullptr, "Average", Options.Average);
Options.FullScreen = get_int(nullptr, "FullScreen", Options.FullScreen);
Options.PriorityAdj = get_int(nullptr, "Priority_Adjustment", Options.PriorityAdj);
Options.Players = get_int(nullptr, "Players", Options.Players);
Options.LeftFlipperKey = get_int(nullptr, "Left Flippper key", Options.LeftFlipperKey);
Options.RightFlipperKey = get_int(nullptr, "Right Flipper key", Options.RightFlipperKey);
Options.PlungerKey = get_int(nullptr, "Plunger key", Options.PlungerKey);
Options.LeftTableBumpKey = get_int(nullptr, "Left Table Bump key", Options.LeftTableBumpKey);
Options.RightTableBumpKey = get_int(nullptr, "Right Table Bump key", Options.RightTableBumpKey);
Options.BottomTableBumpKey = get_int(nullptr, "Bottom Table Bump key", Options.BottomTableBumpKey);
Options.UniformScaling = get_int(nullptr, "Uniform scaling", true);*/
Options.Sounds = get_int("Sounds", Options.Sounds);
Options.Music = get_int("Music", Options.Music);
Options.FullScreen = get_int("FullScreen", Options.FullScreen);
Options.Players = get_int("Players", Options.Players);
Options.LeftFlipperKey = get_int("Left Flipper key", Options.LeftFlipperKey);
Options.RightFlipperKey = get_int("Right Flipper key", Options.RightFlipperKey);
Options.PlungerKey = get_int("Plunger key", Options.PlungerKey);
Options.LeftTableBumpKey = get_int("Left Table Bump key", Options.LeftTableBumpKey);
Options.RightTableBumpKey = get_int("Right Table Bump key", Options.RightTableBumpKey);
Options.BottomTableBumpKey = get_int("Bottom Table Bump key", Options.BottomTableBumpKey);
Options.UniformScaling = get_int("Uniform scaling", Options.UniformScaling);
Sound::Enable(0, 7, Options.Sounds);
update_resolution_menu();
@ -113,98 +105,40 @@ void options::init()
void options::uninit()
{
/*set_int(nullptr, "Sounds", Options.Sounds);
set_int(nullptr, "Music", Options.Music);
set_int(nullptr, "FullScreen", Options.FullScreen);
set_int(nullptr, "Players", Options.Players);
set_int(nullptr, "Left Flippper key", Options.LeftFlipperKey);
set_int(nullptr, "Right Flipper key", Options.RightFlipperKey);
set_int(nullptr, "Plunger key", Options.PlungerKey);
set_int(nullptr, "Left Table Bump key", Options.LeftTableBumpKey);
set_int(nullptr, "Right Table Bump key", Options.RightTableBumpKey);
set_int(nullptr, "Bottom Table Bump key", Options.BottomTableBumpKey);
set_int(nullptr, "Screen Resolution", Options.Resolution);
set_int(nullptr, "Uniform scaling", Options.UniformScaling);*/
}
void options::path_init(LPCSTR regPath)
{
char* buf = memory::allocate(strlen(regPath) + 1);
OptionsRegPath = buf;
if (buf)
strncpy(buf, regPath, strlen(regPath) + 1);
}
void options::path_uninit()
{
if (OptionsRegPath)
memory::free((void*)OptionsRegPath);
OptionsRegPath = nullptr;
}
LPCSTR options::path(LPCSTR regPath)
{
char* buf = OptionsRegPathCur;
if (!OptionsRegPathCur)
{
buf = memory::allocate(2000);
OptionsRegPathCur = buf;
if (!buf)
return OptionsRegPath;
}
strncpy(buf, OptionsRegPath, 2000);
if (!regPath)
return OptionsRegPathCur;
strcat(OptionsRegPathCur, "\\");
strcat(OptionsRegPathCur, regPath);
return OptionsRegPathCur;
}
void options::path_free()
{
if (OptionsRegPathCur)
memory::free(OptionsRegPathCur);
OptionsRegPathCur = nullptr;
set_int("Sounds", Options.Sounds);
set_int("Music", Options.Music);
set_int("FullScreen", Options.FullScreen);
set_int("Players", Options.Players);
set_int("Left Flipper key", Options.LeftFlipperKey);
set_int("Right Flipper key", Options.RightFlipperKey);
set_int("Plunger key", Options.PlungerKey);
set_int("Left Table Bump key", Options.LeftTableBumpKey);
set_int("Right Table Bump key", Options.RightTableBumpKey);
set_int("Bottom Table Bump key", Options.BottomTableBumpKey);
set_int("Screen Resolution", Options.Resolution);
set_int("Uniform scaling", Options.UniformScaling);
}
int options::get_int(LPCSTR optPath, LPCSTR lpValueName, int defaultValue)
int options::get_int(LPCSTR lpValueName, int defaultValue)
{
auto result = defaultValue;
if (!OptionsRegPath)
return result;
auto regPath = path(optPath);
path_free();
return result;
auto value = GetSetting(lpValueName, std::to_string(defaultValue));
return std::stoi(value);
}
void options::set_int(LPCSTR optPath, LPCSTR lpValueName, int data)
void options::set_int(LPCSTR lpValueName, int data)
{
if (!OptionsRegPath)
return;
auto regPath = path(optPath);
path_free();
SetSetting(lpValueName, std::to_string(data));
}
void options::get_string(LPCSTR optPath, LPCSTR lpValueName, LPSTR dst, LPCSTR defaultValue, int iMaxLength)
std::string options::get_string(LPCSTR lpValueName, LPCSTR defaultValue)
{
strncpy(dst, defaultValue, iMaxLength);
if (!OptionsRegPath)
return;
auto regPath = path(optPath);
path_free();
return GetSetting(lpValueName, defaultValue);
}
void options::set_string(LPCSTR optPath, LPCSTR lpValueName, LPCSTR value)
void options::set_string(LPCSTR lpValueName, LPCSTR value)
{
if (!OptionsRegPath)
return;
auto regPath = path(optPath);
path_free();
SetSetting(lpValueName, value);
}
@ -293,7 +227,7 @@ void options::update_resolution_menu()
void options::init_resolution()
{
Options.Resolution = get_int(nullptr, "Screen Resolution", -1);
Options.Resolution = get_int("Screen Resolution", -1);
int maxRes = fullscrn::get_max_supported_resolution();
if (Options.Resolution == -1 || maxRes <= Options.Resolution)
{
@ -310,3 +244,52 @@ void options::keyboard()
{
//DialogBoxParamA(nullptr, "KEYMAPPER", nullptr, KeyMapDlgProc, 0);
}
void options::MyUserData_ReadLine(ImGuiContext* ctx, ImGuiSettingsHandler* handler, void* entry, const char* line)
{
auto& keyValueStore = *static_cast<std::map<std::string, std::string>*>(entry);
std::string keyValue = line;
auto separatorPos = keyValue.find('=');
if (separatorPos != std::string::npos)
{
auto key = keyValue.substr(0, separatorPos);
auto value = keyValue.substr(separatorPos + 1, keyValue.size());
keyValueStore[key] = value;
}
}
void* options::MyUserData_ReadOpen(ImGuiContext* ctx, ImGuiSettingsHandler* handler, const char* name)
{
// There is only one custom entry
return strcmp(name, "Settings") == 0 ? &settings : nullptr;
}
void options::MyUserData_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf)
{
buf->appendf("[%s][%s]\n", handler->TypeName, "Settings");
for (const auto& setting : settings)
{
buf->appendf("%s=%s\n", setting.first.c_str(), setting.second.c_str());
}
buf->append("\n");
}
const std::string& options::GetSetting(const std::string& key, const std::string& value)
{
auto setting = settings.find(key);
if (setting == settings.end())
{
settings[key] = value;
if (ImGui::GetCurrentContext())
ImGui::MarkIniSettingsDirty();
return value;
}
return setting->second;
}
void options::SetSetting(const std::string& key, const std::string& value)
{
settings[key] = value;
if (ImGui::GetCurrentContext())
ImGui::MarkIniSettingsDirty();
}

View file

@ -5,7 +5,6 @@ struct optionsStruct
{
int Sounds;
int Music;
int Average;
int FullScreen;
int Players;
int LeftFlipperKey;
@ -28,26 +27,26 @@ struct optionsStruct
class options
{
public:
static optionsStruct Options;
static void init();
static void uninit();
static void path_init(LPCSTR regPath);
static void path_uninit();
static int get_int(LPCSTR optPath, LPCSTR lpValueName, int defaultValue);
static void set_int(LPCSTR optPath, LPCSTR lpValueName, int data);
static void get_string(LPCSTR optPath, LPCSTR lpValueName, LPSTR dst, LPCSTR defaultValue, int iMaxLength);
static void set_string(LPCSTR optPath, LPCSTR lpValueName, LPCSTR value);
static int get_int(LPCSTR lpValueName, int defaultValue);
static void set_int(LPCSTR lpValueName, int data);
static std::string get_string(LPCSTR lpValueName, LPCSTR defaultValue);
static void set_string(LPCSTR lpValueName, LPCSTR value);
static void toggle(uint32_t uIDCheckItem);
static void update_resolution_menu();
static void init_resolution();
static void keyboard();
static optionsStruct Options;
private:
static LPCSTR OptionsRegPath;
static LPSTR OptionsRegPathCur;
static LPCSTR path(LPCSTR regPath);
static void path_free();
static winhelp_entry keymap_help[18];
static short vk_list[28];
static std::map<std::string, std::string> settings;
static void MyUserData_ReadLine(ImGuiContext* ctx, ImGuiSettingsHandler* handler, void* entry, const char* line);
static void* MyUserData_ReadOpen(ImGuiContext* ctx, ImGuiSettingsHandler* handler, const char* name);
static void MyUserData_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf);
static const std::string& GetSetting(const std::string& key, const std::string& value);
static void SetSetting(const std::string& key, const std::string& value);
};

View file

@ -27,7 +27,7 @@
TPinballTable* pb::MainTable = nullptr;
datFileStruct* pb::record_table = nullptr;
int pb::time_ticks = 0, pb::demo_mode = 0, pb::cheat_mode = 0, pb::game_mode = 2, pb::mode_countdown_, pb::state;
int pb::time_ticks = 0, pb::demo_mode = 0, pb::cheat_mode = 0, pb::game_mode = 2, pb::mode_countdown_;
float pb::time_now, pb::time_next, pb::ball_speed_limit;
high_score_struct pb::highscore_table[5];
bool pb::FullTiltMode = false;
@ -98,7 +98,7 @@ int pb::init()
MainTable = new TPinballTable();
high_score::read(highscore_table, &state);
high_score::read(highscore_table);
ball_speed_limit = MainTable->BallList->Get(0)->Offset * 200.0f;
--memory::critical_allocation;
return 0;
@ -109,7 +109,7 @@ int pb::uninit()
score::unload_msg_font();
loader::unload();
partman::unload_records(record_table);
high_score::write(highscore_table, &state);
high_score::write(highscore_table);
if (MainTable)
delete MainTable;
MainTable = nullptr;

View file

@ -41,5 +41,4 @@ public:
static float collide(float timeNow, float timeDelta, TBall* ball);
private:
static int demo_mode, mode_countdown_;
static int state;
};

View file

@ -34,6 +34,7 @@
//https://github.com/ocornut/imgui 7b913db1ce9dd2fd98e5790aa59974dd4496be3b
#include "imgui.h"
#include "imgui_internal.h"
#include "imgui_impl_sdl.h"
//https://github.com/Tyyppi77/imgui_sdl 01deb04b102b6a1c15c7fdec1977a2c96a885e6f
#include "imgui_sdl.h"

View file

@ -1,12 +1,6 @@
#pragma once
#include "TTextBox.h"
struct winhelp_entry
{
int ControlId;
int ContextId;
};
class pinball
{
public:

View file

@ -33,7 +33,7 @@ DWORD winmain::now;
bool winmain::restart = false;
gdrv_bitmap8 winmain::gfr_display{};
char winmain::DatFileName[300]{};
std::string winmain::DatFileName;
bool winmain::ShowAboutDialog = false;
bool winmain::ShowImGuiDemo = false;
bool winmain::LaunchBallEnabled = true;
@ -53,10 +53,6 @@ uint32_t timeGetTimeAlt()
int winmain::WinMain(LPCSTR lpCmdLine)
{
memory::init(memalloc_failure);
++memory::critical_allocation;
auto optionsRegPath = pinball::get_rc_string(165, 0);
options::path_init(optionsRegPath);
--memory::critical_allocation;
// SDL init
SDL_SetMainReady();
@ -68,8 +64,7 @@ int winmain::WinMain(LPCSTR lpCmdLine)
BasePath = SDL_GetBasePath();
pinball::quickFlag = strstr(lpCmdLine, "-quick") != nullptr;
auto regSpaceCadet = pinball::get_rc_string(166, 0);
options::get_string(regSpaceCadet, "Pinball Data", DatFileName, pinball::get_rc_string(168, 0), 300);
DatFileName = options::get_string("Pinball Data", pinball::get_rc_string(168, 0));
/*Check for full tilt .dat file and switch to it automatically*/
auto cadetFilePath = pinball::make_path_name("CADET.DAT");
@ -77,7 +72,7 @@ int winmain::WinMain(LPCSTR lpCmdLine)
if (cadetDat)
{
fclose(cadetDat);
strncpy(DatFileName, "CADET.DAT", sizeof DatFileName);
DatFileName = "CADET.DAT";
pb::FullTiltMode = true;
}
@ -121,12 +116,17 @@ int winmain::WinMain(LPCSTR lpCmdLine)
// ImGui_ImplSDL2_Init is private, we are not actually using ImGui OpenGl backend
ImGui_ImplSDL2_InitForOpenGL(window, nullptr);
auto prefPath = SDL_GetPrefPath(nullptr, "SpaceCadetPinball");
auto iniPath = std::string(prefPath) + "imgui_pb.ini";
io.IniFilename = iniPath.c_str();
SDL_free(prefPath);
// PB init from message handler
{
++memory::critical_allocation;
options::init();
auto voiceCount = options::get_int(nullptr, "Voices", 8);
auto voiceCount = options::get_int("Voices", 8);
if (Sound::Init(voiceCount))
options::Options.Sounds = 0;
Sound::Activate();
@ -302,7 +302,6 @@ int winmain::WinMain(LPCSTR lpCmdLine)
SDL_DestroyWindow(window);
ImGui::DestroyContext();
SDL_Quit();
options::path_uninit();
if (restart)
{

View file

@ -4,7 +4,7 @@
class winmain
{
public:
static char DatFileName[300];
static std::string DatFileName;
static int single_step;
static SDL_Window* MainWindow;
static SDL_Renderer* Renderer;