diff options
author | Steph Enders <smenders@gmail.com> | 2022-06-24 17:10:50 -0400 |
---|---|---|
committer | Steph Enders <smenders@gmail.com> | 2022-06-24 17:37:11 -0400 |
commit | ab37629c6e4798654fca1d533a611da7986b5053 (patch) | |
tree | 11b8a0b49bff176c9226b24b496588fd69f72cc3 /src | |
parent | 20076f7271e19ecc259014681ba733047bea4d9b (diff) |
Select level via command line (linux tested)
Diffstat (limited to 'src')
-rw-r--r-- | src/Level.cpp | 63 | ||||
-rw-r--r-- | src/Level.h | 27 | ||||
-rw-r--r-- | src/linux/res.h | 80 | ||||
-rw-r--r-- | src/main.cpp | 60 | ||||
-rw-r--r-- | src/resources/Config.h | 47 | ||||
-rw-r--r-- | src/resources/Resources.cpp | 136 | ||||
-rw-r--r-- | src/resources/Resources.h | 76 | ||||
-rw-r--r-- | src/resources/linux/LinuxResources.cpp | 51 | ||||
-rw-r--r-- | src/resources/linux/LinuxResources.h | 45 | ||||
-rw-r--r-- | src/resources/windows/WindowsResources.cpp | 54 | ||||
-rw-r--r-- | src/resources/windows/WindowsResources.h | 40 | ||||
-rw-r--r-- | src/windows/res.h | 94 |
12 files changed, 508 insertions, 265 deletions
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 <fstream> -#include <iostream> #include <string> -bool canStep(Pos pos, int dx, int dy, std::vector<std::vector<char>> map) { +bool canStep(const Pos &pos, int dx, int dy, + std::vector<std::vector<char>> 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<std::string>(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<std::vector<char>> map; // source copy of map std::vector<sf::RectangleShape> 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<std::string> 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 <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> - -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 <SFML/Graphics.hpp> #include <cmath> #include <filesystem> @@ -49,22 +50,31 @@ const int MAX_HEIGHT = static_cast<int>(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<Level>(); + lvl = std::make_shared<Level>( + 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 <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_CONFIG_H +#define DNG_CONFIG_H + +#include <filesystem> +#include <vector> + +/** + * 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<std::filesystem::path> fonts; + std::vector<std::filesystem::path> 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 <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. +// +//======================================================================== + +#include "Resources.h" +#include <algorithm> +#include <cassert> +#include <iostream> + +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<std::filesystem::path> Resources::getFontFiles() { + return this->fonts; +} +std::vector<std::filesystem::path> Resources::getLevels() { + return this->levels; +} +shared_ptr<std::filesystem::path> Resources::getFontFile() { + return this->font; +} +shared_ptr<std::filesystem::path> Resources::updateFont(int idx) { + auto f = this->fonts[idx]; + *this->font = f; + return getFontFile(); +} +shared_ptr<std::filesystem::path> Resources::getDefaultsLuaFile() { + return this->defaultsLua; +} +shared_ptr<std::filesystem::path> 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<std::filesystem::path>(dngMap); +} +std::optional<shared_ptr<std::filesystem::path>> +Resources::getLevelProcLua(int idx) { + auto lvlBase = this->levels[idx]; + auto procLua = lvlBase / PROC_LUA; + if (exists(procLua)) { + return std::make_shared<std::filesystem::path>(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 <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_RESOURCES_H +#define DNG_RESOURCES_H + +#include <filesystem> +#include <optional> +#include <vector> + +// struct LevelInfo { +// filesystem::path dir; +// filesystem::path dngMap; +// optional<filesystem::path> 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<std::filesystem::path> font; // chosen font + shared_ptr<filesystem::path> defaultsLua; // defaults lua file + vector<filesystem::path> fonts; // all found maps + vector<filesystem::path> levels; // all found maps + virtual vector<filesystem::path> levelSearchDirs() = 0; + virtual vector<filesystem::path> defaultsSearchDirs() = 0; + virtual vector<filesystem::path> fontSearchDirs() = 0; + +public: + void loadLevels(); + void loadFontFiles(); + void loadDefaultLuaFile(); + vector<filesystem::path> getFontFiles(); + vector<filesystem::path> getLevels(); + shared_ptr<std::filesystem::path> updateFont(int idx); + shared_ptr<std::filesystem::path> getFontFile(); + shared_ptr<std::filesystem::path> getDefaultsLuaFile(); + shared_ptr<std::filesystem::path> getLevelMap(int idx); + optional<shared_ptr<std::filesystem::path>> 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 <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. +// +//======================================================================== + +#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<std::filesystem::path>(f); + this->defaultsLua = std::make_shared<std::filesystem::path>(); +} + +std::vector<std::filesystem::path> LinuxResources::levelSearchDirs() { + return {workingDir / "maps", exeDir / "maps"}; +} +std::vector<std::filesystem::path> LinuxResources::defaultsSearchDirs() { + return {workingDir / "dnglib", exeDir / "dnglib"}; +} +std::vector<std::filesystem::path> 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 <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_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<std::filesystem::path> levelSearchDirs() override; + std::vector<std::filesystem::path> defaultsSearchDirs() override; + std::vector<std::filesystem::path> 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 <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. +// +//======================================================================== +#include "../Resources.h" +#include <libloaderapi.h> +#define MAX_BUF_SIZE 1024 +class WindowsResources : public Resources { + +protected: + std::filesystem::path exeDir; + std::filesystem::path workingDir; + + std::vector<std::filesystem::path> levelSearchDirs() override { + return {workingDir / "maps", exeDir / "maps"}; + } + std::vector<std::filesystem::path> defaultsSearchDirs() override { + return {workingDir / "dnglib", exeDir / "dnglib"}; + } + std::vector<std::filesystem::path> 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<char *>(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 <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_WINDOWSRESOURCES_H +#define DNG_WINDOWSRESOURCES_H + +#include "../Resources.h" +class WindowsResources : public Resources { +protected: + std::filesystem::path exeDir; + std::filesystem::path workingDir; + std::vector<std::filesystem::path> levelSearchDirs() override; + std::vector<std::filesystem::path> defaultsSearchDirs() override; + std::vector<std::filesystem::path> 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 <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; -}; - -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 |