diff options
author | Stephen Enders <84310577289916ceefd4132143fb36b63a5f0c71> | 2022-06-22 01:28:23 -0400 |
---|---|---|
committer | Steph Enders <smenders@gmail.com> | 2022-06-21 22:28:30 -0400 |
commit | a878cdc42bf41355758dabd353d521d4b12e73fc (patch) | |
tree | 312acaf0cd8724b42e692fc26945f2992467ed19 /src | |
parent | 09615be926efb7302bc348aa66feccb694b23ba8 (diff) |
Add windows support
Create res.h for handling resource loading
Update CMakeLists.txt to ensure compilation is passed properly
Fix unsupported functions (<limits>) on Windows
Use to_str(path) to get path as c_str() returns wchar_t* on windows
Diffstat (limited to 'src')
-rw-r--r-- | src/MessageBox.h | 3 | ||||
-rw-r--r-- | src/linux/res.h | 4 | ||||
-rw-r--r-- | src/main.cpp | 16 | ||||
-rw-r--r-- | src/windows/res.h | 95 |
4 files changed, 110 insertions, 8 deletions
diff --git a/src/MessageBox.h b/src/MessageBox.h index 0e1fa8f..def138c 100644 --- a/src/MessageBox.h +++ b/src/MessageBox.h @@ -31,6 +31,7 @@ #include "SFML/Graphics/Text.hpp" #include "SfmlUtils.h" #include <vector> +#include <limits> const int LARGE_TEXT = 54; const int MEDIUM_TEXT = 36; @@ -57,7 +58,7 @@ inline MessageBox initializeMessageBox(const std::vector<DisplayText> &strs, sf::RectangleShape box; float width = 0.f; float height = 0.f; - float left = MAXFLOAT; + float left = std::numeric_limits<float>::max(); sf::Text prev; for (const auto &str : strs) { sf::Text text = write_text(str.msg.c_str(), str.textSize, LINE_HEIGHT, font, diff --git a/src/linux/res.h b/src/linux/res.h index 9e3b8c5..34ded54 100644 --- a/src/linux/res.h +++ b/src/linux/res.h @@ -42,6 +42,10 @@ struct Res { } typedef Res; +inline const char* to_str(std::filesystem::path file) { + return file.c_str(); +} + inline Res get_resources() { using namespace std::filesystem; diff --git a/src/main.cpp b/src/main.cpp index 99f8603..0034677 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,6 +32,9 @@ #ifdef __linux__ #include "linux/res.h" #endif +#ifdef _WIN32 +#include "windows/res.h" +#endif #include "lua.hpp" #include <SFML/Graphics.hpp> #include <cmath> @@ -55,14 +58,13 @@ int main(int argc, char **argv) { std::string lvl_pfx = argv[1]; - std::filesystem::path mapFile{lvl_pfx + "/dng.map"}; - std::filesystem::path luaFile{lvl_pfx + "/proc.lua"}; + std::filesystem::path mapFile = lvl_pfx / std::filesystem::path{"dng.map"}; + std::filesystem::path luaFile = lvl_pfx / std::filesystem::path{"proc.lua"}; lvl = std::make_shared<Level>(); scene = Scene::INTRO; - lvl->loadLevelFromFile(mapFile.c_str()); - + lvl->loadLevelFromFile(to_str(mapFile)); lua_State *L_lvl = luaL_newstate(); luaL_openlibs(L_lvl); init_c_api(L_lvl); @@ -72,7 +74,7 @@ int main(int argc, char **argv) { init_c_api(L_default); if (std::filesystem::exists(res.defaultsFile) && - luaL_dofile(L_default, res.defaultsFile.c_str()) != LUA_OK) { + luaL_dofile(L_default, to_str(res.defaultsFile)) != LUA_OK) { std::cout << "Failed to load default proc" << std::endl; luaL_error(L_default, "Error: %s", lua_tostring(L_default, -1)); return EXIT_FAILURE; @@ -82,7 +84,7 @@ int main(int argc, char **argv) { LState *l_state = init_default(L_default); if (std::filesystem::exists(luaFile) && - luaL_dofile(L_lvl, luaFile.c_str()) == LUA_OK) { + luaL_dofile(L_lvl, to_str(luaFile)) == LUA_OK) { override_file_fns(L_lvl, l_state); } else if (std::filesystem::exists(luaFile)) { std::cout << "[C] No Good" << std::endl; @@ -124,7 +126,7 @@ int main(int argc, char **argv) { window.setView(view); sf::Clock deltaClock; sf::Font font; - font.loadFromFile(res.fontFile); + font.loadFromFile(to_str(res.fontFile)); MessageBox intro; MessageBox win; diff --git a/src/windows/res.h b/src/windows/res.h new file mode 100644 index 0000000..06b3209 --- /dev/null +++ b/src/windows/res.h @@ -0,0 +1,95 @@ +//======================================================================== +// dng +//------------------------------------------------------------------------ +// Copyright (c) 2022 Steph Enders <steph@senders.io> +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would +// be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, and must not +// be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source +// distribution. +// +//======================================================================== + +#ifndef DNG_RES_H +#define DNG_RES_H + +#include <filesystem> +#include <libloaderapi.h> +#define MAX_BUF_SIZE 1024 +const std::filesystem::path DEFAULT_PROC{R"(dnglib\defaults.lua)"}; +const std::filesystem::path DEFAULT_FONT{R"(res\PressStart2P-vaV7.ttf)"}; +// TODO setup to allow switching to monospace instead of game font +// const std::filesystem::path DEFAULT_MONOSPACE_FONT{ +// "res/LiberationMono-Regular.ttf"}; + +struct Res { + + std::filesystem::path defaultsFile; + std::filesystem::path fontFile; + +} typedef Res; + +inline const char *to_str(const std::filesystem::path &file) { + std::setlocale(LC_ALL, "en_US.utf8"); // TODO more support? + const wchar_t *wstr = file.c_str(); + size_t len = std::wcslen(wstr) + 1; // gotta get that \0 + char *ret = static_cast<char *>(malloc(sizeof(char) * len)); // buffered + std::wcstombs(ret, file.c_str(), len); + return ret; +} + +inline Res get_resources() { + using namespace std::filesystem; + + auto current_dir = current_path(); + char exe_dir_str[255]; + GetModuleFileNameA(nullptr, exe_dir_str, 255); + auto exe_dir = path(exe_dir_str).remove_filename(); + // TODO - Get from PATH or Reg? + auto install_dir = path{R"(C:\Program\ Files\ (x86)\dng)"}; + + Res res; + if (exists(current_dir / DEFAULT_PROC)) { + res.defaultsFile = current_dir / DEFAULT_PROC; + } else if (exists(exe_dir / DEFAULT_PROC)) { + res.defaultsFile = exe_dir / DEFAULT_PROC; + } else if (exists(install_dir / DEFAULT_PROC)) { + res.defaultsFile = install_dir / DEFAULT_PROC; + } else { + res.defaultsFile = DEFAULT_PROC; // just return w/ no path info + } + // TODO make configurable + path fontp = DEFAULT_FONT; + if (exists(current_dir / fontp)) { + res.fontFile = current_dir / fontp; + } else if (exists(exe_dir / fontp)) { + res.fontFile = exe_dir / fontp; + } else if (exists(install_dir / fontp)) { + res.fontFile = install_dir / fontp; + } else { + res.fontFile = fontp; // just return w/ no path info + } + + return res; +} + +#endif // DNG_RES_H + +#ifndef DNG_RES_H +#define DNG_RES_H + +#endif // DNG_RES_H |