From 46ebb88ef286eb11b25f4d74016bf0eccc774e35 Mon Sep 17 00:00:00 2001 From: zulianc Date: Mon, 31 Mar 2025 21:14:06 +0200 Subject: [PATCH] we can load polyos in the app now --- src/Core/PiecesList.cpp | 1 + .../AppMenus/GamePiecesAppMenu.cpp | 94 ++++++++++++++----- src/GraphicalUI/AppMenus/StartUpAppMenu.cpp | 6 +- src/GraphicalUI/Settings.cpp | 4 +- src/GraphicalUI/Settings.h | 6 +- src/GraphicalUI/main.cpp | 2 +- 6 files changed, 78 insertions(+), 35 deletions(-) diff --git a/src/Core/PiecesList.cpp b/src/Core/PiecesList.cpp index f7a0e5a..45ba910 100644 --- a/src/Core/PiecesList.cpp +++ b/src/Core/PiecesList.cpp @@ -29,6 +29,7 @@ PiecesList::PiecesList() { bool PiecesList::loadPieces(int size) { if (size < 1) return false; + if (size <= this->highestLoadedSize) return true; PiecesFiles piecesFiles; for (int i = this->highestLoadedSize + 1; i <= size; i++) { diff --git a/src/GraphicalUI/AppMenus/GamePiecesAppMenu.cpp b/src/GraphicalUI/AppMenus/GamePiecesAppMenu.cpp index 4970c6b..ce31b8c 100644 --- a/src/GraphicalUI/AppMenus/GamePiecesAppMenu.cpp +++ b/src/GraphicalUI/AppMenus/GamePiecesAppMenu.cpp @@ -16,6 +16,10 @@ GamePiecesAppMenu::GamePiecesAppMenu(std::shared_ptr menuStack, std:: for (int i = 1; i <= this->settings->getMaximumPiecesSize(); i++) { this->playerCursor.addRow(i + 1, this->settings->getMenu().readPiecesList().getNumberOfPieces(i) + 4); } + + if (this->settings->getMaximumPiecesSize() < GENERATED_PIECES_SIZE) { + this->playerCursor.addRow(this->settings->getMaximumPiecesSize() + 2, 1); + } } void GamePiecesAppMenu::computeFrame() { @@ -30,7 +34,20 @@ void GamePiecesAppMenu::computeFrame() { if (this->playerCursor.getPosition().y == 0) { this->menuStack->push(std::make_shared(this->menuStack, this->settings, this->renderWindow)); } - if (this->playerCursor.getPosition().y > 1) { + + if (this->playerCursor.getPosition().y == (this->settings->getMaximumPiecesSize() + 2)) { + int newMaxSize = this->settings->getMaximumPiecesSize() + 1; + this->settings->loadPieces(newMaxSize); + + this->playerCursor.removeRow(newMaxSize + 1); + this->playerCursor.addRow(newMaxSize + 1, this->settings->getMenu().readPiecesList().getNumberOfPieces(newMaxSize) + 4); + this->playerCursor.goToPosition({0u, newMaxSize + 1u}); + + if (newMaxSize < GENERATED_PIECES_SIZE) { + this->playerCursor.addRow(newMaxSize + 2, 1); + } + } + else if (this->playerCursor.getPosition().y > 1) { if (this->playerCursor.getPosition().x >= 4) { this->settings->selectPieces(createSinglePieceType(this->playerCursor.getPosition().y - 1), this->playerCursor.getPosition().x - 4); } @@ -78,7 +95,8 @@ 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, this->settings->getMaximumPiecesSize() - 2); + bool addExtraLine = (this->settings->getMaximumPiecesSize() < GENERATED_PIECES_SIZE); + int firstElem = std::clamp(((int) this->playerCursor.getPosition().y) - 2, 1, this->settings->getMaximumPiecesSize() - 2 + (addExtraLine ? 1 : 0)); this->drawRow(firstElem, 25.f, drawFromFirstElem); this->drawRow(firstElem + 1, 35.f, drawFromFirstElem); this->drawRow(firstElem + 2, 45.f, drawFromFirstElem); @@ -140,31 +158,57 @@ void GamePiecesAppMenu::drawSelectedPiecesRow(float yPos) const { } void GamePiecesAppMenu::drawRow(int piecesSize, float yPos, bool drawFromFirstElem) const { - int numberOfPieces = this->settings->getMenu().readPiecesList().getNumberOfPieces(piecesSize); - int firstElem = (drawFromFirstElem) ? -4 : std::max(((int) this->playerCursor.getPosition().x) - 7, -4); - - sf::Text text(this->pressStartFont, "", this->settings->getWindowSizeMultiplier()); - text.setFillColor({0, 0, 0}); - text.setOutlineColor({255, 255, 255}); - - this->placeText(text, {}, "SIZE " + std::to_string(piecesSize), 1.f, yPos, {}); - - for (int i = 0; i < 7; i++) { - if (i + firstElem >= numberOfPieces) return; - - if ((i + firstElem) < 0) { - switch (i + firstElem) { - case -4 : {this->placeText(text, this->playerCursor, "ALL", 10.f + (i * 10.f), yPos, sf::Vector2u{0, piecesSize + 1u}); break;} - case -3 : {this->placeText(text, this->playerCursor, "CONVEX", 10.f + (i * 10.f), yPos, sf::Vector2u{1, piecesSize + 1u}); break;} - case -2 : {this->placeText(text, this->playerCursor, "HOLELESS", 10.f + (i * 10.f), yPos, sf::Vector2u{2, piecesSize + 1u}); break;} - case -1 : {this->placeText(text, this->playerCursor, "OTHER", 10.f + (i * 10.f), yPos, sf::Vector2u{3, piecesSize + 1u}); break;} - } + if (piecesSize > this->settings->getMaximumPiecesSize()) { + sf::Text text(this->pressStartFont, "", this->settings->getWindowSizeMultiplier() * 2); + text.setOutlineThickness(this->settings->getWindowSizeMultiplier() / 2); + if (this->playerCursor.getPosition().y == (piecesSize + 1)) { + text.setOutlineColor({255, 255, 255}); } else { - const Piece& piece = this->settings->getMenu().readPiecesList().lookAtPiece({piecesSize, firstElem + i}); - int cellSize = (8 * this->settings->getWindowSizeMultiplier()) / (piece.getLength()); - sf::FloatRect piecePosition(sf::Vector2f(10.f + (i * 10.f), yPos - 4.f) * (float) this->settings->getWindowSizeMultiplier(), sf::Vector2f(8 , 8) * (float) this->settings->getWindowSizeMultiplier()); - this->drawPiece(piece, cellSize, piecePosition, this->playerCursor.getPosition() == sf::Vector2u{i + firstElem + 4u, piecesSize + 1u}); + text.setOutlineColor({0, 0, 0}); + } + + std::string sizeString = "LOAD SIZE " + std::to_string(piecesSize) + "? "; + if (piecesSize <= 10) { + text.setFillColor({0, 255, 0}); + this->placeText(text, {}, sizeString + "(LOW LOAD TIME)", 1.f, yPos, {}); + } + else if (piecesSize <= 13) { + text.setFillColor({255, 255, 0}); + this->placeText(text, {}, sizeString + "(MEDIUM LOAD TIME)", 1.f, yPos, {}); + } + else { + text.setFillColor({255, 0, 0}); + this->placeText(text, {}, sizeString + "(LONG LOAD TIME)", 1.f, yPos, {}); + } + } + else { + int numberOfPieces = this->settings->getMenu().readPiecesList().getNumberOfPieces(piecesSize); + int firstElem = (drawFromFirstElem) ? -4 : std::max(((int) this->playerCursor.getPosition().x) - 7, -4); + + sf::Text text(this->pressStartFont, "", this->settings->getWindowSizeMultiplier()); + text.setFillColor({0, 0, 0}); + text.setOutlineColor({255, 255, 255}); + + this->placeText(text, {}, "SIZE " + std::to_string(piecesSize), 1.f, yPos, {}); + + for (int i = 0; i < 7; i++) { + if (i + firstElem >= numberOfPieces) return; + + if ((i + firstElem) < 0) { + switch (i + firstElem) { + case -4 : {this->placeText(text, this->playerCursor, "ALL", 10.f + (i * 10.f), yPos, sf::Vector2u{0, piecesSize + 1u}); break;} + case -3 : {this->placeText(text, this->playerCursor, "CONVEX", 10.f + (i * 10.f), yPos, sf::Vector2u{1, piecesSize + 1u}); break;} + case -2 : {this->placeText(text, this->playerCursor, "HOLELESS", 10.f + (i * 10.f), yPos, sf::Vector2u{2, piecesSize + 1u}); break;} + case -1 : {this->placeText(text, this->playerCursor, "OTHER", 10.f + (i * 10.f), yPos, sf::Vector2u{3, piecesSize + 1u}); break;} + } + } + else { + const Piece& piece = this->settings->getMenu().readPiecesList().lookAtPiece({piecesSize, firstElem + i}); + int cellSize = (8 * this->settings->getWindowSizeMultiplier()) / (piece.getLength()); + sf::FloatRect piecePosition(sf::Vector2f(10.f + (i * 10.f), yPos - 4.f) * (float) this->settings->getWindowSizeMultiplier(), sf::Vector2f(8 , 8) * (float) this->settings->getWindowSizeMultiplier()); + this->drawPiece(piece, cellSize, piecePosition, this->playerCursor.getPosition() == sf::Vector2u{i + firstElem + 4u, piecesSize + 1u}); + } } } } diff --git a/src/GraphicalUI/AppMenus/StartUpAppMenu.cpp b/src/GraphicalUI/AppMenus/StartUpAppMenu.cpp index 74fefb5..c11f4ae 100644 --- a/src/GraphicalUI/AppMenus/StartUpAppMenu.cpp +++ b/src/GraphicalUI/AppMenus/StartUpAppMenu.cpp @@ -12,9 +12,9 @@ StartUpAppMenu::StartUpAppMenu(std::shared_ptr menuStack, std::shared_ptr settings, std::shared_ptr renderWindow) : AppMenu(menuStack, settings, renderWindow), - playerCursor({LOADED_PIECES_SIZE + 1}) { + playerCursor({GENERATED_PIECES_SIZE + 1}) { - this->playerCursor.goToPosition({(unsigned int) std::clamp(this->settings->getMaximumPiecesSize(), MINIMUM_PIECES_SIZE, LOADED_PIECES_SIZE), 0u}); + this->playerCursor.goToPosition({(unsigned int) std::clamp(this->settings->getMaximumPiecesSize(), MINIMUM_PIECES_SIZE, GENERATED_PIECES_SIZE), 0u}); } void StartUpAppMenu::computeFrame() { @@ -23,7 +23,7 @@ void StartUpAppMenu::computeFrame() { if (this->playerCursor.getPosition().x < MINIMUM_PIECES_SIZE) { if (this->playerCursor.movedLeft()) { - this->playerCursor.goToPosition({LOADED_PIECES_SIZE, 0}); + this->playerCursor.goToPosition({GENERATED_PIECES_SIZE, 0}); } else { this->playerCursor.goToPosition({MINIMUM_PIECES_SIZE, 0}); diff --git a/src/GraphicalUI/Settings.cpp b/src/GraphicalUI/Settings.cpp index 6a02bb8..3f2eb24 100644 --- a/src/GraphicalUI/Settings.cpp +++ b/src/GraphicalUI/Settings.cpp @@ -31,8 +31,8 @@ void Settings::loadPieces(int maximumPiecesSizeRequest) { if (maximumPiecesSizeRequest < MINIMUM_PIECES_SIZE) { maximumPiecesSizeRequest = MINIMUM_PIECES_SIZE; } - else if (maximumPiecesSizeRequest > LOADED_PIECES_SIZE || maximumPiecesSizeRequest > MAXIMUM_PIECES_SIZE) { - maximumPiecesSizeRequest = LOADED_PIECES_SIZE; + else if (maximumPiecesSizeRequest > GENERATED_PIECES_SIZE || maximumPiecesSizeRequest > MAXIMUM_PIECES_SIZE) { + maximumPiecesSizeRequest = GENERATED_PIECES_SIZE; } bool succeeded = true; diff --git a/src/GraphicalUI/Settings.h b/src/GraphicalUI/Settings.h index 71f0c69..c4cf871 100644 --- a/src/GraphicalUI/Settings.h +++ b/src/GraphicalUI/Settings.h @@ -18,9 +18,9 @@ static const int MAXIMUM_PIECES_SIZE = 15; #define __JMINOS_RELEASE__ #ifdef __JMINOS_RELEASE__ -static const int LOADED_PIECES_SIZE = 15; +static const int GENERATED_PIECES_SIZE = 15; #else -static const int LOADED_PIECES_SIZE = 10; +static const int GENERATED_PIECES_SIZE = 10; #endif static const std::pair DEFAULT_SELECTION = {ALL_PIECES, MINIMUM_PIECES_SIZE}; @@ -42,10 +42,8 @@ class Settings { public: Settings(bool loadPieces); - private: void loadPieces(int maximumPiecesSizeRequest); - public: void loadSettingsFromFile(bool loadPieces, std::optional maximumPiecesSizeRequest); void saveSettingsToFile() const; diff --git a/src/GraphicalUI/main.cpp b/src/GraphicalUI/main.cpp index 6436539..eef0472 100644 --- a/src/GraphicalUI/main.cpp +++ b/src/GraphicalUI/main.cpp @@ -13,7 +13,7 @@ int main() { std::srand(std::time(NULL)); PiecesFiles pf; - for (int i = 1; i <= LOADED_PIECES_SIZE; i++) { + for (int i = 1; i <= GENERATED_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);