From a878cdc42bf41355758dabd353d521d4b12e73fc Mon Sep 17 00:00:00 2001 From: Stephen Enders <84310577289916ceefd4132143fb36b63a5f0c71> Date: Wed, 22 Jun 2022 01:28:23 -0400 Subject: Add windows support Create res.h for handling resource loading Update CMakeLists.txt to ensure compilation is passed properly Fix unsupported functions () on Windows Use to_str(path) to get path as c_str() returns wchar_t* on windows --- CMakeLists.txt | 10 ++++-- src/MessageBox.h | 3 +- src/linux/res.h | 4 +++ src/main.cpp | 16 ++++++---- src/windows/res.h | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 117 insertions(+), 11 deletions(-) create mode 100644 src/windows/res.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 69cd9b2..c1902a0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ project(dng VERSION 0.1.0) set(CMAKE_CXX_STANDARD 20) set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-${PROJECT_VERSION}) +set(CMAKE_SUPPRESS_DEVELOPER_WARNINGS ON) # Setup Lua Libraries # To determine which files look at the lua/makefile to see how the targets are built @@ -42,7 +43,9 @@ add_library(lualib STATIC ${LUA_SRC}/lstrlib.c ${LUA_SRC}/ltablib.c ${LUA_SRC}/lutf8lib.c) -target_compile_definitions(lualib PRIVATE -DLUA_USE_POSIX=1) # to properly use mkstemp +if(UNIX) + target_compile_definitions(lualib PRIVATE -DLUA_USE_POSIX=1) # to properly use mkstemp +endif() target_include_directories(lua PUBLIC lua) target_include_directories(lauxlib PUBLIC lua) target_include_directories(lualib PUBLIC lua) @@ -53,7 +56,9 @@ include_directories(${PROJECT_SOURCE_DIR}/thirdparty/lua) # System specific includes if (UNIX) set(RES_LIB src/linux/res.h) -endif () +elseif(WIN32) + set(RES_LIB src/windows/res.h) +endif() add_executable(${PROJECT_NAME} src/main.cpp src/Level.cpp src/Level.h src/CApi.h src/Scene.h src/LuaApi.h src/MessageBox.h ${RES_LIB}) target_link_libraries(${PROJECT_NAME} PUBLIC ${LUA_LIBRARIES}) @@ -65,7 +70,6 @@ if (WIN32) set(SFML_USE_STATIC_STD_LIBS TRUE) endif () set(SFML_USE_SYSTEM_DEPS ON) -set(CMAKE_SUPPRESS_DEVELOPER_WARNINGS ON) add_subdirectory(${PROJECT_SOURCE_DIR}/thirdparty/SFML EXCLUDE_FROM_ALL) target_link_libraries(${PROJECT_NAME} PUBLIC ${SFML_LIBRARIES} ${LUA_LIBRARIES}) 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 +#include const int LARGE_TEXT = 54; const int MEDIUM_TEXT = 36; @@ -57,7 +58,7 @@ inline MessageBox initializeMessageBox(const std::vector &strs, sf::RectangleShape box; float width = 0.f; float height = 0.f; - float left = MAXFLOAT; + float left = std::numeric_limits::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 #include @@ -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(); 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 +// +// 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 +#include +#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(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 -- cgit v1.2.3-54-g00ecf