forked from Twinklebear/TwinklebearDev-Lessons
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathres_path.h
44 lines (40 loc) · 987 Bytes
/
res_path.h
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
#ifndef RES_PATH_H
#define RES_PATH_H
#include <iostream>
#include <string>
#include <SDL2/SDL.h>
/*
* Get the resource path for resources located in res/subDir
* It's assumed the project directory is structured like:
* bin/
* the executable
* res/
* Lesson1/
* Lesson2/
*
* Paths returned will be Project_Root/res/subDir
*/
std::string getResourcePath(const std::string &subDir = ""){
#ifdef _WIN32
const char PATH_SEP = '\\';
#else
const char PATH_SEP = '/';
#endif
static std::string baseRes;
if (baseRes.empty()){
char *basePath = SDL_GetBasePath();
if (basePath){
baseRes = basePath;
SDL_free(basePath);
}
else {
std::cerr << "Error getting resource path: " << SDL_GetError() << std::endl;
return "";
}
//We replace the last bin/ with res/ to get the the resource path
size_t pos = baseRes.rfind("bin");
baseRes = baseRes.substr(0, pos) + "res" + PATH_SEP;
}
return subDir.empty() ? baseRes : baseRes + subDir + PATH_SEP;
}
#endif