Skip to content

Commit 696c9a3

Browse files
author
Stephen Hall
committed
Modified Configuration to work on OS x platforms.
1 parent 7451e1c commit 696c9a3

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

src/Configuration.h

+28-8
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
#include <unistd.h>
1010
#elif defined(_WIN32)
1111
#include <Windows.h>
12+
#elif defined(__APPLE__)
13+
#include <mach-o/dyld.h>
1214
#endif
1315

1416

1517
#include "Exception.h"
1618
#include "XMLReader.h"
19+
#include "board/logger.h"
1720

1821
namespace chesspp
1922
{
@@ -22,34 +25,51 @@ namespace chesspp
2225
class configuration
2326
{
2427
protected:
25-
std::string executable_path;
26-
std::string getExecutablePath()
28+
std::string res_path;
29+
30+
//Linux and Windows, resource path is defined as the absolute path the folder where the application executable is stored.
31+
// <exe_location>/res/img/... should be where resources are stored.
32+
//OS x, resource path is defined as the absolute path to the Resources folder of the .app structure.
33+
// <.app>/Contents/Resources/res/img... should be where resources are stored.
34+
std::string getResourcePath()
2735
{
2836
char buf[1024];
37+
uint32_t size = sizeof(buf);
2938
memset(buf, 0, sizeof(buf));
3039
std::string ret;
3140
#if defined(__linux__)
3241
if(readlink("/proc/self/exe", buf, sizeof(buf)) == -1)
3342
throw chesspp::exception("Unable to determine executable path on Linux.");
3443
ret = buf;
44+
ret = ret.substr(0, ret.find_last_of('/')+1);
3545

3646
#elif defined(_WIN32)
3747
if(GetModuleFileNameA(NULL, buf, sizeof(buf)) == 0)
3848
throw chesspp::exception("Unable to determine executable path on Windows.");
3949
ret = buf;
4050
boost::replace_all(ret, "\\", "/");
51+
ret = ret.substr(0, ret.find_last_of('/')+1);
52+
53+
#elif defined(__APPLE__)
54+
if (_NSGetExecutablePath(buf, &size) != 0)
55+
throw chesspp::exception("Unable to determine executable path on OS x. (Buffer size too small?)");
56+
ret = buf;
57+
ret = ret.substr(0, ret.find_last_of('/')+1) + "../Resources/";
58+
//Need to go up one directory because the exe is stored in <.app>/Contents/MacOS/,
59+
//And we need <.app>/Contents/Resources
4160

4261
#else
4362
throw chesspp::exception("Unknown OS. Unable to determine executable path.");
4463
#endif
4564

46-
return ret.substr(0, ret.find_last_of('/')+1);
65+
return ret;
4766
}
4867

4968
XMLReader reader;
5069
public:
51-
configuration(const std::string &configFile) : executable_path(getExecutablePath()), reader(executable_path + "/" + configFile) {}
70+
configuration(const std::string &configFile) : res_path(getResourcePath()), reader(getResourcePath() + configFile) {}
5271
virtual ~configuration() {}
72+
5373
};
5474

5575
class BoardConfig : public configuration
@@ -61,7 +81,7 @@ namespace chesspp
6181
public:
6282
BoardConfig() : configuration("config.xml")
6383
{
64-
initial_layout = reader.getProperty<std::string>("chesspp.data.board.initial_layout");
84+
initial_layout = res_path + reader.getProperty<std::string>("chesspp.data.board.initial_layout");
6585
board_width = reader.getProperty<uint8_t>("chesspp.data.board.width");
6686
board_height = reader.getProperty<uint8_t>("chesspp.data.board.height");
6787
cell_width = reader.getProperty<uint16_t>("chesspp.data.board.cell_width");
@@ -82,9 +102,9 @@ namespace chesspp
82102
public:
83103
GraphicsConfig() : configuration("config.xml")
84104
{
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");
105+
path_board = res_path + reader.getProperty<std::string>("chesspp.images.board");
106+
path_pieces = res_path + reader.getProperty<std::string>("chesspp.images.pieces");
107+
path_validMove = res_path + reader.getProperty<std::string>("chesspp.images.validMove");
88108
}
89109

90110
std::string getSpritePath_board() { return path_board; }

0 commit comments

Comments
 (0)