17
17
18
18
namespace chesspp
19
19
{
20
- class configuration
20
+ namespace configuration
21
21
{
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
+ {
23
57
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
+ }
33
70
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
+ }
82
95
}
83
96
84
97
#endif
0 commit comments