diff --git a/src/GraphicalUI/AppMenus/AppMenu.cpp b/src/GraphicalUI/AppMenus/AppMenu.cpp index 280a89f..2852b70 100644 --- a/src/GraphicalUI/AppMenus/AppMenu.cpp +++ b/src/GraphicalUI/AppMenus/AppMenu.cpp @@ -1,12 +1,12 @@ #include "AppMenu.h" -#include "../AssetManager.h" +#include "../../Utils/AssetManager.h" AppMenu::AppMenu(std::shared_ptr menuStack, std::shared_ptr settings, std::shared_ptr renderWindow) : menuStack(menuStack), settings(settings), renderWindow(renderWindow) { - Asset file = getResource(AssetName::data_fonts_pressstart_prstartk_ttf); + const Asset& file = getResource(AssetName::data_fonts_pressstart_prstartk_ttf); this->pressStartFont = sf::Font(file.data, file.size); } \ No newline at end of file diff --git a/src/GraphicalUI/AppMenus/SettingsKeybindsAppMenu.cpp b/src/GraphicalUI/AppMenus/SettingsKeybindsAppMenu.cpp index 330b4f5..95bec2c 100644 --- a/src/GraphicalUI/AppMenus/SettingsKeybindsAppMenu.cpp +++ b/src/GraphicalUI/AppMenus/SettingsKeybindsAppMenu.cpp @@ -2,6 +2,7 @@ #include "AppMenu.h" #include "../PlayerCursor.h" +#include "../../Utils/AssetManager.h" #include #include @@ -22,8 +23,9 @@ SettingsKeybindsAppMenu::SettingsKeybindsAppMenu(std::shared_ptr menu std::string textureName = ACTION_NAMES[action]; textureName = std::regex_replace(textureName, std::regex(" "), ""); - std::filesystem::path texturePath("data/images/keybinds/" + textureName + ".png"); - this->iconTextures[action] = sf::Texture(texturePath, false, {{0, 0}, {16, 16}}); + const Asset& textureData = getResource("data/images/keybinds/" + textureName + ".png"); + + this->iconTextures[action] = sf::Texture(textureData.data, textureData.size, false, {{0, 0}, {16, 16}}); } } diff --git a/src/GraphicalUI/main.cpp b/src/GraphicalUI/main.cpp index f56ba73..b77dd46 100644 --- a/src/GraphicalUI/main.cpp +++ b/src/GraphicalUI/main.cpp @@ -70,6 +70,7 @@ int main() { void resetSettingsFile() { + std::filesystem::create_directories("data/config"); std::ofstream settingsFile("data/config/settings.bin", std::ios::trunc | std::ios::binary); char byte; @@ -137,6 +138,7 @@ void resetSettingsFile() { void resetKeybindFile(int layout) { if (layout < 0 || layout > 4) return; + std::filesystem::create_directories("data/config/keybinds/layout"); std::ofstream layoutFile("data/config/keybinds/layout" + std::to_string(layout) + ".bin", std::ios::trunc | std::ios::binary); std::map keybinds; diff --git a/src/Pieces/PiecesFiles.cpp b/src/Pieces/PiecesFiles.cpp index 6da192f..419279e 100644 --- a/src/Pieces/PiecesFiles.cpp +++ b/src/Pieces/PiecesFiles.cpp @@ -10,6 +10,8 @@ #include #include +namespace fs = std::filesystem; + PiecesFiles::PiecesFiles() { } @@ -132,7 +134,12 @@ bool PiecesFiles::loadPieces(int polyominoSize, std::vector& pieces, std: bool PiecesFiles::getFilePath(int polyominoSize, std::string& filePath) const { std::string dataFolderPath = "data/pieces/"; - if (!std::filesystem::is_directory(dataFolderPath)) { + + if (!fs::exists(dataFolderPath)) { + fs::create_directories(dataFolderPath); + } + + if (!fs::is_directory(dataFolderPath)) { return false; } diff --git a/src/Utils/AssetManager.cpp b/src/Utils/AssetManager.cpp new file mode 100644 index 0000000..f9d5be6 --- /dev/null +++ b/src/Utils/AssetManager.cpp @@ -0,0 +1,97 @@ +#include "./AssetManager.h" + +#include + +static const unsigned char data_fonts_pressstart_prstartk_ttf[] = { + #include +}; + +static const unsigned char data_fonts_pressstart_prstart_ttf[] = { + #include +}; + +static const unsigned char data_images_keybinds_Rotate0_png[] = { + #include +}; + +static const unsigned char data_images_keybinds_Moveright_png[] = { + #include +}; + +static const unsigned char data_images_keybinds_RotateCW_png[] = { + #include +}; + +static const unsigned char data_images_keybinds_Pause_png[] = { + #include +}; + +static const unsigned char data_images_keybinds_Hold_png[] = { + #include +}; + +static const unsigned char data_images_keybinds_Softdrop_png[] = { + #include +}; + +static const unsigned char data_images_keybinds_RotateCCW_png[] = { + #include +}; + +static const unsigned char data_images_keybinds_Moveleft_png[] = { + #include +}; + +static const unsigned char data_images_keybinds_Rotate180_png[] = { + #include +}; + +static const unsigned char data_images_keybinds_Retry_png[] = { + #include +}; + +static const unsigned char data_images_keybinds_Harddrop_png[] = { + #include +}; + +static const Asset assets[] = { + {data_fonts_pressstart_prstartk_ttf, sizeof(data_fonts_pressstart_prstartk_ttf)}, + {data_fonts_pressstart_prstart_ttf, sizeof(data_fonts_pressstart_prstart_ttf)}, + {data_images_keybinds_Rotate0_png, sizeof(data_images_keybinds_Rotate0_png)}, + {data_images_keybinds_Moveright_png, sizeof(data_images_keybinds_Moveright_png)}, + {data_images_keybinds_RotateCW_png, sizeof(data_images_keybinds_RotateCW_png)}, + {data_images_keybinds_Pause_png, sizeof(data_images_keybinds_Pause_png)}, + {data_images_keybinds_Hold_png, sizeof(data_images_keybinds_Hold_png)}, + {data_images_keybinds_Softdrop_png, sizeof(data_images_keybinds_Softdrop_png)}, + {data_images_keybinds_RotateCCW_png, sizeof(data_images_keybinds_RotateCCW_png)}, + {data_images_keybinds_Moveleft_png, sizeof(data_images_keybinds_Moveleft_png)}, + {data_images_keybinds_Rotate180_png, sizeof(data_images_keybinds_Rotate180_png)}, + {data_images_keybinds_Retry_png, sizeof(data_images_keybinds_Retry_png)}, + {data_images_keybinds_Harddrop_png, sizeof(data_images_keybinds_Harddrop_png)}, + +}; + +static const std::map assetMap = { + {"data/fonts/pressstart/prstartk.ttf", AssetName::data_fonts_pressstart_prstartk_ttf}, + {"data/fonts/pressstart/prstart.ttf", AssetName::data_fonts_pressstart_prstart_ttf}, + {"data/images/keybinds/Rotate0.png", AssetName::data_images_keybinds_Rotate0_png}, + {"data/images/keybinds/Moveright.png", AssetName::data_images_keybinds_Moveright_png}, + {"data/images/keybinds/RotateCW.png", AssetName::data_images_keybinds_RotateCW_png}, + {"data/images/keybinds/Pause.png", AssetName::data_images_keybinds_Pause_png}, + {"data/images/keybinds/Hold.png", AssetName::data_images_keybinds_Hold_png}, + {"data/images/keybinds/Softdrop.png", AssetName::data_images_keybinds_Softdrop_png}, + {"data/images/keybinds/RotateCCW.png", AssetName::data_images_keybinds_RotateCCW_png}, + {"data/images/keybinds/Moveleft.png", AssetName::data_images_keybinds_Moveleft_png}, + {"data/images/keybinds/Rotate180.png", AssetName::data_images_keybinds_Rotate180_png}, + {"data/images/keybinds/Retry.png", AssetName::data_images_keybinds_Retry_png}, + {"data/images/keybinds/Harddrop.png", AssetName::data_images_keybinds_Harddrop_png}, + +}; + +const Asset& getResource(AssetName fileName) { + return assets[static_cast(fileName)]; +} + +const Asset& getResource(const std::string& fileName) { + return getResource(assetMap.at(fileName)); +} diff --git a/src/Utils/AssetManager.h b/src/Utils/AssetManager.h new file mode 100644 index 0000000..05172dd --- /dev/null +++ b/src/Utils/AssetManager.h @@ -0,0 +1,30 @@ +#pragma once + +#include +#include + +struct Asset { + const unsigned char* data; + std::size_t size; +}; + +enum class AssetName { + data_fonts_pressstart_prstartk_ttf, + data_fonts_pressstart_prstart_ttf, + data_images_keybinds_Rotate0_png, + data_images_keybinds_Moveright_png, + data_images_keybinds_RotateCW_png, + data_images_keybinds_Pause_png, + data_images_keybinds_Hold_png, + data_images_keybinds_Softdrop_png, + data_images_keybinds_RotateCCW_png, + data_images_keybinds_Moveleft_png, + data_images_keybinds_Rotate180_png, + data_images_keybinds_Retry_png, + data_images_keybinds_Harddrop_png, + +}; + +const Asset& getResource(AssetName fileName); + +const Asset& getResource(const std::string& fileName); diff --git a/xmake.lua b/xmake.lua index 86bfc1a..8cc136d 100644 --- a/xmake.lua +++ b/xmake.lua @@ -17,8 +17,8 @@ target("graph") set_default(true) add_rules("bin2c", { extensions = {".png", ".ttf"}, - outputSource = {"src/GraphicalUI/AssetManager.cpp"}, - outputHeader = {"src/GraphicalUI/AssetManager.h"} + outputSource = {"src/Utils/AssetManager.cpp"}, + outputHeader = {"src/Utils/AssetManager.h"} }) set_kind("binary") add_files("./src/GraphicalUI/**.cpp") diff --git a/xmake/bin2c.lua b/xmake/bin2c.lua index 1bd8799..54aac3b 100644 --- a/xmake/bin2c.lua +++ b/xmake/bin2c.lua @@ -1,21 +1,18 @@ - -buildAsset = function(target, batchcmds, sourcefile_bin, opt) - - -end - - rule("bin2c") set_extensions(".bin") on_load(function (target) local headerdir = path.join(target:autogendir(), "rules", "bin2c") + + local outputSource = table.unpack(target:extraconf("rules", "bin2c", "outputSource")) if not os.isdir(headerdir) then os.mkdir(headerdir) end - + target:add("includedirs", headerdir) + + target:add("files", outputSource) end) before_buildcmd_files(function (target, batchcmds, sourcebatch, opt) @@ -44,32 +41,53 @@ enum class AssetName { }; const Asset& getResource(AssetName fileName); - ]], outputHeaderEnumContent) + +const Asset& getResource(const std::string& fileName); +]], outputHeaderEnumContent) local outputSource = table.unpack(target:extraconf("rules", "bin2c", "outputSource")) - local outputSourceContent = string.format("#include \"%s\"\n", "AssetManager.h") + local relativePath = path.join(path.relative(path.directory(outputHeader), path.directory(outputSource)), path.filename(outputHeader)) + + local outputSourceContent = string.format([[ +#include "%s" + +#include + +]], relativePath) + local outputSourceArrayVars = "" + local outputSourceMapVars = "" for _, filePath in ipairs(sourcebatch.sourcefiles) do local escapedName = string.gsub(filePath, "[/|.]", "_") local varDecl = string.format("static const unsigned char %s[] = {\n\t#include <%s>\n};\n\n", escapedName, filePath .. ".h") outputSourceContent = outputSourceContent .. varDecl outputSourceArrayVars = outputSourceArrayVars .. string.format("\t{%s, sizeof(%s)},\n", escapedName, escapedName) + outputSourceMapVars = outputSourceMapVars .. string.format("\t{\"%s\", AssetName::%s},\n", filePath, escapedName) end outputSourceContent = outputSourceContent .. string.format([[ static const Asset assets[] = { %s }; - ]], outputSourceArrayVars) + +static const std::map assetMap = { +%s +}; + +]], outputSourceArrayVars, outputSourceMapVars) outputSourceContent = outputSourceContent .. [[ const Asset& getResource(AssetName fileName) { return assets[static_cast(fileName)]; } - ]] + +const Asset& getResource(const std::string& fileName) { + return getResource(assetMap.at(fileName)); +} +]] for _, sourcefile_bin in ipairs(sourcebatch.sourcefiles) do -- get header file