diff --git a/data/config/keybinds/layout0.bin b/data/config/keybinds/layout0.bin index e69de29..6b4afd4 100644 Binary files a/data/config/keybinds/layout0.bin and b/data/config/keybinds/layout0.bin differ diff --git a/data/config/keybinds/layout1.bin b/data/config/keybinds/layout1.bin index e69de29..9d7738e 100644 Binary files a/data/config/keybinds/layout1.bin and b/data/config/keybinds/layout1.bin differ diff --git a/data/config/keybinds/layout2.bin b/data/config/keybinds/layout2.bin index e69de29..deae4e8 100644 Binary files a/data/config/keybinds/layout2.bin and b/data/config/keybinds/layout2.bin differ diff --git a/data/config/keybinds/layout3.bin b/data/config/keybinds/layout3.bin index e69de29..a6eddc0 100644 Binary files a/data/config/keybinds/layout3.bin and b/data/config/keybinds/layout3.bin differ diff --git a/data/config/keybinds/layout4.bin b/data/config/keybinds/layout4.bin index e69de29..2a0b30e 100644 Binary files a/data/config/keybinds/layout4.bin and b/data/config/keybinds/layout4.bin differ diff --git a/data/config/settings.bin b/data/config/settings.bin index e69de29..4a6fcc7 100644 Binary files a/data/config/settings.bin and b/data/config/settings.bin differ diff --git a/src/Core/Menu.h b/src/Core/Menu.h index 688338e..aea2c96 100644 --- a/src/Core/Menu.h +++ b/src/Core/Menu.h @@ -5,7 +5,6 @@ #include "Game.h" static const int FRAMES_PER_SECOND = 60; // the number of frames per second, all the values in the app were choosen with this number in mind -static const int MAXIMUM_PIECES_SIZE = 15; // the maximum size of available pieces /** diff --git a/src/GraphicalUI/AppMenus/AppMenu.h b/src/GraphicalUI/AppMenus/AppMenu.h index 1302f87..57b7aec 100644 --- a/src/GraphicalUI/AppMenus/AppMenu.h +++ b/src/GraphicalUI/AppMenus/AppMenu.h @@ -6,15 +6,18 @@ #include #include +class AppMenu; +using MenuStack = std::stack>; + class AppMenu { protected: - std::shared_ptr> menuStack; + std::shared_ptr menuStack; std::shared_ptr settings; std::shared_ptr renderWindow; public: - AppMenu(std::shared_ptr> menuStack, std::shared_ptr settings, std::shared_ptr renderWindow) : + AppMenu(std::shared_ptr menuStack, std::shared_ptr settings, std::shared_ptr renderWindow) : menuStack(menuStack), settings(settings), renderWindow(renderWindow) diff --git a/src/GraphicalUI/AppMenus/InGameAppMenu.cpp b/src/GraphicalUI/AppMenus/InGameAppMenu.cpp index 369c471..fc14b08 100644 --- a/src/GraphicalUI/AppMenus/InGameAppMenu.cpp +++ b/src/GraphicalUI/AppMenus/InGameAppMenu.cpp @@ -7,7 +7,7 @@ #include -InGameAppMenu::InGameAppMenu(std::shared_ptr> menuStack, std::shared_ptr settings, std::shared_ptr renderWindow) : +InGameAppMenu::InGameAppMenu(std::shared_ptr menuStack, std::shared_ptr settings, std::shared_ptr renderWindow) : AppMenu(menuStack, settings, renderWindow), game(this->settings->getMenu().startGame(this->settings->getGamemode())) { diff --git a/src/GraphicalUI/AppMenus/InGameAppMenu.h b/src/GraphicalUI/AppMenus/InGameAppMenu.h index 04b29d4..ec31ffb 100644 --- a/src/GraphicalUI/AppMenus/InGameAppMenu.h +++ b/src/GraphicalUI/AppMenus/InGameAppMenu.h @@ -13,7 +13,7 @@ class InGameAppMenu : public AppMenu { bool paused; public: - InGameAppMenu(std::shared_ptr> menuStack, std::shared_ptr settings, std::shared_ptr renderWindow); + InGameAppMenu(std::shared_ptr menuStack, std::shared_ptr settings, std::shared_ptr renderWindow); void computeFrame(); diff --git a/src/GraphicalUI/AppMenus/MainAppMenu.cpp b/src/GraphicalUI/AppMenus/MainAppMenu.cpp index 73a3e8b..d9cad52 100644 --- a/src/GraphicalUI/AppMenus/MainAppMenu.cpp +++ b/src/GraphicalUI/AppMenus/MainAppMenu.cpp @@ -8,14 +8,14 @@ #include -MainAppMenu::MainAppMenu(std::shared_ptr> menuStack, std::shared_ptr settings, std::shared_ptr renderWindow) : +MainAppMenu::MainAppMenu(std::shared_ptr menuStack, std::shared_ptr settings, std::shared_ptr renderWindow) : AppMenu(menuStack, settings, renderWindow) { } void MainAppMenu::computeFrame() { if (sf::Keyboard::isKeyPressed(sfKey::Enter)) { - this->menuStack->push(InGameAppMenu(this->menuStack, this->settings, this->renderWindow)); + this->menuStack->push(std::make_shared(this->menuStack, this->settings, this->renderWindow)); } else if (sf::Keyboard::isKeyPressed(sfKey::Escape)) { this->menuStack->pop(); diff --git a/src/GraphicalUI/AppMenus/MainAppMenu.h b/src/GraphicalUI/AppMenus/MainAppMenu.h index 1f48733..6731717 100644 --- a/src/GraphicalUI/AppMenus/MainAppMenu.h +++ b/src/GraphicalUI/AppMenus/MainAppMenu.h @@ -9,7 +9,7 @@ class MainAppMenu : public AppMenu { public: - MainAppMenu(std::shared_ptr> menuStack, std::shared_ptr settings, std::shared_ptr renderWindow); + MainAppMenu(std::shared_ptr menuStack, std::shared_ptr settings, std::shared_ptr renderWindow); void computeFrame(); diff --git a/src/GraphicalUI/GraphApp.cpp b/src/GraphicalUI/GraphApp.cpp index ba7eff2..d134026 100644 --- a/src/GraphicalUI/GraphApp.cpp +++ b/src/GraphicalUI/GraphApp.cpp @@ -13,19 +13,19 @@ static const double TIME_BETWEEN_FRAMES = (1000.f / FRAMES_PER_SECOND); GraphApp::GraphApp() { this->settings = std::make_shared(); - this->menuStack = std::make_shared>(); - this->window = std::make_shared(); + this->menuStack = std::make_shared(); + this->renderWindow = std::make_shared(); } void GraphApp::run() { - changeVideoMode(*this->window, this->settings->getVideoMode()); - this->menuStack->push(MainAppMenu(this->menuStack, this->settings, this->window)); + changeVideoMode(*this->renderWindow, this->settings->getVideoMode()); + this->menuStack->push(std::make_shared(this->menuStack, this->settings, this->renderWindow)); bool quit = false; double timeAtNextFrame = 0; sf::Clock clock; while (!quit) { - while (const std::optional event = this->window->pollEvent()) { + while (const std::optional event = this->renderWindow->pollEvent()) { if (event->is()) { quit = true; } @@ -34,16 +34,16 @@ void GraphApp::run() { if (!quit) { if (clock.getElapsedTime().asMilliseconds() > timeAtNextFrame) { timeAtNextFrame += TIME_BETWEEN_FRAMES; - this->menuStack->top().computeFrame(); + this->menuStack->top()->computeFrame(); if (this->menuStack->empty()) { quit = true; } else { - this->menuStack->top().drawFrame(); + this->menuStack->top()->drawFrame(); } } } } - window->close(); + renderWindow->close(); } diff --git a/src/GraphicalUI/GraphApp.h b/src/GraphicalUI/GraphApp.h index effaac8..4f2c694 100644 --- a/src/GraphicalUI/GraphApp.h +++ b/src/GraphicalUI/GraphApp.h @@ -11,8 +11,8 @@ class GraphApp { private: std::shared_ptr settings; - std::shared_ptr> menuStack; - std::shared_ptr window; + std::shared_ptr menuStack; + std::shared_ptr renderWindow; public: GraphApp(); diff --git a/src/GraphicalUI/Keybinds.cpp b/src/GraphicalUI/Keybinds.cpp new file mode 100644 index 0000000..9b6ac5c --- /dev/null +++ b/src/GraphicalUI/Keybinds.cpp @@ -0,0 +1,81 @@ +#include "Keybinds.h" + +#include "../Core/Action.h" + +#include +#include +#include +#include + + +Keybinds::Keybinds(int layoutNumber) : + layoutNumber(layoutNumber) { + + for (Action action : ACTION_LIST_IN_ORDER) { + this->keybinds.insert({action, std::set()}); + } + + this->loadKeybindsFromFile(); +} + +void Keybinds::loadKeybindsFromFile() { + std::ifstream layoutFile("data/config/keybinds/layout" + std::to_string(this->layoutNumber) + ".bin", std::ios::binary); + + for (Action action : ACTION_LIST_IN_ORDER) { + this->keybinds.at(action).clear(); + } + + char byte; + while (layoutFile.get(&byte, 1)) { + Action action = Action(byte); + + do { + layoutFile.get(&byte, 1); + if (byte != 0xFF) { + this->keybinds.at(action).insert(sfKey(byte)); + } + } while (byte != 0xFF); + } +} + +void Keybinds::saveKeybindsToFile() const { + std::ofstream layoutFile("data/config/keybinds/layout" + std::to_string(this->layoutNumber) + ".bin", std::ios::trunc | std::ios::binary); + + char byte; + for (Action action : ACTION_LIST_IN_ORDER) { + byte = action; + layoutFile.write(&byte, 1); + + for (sfKey key : this->keybinds.at(action)) { + byte = (int) key; + layoutFile.write(&byte, 1); + } + + byte = 0xFF; + layoutFile.write(&byte, 1); + } +} + +void Keybinds::addKey(Action action, sfKey key) { + this->keybinds.at(action).insert(key); +} + +void Keybinds::clearKeys(Action action) { + this->keybinds.at(action).clear(); +} + +const std::set Keybinds::getActions(sfKey key) const { + std::set actions; + + for (const auto& [action, keys] : this->keybinds) { + if (keys.contains(key)) { + actions.insert(action); + } + } + + return actions; +} + +const std::set& Keybinds::getKeybinds(Action action) const { + return this->keybinds.at(action); +} diff --git a/src/GraphicalUI/Keybinds.h b/src/GraphicalUI/Keybinds.h index 4c62f4c..e219520 100644 --- a/src/GraphicalUI/Keybinds.h +++ b/src/GraphicalUI/Keybinds.h @@ -3,7 +3,7 @@ #include "../Core/Action.h" #include -#include +#include #include using sfKey = sf::Keyboard::Key; @@ -14,7 +14,7 @@ static const int CUSTOMIZABLE_KEYBINDS = NUMBER_OF_KEYBINDS - 1; class Keybinds { private: - std::map> keybinds; + std::map> keybinds; int layoutNumber; public: @@ -28,7 +28,7 @@ class Keybinds { void clearKeys(Action action); - const std::vector& getActions(sfKey key) const; + const std::set getActions(sfKey key) const; - const std::vector& getKeybinds(Action action) const; + const std::set& getKeybinds(Action action) const; }; diff --git a/src/GraphicalUI/Settings.h b/src/GraphicalUI/Settings.h index e088197..4531aa9 100644 --- a/src/GraphicalUI/Settings.h +++ b/src/GraphicalUI/Settings.h @@ -10,6 +10,8 @@ static const int MAXIMUM_BOARD_WIDTH = 40; static const int MAXIMUM_BOARD_HEIGHT = 40; +static const int MAXIMUM_PIECES_SIZE = 10; + class Settings { private: diff --git a/src/GraphicalUI/main.cpp b/src/GraphicalUI/main.cpp index 983ff93..f238f6f 100644 --- a/src/GraphicalUI/main.cpp +++ b/src/GraphicalUI/main.cpp @@ -137,17 +137,17 @@ void resetKeybindFile(int layout) { keybinds.insert({HOLD, sfKey::Z}); } - char contentChar; + char byte; for (Action action : ACTION_LIST_IN_ORDER) { - contentChar = action; - layoutFile.write(&contentChar, 1); + byte = action; + layoutFile.write(&byte, 1); if (keybinds.contains(action)) { - contentChar = (int) keybinds.at(action); - layoutFile.write(&contentChar, 1); + byte = (int) keybinds.at(action); + layoutFile.write(&byte, 1); } - contentChar = 0xFF; - layoutFile.write(&contentChar, 1); + byte = 0xFF; + layoutFile.write(&byte, 1); } }