From de7cc1f12273ae80f4d972b0dbfc48066f851684 Mon Sep 17 00:00:00 2001 From: Stephen Enders <84310577289916ceefd4132143fb36b63a5f0c71> Date: Fri, 24 Jun 2022 21:35:03 -0400 Subject: Fix compilation for Windows I left a few changes undone in the Windows API. This should be cross platform now --- src/main.cpp | 11 ++++--- src/resources/Resources.cpp | 5 +++ src/resources/Resources.h | 1 + src/resources/windows/WindowsResources.cpp | 52 ++++++++++++++++-------------- src/resources/windows/WindowsResources.h | 4 +++ 5 files changed, 44 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index 7de3c11..4ebb8e6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -52,20 +52,21 @@ int main(int argc, char **argv) { #ifdef __linux__ Resources *res = new LinuxResources(); - res->loadDefaultLuaFile(); - res->loadFontFiles(); - res->loadLevels(); #endif // __linux__ #ifdef _WIN32 Resources *res = new WindowsResources(); #endif + res->loadDefaultLuaFile(); + res->loadFontFiles(); + res->loadLevels(); int levelIndex = 0; std::cout << "Select level from list: " << std::endl; int i = 0; for (auto &p : res->getLevels()) { - std::cout << "\t[" << i++ << "] " << p.parent_path().filename().c_str() - << "/" << p.filename().c_str() << std::endl; + std::cout << "\t[" << i++ << "] " + << p.parent_path().filename().generic_string() << "/" + << p.filename().generic_string() << std::endl; } std::cout << "Enter Number: "; std::cin >> levelIndex; diff --git a/src/resources/Resources.cpp b/src/resources/Resources.cpp index 11f762f..3ae93f4 100644 --- a/src/resources/Resources.cpp +++ b/src/resources/Resources.cpp @@ -29,6 +29,11 @@ #include #include +Resources::Resources() { + this->font = std::make_shared(); + this->defaultsLua = std::make_shared(); +} + void Resources::loadFontFiles() { // We will search 1 level deep for (auto &base : this->fontSearchDirs()) { diff --git a/src/resources/Resources.h b/src/resources/Resources.h index 194c031..6e1a95f 100644 --- a/src/resources/Resources.h +++ b/src/resources/Resources.h @@ -56,6 +56,7 @@ protected: virtual vector fontSearchDirs() = 0; public: + Resources(); void loadLevels(); void loadFontFiles(); void loadDefaultLuaFile(); diff --git a/src/resources/windows/WindowsResources.cpp b/src/resources/windows/WindowsResources.cpp index cfe264f..4ef0e2e 100644 --- a/src/resources/windows/WindowsResources.cpp +++ b/src/resources/windows/WindowsResources.cpp @@ -23,32 +23,36 @@ // distribution. // //======================================================================== -#include "../Resources.h" +#include "WindowsResources.h" #include #define MAX_BUF_SIZE 1024 -class WindowsResources : public Resources { +std::filesystem::path exeDir; +std::filesystem::path workingDir; -protected: - std::filesystem::path exeDir; - std::filesystem::path workingDir; +std::vector WindowsResources::levelSearchDirs() { + return {workingDir / "maps", exeDir / "maps"}; +} +std::vector WindowsResources::defaultsSearchDirs() { + return {workingDir / "dnglib", exeDir / "dnglib"}; +} +std::vector WindowsResources::fontSearchDirs() { + return {workingDir / "res", exeDir / "res"}; +} +WindowsResources::WindowsResources() : Resources() { + this->workingDir = std::filesystem::current_path(); + this->font = std::make_shared(workingDir / "res" / + DEFAULT_FONT); - std::vector levelSearchDirs() override { - return {workingDir / "maps", exeDir / "maps"}; - } - std::vector defaultsSearchDirs() override { - return {workingDir / "dnglib", exeDir / "dnglib"}; - } - std::vector fontSearchDirs() override { - return {workingDir / "res", exeDir / "res"}; - } + char exe_dir_str[255]; + GetModuleFileNameA(nullptr, exe_dir_str, 255); + this->exeDir = std::filesystem::path{exe_dir_str}.remove_filename(); +} -public: - const char *convert_to_str(std::filesystem::path &path) override { - std::setlocale(LC_ALL, "en_US.utf8"); // TODO more support? - const wchar_t *wstr = path.c_str(); - size_t len = std::wcslen(wstr) + 1; // gotta get that \0 - char *ret = static_cast(malloc(sizeof(char) * len)); // buffered - std::wcstombs(ret, path.c_str(), len); - return ret; - } -}; \ No newline at end of file +const char *WindowsResources::convert_to_str(std::filesystem::path &path) { + std::setlocale(LC_ALL, "en_US.utf8"); // TODO more support? + const wchar_t *wstr = path.c_str(); + size_t len = std::wcslen(wstr) + 1; // gotta get that \0 + char *ret = static_cast(malloc(sizeof(char) * len)); // buffered + std::wcstombs(ret, path.c_str(), len); + return ret; +} diff --git a/src/resources/windows/WindowsResources.h b/src/resources/windows/WindowsResources.h index b6a1cd6..5ca662b 100644 --- a/src/resources/windows/WindowsResources.h +++ b/src/resources/windows/WindowsResources.h @@ -29,6 +29,10 @@ #include "../Resources.h" class WindowsResources : public Resources { +public: + WindowsResources(); + const char *convert_to_str(std::filesystem::path &path) override; + protected: std::filesystem::path exeDir; std::filesystem::path workingDir; -- cgit v1.2.3-54-g00ecf