-
Notifications
You must be signed in to change notification settings - Fork 6
/
Compatibility.cpp
120 lines (86 loc) · 3.2 KB
/
Compatibility.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "Compatibility.h"
#include <cassert>
#include <iostream>
#include "GUIUtility.h"
using namespace std;
void nav_to_resources();
#ifdef _WIN32 // other possibilities are WIN32 _WIN32 or _WIN64
const char * MY_RESOURCES_FOLDER = "Resources";
void initGUI(){
GUI::initSDL(SDL_INIT_EVERYTHING);
// Open the stdout as an output file. (By default, SDL on windows supresses stdout)
FILE* fileout;
freopen_s(&fileout, "GUILog.txt", "w", stdout);
// Move the working directory to the resources folder.
nav_to_resources();
}
void nav_to_resources() {
char outbuf[100];
_getcwd(outbuf, 100);
cout << "current dir: " << outbuf << endl;
int chdir_success = _chdir(MY_RESOURCES_FOLDER);
assert(chdir_success == 0);
}
const char* getResourcePath() {
const size_t size = strlen(MY_RESOURCES_FOLDER);
char *str = new char[size];
strcpy_s(str, size, MY_RESOURCES_FOLDER);//(str, MY_RESOURCES_FOLDER);
return (const char*)str;
}
#elif __APPLE__ // Mac OSX
# include <CoreFoundation/CFBundle.h>
# include <string>
# include <iostream>
using namespace std;
// For App bundle created in Xcode
const char * BUNDLE_RESOURCES_FOLDER = "Contents/Resources";
// For Standalone executable compiled with Makefile.
const char * STANDALONE_RESOURCES_FOLDER = "Resources";
void initGUI(){
GUI::initSDL(SDL_INIT_EVERYTHING);
// Move the working directory to resources folder.
nav_to_resources();
}
const char* getResourcePath() {
// Get the name of the application bundle
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef url = CFBundleCopyBundleURL(mainBundle);
UInt8 bundlePath[PATH_MAX];
int chdir_success = (CFURLGetFileSystemRepresentation(url, true, bundlePath, sizeof(bundlePath)));
assert(chdir_success != 0);
cout << "BUNDLE PATH: " << (const char*)bundlePath << endl;
// Create the path to the resources folder
std::string path((const char*)bundlePath);
path.append("/");
path.append(BUNDLE_RESOURCES_FOLDER);
char *str = new char[path.length()+1];
strcpy(str, path.c_str());
return (const char*)str;
}
void nav_to_resources() {
// Get the name of the application bundle
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef url = CFBundleCopyBundleURL(mainBundle);
UInt8 bundlePath[PATH_MAX];
int success = (CFURLGetFileSystemRepresentation(url, true, bundlePath, sizeof(bundlePath)));
assert(success != 0);
cout << "BUNDLE PATH: " << (const char*)bundlePath << endl;
// Create the path to the resources folder
std::string path((const char*)bundlePath);
path.append("/");
path.append(BUNDLE_RESOURCES_FOLDER);
// chdir to the resources folder
success = chdir(path.c_str());
// If failed, try with standalone directory
if (!(success == 0)) {
std::string path((const char*)bundlePath);
path.append("/");
path.append(STANDALONE_RESOURCES_FOLDER);
// chdir to the resources folder
success = chdir(path.c_str());
assert(success == 0);
}
CFRelease(url);
}
#else // Linux?
#endif