This repository was archived by the owner on Mar 11, 2025. It is now read-only.
File tree 4 files changed +64
-18
lines changed
4 files changed +64
-18
lines changed Original file line number Diff line number Diff line change 2
2
#include < Tkge/Engine.hpp>
3
3
#include < Tkge/Graphics/Drawable.hpp>
4
4
#include < Tkge/Graphics/Shader.hpp>
5
+ #include < Tkge/Utilities.hpp>
5
6
#include < klib/assert.hpp>
6
7
#include < kvf/time.hpp>
7
8
#include < cmath>
@@ -107,12 +108,12 @@ namespace
107
108
}
108
109
} // namespace
109
110
110
- int main ([[maybe_unused]] int argc, char ** argv)
111
+ int main ([[maybe_unused]] int argc, [[maybe_unused]] char ** argv)
111
112
{
112
113
try
113
114
{
114
115
KLIB_ASSERT (argc > 0 );
115
- const auto assets_path = Upfind (*argv , " Assets" );
116
+ const auto assets_path = Upfind (Tkge::Utilities::GetCurrentExecutablePath () , " Assets" );
116
117
Run (assets_path);
117
118
}
118
119
catch (const std::exception & e)
Original file line number Diff line number Diff line change
1
+ #pragma once
2
+
3
+ #include < filesystem>
4
+ #include < string>
5
+
6
+ namespace Tkge
7
+ {
8
+ class Utilities
9
+ {
10
+ public:
11
+ ~Utilities () = delete ;
12
+ Utilities () = delete ;
13
+ Utilities (const Utilities&) = delete ;
14
+ Utilities (Utilities&) = delete ;
15
+ Utilities& operator =(const Utilities&) = delete ;
16
+ Utilities& operator =(Utilities&) = delete ;
17
+
18
+ // / <summary>
19
+ // / Gets the current executable directory.
20
+ // / </summary>
21
+ // / <returns>The parent directory of the main module</returns>
22
+ [[nodiscard]] static std::filesystem::path GetCurrentExecutablePath ();
23
+ };
24
+ } // namespace Tkge
Original file line number Diff line number Diff line change 1
1
#include < Tkge/AssetLoader.hpp>
2
+ #include < Tkge/Utilities.hpp>
2
3
#include < filesystem>
3
4
4
- #ifdef _WIN32
5
- #ifndef WIN32_MEAN_AND_LEAN
6
- #define WIN32_MEAN_AND_LEAN
7
- #define _AMD64_
8
- #endif
9
- #include < libloaderapi.h>
10
- #include < minwindef.h>
11
- #endif
12
-
13
5
std::vector<std::filesystem::path> Tkge::AssetLoader::GetSearchPaths () const
14
6
{
15
7
std::vector<std::filesystem::path> paths{};
16
8
17
9
paths.emplace_back (" ." );
18
10
19
- #ifdef _WIN32
20
- char buffer[MAX_PATH]{};
21
- GetModuleFileNameA (nullptr , buffer, MAX_PATH);
22
-
23
- paths.push_back (std::filesystem::path{buffer}.parent_path ());
11
+ paths.push_back (Tkge::Utilities::GetCurrentExecutablePath ());
24
12
paths.push_back (paths.back () / " Assets" );
25
- #else
26
- #endif
27
13
28
14
return paths;
29
15
}
Original file line number Diff line number Diff line change
1
+ #include < Tkge/Utilities.hpp>
2
+
3
+ #ifdef _WIN32
4
+ #ifndef WIN32_MEAN_AND_LEAN
5
+ #define WIN32_MEAN_AND_LEAN
6
+ #define _AMD64_
7
+ #endif
8
+ #include < libloaderapi.h>
9
+ #include < minwindef.h>
10
+ #else // defined(_WIN32)
11
+ #include < limits.h>
12
+ #include < unistd.h>
13
+ #endif
14
+
15
+ std::filesystem::path Tkge::Utilities::GetCurrentExecutablePath ()
16
+ {
17
+ static std::filesystem::path CachePath{}; // can never change throughout the process existance
18
+ if (!CachePath.empty ()) return CachePath;
19
+
20
+ #ifdef _WIN32
21
+ char buffer[MAX_PATH]{};
22
+ DWORD length = GetModuleFileNameA (nullptr , buffer, MAX_PATH);
23
+ if (length == 0 ) return {}; // Error case
24
+ CachePath = std::filesystem::path{std::string (buffer, length)}.parent_path ();
25
+ #elif defined(__linux__)
26
+ char buffer[PATH_MAX]{};
27
+ ssize_t length = readlink (" /proc/self/exe" , buffer, PATH_MAX);
28
+ if (length == -1 ) return {}; // Error case
29
+ CachePath = std::filesystem::path{std::string (buffer, static_cast <std::size_t >(length))}.parent_path ();
30
+ #else
31
+ static_assert (false , " Unsupported platform" );
32
+ #endif
33
+
34
+ return CachePath;
35
+ }
You can’t perform that action at this time.
0 commit comments