Skip to content

Commit

Permalink
[ClientApplication|Config] Now saving config on client exit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed May 14, 2020
1 parent a2c2dba commit db78953
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion external/gamekit
4 changes: 4 additions & 0 deletions source/client/core/ClientApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ void ClientApplication::onEvent(const sf::Event &event) {
Config::isFullscreenModeEnabled ^= 1;
}

void ClientApplication::onExit() {
Config::saveConfigToFile("config/client.lua");
}

void ClientApplication::initOpenGL() {
// Enable textures
glCheck(glEnable(GL_TEXTURE_2D));
Expand Down
1 change: 1 addition & 0 deletions source/client/core/ClientApplication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class ClientApplication : public gk::CoreApplication {
void handleEvents() override;

void onEvent(const sf::Event &event) override;
void onExit() override;

static void initOpenGL();

Expand Down
35 changes: 32 additions & 3 deletions source/client/core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ u8 Config::mouseSensitivity = 8;

#include <sol.hpp>

void Config::loadConfigFromFile(const char *file) {
if (gk::Filesystem::fileExists(file)) {
void Config::loadConfigFromFile(const char *filename) {
if (gk::Filesystem::fileExists(filename)) {
sol::state lua;

try {
lua.safe_script_file(file);
lua.safe_script_file(filename);

isFlyModeEnabled = lua["isFlyModeEnabled"].get_or(isFlyModeEnabled);
isNoClipEnabled = lua["isNoClipEnabled"].get_or(isNoClipEnabled);
Expand Down Expand Up @@ -98,3 +98,32 @@ void Config::loadConfigFromFile(const char *file) {
}
}

#include <fstream>

void Config::saveConfigToFile(const char *filename) {
std::ofstream file{filename, std::ofstream::out | std::ofstream::trunc};
file << "isFlyModeEnabled = " << (isFlyModeEnabled ? "true" : "false") << std::endl;
file << "isNoClipEnabled = " << (isNoClipEnabled ? "true" : "false") << std::endl;
file << std::endl;
file << "isBlockInfoWidgetEnabled = " << (isBlockInfoWidgetEnabled ? "true" : "false") << std::endl;
file << "isFpsCounterEnabled = " << (isFpsCounterEnabled ? "true" : "false") << std::endl;
file << "isHotbarVisible = " << (isHotbarVisible ? "true" : "false") << std::endl;
file << "isCrosshairVisible = " << (isCrosshairVisible ? "true" : "false") << std::endl;
file << std::endl;
file << "renderDistance = " << renderDistance << std::endl;
file << "isTorchSmoothLightingEnabled = " << (isTorchSmoothLightingEnabled ? "true" : "false") << std::endl;
file << "isSunSmoothLightingEnabled = " << (isSunSmoothLightingEnabled ? "true" : "false") << std::endl;
file << "isAmbientOcclusionEnabled = " << (isAmbientOcclusionEnabled ? "true" : "false") << std::endl;
file << "isWireframeModeEnabled = " << (isWireframeModeEnabled ? "true" : "false") << std::endl;
file << "isFullscreenModeEnabled = " << (isFullscreenModeEnabled ? "true" : "false") << std::endl;
file << "isVerticalSyncEnabled = " << (isVerticalSyncEnabled ? "true" : "false") << std::endl;
file << "cameraFOV = " << cameraFOV << std::endl;
file << "screenWidth = " << screenWidth << std::endl;
file << "screenHeight = " << screenHeight << std::endl;
file << "guiScale = " << (u16)guiScale << std::endl;
file << "mipmapLevels = " << (u16)mipmapLevels << std::endl;
file << "aoStrength = " << aoStrength << std::endl;
file << std::endl;
file << "mouseSensitivity = " << (u16)mouseSensitivity << std::endl;
}

3 changes: 2 additions & 1 deletion source/client/core/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ namespace Config {
// Input
extern u8 mouseSensitivity;

void loadConfigFromFile(const char *file);
void loadConfigFromFile(const char *filename);
void saveConfigToFile(const char *filename);
}

#endif // CONFIG_HPP_

0 comments on commit db78953

Please sign in to comment.