Skip to content

Commit bd1965d

Browse files
author
Thumperrr
committed
Cleaned up Configuration.
Separated into BoardConfig and GraphicsConfig classes. Can be added on to in the future when more things need to store config data.
1 parent 45a91d5 commit bd1965d

File tree

1 file changed

+72
-59
lines changed

1 file changed

+72
-59
lines changed

src/Configuration.h

+72-59
Original file line numberDiff line numberDiff line change
@@ -17,68 +17,81 @@
1717

1818
namespace chesspp
1919
{
20-
class configuration
20+
namespace configuration
2121
{
22-
struct _board {
22+
class configuration
23+
{
24+
protected:
25+
std::string executable_path;
26+
std::string getExecutablePath()
27+
{
28+
char buf[1024];
29+
memset(buf, 0, sizeof(buf));
30+
std::string ret;
31+
#if defined(__linux__)
32+
if(readlink("/proc/self/exe", buf, sizeof(buf)) == -1)
33+
throw chesspp::exception("Unable to determine executable path on Linux.");
34+
ret = buf;
35+
36+
#elif defined(_WIN32)
37+
if(GetModuleFileNameA(NULL, buf, sizeof(buf)) == 0)
38+
throw chesspp::exception("Unable to determine executable path on Windows.");
39+
ret = buf;
40+
boost::replace_all(ret, "\\", "/");
41+
42+
#else
43+
throw chesspp::exception("Unknown OS. Unable to determine executable path.");
44+
#endif
45+
46+
return ret.substr(0, ret.find_last_of('/')+1);
47+
}
48+
49+
XMLReader reader;
50+
public:
51+
configuration(const std::string &configFile) : executable_path(getExecutablePath()), reader(executable_path + "/" + configFile) {}
52+
virtual ~configuration() {}
53+
};
54+
55+
class BoardConfig : public configuration
56+
{
2357
std::string initial_layout;
24-
int width, height, cell_width, cell_height;
25-
} board;
26-
27-
std::string sprite_board, sprite_pieces, sprite_validMove;
28-
std::string executable_path;
29-
30-
public:
31-
configuration() : executable_path(getExecutablePath()) {
32-
XMLReader config(executable_path + "/config.xml");
58+
uint8_t board_width, board_height;
59+
uint16_t cell_width, cell_height;
60+
61+
public:
62+
BoardConfig() : configuration("config.xml")
63+
{
64+
initial_layout = reader.getProperty<std::string>("chesspp.data.board.initial_layout");
65+
board_width = reader.getProperty<uint8_t>("chesspp.data.board.width");
66+
board_height = reader.getProperty<uint8_t>("chesspp.data.board.height");
67+
cell_width = reader.getProperty<uint16_t>("chesspp.data.board.cell_width");
68+
cell_height = reader.getProperty<uint16_t>("chesspp.data.board.cell_height");
69+
}
3370

34-
board.initial_layout = executable_path + config.getProperty<std::string>("chesspp.data.board.initial_layout");
35-
board.width = config.getProperty<int>("chesspp.data.board.width");
36-
board.height = config.getProperty<int>("chesspp.data.board.height");
37-
board.cell_width = config.getProperty<int>("chesspp.data.board.cell_width");
38-
board.cell_height = config.getProperty<int>("chesspp.data.board.cell_height");
39-
40-
sprite_board = executable_path + config.getProperty<std::string>("chesspp.images.board");
41-
sprite_pieces = executable_path + config.getProperty<std::string>("chesspp.images.pieces");
42-
sprite_validMove = executable_path + config.getProperty<std::string>("chesspp.images.validMove");
43-
}
44-
static configuration &instance() {
45-
static configuration instance;
46-
return instance;
47-
}
48-
49-
std::string getBoardInitialLayout() const { return board.initial_layout; }
50-
int getBoardWidth() const { return board.width; }
51-
int getBoardHeight() const { return board.height; }
52-
int getCellWidth() const { return board.cell_width; }
53-
int getCellHeight() const { return board.cell_width; }
54-
55-
std::string getSpritePath_board() const { return sprite_board; }
56-
std::string getSpritePath_pieces() const { return sprite_pieces; }
57-
std::string getSpritePath_validMove() const { return sprite_validMove; }
58-
59-
std::string getExecutablePath() {
60-
char buf[1024];
61-
memset(buf, 0, sizeof(buf));
62-
std::string ret;
63-
64-
#if defined(__linux__)
65-
if(readlink("/proc/self/exe", buf, sizeof(buf)) == -1)
66-
throw chesspp::exception("Unable to determine executable path on Linux.");
67-
ret = buf;
68-
69-
#elif defined(_WIN32)
70-
if(GetModuleFileNameA(NULL, buf, sizeof(buf)) == 0)
71-
throw chesspp::exception("Unable to determine executable path on Windows.");
72-
ret = buf;
73-
boost::replace_all(ret, "\\", "/");
74-
75-
#else
76-
throw chesspp::exception("Unknown OS. Unable to determine executable path.");
77-
#endif
78-
79-
return ret.substr(0, ret.find_last_of('/')+1);
80-
}
81-
};
71+
std::string getInitialLayout() { return initial_layout; }
72+
uint8_t getBoardWidth() { return board_width; }
73+
uint8_t getBoardHeight() { return board_height; }
74+
uint16_t getCellWidth() { return cell_width; }
75+
uint16_t getCellHeight() { return cell_height; }
76+
};
77+
78+
class GraphicsConfig : public configuration
79+
{
80+
std::string path_board, path_pieces, path_validMove;
81+
82+
public:
83+
GraphicsConfig() : configuration("config.xml")
84+
{
85+
path_board = reader.getProperty<std::string>("chesspp.images.board");
86+
path_pieces = reader.getProperty<std::string>("chesspp.images.pieces");
87+
path_validMove = reader.getProperty<std::string>("chesspp.images.validMove");
88+
}
89+
90+
std::string getSpritePath_board() { return path_board; }
91+
std::string getSpritePath_pieces() { return path_pieces; }
92+
std::string getSpritePath_validMove() { return path_validMove; }
93+
};
94+
}
8295
}
8396

8497
#endif

0 commit comments

Comments
 (0)