From 69b91d64979d2ee2bc7576f8e4409f6bec07a6be Mon Sep 17 00:00:00 2001 From: zulianc Date: Wed, 28 May 2025 19:24:36 +0200 Subject: [PATCH] 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 --