Browse Source

Add settings saving functionality

master
C. J. Howard 5 years ago
parent
commit
d5da920528
Signed by: cjhoward GPG Key ID: 03E1FABA9C3EC195
2 changed files with 100 additions and 1 deletions
  1. +96
    -1
      src/game.cpp
  2. +4
    -0
      src/game.hpp

+ 96
- 1
src/game.cpp View File

@ -160,6 +160,77 @@ bool Game::readSetting(const std::string& name, Vector2* value) const
return (!stream.fail()); return (!stream.fail());
} }
template <>
bool Game::writeSetting<std::string>(const std::string& name, const std::string& value) const
{
auto it = settingsTableIndex.find(name);
if (it == settingsTableIndex.end())
{
return false;
}
(*settingsTable)[it->second][1] = value;
return true;
}
template <>
bool Game::writeSetting<bool>(const std::string& name, const bool& value) const
{
auto it = settingsTableIndex.find(name);
if (it == settingsTableIndex.end())
{
return false;
}
(*settingsTable)[it->second][1] = (value) ? "on" : "off";
return true;
}
template <>
bool Game::writeSetting<int>(const std::string& name, const int& value) const
{
auto it = settingsTableIndex.find(name);
if (it == settingsTableIndex.end())
{
return false;
}
(*settingsTable)[it->second][1] = std::to_string(value);
return true;
}
template <>
bool Game::writeSetting<float>(const std::string& name, const float& value) const
{
auto it = settingsTableIndex.find(name);
if (it == settingsTableIndex.end())
{
return false;
}
(*settingsTable)[it->second][1] = std::to_string(value);
return true;
}
template <>
bool Game::writeSetting<Vector2>(const std::string& name, const Vector2& value) const
{
auto it = settingsTableIndex.find(name);
if (it == settingsTableIndex.end())
{
return false;
}
(*settingsTable)[it->second][1] = std::to_string(value.x);
(*settingsTable)[it->second][2] = std::to_string(value.y);
return true;
}
Game::Game(int argc, char* argv[]): Game::Game(int argc, char* argv[]):
currentState(nullptr), currentState(nullptr),
window(nullptr) window(nullptr)
@ -285,6 +356,10 @@ void Game::changeLanguage(std::size_t nextLanguageIndex)
uiRootElement->update(); uiRootElement->update();
currentMenu->getContainer()->resetTweens(); currentMenu->getContainer()->resetTweens();
} }
// Save settings
language = getString("language-code");
saveSettings();
} }
void Game::nextLanguage() void Game::nextLanguage()
@ -473,6 +548,9 @@ void Game::toggleFullscreen()
toggleFullscreenDisabled = false; toggleFullscreenDisabled = false;
}; };
eventDispatcher.schedule(event, time + 0.5f); eventDispatcher.schedule(event, time + 0.5f);
// Save settings
saveSettings();
} }
} }
@ -481,6 +559,9 @@ void Game::toggleVSync()
vsync = !vsync; vsync = !vsync;
window->setVSync(vsync); window->setVSync(vsync);
restringUI(); restringUI();
// Save settings
saveSettings();
} }
void Game::setUpdateRate(double frequency) void Game::setUpdateRate(double frequency)
@ -1097,7 +1178,7 @@ void Game::setupUI()
fontSizePX = fontSizePT * (1.0f / 96.0f) * dpi; fontSizePX = fontSizePT * (1.0f / 96.0f) * dpi;
logger->log("Detected display DPI as " + std::to_string(dpi) + "."); logger->log("Detected display DPI as " + std::to_string(dpi) + ".");
logger->log("Fonts size = " + std::to_string(fontSizePT) + " PT = " + std::to_string(fontSizePX) + " PX.");
logger->log("Font size = " + std::to_string(fontSizePT) + " PT = " + std::to_string(fontSizePX) + " PX.");
// Load fonts // Load fonts
loadFonts(); loadFonts();
@ -2057,6 +2138,20 @@ void Game::loadSettings()
void Game::saveSettings() void Game::saveSettings()
{ {
// Update settings table
writeSetting("language", language);
writeSetting("windowed-resolution", windowedResolution);
writeSetting("fullscreen-resolution", fullscreenResolution);
writeSetting("fullscreen", fullscreen);
writeSetting("vsync", vsync);
writeSetting("font-size", fontSizePT);
writeSetting("control-profile", controlProfileName);
// Form full path to control profile file
std::string settingsPath = configPath + "settings.csv";
// Save control profile
resourceManager->save<StringTable>(settingsTable, settingsPath);
} }
void Game::loadStrings() void Game::loadStrings()

+ 4
- 0
src/game.hpp View File

@ -236,6 +236,10 @@ public:
template <typename T> template <typename T>
bool readSetting(const std::string& name, T* value) const; bool readSetting(const std::string& name, T* value) const;
template <typename T>
bool writeSetting(const std::string& name, const T& value) const;
public: public:
// Game states // Game states

Loading…
Cancel
Save