Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config cleanup #141

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ texture_packs/ElijahHyper

custom_run.sh

*.obd

#
# Linux
#
Expand Down
19 changes: 0 additions & 19 deletions config.obd

This file was deleted.

1 change: 1 addition & 0 deletions src/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ add_library(ob-client
renderer/chunk_renderer.cpp
network/client_commands.cpp
client.cpp
client_config.cpp
maths.cpp
client_engine.cpp
window.cpp
Expand Down
10 changes: 5 additions & 5 deletions src/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ bool isVoxelSelectable(VoxelType voxelType)

Client::Client(const ClientConfig& config)
: NetworkHost("Client")
, m_gui(config.windowWidth, config.windowHeight)
, m_gui(config.get_windowWidth(), config.get_windowHeight())
{
// clang-format off
m_commandDispatcher.addCommand(ClientCommand::BlockUpdate, &Client::onBlockUpdate);
Expand Down Expand Up @@ -70,10 +70,10 @@ bool Client::init(const ClientConfig& config, float aspect)
m_errorSkinTexture.create("skins/error");
m_errorSkinTexture.bind();

m_texturePack = config.texturePack;
m_texturePack = config.get_texturePack();

// Set up the server connection
auto peer = NetworkHost::createAsClient(config.serverIp);
auto peer = NetworkHost::createAsClient(config.get_serverIp());
if (!peer) {
return false;
}
Expand All @@ -83,14 +83,14 @@ bool Client::init(const ClientConfig& config, float aspect)
mp_player = &m_entities[NetworkHost::getPeerId()];
mp_player->position = {CHUNK_SIZE * 2, CHUNK_SIZE * 2 + 1, CHUNK_SIZE * 2};

m_rawPlayerSkin = gl::loadRawImageFile("skins/" + config.skinName);
m_rawPlayerSkin = gl::loadRawImageFile("skins/" + config.get_skinName());
sendPlayerSkin(m_rawPlayerSkin);

m_projectionMatrix = glm::perspective(3.14f / 2.0f, aspect, 0.01f, 2000.0f);

// Font and text
m_debugTextFont.init("res/VeraMono-Bold.ttf", 512);
m_debugText.setPosition({2, config.windowHeight - 16, 0});
m_debugText.setPosition({2, config.get_windowHeight() - 16, 0});
m_debugText.setCharSize(16);
m_debugText.setFont(m_debugTextFont);
m_debugText.setText("Current FPS: 60");
Expand Down
46 changes: 46 additions & 0 deletions src/client/client_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <common/obd_config.h>

#include "client_config.h"

ClientConfig::ClientConfig(const std::string& fileName)
: ObdConfig(fileName)
{
if (m_keyvalues.count("isFpsCapped"))
m_isFpsCapped = m_keyvalues["isFpsCapped"] == "true" ? true : false;
if (m_keyvalues.count("shouldShowInstructions"))
m_shouldShowInstructions = m_keyvalues["shouldShowInstructions"] == "true" ? true : false;
if (m_keyvalues.count("fullScreen"))
m_fullScreen = m_keyvalues["fullScreen"] == "true" ? true : false;

if (m_keyvalues.count("windowWidth"))
m_windowWidth = std::stoi(m_keyvalues["windowWidth"]);
if (m_keyvalues.count("windowHeight"))
m_windowHeight = std::stoi(m_keyvalues["windowHeight"]);
if (m_keyvalues.count("fpsLimit"))
m_fpsLimit = std::stoi(m_keyvalues["fpsLimit"]);
if (m_keyvalues.count("fov"))
m_fov = std::stoi(m_keyvalues["fov"]);

if (m_keyvalues.count("skinName"))
m_skinName = m_keyvalues["skinName"];
if (m_keyvalues.count("texturePack"))
m_texturePack = m_keyvalues["texturePack"];
if (m_keyvalues.count("serverIp"))
m_serverIp = m_keyvalues["serverIp"];
}

ClientConfig::~ClientConfig()
{
m_keyvalues["isFpsCapped"] = m_isFpsCapped ? "true" : "false";
m_keyvalues["shouldShowInstructions"] = m_shouldShowInstructions ? "true" : "false";
m_keyvalues["fullScreen"] = m_fullScreen ? "true" : "false";

m_keyvalues["windowWidth"] = std::to_string(m_windowWidth);
m_keyvalues["windowHeight"] = std::to_string(m_windowHeight);
m_keyvalues["fpsLimit"] = std::to_string(m_fpsLimit);
m_keyvalues["fov"] = std::to_string(m_fov);

m_keyvalues["skinName"] = m_skinName;
m_keyvalues["texturePack"] = m_texturePack;
m_keyvalues["serverIp"] = m_serverIp;
}
61 changes: 44 additions & 17 deletions src/client/client_config.h
Original file line number Diff line number Diff line change
@@ -1,26 +1,53 @@
#pragma once

#include <SFML/System/Time.hpp>
#include <string>
#include <common/obd_config.h>

#include <common/network/net_constants.h>
class ClientConfig : public ObdConfig {
bool m_fullScreen = false;
int m_windowWidth = 1280;
int m_windowHeight = 720;
int m_fpsLimit = 60;
int m_fov = 65;

/**
* @brief Config options for client
* Mostly for the window and general graphics options
*/
struct ClientConfig {
bool fullScreen = false;
int windowWidth = 1280;
int windowHeight = 720;
int fpsLimit = 60;
int fov = 65;
bool m_isFpsCapped = true;
bool m_shouldShowInstructions = true;

bool isFpsCapped = true;
bool shouldShowInstructions = true;
std::string m_skinName = "player";
std::string m_texturePack = "default";

std::string skinName = "player";
std::string texturePack = "default";
std::string m_serverIp = "127.0.0.1";
public:
ClientConfig(const std::string& fileName);
~ClientConfig();

std::string serverIp = LOCAL_HOST;
bool get_fullScreen() const { return m_fullScreen; }
void set_fullScreen(const bool value) { m_fullScreen = value; }

bool get_isFpsCapped() const { return m_isFpsCapped; }
void set_isFpsCapped(const bool value) { m_isFpsCapped = value; }

bool get_shouldShowInstructions() const { return m_shouldShowInstructions; }
void set_shouldShowInstructions(const bool value) { m_shouldShowInstructions = value; }

int get_windowWidth() const { return m_windowWidth; }
void set_windowWidth(const int value) { m_windowWidth = value; }

int get_windowHeight() const { return m_windowHeight; }
void set_windowHeight(const int value) { m_windowHeight = value; }

int get_fpsLimit() const { return m_fpsLimit; }
void set_fpsLimit(const int value) { m_fpsLimit = value; }

int get_fov() const { return m_fov; }
void set_fov(const int value) { m_fov = value; }

std::string get_skinName() const { return m_skinName; }
void set_skinName(const std::string& value) { m_skinName = value; }

std::string get_texturePack() const { return m_texturePack; }
void set_texturePack(const std::string& value) { m_texturePack = value; }

std::string get_serverIp() const { return m_serverIp; }
void set_serverIp(const std::string& value) { m_serverIp = value; }
MarkusG marked this conversation as resolved.
Show resolved Hide resolved
};
10 changes: 5 additions & 5 deletions src/client/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
Window::Window(const ClientConfig& config)
{
window.setKeyRepeatEnabled(false);
if (config.fullScreen) {
if (config.get_fullScreen()) {
create(sf::VideoMode::getDesktopMode(), sf::Style::Fullscreen);
width = window.getSize().x;
height = window.getSize().y;
aspect = static_cast<float>(width) / static_cast<float>(height);
}
else {
width = static_cast<unsigned>(config.windowWidth);
height = static_cast<unsigned>(config.windowHeight);
width = static_cast<unsigned>(config.get_windowWidth());
height = static_cast<unsigned>(config.get_windowHeight());
aspect = static_cast<float>(width) / static_cast<float>(height);
create({width, height}, sf::Style::Close);
}
if (config.isFpsCapped) {
window.setFramerateLimit(config.fpsLimit);
if (config.get_isFpsCapped()) {
window.setFramerateLimit(config.get_fpsLimit());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/common/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

add_library(ob-common
file_io.cpp
obd_parser.cpp
obd_config.cpp
debug.cpp
network/net_host.cpp
scripting/script_engine.cpp
Expand All @@ -21,4 +21,4 @@ target_link_libraries(ob-common
${SFML_LIBRARIES}
${SFML_DEPENDENCIES}
${CMAKE_DL_LIBS}
)
)
1 change: 1 addition & 0 deletions src/common/common/file_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
* @return std::string Contents of the file
*/
std::string loadFileContents(const std::string& path);
bool fileExists(const std::string& path);
3 changes: 0 additions & 3 deletions src/common/common/network/net_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,3 @@ constexpr int MIN_CONNECTIONS = 1;

// The maximum number of connections the server can hold simultaneously
constexpr int MAX_CONNECTIONS = 8;

// Localhost
const std::string LOCAL_HOST = "127.0.0.1";
60 changes: 60 additions & 0 deletions src/common/common/obd_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <unordered_map>
#include <filesystem>

#include "obd_config.h"

// member definitions for ObdConfig
ObdConfig::ObdConfig(const std::string& filePath)
: m_filePath(filePath)
{
if (std::filesystem::exists(filePath)) {
m_filePath = filePath;
std::ifstream file(m_filePath);
if (!file.is_open()) {
std::cerr << "Could not open file " << m_filePath << std::endl;
return;
}
std::stringstream stream;
stream << file.rdbuf();

m_keyvalues = parseObdData(stream.str());
}
}

ObdConfig::~ObdConfig()
{
std::ofstream file(m_filePath);
if (!file.is_open()) {
std::cerr << "Could not write to " << m_filePath << std::endl;
return;
}
file << serializeObdData(m_keyvalues);
}

// helper functions
std::unordered_map<std::string, std::string> parseObdData(const std::string& obdData)
{
std::istringstream contents(obdData);
std::string key;
std::string value;
std::unordered_map<std::string, std::string> data;

while (contents >> key) {
contents >> value;
data.emplace(key, value);
}

return data;
}

std::string serializeObdData(const std::unordered_map<std::string, std::string>& obdData)
{
std::ostringstream output;
for (auto const& pair : obdData) {
output << pair.first << " " << pair.second << "\n";
}
return output.str();
}
17 changes: 17 additions & 0 deletions src/common/common/obd_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <iostream>
MarkusG marked this conversation as resolved.
Show resolved Hide resolved
#include <unordered_map>

std::unordered_map<std::string, std::string> parseObdData(const std::string& obdData);
std::string serializeObdData(const std::unordered_map<std::string, std::string>& obdData);
MarkusG marked this conversation as resolved.
Show resolved Hide resolved

class ObdConfig
{
std::string m_filePath;
public:
ObdConfig(const std::string& filePath); // implicitly read config file
~ObdConfig(); // write back to file when the config goes out of scope
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rule of 5 should apply here, the copy constructor/assignment operator and move constructor/ assignment operator should be also defined (as =delete)

eg

ObdConfig(const ObdConfig&) = delete;

etc

https://en.cppreference.com/w/cpp/language/rule_of_three

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary if all of the resources managed by ObdConfig are classes?

protected:
std::unordered_map<std::string, std::string> m_keyvalues;
};
56 changes: 0 additions & 56 deletions src/common/common/obd_parser.cpp

This file was deleted.

Loading