forked from AmigurumiShaders/DX11Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
87 lines (72 loc) · 3.01 KB
/
Utils.cpp
File metadata and controls
87 lines (72 loc) · 3.01 KB
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
#include "Utils.h"
// --------------------------------------------------------------------------
// Gets the actual path to this executable
//
// - As it turns out, the relative path for a program is different when
// running through VS and when running the .exe directly, which makes
// it a pain to properly load external files (like textures)
// - Running through VS: Current Dir is the *project folder*
// - Running from .exe: Current Dir is the .exe's folder
// - This has nothing to do with DEBUG and RELEASE modes - it's purely a
// Visual Studio "thing", and isn't obvious unless you know to look
// for it. In fact, it could be fixed by changing a setting in VS, but
// the option is stored in a user file (.suo), which is ignored by most
// version control packages by default. Meaning: the option must be
// changed on every PC. Ugh. So instead, here's a helper.
// --------------------------------------------------------------------------
std::string Utils::GetExePath()
{
// Assume the path is just the "current directory" for now
std::string path = ".\\";
// Get the real, full path to this executable
char currentDir[1024] = {};
GetModuleFileName(0, currentDir, 1024);
// Find the location of the last slash charaacter
char* lastSlash = strrchr(currentDir, '\\');
if (lastSlash)
{
// End the string at the last slash character, essentially
// chopping off the exe's file name. Remember, c-strings
// are null-terminated, so putting a "zero" character in
// there simply denotes the end of the string.
*lastSlash = 0;
// Set the remainder as the path
path = currentDir;
}
// Toss back whatever we've found
return path;
}
// ---------------------------------------------------
// Same as GetExePath(), except it returns a wide character
// string, which most of the Windows API requires.
// ---------------------------------------------------
std::wstring Utils::GetExePath_Wide()
{
// Grab the path as a standard string
std::string path = GetExePath();
// Convert to a wide string
wchar_t widePath[1024] = {};
mbstowcs_s(0, widePath, path.c_str(), 1024);
// Create a wstring for it and return
return std::wstring(widePath);
}
// ----------------------------------------------------
// Gets the full path to a given file. NOTE: This does
// NOT "find" the file, it simply concatenates the given
// relative file path onto the executable's path
// ----------------------------------------------------
std::string Utils::GetFullPathTo(std::string relativeFilePath)
{
return GetExePath() + "\\" + relativeFilePath;
}
// ----------------------------------------------------
// Same as GetFullPathTo, but with wide char strings.
//
// Gets the full path to a given file. NOTE: This does
// NOT "find" the file, it simply concatenates the given
// relative file path onto the executable's path
// ----------------------------------------------------
std::wstring Utils::GetFullPathTo_Wide(std::wstring relativeFilePath)
{
return GetExePath_Wide() + L"\\" + relativeFilePath;
}