diff --git a/src/GraphicalUI/AppMenus/GameDistributionAppMenu.cpp b/src/GraphicalUI/AppMenus/GameDistributionAppMenu.cpp index 7d5e2a8..56feafb 100644 --- a/src/GraphicalUI/AppMenus/GameDistributionAppMenu.cpp +++ b/src/GraphicalUI/AppMenus/GameDistributionAppMenu.cpp @@ -12,7 +12,7 @@ GameDistributionAppMenu::GameDistributionAppMenu(std::shared_ptr menu AppMenu(menuStack, settings, renderWindow), playerCursor({1}) { - for (int i = 1; i <= MAXIMUM_PIECES_SIZE; i++) { + for (int i = 1; i <= this->settings->getMaximumPiecesSize(); i++) { this->playerCursor.addRow(i, 1); } } @@ -65,7 +65,7 @@ void GameDistributionAppMenu::drawFrame() const { const DistributionMode distributionMode = this->settings->getMenu().readPiecesList().getDistributionMode(); const std::vector& distributions = this->settings->getDistributions(); - int firstElem = std::clamp(((int) this->playerCursor.getPosition().y) - 1, 0, MAXIMUM_PIECES_SIZE - 3); + int firstElem = std::clamp(((int) this->playerCursor.getPosition().y) - 1, 0, this->settings->getMaximumPiecesSize() - 3); if (firstElem == 0) { this->placeText(text, this->playerCursor, "< DISTRIBUTION MODE: " + getPiecesDistributionName(distributionMode) + " >", 5.f, 15.f, sf::Vector2u{0, 0}); } diff --git a/src/GraphicalUI/AppMenus/GamePiecesAppMenu.cpp b/src/GraphicalUI/AppMenus/GamePiecesAppMenu.cpp index 955a240..0e8d164 100644 --- a/src/GraphicalUI/AppMenus/GamePiecesAppMenu.cpp +++ b/src/GraphicalUI/AppMenus/GamePiecesAppMenu.cpp @@ -13,7 +13,7 @@ GamePiecesAppMenu::GamePiecesAppMenu(std::shared_ptr menuStack, std:: AppMenu(menuStack, settings, renderWindow), playerCursor({1, (unsigned int) this->settings->getSelectedPieces().size() + 1u}) { - for (int i = 1; i <= MAXIMUM_PIECES_SIZE; i++) { + for (int i = 1; i <= this->settings->getMaximumPiecesSize(); i++) { this->playerCursor.addRow(i + 1, this->settings->getMenu().readPiecesList().getNumberOfPieces(i) + 4); } } @@ -53,11 +53,7 @@ void GamePiecesAppMenu::computeFrame() { } } else { - if (this->settings->getSelectedPieces().size() == 0) { - this->settings->selectPieces(ALL_PIECES, 4); - } this->settings->confirmSelectedPieces(); - this->menuStack->pop(); } } @@ -82,7 +78,7 @@ void GamePiecesAppMenu::drawFrame() const { this->drawSelectedPiecesRow(15.f); bool drawFromFirstElem = (this->playerCursor.getPosition().y == 1); - int firstElem = std::clamp(((int) this->playerCursor.getPosition().y) - 2, 1, MAXIMUM_PIECES_SIZE - 2); + int firstElem = std::clamp(((int) this->playerCursor.getPosition().y) - 2, 1, this->settings->getMaximumPiecesSize() - 2); this->drawRow(firstElem, 25.f, drawFromFirstElem); this->drawRow(firstElem + 1, 35.f, drawFromFirstElem); this->drawRow(firstElem + 2, 45.f, drawFromFirstElem); diff --git a/src/GraphicalUI/GraphApp.cpp b/src/GraphicalUI/GraphApp.cpp index c802bcf..e9c76ec 100644 --- a/src/GraphicalUI/GraphApp.cpp +++ b/src/GraphicalUI/GraphApp.cpp @@ -11,8 +11,8 @@ static const double TIME_BETWEEN_FRAMES = (1000.f / FRAMES_PER_SECOND); -GraphApp::GraphApp() { - this->settings = std::make_shared(); +GraphApp::GraphApp(int maximumPiecesSize) { + this->settings = std::make_shared(maximumPiecesSize); this->menuStack = std::make_shared(); this->renderWindow = std::make_shared(); } diff --git a/src/GraphicalUI/GraphApp.h b/src/GraphicalUI/GraphApp.h index 4f2c694..3502088 100644 --- a/src/GraphicalUI/GraphApp.h +++ b/src/GraphicalUI/GraphApp.h @@ -15,7 +15,7 @@ class GraphApp { std::shared_ptr renderWindow; public: - GraphApp(); + GraphApp(int maximumPiecesSize); void run(); }; diff --git a/src/GraphicalUI/Settings.cpp b/src/GraphicalUI/Settings.cpp index 44a31ad..afef4bf 100644 --- a/src/GraphicalUI/Settings.cpp +++ b/src/GraphicalUI/Settings.cpp @@ -14,8 +14,9 @@ static const int START_TIMER_MAX = 4; static const int DISTRIBUTION_MAX = 10; -Settings::Settings() { - for (int i = 1; i <= MAXIMUM_PIECES_SIZE; i++) { +Settings::Settings(int maximumPiecesSize) { + this->maximumPiecesSize = maximumPiecesSize; + for (int i = 1; i <= this->maximumPiecesSize; i++) { this->menu.getPiecesList().loadPieces(i); } @@ -81,7 +82,7 @@ void Settings::loadSettingsFromFile() { // selected pieces char pieceType; - char pieceValue; + char pieceSize; char lowByte; char midByte; char highByte; @@ -91,15 +92,20 @@ void Settings::loadSettingsFromFile() { if (settingsFile.eof()) break; if (getSizeOfPieces(PiecesType(pieceType)) == 0) { - settingsFile.get(pieceValue); - this->selectedPieces.emplace_back(PiecesType(pieceType), pieceValue); + settingsFile.get(pieceSize); + + if (!(pieceSize > this->maximumPiecesSize)) { + this->selectedPieces.emplace_back(PiecesType(pieceType), pieceSize); + } } else { - settingsFile.get(lowByte); - settingsFile.get(midByte); - settingsFile.get(highByte); - int pieceNumber = ((unsigned char) lowByte) + ((unsigned char) midByte << 8) + ((unsigned char) highByte << 16); - this->selectedPieces.emplace_back(PiecesType(pieceType), pieceNumber); + if (!(getSizeOfPieces(PiecesType(pieceType)) > this->maximumPiecesSize)) { + settingsFile.get(lowByte); + settingsFile.get(midByte); + settingsFile.get(highByte); + int pieceNumber = ((unsigned char) lowByte) + ((unsigned char) midByte << 8) + ((unsigned char) highByte << 16); + this->selectedPieces.emplace_back(PiecesType(pieceType), pieceNumber); + } } } this->confirmSelectedPieces(); @@ -253,6 +259,10 @@ void Settings::unselectPieces(int index) { void Settings::confirmSelectedPieces() { this->menu.getPiecesList().unselectAll(); + if (this->getSelectedPieces().size() == 0) { + this->selectedPieces.push_back(DEFAULT_SELECTION); + } + for (const auto& [type, value] : this->selectedPieces) { int size = getSizeOfPieces(type); @@ -265,15 +275,13 @@ void Settings::confirmSelectedPieces() { } } else { - if (size > MAXIMUM_PIECES_SIZE) return; - this->menu.getPiecesList().selectPiece(size, value); } } } bool Settings::increaseDistribution(int size) { - if (size < 1 || size > MAXIMUM_PIECES_SIZE) return false; + if (size < 1 || size > this->maximumPiecesSize) return false; if (this->distributions.at(size) < DISTRIBUTION_MAX) { this->distributions.at(size)++; @@ -283,7 +291,7 @@ bool Settings::increaseDistribution(int size) { } bool Settings::decreaseDistribution(int size) { - if (size < 1 || size > MAXIMUM_PIECES_SIZE) return false; + if (size < 1 || size > this->maximumPiecesSize) return false; if (this->distributions.at(size) > 0) { this->distributions.at(size)--; @@ -306,6 +314,10 @@ Keybinds& Settings::getKeybinds() { return this->keybinds.at(this->chosenKeybinds); } +int Settings::getMaximumPiecesSize() const { + return this->maximumPiecesSize; +} + int Settings::getKeybindsLayout() const { return this->chosenKeybinds; } diff --git a/src/GraphicalUI/Settings.h b/src/GraphicalUI/Settings.h index 606b87d..f3cb917 100644 --- a/src/GraphicalUI/Settings.h +++ b/src/GraphicalUI/Settings.h @@ -1,6 +1,7 @@ #pragma once #include "../Core/Menu.h" +#include "StartUpMenu.h" #include "Keybinds.h" #include "PiecesType.h" @@ -10,17 +11,11 @@ static const int MAXIMUM_BOARD_WIDTH = 40; static const int MAXIMUM_BOARD_HEIGHT = 40; -//#define __JMINOS_RELEASE__ -#ifdef __JMINOS_RELEASE__ -static const int MAXIMUM_PIECES_SIZE = 15; -#else -static const int MAXIMUM_PIECES_SIZE = 10; -#endif - class Settings { private: Menu menu; + int maximumPiecesSize; std::vector keybinds; int chosenKeybinds; int windowSizeMode; @@ -30,7 +25,7 @@ class Settings { std::vector distributions; public: - Settings(); + Settings(int maximumPiecesSize); void loadSettingsFromFile(); @@ -70,6 +65,8 @@ class Settings { Keybinds& getKeybinds(); + int getMaximumPiecesSize() const; + int getKeybindsLayout() const; Gamemode getGamemode() const; diff --git a/src/GraphicalUI/StartUpMenu.cpp b/src/GraphicalUI/StartUpMenu.cpp new file mode 100644 index 0000000..facb931 --- /dev/null +++ b/src/GraphicalUI/StartUpMenu.cpp @@ -0,0 +1,15 @@ +#include "StartUpMenu.h" + +#include "PlayerCursor.h" + + +StartUpMenu::StartUpMenu() : + playerCursor({LOADED_PIECES_SIZE}) { + + this->playerCursor.goToPosition({MINIMUM_PIECES_SIZE, 0}); +} + +int StartUpMenu::getMaximumPiecesSize() { + return LOADED_PIECES_SIZE; + //TODO +} diff --git a/src/GraphicalUI/StartUpMenu.h b/src/GraphicalUI/StartUpMenu.h new file mode 100644 index 0000000..585e686 --- /dev/null +++ b/src/GraphicalUI/StartUpMenu.h @@ -0,0 +1,28 @@ +#pragma once + +#include "PiecesType.h" +#include "PlayerCursor.h" + + +static const int MINIMUM_PIECES_SIZE = 4; +static const int MAXIMUM_PIECES_SIZE = 15; + +//#define __JMINOS_RELEASE__ +#ifdef __JMINOS_RELEASE__ +static const int LOADED_PIECES_SIZE = 15; +#else +static const int LOADED_PIECES_SIZE = 10; +#endif + +static const std::pair DEFAULT_SELECTION = {ALL_PIECES, MINIMUM_PIECES_SIZE}; + + +class StartUpMenu { + private: + PlayerCursor playerCursor; + + public: + StartUpMenu(); + + int getMaximumPiecesSize(); +}; diff --git a/src/GraphicalUI/main.cpp b/src/GraphicalUI/main.cpp index 46f3212..c7cd856 100644 --- a/src/GraphicalUI/main.cpp +++ b/src/GraphicalUI/main.cpp @@ -1,3 +1,4 @@ +#include "StartUpMenu.h" #include "GraphApp.h" #include "../Pieces/PiecesFiles.h" @@ -13,7 +14,7 @@ int main() { std::srand(std::time(NULL)); PiecesFiles pf; - for (int i = 1; i <= MAXIMUM_PIECES_SIZE; i++) { + for (int i = 1; i <= LOADED_PIECES_SIZE; i++) { if (!std::filesystem::exists("data/pieces/" + std::to_string(i) + "minos.bin")) { std::cout << "pieces files for size " << i << " not found, generating..." << std::endl; pf.savePieces(i); @@ -30,7 +31,10 @@ int main() { } } - GraphApp UI; + StartUpMenu startUp; + int maximumPiecesSize = startUp.getMaximumPiecesSize(); + + GraphApp UI(maximumPiecesSize); UI.run(); return 0;