From 3d1feb62951f915110ac452e237bd4f483d4e54d Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Tue, 27 May 2025 17:09:36 +0200 Subject: [PATCH 1/3] start asset manager --- src/GraphicalUI/AppMenus/AppMenu.cpp | 12 +++ src/GraphicalUI/AppMenus/AppMenu.h | 10 +-- xmake.lua | 8 ++ xmake/bin2c.lua | 106 +++++++++++++++++++++++++++ 4 files changed, 128 insertions(+), 8 deletions(-) create mode 100644 src/GraphicalUI/AppMenus/AppMenu.cpp create mode 100644 xmake/bin2c.lua diff --git a/src/GraphicalUI/AppMenus/AppMenu.cpp b/src/GraphicalUI/AppMenus/AppMenu.cpp new file mode 100644 index 0000000..280a89f --- /dev/null +++ b/src/GraphicalUI/AppMenus/AppMenu.cpp @@ -0,0 +1,12 @@ +#include "AppMenu.h" + +#include "../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); + + this->pressStartFont = sf::Font(file.data, file.size); +} \ No newline at end of file diff --git a/src/GraphicalUI/AppMenus/AppMenu.h b/src/GraphicalUI/AppMenus/AppMenu.h index 762fd5e..45591d7 100644 --- a/src/GraphicalUI/AppMenus/AppMenu.h +++ b/src/GraphicalUI/AppMenus/AppMenu.h @@ -21,16 +21,10 @@ class AppMenu { bool enterReleased = false; bool escPressed = false; bool escReleased = false; - sf::Font pressStartFont = sf::Font("data/fonts/pressstart/prstartk.ttf"); + sf::Font pressStartFont; public: - AppMenu(std::shared_ptr menuStack, std::shared_ptr settings, std::shared_ptr renderWindow) : - menuStack(menuStack), - settings(settings), - renderWindow(renderWindow) - { - - } + AppMenu(std::shared_ptr menuStack, std::shared_ptr settings, std::shared_ptr renderWindow); virtual void computeFrame() = 0; diff --git a/xmake.lua b/xmake.lua index 493efd2..86bfc1a 100644 --- a/xmake.lua +++ b/xmake.lua @@ -1,5 +1,7 @@ add_rules("mode.debug", "mode.release") +includes("xmake/bin2c.lua") + add_requires("sfml 3.0.0") set_languages("c++20") @@ -13,8 +15,14 @@ target("core") target("graph") set_default(true) + add_rules("bin2c", { + extensions = {".png", ".ttf"}, + outputSource = {"src/GraphicalUI/AssetManager.cpp"}, + outputHeader = {"src/GraphicalUI/AssetManager.h"} + }) set_kind("binary") add_files("./src/GraphicalUI/**.cpp") + add_files("data/fonts/**.ttf", "data/images/**.png") add_deps("core") add_packages("sfml") diff --git a/xmake/bin2c.lua b/xmake/bin2c.lua new file mode 100644 index 0000000..1bd8799 --- /dev/null +++ b/xmake/bin2c.lua @@ -0,0 +1,106 @@ + +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") + + if not os.isdir(headerdir) then + os.mkdir(headerdir) + end + + target:add("includedirs", headerdir) + end) + + before_buildcmd_files(function (target, batchcmds, sourcebatch, opt) + local outputHeader = table.unpack(target:extraconf("rules", "bin2c", "outputHeader")) + + local outputHeaderEnumContent = "" + + for _, filePath in ipairs(sourcebatch.sourcefiles) do + local escapedName = string.gsub(filePath, "[/|.]", "_") + outputHeaderEnumContent = outputHeaderEnumContent .. "\t" .. escapedName .. ",\n" + end + + local outputHeaderContent = string.format([[ +#pragma once + +#include +#include + +struct Asset { + const unsigned char* data; + std::size_t size; +}; + +enum class AssetName { +%s +}; + +const Asset& getResource(AssetName fileName); + ]], outputHeaderEnumContent) + + local outputSource = table.unpack(target:extraconf("rules", "bin2c", "outputSource")) + + local outputSourceContent = string.format("#include \"%s\"\n", "AssetManager.h") + + local outputSourceArrayVars = "" + + 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) + end + + outputSourceContent = outputSourceContent .. string.format([[ +static const Asset assets[] = { +%s +}; + ]], outputSourceArrayVars) + + outputSourceContent = outputSourceContent .. [[ +const Asset& getResource(AssetName fileName) { + return assets[static_cast(fileName)]; +} + ]] + + for _, sourcefile_bin in ipairs(sourcebatch.sourcefiles) do + -- get header file + local headerdir = path.join(target:autogendir(), "rules", "bin2c") + local headerfile = path.join(headerdir, sourcefile_bin .. ".h") + target:add("includedirs", headerdir) + + -- add commands + batchcmds:show_progress(opt.progress, "${color.build.object}generating.bin2c %s", sourcefile_bin) + batchcmds:mkdir(headerdir) + local argv = {"lua", "private.utils.bin2c", "-i", path(sourcefile_bin), "-o", path(headerfile)} + local linewidth = target:extraconf("rules", "bin2c", "linewidth") + if linewidth then + table.insert(argv, "-w") + table.insert(argv, tostring(linewidth)) + end + local nozeroend = target:extraconf("rules", "bin2c", "nozeroend") + if nozeroend then + table.insert(argv, "--nozeroend") + end + batchcmds:vrunv(os.programfile(), argv, {envs = {XMAKE_SKIP_HISTORY = "y"}}) + + -- add deps + batchcmds:add_depfiles(sourcefile_bin) + batchcmds:set_depmtime(os.mtime(headerfile)) + batchcmds:set_depcache(target:dependfile(headerfile)) + + end + + batchcmds:show_progress(opt.progress, "${color.build.object}generating.bin2c %s", outputHeader) + io.writefile(outputHeader, outputHeaderContent) + batchcmds:show_progress(opt.progress, "${color.build.object}generating.bin2c %s", outputSource) + io.writefile(outputSource, outputSourceContent) + end) \ No newline at end of file From d50714ef8c2a661dde851885d14ea5cee919cb9e Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Tue, 27 May 2025 21:29:56 +0200 Subject: [PATCH 2/3] working asset manager ! --- src/GraphicalUI/AppMenus/AppMenu.cpp | 4 +- .../AppMenus/SettingsKeybindsAppMenu.cpp | 6 +- src/GraphicalUI/main.cpp | 2 + src/Pieces/PiecesFiles.cpp | 9 +- src/Utils/AssetManager.cpp | 97 +++++++++++++++++++ src/Utils/AssetManager.h | 30 ++++++ xmake.lua | 4 +- xmake/bin2c.lua | 42 +++++--- 8 files changed, 175 insertions(+), 19 deletions(-) create mode 100644 src/Utils/AssetManager.cpp create mode 100644 src/Utils/AssetManager.h 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 From 69b91d64979d2ee2bc7576f8e4409f6bec07a6be Mon Sep 17 00:00:00 2001 From: zulianc Date: Wed, 28 May 2025 19:24:36 +0200 Subject: [PATCH 3/3] simon PR review --- src/GraphicalUI/AppMenus/AppMenu.cpp | 70 ++++++++++++++++++++++-- src/GraphicalUI/AppMenus/AppMenu.h | 51 ++---------------- src/GraphicalUI/main.cpp | 10 ++-- src/Pieces/PiecesFiles.cpp | 8 ++- src/TextUI/main.cpp | 7 +-- src/Utils/AssetManager.cpp | 80 ++++++++++++++-------------- src/Utils/AssetManager.h | 16 +++--- xmake.lua | 30 +++++++---- 8 files changed, 149 insertions(+), 123 deletions(-) diff --git a/src/GraphicalUI/AppMenus/AppMenu.cpp b/src/GraphicalUI/AppMenus/AppMenu.cpp index 2852b70..b927da6 100644 --- a/src/GraphicalUI/AppMenus/AppMenu.cpp +++ b/src/GraphicalUI/AppMenus/AppMenu.cpp @@ -1,12 +1,72 @@ #include "AppMenu.h" +#include "../Settings.h" +#include "../PlayerCursor.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) -{ +#include +#include +#include +#include + + +AppMenu::AppMenu(std::shared_ptr menuStack, std::shared_ptr settings, std::shared_ptr renderWindow) : + menuStack(menuStack), + settings(settings), + renderWindow(renderWindow) { + const Asset& file = getResource(AssetName::data_fonts_pressstart_prstartk_ttf); this->pressStartFont = sf::Font(file.data, file.size); -} \ No newline at end of file +} + +void AppMenu::updateMetaBinds() { + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Enter)) { + this->enterPressed = true; + this->enterReleased = false; + } + else { + this->enterReleased = this->enterPressed; + this->enterPressed = false; + } + + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) { + this->escPressed = true; + this->escReleased = false; + } + else { + this->escReleased = this->escPressed; + this->escPressed = false; + } +} + +void AppMenu::placeText(sf::Text& text, const std::optional& playerCursor, const sf::String& string, float xPos, float yPos, const std::optional& cursorPos) const { + float sizeMultiplier = this->settings->getWindowSizeMultiplier(); + + text.setString(string); + if (playerCursor.has_value() && cursorPos.has_value()) { + text.setOutlineThickness((playerCursor.value().getPosition() == cursorPos.value()) ? (sizeMultiplier / 2) : 0); + } + text.setOrigin(sf::Vector2f({0, text.getLocalBounds().size.y / 2})); + text.setPosition(sf::Vector2f({sizeMultiplier * xPos, sizeMultiplier * yPos})); + this->renderWindow->draw(text); +} + +void AppMenu::placeTitle(sf::Text& text, const std::optional& playerCursor, const sf::String& string, float yPos, const std::optional& cursorPos) const { + float sizeMultiplier = this->settings->getWindowSizeMultiplier(); + + text.setString(string); + if (playerCursor.has_value() && cursorPos.has_value()) { + text.setOutlineThickness((playerCursor.value().getPosition() == cursorPos.value()) ? (sizeMultiplier / 2) : 0); + } + text.setOrigin({text.getLocalBounds().getCenter().x, text.getLocalBounds().size.y / 2}); + text.setPosition(sf::Vector2f({sizeMultiplier * 40.f, sizeMultiplier * yPos})); + this->renderWindow->draw(text); +} + +sf::Color AppMenu::getColorOfBlock(Block block, int luminosityShift) const { + Color rgbColor = BLOCKS_COLOR[block]; + return sf::Color(std::clamp(rgbColor.red + luminosityShift, 0, 255), + std::clamp(rgbColor.green + luminosityShift, 0, 255), + std::clamp(rgbColor.blue + luminosityShift, 0, 255)); +} diff --git a/src/GraphicalUI/AppMenus/AppMenu.h b/src/GraphicalUI/AppMenus/AppMenu.h index 45591d7..b2e6ba6 100644 --- a/src/GraphicalUI/AppMenus/AppMenu.h +++ b/src/GraphicalUI/AppMenus/AppMenu.h @@ -31,54 +31,11 @@ class AppMenu { virtual void drawFrame() const = 0; protected: - void updateMetaBinds() { - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Enter)) { - enterPressed = true; - enterReleased = false; - } - else { - enterReleased = enterPressed; - enterPressed = false; - } + void updateMetaBinds(); - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) { - escPressed = true; - escReleased = false; - } - else { - escReleased = escPressed; - escPressed = false; - } - } + void placeText(sf::Text& text, const std::optional& playerCursor, const sf::String& string, float xPos, float yPos, const std::optional& cursorPos) const; - void placeText(sf::Text& text, const std::optional& playerCursor, const sf::String& string, float xPos, float yPos, const std::optional& cursorPos) const { - float sizeMultiplier = this->settings->getWindowSizeMultiplier(); + void placeTitle(sf::Text& text, const std::optional& playerCursor, const sf::String& string, float yPos, const std::optional& cursorPos) const; - text.setString(string); - if (playerCursor.has_value() && cursorPos.has_value()) { - text.setOutlineThickness((playerCursor.value().getPosition() == cursorPos.value()) ? (sizeMultiplier / 2) : 0); - } - text.setOrigin(sf::Vector2f({0, text.getLocalBounds().size.y / 2})); - text.setPosition(sf::Vector2f({sizeMultiplier * xPos, sizeMultiplier * yPos})); - this->renderWindow->draw(text); - } - - void placeTitle(sf::Text& text, const std::optional& playerCursor, const sf::String& string, float yPos, const std::optional& cursorPos) const { - float sizeMultiplier = this->settings->getWindowSizeMultiplier(); - - text.setString(string); - if (playerCursor.has_value() && cursorPos.has_value()) { - text.setOutlineThickness((playerCursor.value().getPosition() == cursorPos.value()) ? (sizeMultiplier / 2) : 0); - } - text.setOrigin({text.getLocalBounds().getCenter().x, text.getLocalBounds().size.y / 2}); - text.setPosition(sf::Vector2f({sizeMultiplier * 40.f, sizeMultiplier * yPos})); - this->renderWindow->draw(text); - } - - sf::Color getColorOfBlock(Block block, int luminosityShift) const { - Color rgbColor = BLOCKS_COLOR[block]; - return sf::Color(std::clamp(rgbColor.red + luminosityShift, 0, 255), - std::clamp(rgbColor.green + luminosityShift, 0, 255), - std::clamp(rgbColor.blue + luminosityShift, 0, 255)); - } + sf::Color getColorOfBlock(Block block, int luminosityShift) const; }; diff --git a/src/GraphicalUI/main.cpp b/src/GraphicalUI/main.cpp index b77dd46..1aeaba7 100644 --- a/src/GraphicalUI/main.cpp +++ b/src/GraphicalUI/main.cpp @@ -24,7 +24,7 @@ int main() { } } #ifndef NDEBUG - std::cout << "IMPORTANT: you are currently in debug mode, if you wish to use bigger pieces, type 'xmake f -m release'." << std::endl; + std::cout << "IMPORTANT: You are currently in debug mode, if you wish to use bigger pieces, type 'xmake f -m release'." << std::endl; bool everythingGenerated = true; for (int i = DEBUG_PIECES_SIZE; i <= RELEASE_PIECES_SIZE; i++) { @@ -33,7 +33,7 @@ int main() { } } if (!everythingGenerated) { - std::cout << "NOTE : you do not have all pieces generated, generating can take several minutes." << std::endl; + std::cout << "NOTE: You do not have all pieces generated, generating can take several minutes." << std::endl; } #endif @@ -70,7 +70,10 @@ int main() { void resetSettingsFile() { - std::filesystem::create_directories("data/config"); + if (!std::filesystem::exists("data/config")) { + std::filesystem::create_directories("data/config"); + } + std::ofstream settingsFile("data/config/settings.bin", std::ios::trunc | std::ios::binary); char byte; @@ -138,7 +141,6 @@ 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 419279e..e75b0af 100644 --- a/src/Pieces/PiecesFiles.cpp +++ b/src/Pieces/PiecesFiles.cpp @@ -10,8 +10,6 @@ #include #include -namespace fs = std::filesystem; - PiecesFiles::PiecesFiles() { } @@ -135,11 +133,11 @@ bool PiecesFiles::loadPieces(int polyominoSize, std::vector& pieces, std: bool PiecesFiles::getFilePath(int polyominoSize, std::string& filePath) const { std::string dataFolderPath = "data/pieces/"; - if (!fs::exists(dataFolderPath)) { - fs::create_directories(dataFolderPath); + if (!std::filesystem::exists(dataFolderPath)) { + std::filesystem::create_directories(dataFolderPath); } - if (!fs::is_directory(dataFolderPath)) { + if (!std::filesystem::is_directory(dataFolderPath)) { return false; } diff --git a/src/TextUI/main.cpp b/src/TextUI/main.cpp index 5663a3f..510ef6f 100644 --- a/src/TextUI/main.cpp +++ b/src/TextUI/main.cpp @@ -22,11 +22,12 @@ int main(int argc, char** argv) { std::srand(std::time(NULL)); #ifdef BENCHMARK + #ifndef NDEBUG + std::cout << "IMPORTANT: You are currently in debug mode, debug mode has lowest optimization settings and thus yields worse benchmarking results, to switch to release mode, type 'xmake f -m debug'." << std::endl; + #endif + benchmarking(1, BENCHMARK_PIECES_SIZE); #else - // dev: generate files if it hasn't been done before, UI will NOT generate the files - //generateFilesForAllSizes(10); - PiecesFiles pf; for (int i = 1; i <= MAXIMUM_PIECES_SIZE; i++) { if (!std::filesystem::exists("data/pieces/" + std::to_string(i) + "minos.bin")) { diff --git a/src/Utils/AssetManager.cpp b/src/Utils/AssetManager.cpp index f9d5be6..75b2c5d 100644 --- a/src/Utils/AssetManager.cpp +++ b/src/Utils/AssetManager.cpp @@ -2,28 +2,44 @@ #include +static const unsigned char data_fonts_pressstart_prstart_ttf[] = { + #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_Rotate180_png[] = { + #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_RotateCCW_png[] = { + #include +}; + +static const unsigned char data_images_keybinds_Retry_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_Moveright_png[] = { + #include +}; + +static const unsigned char data_images_keybinds_Harddrop_png[] = { + #include +}; + +static const unsigned char data_images_keybinds_Moveleft_png[] = { + #include }; static const unsigned char data_images_keybinds_Hold_png[] = { @@ -34,57 +50,41 @@ 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 unsigned char data_images_keybinds_Pause_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_fonts_pressstart_prstartk_ttf, sizeof(data_fonts_pressstart_prstartk_ttf)}, + {data_images_keybinds_Rotate180_png, sizeof(data_images_keybinds_Rotate180_png)}, {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_RotateCCW_png, sizeof(data_images_keybinds_RotateCCW_png)}, + {data_images_keybinds_Retry_png, sizeof(data_images_keybinds_Retry_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_Moveright_png, sizeof(data_images_keybinds_Moveright_png)}, + {data_images_keybinds_Harddrop_png, sizeof(data_images_keybinds_Harddrop_png)}, + {data_images_keybinds_Moveleft_png, sizeof(data_images_keybinds_Moveleft_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)}, + {data_images_keybinds_Pause_png, sizeof(data_images_keybinds_Pause_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/fonts/pressstart/prstartk.ttf", AssetName::data_fonts_pressstart_prstartk_ttf}, + {"data/images/keybinds/Rotate180.png", AssetName::data_images_keybinds_Rotate180_png}, {"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/RotateCCW.png", AssetName::data_images_keybinds_RotateCCW_png}, + {"data/images/keybinds/Retry.png", AssetName::data_images_keybinds_Retry_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/Moveright.png", AssetName::data_images_keybinds_Moveright_png}, + {"data/images/keybinds/Harddrop.png", AssetName::data_images_keybinds_Harddrop_png}, + {"data/images/keybinds/Moveleft.png", AssetName::data_images_keybinds_Moveleft_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}, + {"data/images/keybinds/Pause.png", AssetName::data_images_keybinds_Pause_png}, }; diff --git a/src/Utils/AssetManager.h b/src/Utils/AssetManager.h index 05172dd..e4eacae 100644 --- a/src/Utils/AssetManager.h +++ b/src/Utils/AssetManager.h @@ -9,19 +9,19 @@ struct Asset { }; enum class AssetName { - data_fonts_pressstart_prstartk_ttf, data_fonts_pressstart_prstart_ttf, + data_fonts_pressstart_prstartk_ttf, + data_images_keybinds_Rotate180_png, data_images_keybinds_Rotate0_png, - data_images_keybinds_Moveright_png, + data_images_keybinds_RotateCCW_png, + data_images_keybinds_Retry_png, data_images_keybinds_RotateCW_png, - data_images_keybinds_Pause_png, + data_images_keybinds_Moveright_png, + data_images_keybinds_Harddrop_png, + data_images_keybinds_Moveleft_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, + data_images_keybinds_Pause_png, }; diff --git a/xmake.lua b/xmake.lua index 8cc136d..64a8a00 100644 --- a/xmake.lua +++ b/xmake.lua @@ -13,6 +13,19 @@ target("core") add_files("src/Pieces/*.cpp") add_files("src/Core/*.cpp") +target("text") + set_default(false) + set_kind("binary") + add_files("./src/TextUI/*.cpp") + add_deps("core") + +target("benchmark") + set_default(false) + set_kind("binary") + add_files("./src/TextUI/*.cpp") + add_deps("core") + add_defines("BENCHMARK") + target("graph") set_default(true) add_rules("bin2c", { @@ -26,18 +39,13 @@ target("graph") add_deps("core") add_packages("sfml") -target("text") - set_default(false) - set_kind("binary") - add_files("./src/TextUI/*.cpp") - add_deps("core") +if is_mode("release") then + add_defines("NDEBUG") +end -target("benchmark") - set_default(false) - set_kind("binary") - add_files("./src/TextUI/*.cpp") - add_deps("core") - add_defines("BENCHMARK") +if is_plat("mingw") then + add_ldflags("-static-libstdc++") +end --