Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "lib/boost"]
path = lib/boost
url = git://github.com/ryppl/boost-svn.git
[submodule "lib/json-parser"]
path = lib/json-parser
url = git://github.com/udp/json-parser.git
2 changes: 1 addition & 1 deletion build_clang_win.bat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
clang++ -std=c++11 -o chesspp.exe -isystem lib/boost/boost/ -isystem lib/SFML/include/ -Llib/SFML/lib/ -lsfml-audio -lsfml-graphics -lsfml-main -lsfml-system -lsfml-window src/*.cpp src/board/*.cpp
clang++ -std=c++11 -o chesspp.exe -isystem lib/boost/boost/ -isystem lib/SFML/include/ -isystem lib/json-parser/ -Llib/SFML/lib/ -lsfml-audio -lsfml-graphics -lsfml-main -lsfml-system -lsfml-window lib/json-parser/json.c src/*.cpp src/board/*.cpp
pause
20 changes: 20 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"chesspp":
{
"board":
{
"initial_layout": "res/games/new_game.txt",
"width": 8,
"height": 8,
"cell_width": 80,
"cell_height": 80,

"images":
{
"board": "res/img/chessboard_640x640.png",
"pieces": "res/img/chess_pieces_80x80_each.png",
"validMove": "res/img/valid_move.png"
}
}
}
}
16 changes: 0 additions & 16 deletions config.xml

This file was deleted.

1 change: 1 addition & 0 deletions lib/json-parser
Submodule json-parser added at 1e5869
2 changes: 1 addition & 1 deletion src/AppStateGame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace chesspp
Board* board;

graphics::GraphicsHandler graphics;
configuration::BoardConfig boardConfig;
config::BoardConfig boardConfig;

public:
AppStateGame(Application* _app, sf::RenderWindow *_display);
Expand Down
59 changes: 33 additions & 26 deletions src/Configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cstring>
#include <cstdint>
#include <boost/algorithm/string/replace.hpp>
#include <fstream>

#if defined(__linux__)
#include <unistd.h>
Expand All @@ -15,14 +16,14 @@


#include "Exception.hpp"
#include "XMLReader.hpp"
#include "JsonReader.hpp"
#include "board/logger.hpp"

namespace chesspp
{
namespace configuration
namespace config
{
class configuration
class Configuration
{
protected:
std::string res_path;
Expand All @@ -31,62 +32,67 @@ namespace chesspp
// <exe_location>/res/img/... should be where resources are stored.
//OS x, resource path is defined as the absolute path to the Resources folder of the .app structure.
// <.app>/Contents/Resources/res/img... should be where resources are stored.
std::string getResourcePath()
static std::string getResourcePath()
{

char buf[1024];
uint32_t size = sizeof(buf);
std::uint32_t size = sizeof(buf);
memset(buf, 0, size);
std::string ret;
#if defined(__linux__)
if(readlink("/proc/self/exe", buf, size) == -1)
throw chesspp::exception("Unable to determine executable path on Linux.");
throw Exception("Unable to determine executable path on Linux.");
ret = buf;
ret = ret.substr(0, ret.find_last_of('/')+1);

#elif defined(_WIN32)
if(GetModuleFileNameA(NULL, buf, size) == 0)
throw chesspp::exception("Unable to determine executable path on Windows.");
throw Exception("Unable to determine executable path on Windows.");
ret = buf;
boost::replace_all(ret, "\\", "/");
ret = ret.substr(0, ret.find_last_of('/')+1);

#elif defined(__APPLE__)
if (_NSGetExecutablePath(buf, &size) != 0)
throw chesspp::exception("Unable to determine executable path on OS x. (Buffer size too small?)");
throw Exception("Unable to determine executable path on OS x. (Buffer size too small?)");
ret = buf;
ret = ret.substr(0, ret.find_last_of('/')+1) + "../Resources/";
//Need to go up one directory because the exe is stored in <.app>/Contents/MacOS/,
//And we need <.app>/Contents/Resources

#else
throw chesspp::exception("Unknown OS. Unable to determine executable path.");
throw Exception("Unknown OS. Unable to determine executable path.");
#endif

return ret;
}

XMLReader reader;
JsonReader reader;
public:
configuration(const std::string &configFile) : res_path(getResourcePath()), reader(getResourcePath() + configFile) {}
virtual ~configuration() {}
Configuration(const std::string &configFile) noexcept(false) : res_path(getResourcePath()), reader(std::ifstream(getResourcePath() + configFile))
{
}
virtual ~Configuration() noexcept
{
}

};

class BoardConfig : public configuration
class BoardConfig : public Configuration
{
std::string initial_layout;
uint8_t board_width, board_height;
uint16_t cell_width, cell_height;
std::uint8_t board_width, board_height;
std::uint16_t cell_width, cell_height;

public:
BoardConfig() : configuration("config.xml")
BoardConfig()
: Configuration("config.json")
, initial_layout (res_path + std::string(reader()["chesspp"]["board"]["initial_layout"]))
, board_width(reader()["chesspp"]["board"]["width"])
, board_height(reader()["chesspp"]["board"]["height"])
, cell_width(reader()["chesspp"]["board"]["cell_width"])
, cell_height(reader()["chesspp"]["board"]["cell_height"])
{
initial_layout = res_path + reader.getProperty<std::string>("chesspp.data.board.initial_layout");
board_width = reader.getProperty<uint8_t>("chesspp.data.board.width");
board_height = reader.getProperty<uint8_t>("chesspp.data.board.height");
cell_width = reader.getProperty<uint16_t>("chesspp.data.board.cell_width");
cell_height = reader.getProperty<uint16_t>("chesspp.data.board.cell_height");
}

std::string getInitialLayout() { return initial_layout; }
Expand All @@ -96,16 +102,17 @@ namespace chesspp
uint16_t getCellHeight() { return cell_height; }
};

class GraphicsConfig : public configuration
class GraphicsConfig : public Configuration
{
std::string path_board, path_pieces, path_validMove;

public:
GraphicsConfig() : configuration("config.xml")
GraphicsConfig()
: Configuration("config.json")
, path_board(res_path + std::string(reader()["chesspp"]["board"]["images"]["board"]))
, path_pieces(res_path + std::string(reader()["chesspp"]["board"]["images"]["pieces"]))
, path_validMove(res_path + std::string(reader()["chesspp"]["board"]["images"]["validMove"]))
{
path_board = res_path + reader.getProperty<std::string>("chesspp.images.board");
path_pieces = res_path + reader.getProperty<std::string>("chesspp.images.pieces");
path_validMove = res_path + reader.getProperty<std::string>("chesspp.images.validMove");
}

std::string getSpritePath_board() { return path_board; }
Expand Down
67 changes: 58 additions & 9 deletions src/Exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,72 @@

#include <string>
#include <exception>
#include <utility>
#include <typeinfo>

namespace chesspp
{
class exception : public std::exception
class Exception : public std::exception
{
using std::exception::what;
std::string e; //message
std::exception const&by; //caused by other exception, == *this otherwise

public:
exception() {}
virtual ~exception() {};
exception(const exception &) {}
Exception(std::string const&e = "") noexcept(noexcept(std::string(std::string(""))))
: e(e), by(*this) //ignore benign warning about use of *this
{
}
Exception(std::string const&e, std::exception const&by) noexcept(noexcept(std::string(std::string(""))))
: e(e), by(by)
{
}
Exception(Exception const&) = default;
Exception(Exception &&) noexcept = default;
Exception &operator=(Exception const&) = default;
Exception &operator=(Exception &&) = default;
virtual ~Exception() noexcept = default;

exception(const std::string &_e) : e(_e) {}
virtual const char *what() { return e.c_str(); }
virtual bool operator==(std::exception const&other) const noexcept
{
return dynamic_cast<Exception const*>(&other) == this;
}
friend bool operator==(std::exception const&e1, Exception const&e2) noexcept
{
return e2 == e1; //calls above operator==
}

private:
std::string e;
virtual const char *what() const noexcept
{
return e.c_str();
}
virtual operator std::string() const noexcept
{
return e;
}
virtual std::exception const&cause() const noexcept
{
return by;
}
std::string fullMessage() const noexcept
{
std::string full = std::string(typeid(*this).name()) + " (::chespp::Exception): message = {" + e + "}";
if(!(by == *this)) //cannot use !=, `using rel_ops` is below
{
full += ", caused by {";
if(typeid(by) == typeid(Exception))
{
full += dynamic_cast<Exception const&>(by).fullMessage();
}
else
{
full += by.what();
}
full += "}";
}
return full;
}
};
using std::rel_ops::operator!=; //to avoid defining above
}

#endif
6 changes: 3 additions & 3 deletions src/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ namespace chesspp
{
try
{
configuration::BoardConfig boardConfig;
configuration::GraphicsConfig graphicsConfig;
config::BoardConfig boardConfig;
config::GraphicsConfig graphicsConfig;

board = sf::Sprite(TextureManager::getInstance().Load(graphicsConfig.getSpritePath_board()));
pieces = sf::Sprite(TextureManager::getInstance().Load(graphicsConfig.getSpritePath_pieces()));
validMove = sf::Sprite(TextureManager::getInstance().Load(graphicsConfig.getSpritePath_validMove()));

cell_size = boardConfig.getCellWidth();
}
catch (chesspp::exception &e)
catch(Exception &e)
{
#ifdef _DEBUG
cout << "Error: " << e.what() << endl;
Expand Down
Loading