From ab37629c6e4798654fca1d533a611da7986b5053 Mon Sep 17 00:00:00 2001 From: Steph Enders Date: Fri, 24 Jun 2022 17:10:50 -0400 Subject: Select level via command line (linux tested) --- CMakeLists.txt | 21 ++--- README.md | 9 +- src/Level.cpp | 63 +++---------- src/Level.h | 27 +++--- src/linux/res.h | 80 ----------------- src/main.cpp | 60 ++++++++----- src/resources/Config.h | 47 ++++++++++ src/resources/Resources.cpp | 136 +++++++++++++++++++++++++++++ src/resources/Resources.h | 76 ++++++++++++++++ src/resources/linux/LinuxResources.cpp | 51 +++++++++++ src/resources/linux/LinuxResources.h | 45 ++++++++++ src/resources/windows/WindowsResources.cpp | 54 ++++++++++++ src/resources/windows/WindowsResources.h | 40 +++++++++ src/windows/res.h | 94 -------------------- 14 files changed, 525 insertions(+), 278 deletions(-) delete mode 100644 src/linux/res.h create mode 100644 src/resources/Config.h create mode 100644 src/resources/Resources.cpp create mode 100644 src/resources/Resources.h create mode 100644 src/resources/linux/LinuxResources.cpp create mode 100644 src/resources/linux/LinuxResources.h create mode 100644 src/resources/windows/WindowsResources.cpp create mode 100644 src/resources/windows/WindowsResources.h delete mode 100644 src/windows/res.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 154a49c..a0a0cd9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ project(dng set(CMAKE_CXX_STANDARD 20) if (WIN32) set(CMAKE_CXX_FLAGS -static) -endif() +endif () set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-${PROJECT_VERSION}) set(CMAKE_SUPPRESS_DEVELOPER_WARNINGS ON) @@ -46,9 +46,9 @@ add_library(lualib STATIC ${LUA_SRC}/lstrlib.c ${LUA_SRC}/ltablib.c ${LUA_SRC}/lutf8lib.c) -if(UNIX) +if (UNIX) target_compile_definitions(lualib PRIVATE -DLUA_USE_POSIX=1) # to properly use mkstemp -endif() +endif () target_include_directories(lua PUBLIC lua) target_include_directories(lauxlib PUBLIC lua) target_include_directories(lualib PUBLIC lua) @@ -58,10 +58,10 @@ include_directories(${PROJECT_SOURCE_DIR}/thirdparty/lua) # System specific includes if (UNIX) - set(RES_LIB src/linux/res.h) -elseif(WIN32) - set(RES_LIB src/windows/res.h) -endif() + set(RES_LIB src/resources/Resources.cpp src/resources/Resources.h src/resources/linux/LinuxResources.cpp src/resources/linux/LinuxResources.h) +elseif (WIN32) + set(RES_LIB src/resources/Resources.cpp src/resources/Resources.h src/resources/windows/WindowsResources.cpp src/resources/windows/WindowsResources.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}) @@ -70,6 +70,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC ${LUA_LIBRARIES}) set(BUILD_SHARED_LIBS FALSE) set(SFML_LIBRARIES sfml-system sfml-window sfml-graphics) if (WIN32) + set(SFML_STATIC TRUE) set(SFML_USE_STATIC_STD_LIBS TRUE) endif () set(SFML_USE_SYSTEM_DEPS ON) @@ -94,12 +95,12 @@ install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/thirdparty-licenses.txt DES # Package Directives if (WIN32) set(CPACK_GEN ZIP) -else() +else () set(CPACK_GEN TGZ) -endif() +endif () set(CPACK_GENERATOR ${CPACK_GEN}) set(CPACK_SOURCE_GENERATOR ${CPACK_GEN}) set(CPACK_RESOURCE_FILE_README ${CMAKE_CURRENT_SOURCE_DIR}/README.md) set(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_CURRENT_SOURCE_DIR}/LICENSE) -include(CPack) \ No newline at end of file +include(CPack) diff --git a/README.md b/README.md index 72c7c9f..a56dc17 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,14 @@ treasure! ## How to run -You can run the game by `dng path/to/map/lvl` +To run the game you can just launch `dng` or `dng.exe` on windows. -To run the first level: +Follow the onscreen prompts to load a level and play! -`dng maps/lvl1` +### Configurations + +By default the game will look for its configuration file in a few OS specific places. +The default config is bundled along side the executable: `config.toml` ## Create your own level! diff --git a/src/Level.cpp b/src/Level.cpp index 6db4b4e..5c8fbe0 100644 --- a/src/Level.cpp +++ b/src/Level.cpp @@ -27,17 +27,20 @@ #include "Level.h" #include "SfmlUtils.h" #include -#include #include -bool canStep(Pos pos, int dx, int dy, std::vector> map) { +bool canStep(const Pos &pos, int dx, int dy, + std::vector> map) { return map[pos.y + dy][pos.x + dx] != WALL_SPACE; } -void Level::loadLevelFromFile(const char *filePath) { - std::ifstream mapFile(filePath); +Level::Level(const char *filePath) { + this->file = std::make_unique(filePath); +} + +void Level::load() { + std::ifstream mapFile(this->file->c_str()); if (mapFile.is_open()) { - this->file = filePath; // each element in the map has a unique ID // some magic but player is always 0 const int playerId = 0; @@ -96,53 +99,13 @@ void Level::reset() { this->treasurePositions.clear(); this->displayMap.clear(); this->enemyPositions.clear(); - this->loadLevelFromFile(this->file); + this->load(); } -bool Level::isEmpty(int x, int y) { return map[y][x] == BLANK_SPACE; } - -bool Level::playerCanStep(int dx, int dy) { +bool Level::playerCanStep(int dx, int dy) const { return canStep(player, dx, dy, map); } -void Level::print() { - int x = 0; - int y = 0; - for (auto &row : map) { - for (auto &tile : row) { - bool printed = false; - if (player.x == x && player.y == y) { - std::cout << "p"; - printed = true; - } - for (auto pos : enemyPositions) { - if (pos.x == x && pos.y == y) { - std::cout << "e"; - printed = true; - } - } - for (auto pos : treasurePositions) { - if (pos.x == x && pos.y == y) { - std::cout << "t"; - printed = true; - } - } - if (tile == WALL_SPACE) { - std::cout << tile; - printed = true; - } - if (!printed) { - std::cout << " "; - } - std::cout << " "; - ++x; - } - std::cout << "\n"; - ++y; - x = 0; - } -} - int Level::nextId() { return idCounter++; } int Level::getEnemyIndex(int id) { @@ -154,8 +117,8 @@ int Level::getEnemyIndex(int id) { return -1; } -bool Level::enemyCanStep(Pos pos, int dx, int dy) { +bool Level::enemyCanStep(const Pos &pos, int dx, int dy) const { return canStep(pos, dx, dy, map); } -int Level::getWidth() { return this->width; } -int Level::getHeight() { return this->height; } +int Level::getWidth() const { return this->width; } +int Level::getHeight() const { return this->height; } diff --git a/src/Level.h b/src/Level.h index a0b571e..7b25d0b 100644 --- a/src/Level.h +++ b/src/Level.h @@ -50,23 +50,16 @@ struct Pos { class Level { public: - void loadLevelFromFile(const char *filePath); - - bool isEmpty(int x, int y); - - bool playerCanStep(int dx, int dy); - + explicit Level(const char *filePath); + ~Level() = default; + void load(); + bool playerCanStep(int dx, int dy) const; int getEnemyIndex(int id); - - bool enemyCanStep(Pos pos, int dx, int dy); - + bool enemyCanStep(const Pos &pos, int dx, int dy) const; void reset(); - /* deprecate*/ - void print(); - int nextId(); - int getWidth(); - int getHeight(); + int getWidth() const; + int getHeight() const; std::vector> map; // source copy of map std::vector displayMap; @@ -76,9 +69,9 @@ public: private: int idCounter = 1; // defaults at 1 (player always 0) - int width; - int height; - const char *file; + int width{}; + int height{}; + std::unique_ptr file; }; #endif // DNG_LEVEL_H diff --git a/src/linux/res.h b/src/linux/res.h deleted file mode 100644 index 3c61d1a..0000000 --- a/src/linux/res.h +++ /dev/null @@ -1,80 +0,0 @@ -//======================================================================== -// 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 - -const std::filesystem::path DEFAULT_PROC{"dnglib/defaults.lua"}; -const std::filesystem::path DEFAULT_FONT{"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; -}; - -inline const char *to_str(std::filesystem::path file) { return file.c_str(); } - -inline Res get_resources() { - using namespace std::filesystem; - - auto current_dir = current_path(); - auto exe_dir = canonical("/proc/self/exe").remove_filename(); - auto install_dir = path{"/usr/local/share/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; -} - -inline Res discover_levels() {} - -#endif // DNG_RES_H \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 0034677..7de3c11 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,13 +29,14 @@ #include "LuaApi.h" #include "MessageBox.h" #include "SfmlUtils.h" +#include "lua.hpp" +#include "resources/Resources.h" #ifdef __linux__ -#include "linux/res.h" -#endif +#include "resources/linux/LinuxResources.h" +#endif // __linux__ #ifdef _WIN32 -#include "windows/res.h" -#endif -#include "lua.hpp" +#include "resources/windows/WindowsResources.h" +#endif // _WIN32 #include #include #include @@ -49,22 +50,31 @@ const int MAX_HEIGHT = static_cast(SPRITE_SIZE) * 20 * 3; int main(int argc, char **argv) { - if (argc <= 1) { - std::cerr << "Must pass in path to level directory" << std::endl; - return -1; - } - - auto res = get_resources(); - - std::string lvl_pfx = argv[1]; +#ifdef __linux__ + Resources *res = new LinuxResources(); + res->loadDefaultLuaFile(); + res->loadFontFiles(); + res->loadLevels(); +#endif // __linux__ +#ifdef _WIN32 + Resources *res = new WindowsResources(); +#endif - std::filesystem::path mapFile = lvl_pfx / std::filesystem::path{"dng.map"}; - std::filesystem::path luaFile = lvl_pfx / std::filesystem::path{"proc.lua"}; + 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 << "Enter Number: "; + std::cin >> levelIndex; - lvl = std::make_shared(); + lvl = std::make_shared( + res->convert_to_str(*res->getLevelMap(levelIndex))); scene = Scene::INTRO; - lvl->loadLevelFromFile(to_str(mapFile)); + lvl->load(); lua_State *L_lvl = luaL_newstate(); luaL_openlibs(L_lvl); init_c_api(L_lvl); @@ -73,8 +83,8 @@ int main(int argc, char **argv) { luaL_openlibs(L_default); init_c_api(L_default); - if (std::filesystem::exists(res.defaultsFile) && - luaL_dofile(L_default, to_str(res.defaultsFile)) != LUA_OK) { + if (luaL_dofile(L_default, res->convert_to_str(*res->getDefaultsLuaFile())) != + 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; @@ -83,10 +93,12 @@ int main(int argc, char **argv) { // Initialize to default LState *l_state = init_default(L_default); - if (std::filesystem::exists(luaFile) && - luaL_dofile(L_lvl, to_str(luaFile)) == LUA_OK) { + if (res->getLevelProcLua(levelIndex).has_value() && + luaL_dofile(L_lvl, res->convert_to_str( + *res->getLevelProcLua(levelIndex).value())) == + LUA_OK) { override_file_fns(L_lvl, l_state); - } else if (std::filesystem::exists(luaFile)) { + } else if (res->getLevelProcLua(levelIndex).has_value()) { std::cout << "[C] No Good" << std::endl; luaL_error(L_lvl, "Error: %s\n", lua_tostring(L_lvl, -1)); return EXIT_FAILURE; @@ -126,7 +138,8 @@ int main(int argc, char **argv) { window.setView(view); sf::Clock deltaClock; sf::Font font; - font.loadFromFile(to_str(res.fontFile)); + const char *ff = res->convert_to_str(*res->getFontFile()); + font.loadFromFile(ff); MessageBox intro; MessageBox win; @@ -156,7 +169,6 @@ int main(int argc, char **argv) { } } else if (scene == Scene::WIN) { lua_onwin(l_state->onwin); - // window.close(); } else if (scene == Scene::LOSS) { lua_onloss(l_state->onloss); } diff --git a/src/resources/Config.h b/src/resources/Config.h new file mode 100644 index 0000000..b67ca0d --- /dev/null +++ b/src/resources/Config.h @@ -0,0 +1,47 @@ +//======================================================================== +// 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_CONFIG_H +#define DNG_CONFIG_H + +#include +#include + +/** + * Simple config struct mapping to the default locations + * Map just include directly in Resources + */ +struct Config { + bool fixed_resolution; + // configured as WidthxHeight + unsigned int fixed_width; + unsigned int fixed_height; + std::filesystem::path defaultsDir; + std::vector fonts; + std::vector levels; +}; + +#endif // DNG_CONFIG_H diff --git a/src/resources/Resources.cpp b/src/resources/Resources.cpp new file mode 100644 index 0000000..11f762f --- /dev/null +++ b/src/resources/Resources.cpp @@ -0,0 +1,136 @@ +//======================================================================== +// 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. +// +//======================================================================== + +#include "Resources.h" +#include +#include +#include + +void Resources::loadFontFiles() { + // We will search 1 level deep + for (auto &base : this->fontSearchDirs()) { + if (std::filesystem::exists(base) && std::filesystem::is_directory(base)) { + for (auto &item : std::filesystem::directory_iterator(base)) { + // only 1 deep + if (item.exists() && item.is_directory()) { + for (auto &file : std::filesystem::directory_iterator(item)) { + if (file.exists() && file.is_regular_file()) { + auto ext = file.path().extension(); + bool matched = + std::any_of(std::begin(FONT_TYPES), std::end(FONT_TYPES), + [ext](auto &supported) { + return supported == ext.generic_string(); + }); + if (matched) { + fonts.push_back(file.path()); + // set default if not set + if (!exists(*this->font) || + !is_regular_file(*this->font) && + file.path().filename() == DEFAULT_FONT) { + *this->font = file.path(); + } + } + } + } + } else if (item.is_regular_file()) { + auto ext = item.path().extension(); + bool matched = + std::any_of(std::begin(FONT_TYPES), std::end(FONT_TYPES), + [ext](auto &supported) { + return supported == ext.generic_string(); + }); + if (matched) { + fonts.push_back(item.path()); + // set default if not set + if (!exists(*this->font) || + !is_regular_file(*this->font) && + item.path().filename() == DEFAULT_FONT) { + *this->font = item.path(); + } + } + } + } + } + } +} + +void Resources::loadLevels() { + for (auto &base : this->levelSearchDirs()) { + if (std::filesystem::exists(base) && std::filesystem::is_directory(base)) { + for (auto &dir : std::filesystem::directory_iterator(base)) { + if (dir.exists() && dir.is_directory()) { + if (std::filesystem::exists(dir / DNG_MAP)) { + this->levels.push_back(dir.path()); + } + } + } + } + } + std::sort(this->levels.begin(), this->levels.end()); +} +void Resources::loadDefaultLuaFile() { + for (auto &base : this->defaultsSearchDirs()) { + auto f = base / DEFAULT_LUA; + if (std::filesystem::exists(f)) { + *this->defaultsLua = f; + break; + } + } +} +std::vector Resources::getFontFiles() { + return this->fonts; +} +std::vector Resources::getLevels() { + return this->levels; +} +shared_ptr Resources::getFontFile() { + return this->font; +} +shared_ptr Resources::updateFont(int idx) { + auto f = this->fonts[idx]; + *this->font = f; + return getFontFile(); +} +shared_ptr Resources::getDefaultsLuaFile() { + return this->defaultsLua; +} +shared_ptr Resources::getLevelMap(int idx) { + auto lvlBase = this->levels[idx]; + std::filesystem::path dngMap = lvlBase / DNG_MAP; + // existence of the level dng.map is asserted in the initializer + assert(std::filesystem::exists(dngMap)); + return std::make_shared(dngMap); +} +std::optional> +Resources::getLevelProcLua(int idx) { + auto lvlBase = this->levels[idx]; + auto procLua = lvlBase / PROC_LUA; + if (exists(procLua)) { + return std::make_shared(procLua); + } else { + return nullopt; + } +} \ No newline at end of file diff --git a/src/resources/Resources.h b/src/resources/Resources.h new file mode 100644 index 0000000..194c031 --- /dev/null +++ b/src/resources/Resources.h @@ -0,0 +1,76 @@ +//======================================================================== +// 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_RESOURCES_H +#define DNG_RESOURCES_H + +#include +#include +#include + +// struct LevelInfo { +// filesystem::path dir; +// filesystem::path dngMap; +// optional procLua; +// }; +using namespace std; + +static const string FONT_TYPES[] = {".otf", ".ttf"}; +static const filesystem::path DEFAULT_LUA{"defaults.lua"}; +static const filesystem::path DNG_MAP{"dng.map"}; +static const filesystem::path PROC_LUA{"proc.lua"}; +static const char *DEFAULT_FONT = "PressStart2P-vaV7.ttf"; + +class Resources { + +protected: + shared_ptr font; // chosen font + shared_ptr defaultsLua; // defaults lua file + vector fonts; // all found maps + vector levels; // all found maps + virtual vector levelSearchDirs() = 0; + virtual vector defaultsSearchDirs() = 0; + virtual vector fontSearchDirs() = 0; + +public: + void loadLevels(); + void loadFontFiles(); + void loadDefaultLuaFile(); + vector getFontFiles(); + vector getLevels(); + shared_ptr updateFont(int idx); + shared_ptr getFontFile(); + shared_ptr getDefaultsLuaFile(); + shared_ptr getLevelMap(int idx); + optional> getLevelProcLua(int idx); + /* Helper method to convert any path to a const char * + * Windows uses wchar_t so this can help provide a common way to + * interact with files + */ + virtual const char *convert_to_str(filesystem::path &path) = 0; +}; + +#endif // DNG_RESOURCES_H diff --git a/src/resources/linux/LinuxResources.cpp b/src/resources/linux/LinuxResources.cpp new file mode 100644 index 0000000..a8bcca0 --- /dev/null +++ b/src/resources/linux/LinuxResources.cpp @@ -0,0 +1,51 @@ +//======================================================================== +// 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. +// +//======================================================================== + +#include "LinuxResources.h" + +LinuxResources::LinuxResources() : Resources() { + this->workingDir = std::filesystem::current_path(); + this->exeDir = std::filesystem::canonical("/proc/self/exe").remove_filename(); + // set an initial value - will get hardset in load if found + std::filesystem::path f = + workingDir / "res" / DEFAULT_FONT; // default as fallback + this->font = std::make_shared(f); + this->defaultsLua = std::make_shared(); +} + +std::vector LinuxResources::levelSearchDirs() { + return {workingDir / "maps", exeDir / "maps"}; +} +std::vector LinuxResources::defaultsSearchDirs() { + return {workingDir / "dnglib", exeDir / "dnglib"}; +} +std::vector LinuxResources::fontSearchDirs() { + return {workingDir / "res", exeDir / "res"}; +} + +const char *LinuxResources::convert_to_str(std::filesystem::path &path) { + return path.c_str(); +} diff --git a/src/resources/linux/LinuxResources.h b/src/resources/linux/LinuxResources.h new file mode 100644 index 0000000..35fe049 --- /dev/null +++ b/src/resources/linux/LinuxResources.h @@ -0,0 +1,45 @@ +//======================================================================== +// 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_LINUXRESOURCES_H +#define DNG_LINUXRESOURCES_H +#include "../Resources.h" +class LinuxResources : public Resources { +public: + LinuxResources(); + +protected: +public: + const char *convert_to_str(std::filesystem::path &path) override; + +protected: + std::filesystem::path exeDir; + std::filesystem::path workingDir; + std::vector levelSearchDirs() override; + std::vector defaultsSearchDirs() override; + std::vector fontSearchDirs() override; +}; +#endif // DNG_LINUXRESOURCES_H diff --git a/src/resources/windows/WindowsResources.cpp b/src/resources/windows/WindowsResources.cpp new file mode 100644 index 0000000..cfe264f --- /dev/null +++ b/src/resources/windows/WindowsResources.cpp @@ -0,0 +1,54 @@ +//======================================================================== +// 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. +// +//======================================================================== +#include "../Resources.h" +#include +#define MAX_BUF_SIZE 1024 +class WindowsResources : public Resources { + +protected: + std::filesystem::path exeDir; + std::filesystem::path workingDir; + + 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"}; + } + +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 diff --git a/src/resources/windows/WindowsResources.h b/src/resources/windows/WindowsResources.h new file mode 100644 index 0000000..b6a1cd6 --- /dev/null +++ b/src/resources/windows/WindowsResources.h @@ -0,0 +1,40 @@ +//======================================================================== +// 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_WINDOWSRESOURCES_H +#define DNG_WINDOWSRESOURCES_H + +#include "../Resources.h" +class WindowsResources : public Resources { +protected: + std::filesystem::path exeDir; + std::filesystem::path workingDir; + std::vector levelSearchDirs() override; + std::vector defaultsSearchDirs() override; + std::vector fontSearchDirs() override; +}; + +#endif // DNG_WINDOWSRESOURCES_H diff --git a/src/windows/res.h b/src/windows/res.h deleted file mode 100644 index 9f8def3..0000000 --- a/src/windows/res.h +++ /dev/null @@ -1,94 +0,0 @@ -//======================================================================== -// 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; -}; - -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