diff --git a/src/Core/GameBoard.cpp b/src/Core/GameBoard.cpp index d449ff5..2459569 100644 --- a/src/Core/GameBoard.cpp +++ b/src/Core/GameBoard.cpp @@ -10,6 +10,7 @@ #include #include #include +#include GameBoard::GameBoard(int boardWidth, int boardHeight, const std::shared_ptr& piecesList, int nextQueueLength) : diff --git a/src/Core/GameParameters.cpp b/src/Core/GameParameters.cpp index cb22008..e5f9efe 100644 --- a/src/Core/GameParameters.cpp +++ b/src/Core/GameParameters.cpp @@ -7,7 +7,7 @@ GameParameters::GameParameters(Gamemode gamemode, const Player& controls) : gamemode(gamemode), controls(controls) { - + this->reset(); } diff --git a/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp b/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp index 8f0a280..809b28c 100644 --- a/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp +++ b/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp @@ -120,8 +120,6 @@ void GamePlayingAppMenu::drawFrame() const { } } - // end coutdown - if (drawActivePiece) { // ghost piece sf::Color ghostColor = this->getColorOfBlock(this->game.getActivePiece()->getBlockType(), 100); diff --git a/src/GraphicalUI/AppMenus/StartUpAppMenu.cpp b/src/GraphicalUI/AppMenus/StartUpAppMenu.cpp new file mode 100644 index 0000000..efdb735 --- /dev/null +++ b/src/GraphicalUI/AppMenus/StartUpAppMenu.cpp @@ -0,0 +1,74 @@ +#include "StartUpAppMenu.h" + +#include "AppMenu.h" +#include "MainAppMenu.h" +#include "../PlayerCursor.h" + +#include +#include +#include + + +StartUpAppMenu::StartUpAppMenu(std::shared_ptr menuStack, std::shared_ptr settings, std::shared_ptr renderWindow) : + AppMenu(menuStack, settings, renderWindow), + playerCursor({LOADED_PIECES_SIZE + 1}) { + + this->playerCursor.goToPosition({MINIMUM_PIECES_SIZE, 0}); +} + +void StartUpAppMenu::computeFrame() { + this->updateMetaBinds(); + this->playerCursor.updatePosition(); + + if (this->playerCursor.getPosition().x < MINIMUM_PIECES_SIZE) { + if (this->playerCursor.movedLeft()) { + this->playerCursor.goToPosition({LOADED_PIECES_SIZE, 0}); + } + else { + this->playerCursor.goToPosition({MINIMUM_PIECES_SIZE, 0}); + } + } + + if (this->enterReleased) { + *this->settings = Settings(this->playerCursor.getPosition().x); + this->menuStack->pop(); + + if (this->settings->hasLoadedPieces()) { + this->menuStack->push(std::make_shared(this->menuStack, this->settings, this->renderWindow)); + } + else { + std::cout << "ERROR: COULD NOT LOAD PIECES" << std::endl; + std::cout << "ARGUMENT WAS: " << this->playerCursor.getPosition().x << std::endl; + } + } + else if (this->escReleased) { + this->menuStack->pop(); + } +} + +void StartUpAppMenu::drawFrame() const { + this->renderWindow->clear(sf::Color(200, 200, 200)); + + sf::Text text(this->pressStartFont, "", this->settings->getWindowSizeMultiplier() * 2); + text.setFillColor(sf::Color(0, 0, 0)); + text.setOutlineColor(sf::Color(255, 255, 255)); + + this->placeTitle(text, {}, "SELECT MAXIMUM PIECES SIZE", 10.f, {}); + this->placeTitle(text, this->playerCursor, "< " + std::to_string(this->playerCursor.getPosition().x) + " >", 25.f, this->playerCursor.getPosition()); + + text.setOutlineColor({0, 0, 0}); + if (this->playerCursor.getPosition().x <= 10) { + text.setFillColor({0, 255, 0}); + this->placeTitle(text, {}, "LOW LOAD TIME", 40.f, {}); + } + else if (this->playerCursor.getPosition().x <= 13) { + text.setFillColor({255, 255, 0}); + this->placeTitle(text, {}, "MEDIUM LOAD TIME", 40.f, {}); + } + else { + text.setFillColor({255, 0, 0}); + this->placeTitle(text, {}, "LONG LOAD TIME", 40.f, {}); + } + + this->renderWindow->display(); +} diff --git a/src/GraphicalUI/AppMenus/StartUpAppMenu.h b/src/GraphicalUI/AppMenus/StartUpAppMenu.h new file mode 100644 index 0000000..ba3d0ba --- /dev/null +++ b/src/GraphicalUI/AppMenus/StartUpAppMenu.h @@ -0,0 +1,21 @@ +#pragma once + +#include "AppMenu.h" +#include "../PlayerCursor.h" + +#include +#include +#include + + +class StartUpAppMenu : public AppMenu { + private: + PlayerCursor playerCursor; + + public: + StartUpAppMenu(std::shared_ptr menuStack, std::shared_ptr settings, std::shared_ptr renderWindow); + + void computeFrame() override; + + void drawFrame() const override; +}; diff --git a/src/GraphicalUI/GraphApp.cpp b/src/GraphicalUI/GraphApp.cpp index e9c76ec..5db3ea8 100644 --- a/src/GraphicalUI/GraphApp.cpp +++ b/src/GraphicalUI/GraphApp.cpp @@ -1,7 +1,7 @@ #include "GraphApp.h" #include "AppMenus/AppMenu.h" -#include "AppMenus/MainAppMenu.h" +#include "AppMenus/StartUpAppMenu.h" #include "Settings.h" #include @@ -11,15 +11,15 @@ static const double TIME_BETWEEN_FRAMES = (1000.f / FRAMES_PER_SECOND); -GraphApp::GraphApp(int maximumPiecesSize) { - this->settings = std::make_shared(maximumPiecesSize); +GraphApp::GraphApp() { + this->settings = std::make_shared(0); this->menuStack = std::make_shared(); this->renderWindow = std::make_shared(); } void GraphApp::run() { this->settings->changeVideoMode(*this->renderWindow); - this->menuStack->push(std::make_shared(this->menuStack, this->settings, this->renderWindow)); + this->menuStack->push(std::make_shared(this->menuStack, this->settings, this->renderWindow)); bool quit = false; double timeAtNextFrame = 0; diff --git a/src/GraphicalUI/GraphApp.h b/src/GraphicalUI/GraphApp.h index 3502088..4f2c694 100644 --- a/src/GraphicalUI/GraphApp.h +++ b/src/GraphicalUI/GraphApp.h @@ -15,7 +15,7 @@ class GraphApp { std::shared_ptr renderWindow; public: - GraphApp(int maximumPiecesSize); + GraphApp(); void run(); }; diff --git a/src/GraphicalUI/Settings.cpp b/src/GraphicalUI/Settings.cpp index afef4bf..9c5ee00 100644 --- a/src/GraphicalUI/Settings.cpp +++ b/src/GraphicalUI/Settings.cpp @@ -2,7 +2,9 @@ #include "../Core/Menu.h" #include "Keybinds.h" +#include "PiecesType.h" +#include #include #include #include @@ -15,9 +17,26 @@ static const int DISTRIBUTION_MAX = 10; Settings::Settings(int maximumPiecesSize) { - this->maximumPiecesSize = maximumPiecesSize; - for (int i = 1; i <= this->maximumPiecesSize; i++) { - this->menu.getPiecesList().loadPieces(i); + if (maximumPiecesSize < MINIMUM_PIECES_SIZE || maximumPiecesSize > LOADED_PIECES_SIZE) { + this->maximumPiecesSize = 0; + this->loadedPieces = false; + } + else { + bool loaded = true; + int i = 1; + while (loaded && (i <= maximumPiecesSize)) { + loaded = this->menu.getPiecesList().loadPieces(i); + i++; + } + + if (loaded) { + this->maximumPiecesSize = maximumPiecesSize; + this->loadedPieces = true; + } + else { + this->maximumPiecesSize = 0; + this->loadedPieces = false; + } } this->keybinds.reserve(NUMBER_OF_KEYBINDS); @@ -68,50 +87,58 @@ void Settings::loadSettingsFromFile() { settingsFile.get(byte); this->menu.setBoardHeight(byte); - // piece distribution - settingsFile.get(byte); - this->menu.getPiecesList().setDistributionMode(DistributionMode(byte)); - - this->distributions.clear(); - this->distributions.push_back(0); - for (int i = 1; i <= 15; i++) { + if (this->loadedPieces) { + // piece distribution settingsFile.get(byte); - this->distributions.push_back(byte); - } - this->confirmDistribution(); + this->menu.getPiecesList().setDistributionMode(DistributionMode(byte)); - // selected pieces - char pieceType; - char pieceSize; - char lowByte; - char midByte; - char highByte; + this->distributions.clear(); + this->distributions.push_back(0); + for (int i = 1; i <= 15; i++) { + settingsFile.get(byte); + this->distributions.push_back(byte); + } + this->confirmDistribution(); - this->selectedPieces.clear(); - while (settingsFile.get(pieceType)) { - if (settingsFile.eof()) break; - - if (getSizeOfPieces(PiecesType(pieceType)) == 0) { - settingsFile.get(pieceSize); + // selected pieces + char pieceType; + char pieceSize; + char lowByte; + char midByte; + char highByte; - if (!(pieceSize > this->maximumPiecesSize)) { - this->selectedPieces.emplace_back(PiecesType(pieceType), pieceSize); - } - } - else { - 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->selectedPieces.clear(); + while (settingsFile.get(pieceType)) { + if (settingsFile.eof()) break; + + if (getSizeOfPieces(PiecesType(pieceType)) == 0) { + settingsFile.get(pieceSize); + + if (!(pieceSize > this->maximumPiecesSize)) { + this->selectedPieces.emplace_back(PiecesType(pieceType), pieceSize); + } + } + else { + 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(); + } + else { + this->distributions.clear(); + this->selectedPieces.clear(); } - this->confirmSelectedPieces(); } void Settings::saveSettingsToFile() const { + if (!this->loadedPieces) return; + this->keybinds.at(CUSTOMIZABLE_KEYBINDS).saveKeybindsToFile(); std::ofstream settingsFile("data/config/settings.bin", std::ios::trunc | std::ios::binary); @@ -247,16 +274,21 @@ void Settings::setGamemode(Gamemode gamemode) { } void Settings::selectPieces(PiecesType type, int value) { + if (!this->loadedPieces) return; + this->selectedPieces.emplace_back(type, value); } void Settings::unselectPieces(int index) { + if (!this->loadedPieces) return; if (index >= this->selectedPieces.size()) return; this->selectedPieces.erase(this->selectedPieces.begin() + index); } void Settings::confirmSelectedPieces() { + if (!this->loadedPieces) return; + this->menu.getPiecesList().unselectAll(); if (this->getSelectedPieces().size() == 0) { @@ -281,6 +313,7 @@ void Settings::confirmSelectedPieces() { } bool Settings::increaseDistribution(int size) { + if (!this->loadedPieces) return false; if (size < 1 || size > this->maximumPiecesSize) return false; if (this->distributions.at(size) < DISTRIBUTION_MAX) { @@ -291,6 +324,7 @@ bool Settings::increaseDistribution(int size) { } bool Settings::decreaseDistribution(int size) { + if (!this->loadedPieces) return false; if (size < 1 || size > this->maximumPiecesSize) return false; if (this->distributions.at(size) > 0) { @@ -301,6 +335,8 @@ bool Settings::decreaseDistribution(int size) { } void Settings::confirmDistribution() { + if (!this->loadedPieces) return; + for (int i = 1; i <= 15; i++) { this->menu.getPiecesList().changeCustomDistribution(i, (double) 1 / (this->distributions.at(i) + 0.001)); } @@ -318,6 +354,10 @@ int Settings::getMaximumPiecesSize() const { return this->maximumPiecesSize; } +bool Settings::hasLoadedPieces() const { + return this->loadedPieces; +} + int Settings::getKeybindsLayout() const { return this->chosenKeybinds; } diff --git a/src/GraphicalUI/Settings.h b/src/GraphicalUI/Settings.h index f3cb917..e1c6897 100644 --- a/src/GraphicalUI/Settings.h +++ b/src/GraphicalUI/Settings.h @@ -1,21 +1,33 @@ #pragma once #include "../Core/Menu.h" -#include "StartUpMenu.h" #include "Keybinds.h" #include "PiecesType.h" -#include #include +#include static const int MAXIMUM_BOARD_WIDTH = 40; static const int MAXIMUM_BOARD_HEIGHT = 40; +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 Settings { private: Menu menu; int maximumPiecesSize; + bool loadedPieces; std::vector keybinds; int chosenKeybinds; int windowSizeMode; @@ -67,6 +79,8 @@ class Settings { int getMaximumPiecesSize() const; + bool hasLoadedPieces() const; + int getKeybindsLayout() const; Gamemode getGamemode() const; diff --git a/src/GraphicalUI/StartUpMenu.cpp b/src/GraphicalUI/StartUpMenu.cpp deleted file mode 100644 index facb931..0000000 --- a/src/GraphicalUI/StartUpMenu.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#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 deleted file mode 100644 index 585e686..0000000 --- a/src/GraphicalUI/StartUpMenu.h +++ /dev/null @@ -1,28 +0,0 @@ -#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 c7cd856..16b7922 100644 --- a/src/GraphicalUI/main.cpp +++ b/src/GraphicalUI/main.cpp @@ -1,4 +1,3 @@ -#include "StartUpMenu.h" #include "GraphApp.h" #include "../Pieces/PiecesFiles.h" @@ -31,10 +30,7 @@ int main() { } } - StartUpMenu startUp; - int maximumPiecesSize = startUp.getMaximumPiecesSize(); - - GraphApp UI(maximumPiecesSize); + GraphApp UI; UI.run(); return 0;