Skip to content

Commit

Permalink
[Config] Now storing default username and server address.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jun 26, 2020
1 parent be369f8 commit 992d2de
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
3 changes: 3 additions & 0 deletions config/client.example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ aoStrength = 1
-- Input
mouseSensitivity = 8

-- Other
username = ""

10 changes: 10 additions & 0 deletions source/client/core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ float Config::aoStrength = 1.0f;
// Input
u8 Config::mouseSensitivity = 8;

// Other
std::string Config::defaultUsername = "";
std::string Config::defaultServerAddress = "localhost:4242";

void Config::loadConfigFromFile(const char *filename) {
if (gk::Filesystem::fileExists(filename)) {
sol::state lua;
Expand Down Expand Up @@ -92,6 +96,9 @@ void Config::loadConfigFromFile(const char *filename) {

mouseSensitivity = lua["mouseSensitivity"].get_or(mouseSensitivity);

defaultUsername = lua["defaultUsername"].get_or(defaultUsername);
defaultServerAddress = lua["defaultServerAddress"].get_or(defaultServerAddress);

gkInfo() << "Config file loaded successfully";
}
catch (sol::error &e) {
Expand Down Expand Up @@ -125,5 +132,8 @@ void Config::saveConfigToFile(const char *filename) {
file << "aoStrength = " << aoStrength << std::endl;
file << std::endl;
file << "mouseSensitivity = " << (u16)mouseSensitivity << std::endl;
file << std::endl;
file << "defaultUsername = \"" << defaultUsername << "\"" << std::endl;
file << "defaultServerAddress = \"" << defaultServerAddress << "\"" << std::endl;
}

6 changes: 6 additions & 0 deletions source/client/core/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#ifndef CONFIG_HPP_
#define CONFIG_HPP_

#include <string>

#include <gk/core/IntTypes.hpp>

namespace Config {
Expand Down Expand Up @@ -58,6 +60,10 @@ namespace Config {
// Input
extern u8 mouseSensitivity;

// Other
extern std::string defaultUsername;
extern std::string defaultServerAddress;

void loadConfigFromFile(const char *filename);
void saveConfigToFile(const char *filename);
}
Expand Down
6 changes: 4 additions & 2 deletions source/client/states/ServerConnectState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include "ServerLoadingState.hpp"

ServerConnectState::ServerConnectState(gk::ApplicationState *parent) : InterfaceState(parent) {
m_usernameInput.setString("");
m_usernameInput.setString(Config::defaultUsername);
m_usernameInput.setCharacterLimit(20);
m_usernameInput.setBackgroundSize(150, 20);
m_usernameInput.setBackgroundOutline(1, gk::Color::White);
Expand All @@ -40,7 +40,7 @@ ServerConnectState::ServerConnectState(gk::ApplicationState *parent) : Interface
m_usernameInput.setPlaceholder("Username");
m_usernameInput.setFocus(false);

m_addressInput.setString("localhost:4242");
m_addressInput.setString(Config::defaultServerAddress);
m_addressInput.setCharacterLimit(15 + 1 + 6);
m_addressInput.setBackgroundSize(150, 20);
m_addressInput.setBackgroundOutline(1, gk::Color::White);
Expand Down Expand Up @@ -83,6 +83,8 @@ ServerConnectState::ServerConnectState(gk::ApplicationState *parent) : Interface
auto &serverLoadingState = m_stateStack->push<ServerLoadingState>(game, true, host, port, this);
serverLoadingState.setTexturePack(m_texturePack);
serverLoadingState.setUsername(username);
Config::defaultUsername = username;
Config::defaultServerAddress = m_addressInput.string();
}
});

Expand Down

0 comments on commit 992d2de

Please sign in to comment.