From d5b51213c89a5d02e61f831f67d8d946700bcfb3 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sat, 19 Jul 2025 20:50:36 +0200 Subject: [PATCH 01/10] change Position and Polynomio structs (less memory usage) --- src/Core/GameBoard.cpp | 36 ++++++++--------- src/Core/GameBoard.h | 4 +- .../AppMenus/GamePlayingAppMenu.cpp | 6 +-- src/Pieces/Generator.cpp | 16 ++++---- src/Pieces/Generator.h | 2 +- src/Pieces/PiecesFiles.cpp | 2 +- src/Pieces/Polyomino.cpp | 40 +++++++++---------- src/Pieces/Polyomino.h | 4 +- src/Pieces/Position.h | 8 ++-- src/TextUI/TextApp.cpp | 10 ++--- 10 files changed, 64 insertions(+), 64 deletions(-) diff --git a/src/Core/GameBoard.cpp b/src/Core/GameBoard.cpp index 75a4ab1..c57784c 100644 --- a/src/Core/GameBoard.cpp +++ b/src/Core/GameBoard.cpp @@ -44,7 +44,7 @@ void GameBoard::initialize() { bool GameBoard::moveLeft() { this->movedLeftLast = true; - if (this->activePieceInWall(Position{-1, 0})) { + if (this->activePieceInWall(Position(-1, 0))) { return false; } else { @@ -57,7 +57,7 @@ bool GameBoard::moveLeft() { bool GameBoard::moveRight() { this->movedLeftLast = false; - if (this->activePieceInWall(Position{1, 0})) { + if (this->activePieceInWall(Position(1, 0))) { return false; } else { @@ -68,7 +68,7 @@ bool GameBoard::moveRight() { } bool GameBoard::moveDown() { - if (this->activePieceInWall(Position{0, -1})) { + if (this->activePieceInWall(Position(0, -1))) { return false; } else { @@ -100,10 +100,10 @@ bool GameBoard::rotate(Rotation rotation) { for (Position position : stored.getPositions()) { Position positionInGrid(position + this->activePiecePosition); safePositions.insert(positionInGrid); - safePositions.insert(positionInGrid + Position{0, 1}); - safePositions.insert(positionInGrid + Position{1, 0}); - safePositions.insert(positionInGrid + Position{0, -1}); - safePositions.insert(positionInGrid + Position{-1, 0}); + safePositions.insert(positionInGrid + Position(0, 1)); + safePositions.insert(positionInGrid + Position(1, 0)); + safePositions.insert(positionInGrid + Position(0, -1)); + safePositions.insert(positionInGrid + Position(-1, 0)); } // first try kicking the piece down @@ -147,18 +147,18 @@ bool GameBoard::tryKicking(bool testingBottom, const std::set& safePos // we first check the side to which the player moved last if (movedLeftLast) { if (overlapsLeft) { - if (this->tryFittingKickedPiece(safePositions, Position({-i, j}), overlapsLeft)) return true; + if (this->tryFittingKickedPiece(safePositions, Position(-i, j), overlapsLeft)) return true; } if (overlapsRight) { - if (this->tryFittingKickedPiece(safePositions, Position({+i, j}), overlapsRight)) return true; + if (this->tryFittingKickedPiece(safePositions, Position(+i, j), overlapsRight)) return true; } } else { if (overlapsRight) { - if (this->tryFittingKickedPiece(safePositions, Position({+i, j}), overlapsRight)) return true; + if (this->tryFittingKickedPiece(safePositions, Position(+i, j), overlapsRight)) return true; } if (overlapsLeft) { - if (this->tryFittingKickedPiece(safePositions, Position({-i, j}), overlapsLeft)) return true; + if (this->tryFittingKickedPiece(safePositions, Position(-i, j), overlapsLeft)) return true; } } @@ -265,11 +265,11 @@ bool GameBoard::activePieceInWall(const Position& shift) const { } bool GameBoard::touchesGround() const { - return this->activePieceInWall(Position{0, -1}); + return this->activePieceInWall(Position(0, -1)); } Position GameBoard::lowestPosition() const { - Position shift = Position{0, -1}; + Position shift = Position(0, -1); while (!activePieceInWall(shift)) { shift.y -= 1; } @@ -278,8 +278,8 @@ Position GameBoard::lowestPosition() const { } LineClear GameBoard::lockPiece() { - bool isLockedInPlace = (this->activePieceInWall(Position{0, 1}) && this->activePieceInWall(Position{1, 0}) - && this->activePieceInWall(Position{-1, 0}) && this->activePieceInWall(Position{0, -1})); + bool isLockedInPlace = (this->activePieceInWall(Position(0, 1)) && this->activePieceInWall(Position(1, 0)) + && this->activePieceInWall(Position(-1, 0)) && this->activePieceInWall(Position(0, -1))); for (Position position : this->activePiece->getPositions()) { this->board.changeBlock(position + this->activePiecePosition, this->activePiece->getBlockType()); @@ -345,7 +345,7 @@ std::ostream& operator<<(std::ostream& os, const GameBoard& gameboard) { // print only the position were the active piece is for (int y = gameboard.activePiecePosition.y + gameboard.activePiece->getLength() - 1; y >= gameboard.board.getBaseHeight(); y--) { for (int x = 0; x < gameboard.board.getWidth(); x++) { - bool hasActivePiece = gameboard.activePiece->getPositions().contains(Position{x, y} - gameboard.activePiecePosition); + bool hasActivePiece = gameboard.activePiece->getPositions().contains(Position(x, y) - gameboard.activePiecePosition); if (hasActivePiece) { os << "*"; } @@ -361,7 +361,7 @@ std::ostream& operator<<(std::ostream& os, const GameBoard& gameboard) { Block pieceBlockType = (gameboard.activePiece == nullptr) ? NOTHING : gameboard.activePiece->getBlockType(); for (int y = gameboard.board.getBaseHeight() - 1; y >= 0; y--) { for (int x = 0; x < gameboard.board.getWidth(); x++) { - bool hasActivePiece = (gameboard.activePiece == nullptr) ? false : gameboard.activePiece->getPositions().contains(Position{x, y} - gameboard.activePiecePosition); + bool hasActivePiece = (gameboard.activePiece == nullptr) ? false : gameboard.activePiece->getPositions().contains(Position(x, y) - gameboard.activePiecePosition); // the active piece takes visual priority over the board if (hasActivePiece) { @@ -369,7 +369,7 @@ std::ostream& operator<<(std::ostream& os, const GameBoard& gameboard) { os << "*"; } else { - Block block = gameboard.board.getBlock(Position{x, y}); + Block block = gameboard.board.getBlock(Position(x, y)); os << getConsoleColorCode(block); if (block != NOTHING) { os << "*"; diff --git a/src/Core/GameBoard.h b/src/Core/GameBoard.h index 19a0ae3..58480de 100644 --- a/src/Core/GameBoard.h +++ b/src/Core/GameBoard.h @@ -84,7 +84,7 @@ class GameBoard { * Check if one of the active piece's positions shifted by a specified position would overlap with a set of positions * @return If the shifted active piece overlaps with one of the position */ - bool activePieceOverlaps(const std::set& safePositions, const Position& shift = Position{0, 0}) const; + bool activePieceOverlaps(const std::set& safePositions, const Position& shift = Position(0, 0)) const; public: /** @@ -103,7 +103,7 @@ class GameBoard { * Checks if one of the active piece's positions touches a wall in the board * @return If the active piece is in a wall */ - bool activePieceInWall(const Position& shift = Position{0, 0}) const; + bool activePieceInWall(const Position& shift = Position(0, 0)) const; /** * Checks is the active piece as a wall directly below one of its position diff --git a/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp b/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp index 09772df..0245e0d 100644 --- a/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp +++ b/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp @@ -117,7 +117,7 @@ void GamePlayingAppMenu::drawFrame() const { // board for (int y = this->game.getBoard().getBaseHeight() + 9; y >= 0; y--) { for (int x = 0; x < this->game.getBoard().getWidth(); x++) { - Block block = this->game.getBoard().getBlock(Position{x, y}); + Block block = this->game.getBoard().getBlock(Position(x, y)); if (isBoardInvisible) block = NOTHING; sf::RectangleShape cell(cellSize); @@ -196,7 +196,7 @@ void GamePlayingAppMenu::drawFrame() const { for (int y = 0; y < this->game.getNextPieces().at(i).getLength(); y++) { for (int x = 0; x < this->game.getNextPieces().at(i).getLength(); x++) { sf::RectangleShape cell(nextCellSize); - if (this->game.getNextPieces().at(i).getPositions().contains(Position{x, y})) { + if (this->game.getNextPieces().at(i).getPositions().contains(Position(x, y))) { cell.setFillColor(pieceColor); lowestRank = y; } @@ -223,7 +223,7 @@ void GamePlayingAppMenu::drawFrame() const { for (int y = 0; y < this->game.getHeldPiece()->getLength(); y++) { for (int x = 0; x < this->game.getHeldPiece()->getLength(); x++) { sf::RectangleShape cell(holdCellSize); - if (this->game.getHeldPiece()->getPositions().contains(Position{x, y})) { + if (this->game.getHeldPiece()->getPositions().contains(Position(x, y))) { cell.setFillColor(color); } else { diff --git a/src/Pieces/Generator.cpp b/src/Pieces/Generator.cpp index 34d4af0..6068e19 100644 --- a/src/Pieces/Generator.cpp +++ b/src/Pieces/Generator.cpp @@ -11,19 +11,19 @@ Generator::Generator() { } -std::vector Generator::generatePolyominoes(int polyominoSize) { +std::vector&& Generator::generatePolyominoes(int polyominoSize) { this->validPolyominoes.clear(); this->currentTestedShape.clear(); // a polyomino has at least 1 square - if (polyominoSize < 1) return this->validPolyominoes; + if (polyominoSize < 1) return std::move(this->validPolyominoes); // always place the first cell at (0, 0) - this->currentTestedShape.insert(Position{0, 0}); + this->currentTestedShape.insert(Position(0, 0)); std::map candidatePositions; this->generate(polyominoSize, 0, 1, candidatePositions); - return this->validPolyominoes; + return std::move(this->validPolyominoes); } void Generator::generate(int polyominoSize, int lastAddedPositionNumber, int nextAvaibleNumber, std::map candidatePositions) { @@ -51,10 +51,10 @@ void Generator::generate(int polyominoSize, int lastAddedPositionNumber, int nex // generate the list of candidate positions for (const Position position : this->currentTestedShape) { - this->tryToAddCandidatePosition(Position{position.x, position.y + 1}, nextAvaibleNumber, candidatePositions); - this->tryToAddCandidatePosition(Position{position.x + 1, position.y}, nextAvaibleNumber, candidatePositions); - this->tryToAddCandidatePosition(Position{position.x, position.y - 1}, nextAvaibleNumber, candidatePositions); - this->tryToAddCandidatePosition(Position{position.x - 1, position.y}, nextAvaibleNumber, candidatePositions); + this->tryToAddCandidatePosition(Position(position.x, position.y + 1), nextAvaibleNumber, candidatePositions); + this->tryToAddCandidatePosition(Position(position.x + 1, position.y), nextAvaibleNumber, candidatePositions); + this->tryToAddCandidatePosition(Position(position.x, position.y - 1), nextAvaibleNumber, candidatePositions); + this->tryToAddCandidatePosition(Position(position.x - 1, position.y), nextAvaibleNumber, candidatePositions); } // try adding a square only to positions with a higher number than the last one diff --git a/src/Pieces/Generator.h b/src/Pieces/Generator.h index 47de7d5..3cdaeac 100644 --- a/src/Pieces/Generator.h +++ b/src/Pieces/Generator.h @@ -25,7 +25,7 @@ class Generator { * Generates the list of all one-sided polyominoes of the specified size * @return The list of polyominoes */ - std::vector generatePolyominoes(int polyominoSize); + std::vector&& generatePolyominoes(int polyominoSize); private: /** diff --git a/src/Pieces/PiecesFiles.cpp b/src/Pieces/PiecesFiles.cpp index 6500f41..6cc2fb6 100644 --- a/src/Pieces/PiecesFiles.cpp +++ b/src/Pieces/PiecesFiles.cpp @@ -115,7 +115,7 @@ bool PiecesFiles::loadPieces(int polyominoSize, std::vector& pieces, std: buffer >> positionByte; int x = ((unsigned char) positionByte & xMask) >> 4; int y = positionByte & yMask; - piecePositions.insert(Position{x, y}); + piecePositions.insert(Position(x, y)); } // create piece diff --git a/src/Pieces/Polyomino.cpp b/src/Pieces/Polyomino.cpp index a9fe094..0acbb83 100644 --- a/src/Pieces/Polyomino.cpp +++ b/src/Pieces/Polyomino.cpp @@ -27,12 +27,12 @@ Polyomino::Polyomino(const std::set& positions) { // we normalize here instead of calling this->normalize() to reduce the number of calculations for the generation algorithm std::set newPositions; for (Position position : positions) { - newPositions.insert(Position{position.x - minX, position.y - minY}); + newPositions.insert(Position(position.x - minX, position.y - minY)); } this->positions = std::move(newPositions); } -Polyomino::Polyomino(const std::set& positions, int length) : +Polyomino::Polyomino(const std::set& positions, std::int8_t length) : positions(positions), length(length) { } @@ -47,7 +47,7 @@ void Polyomino::normalize() { std::set newPositions; for (const Position position : this->positions) { - newPositions.insert(Position{position.x - minX, position.y - minY}); + newPositions.insert(Position(position.x - minX, position.y - minY)); } this->positions = std::move(newPositions); } @@ -55,7 +55,7 @@ void Polyomino::normalize() { void Polyomino::rotateCW() { std::set newPositions; for (const Position position : this->positions) { - newPositions.insert(Position{position.y, (length - 1) - (position.x)}); + newPositions.insert(Position(position.y, (length - 1) - (position.x))); } this->positions = std::move(newPositions); } @@ -63,7 +63,7 @@ void Polyomino::rotateCW() { void Polyomino::rotate180() { std::set newPositions; for (const Position position : this->positions) { - newPositions.insert(Position{(length - 1) - (position.x), (length - 1) - (position.y)}); + newPositions.insert(Position((length - 1) - (position.x), (length - 1) - (position.y))); } this->positions = std::move(newPositions); } @@ -71,7 +71,7 @@ void Polyomino::rotate180() { void Polyomino::rotateCCW() { std::set newPositions; for (const Position position : this->positions) { - newPositions.insert(Position{(length - 1) - (position.y), position.x}); + newPositions.insert(Position((length - 1) - (position.y), position.x)); } this->positions = std::move(newPositions); } @@ -166,7 +166,7 @@ void Polyomino::goToSpawnPosition() { // center the piece with an up bias std::set newPositions; for (const Position position : positions) { - newPositions.insert(Position{(position.x - minX) + (verticalEmptyLines / 2), (position.y - minY) + ((horizontalEmptyLines + 1) / 2)}); + newPositions.insert(Position((position.x - minX) + (verticalEmptyLines / 2), (position.y - minY) + ((horizontalEmptyLines + 1) / 2))); } this->positions = std::move(newPositions); } @@ -216,7 +216,7 @@ bool Polyomino::isConvex() const { bool startedColumn = false; bool completedColumn = false; for (int i = 0; i < this->length; i++) { - if (this->positions.contains(Position{i, j})) { + if (this->positions.contains(Position(i, j))) { if (completedLine) return false; else startedLine = true; } @@ -224,7 +224,7 @@ bool Polyomino::isConvex() const { if (startedLine) completedLine = true; } - if (this->positions.contains(Position{j, i})) { + if (this->positions.contains(Position(j, i))) { if (completedColumn) return false; else startedColumn = true; } @@ -240,10 +240,10 @@ bool Polyomino::hasHole() const { // add every empty square on the outer of the box containing the polyomino std::set emptyPositions; for (int i = 0; i < this->length - 1; i++) { - this->tryToInsertPosition(emptyPositions, Position{i, 0}); // up row - this->tryToInsertPosition(emptyPositions, Position{this->length - 1, i}); // rigth column - this->tryToInsertPosition(emptyPositions, Position{this->length - 1 - i, this->length - 1}); // bottom row - this->tryToInsertPosition(emptyPositions, Position{0, this->length - 1 - i}); // left column + this->tryToInsertPosition(emptyPositions, Position(i, 0)); // up row + this->tryToInsertPosition(emptyPositions, Position(this->length - 1, i)); // rigth column + this->tryToInsertPosition(emptyPositions, Position(this->length - 1 - i, this->length - 1)); // bottom row + this->tryToInsertPosition(emptyPositions, Position(0, this->length - 1 - i)); // left column } // if we didn't reached all empty squares in the box then there was some contained within the polyomino, i.e. there was a hole @@ -256,10 +256,10 @@ void Polyomino::tryToInsertPosition(std::set& emptyPositions, const Po // if it's a new empty square, try its neighbors emptyPositions.insert(candidate); - tryToInsertPosition(emptyPositions, Position{candidate.x, candidate.y + 1}); - tryToInsertPosition(emptyPositions, Position{candidate.x + 1, candidate.y}); - tryToInsertPosition(emptyPositions, Position{candidate.x, candidate.y - 1}); - tryToInsertPosition(emptyPositions, Position{candidate.x - 1, candidate.y}); + tryToInsertPosition(emptyPositions, Position(candidate.x, candidate.y + 1)); + tryToInsertPosition(emptyPositions, Position(candidate.x + 1, candidate.y)); + tryToInsertPosition(emptyPositions, Position(candidate.x, candidate.y - 1)); + tryToInsertPosition(emptyPositions, Position(candidate.x - 1, candidate.y)); } const std::set& Polyomino::getPositions() const { @@ -279,8 +279,8 @@ bool Polyomino::operator<(const Polyomino& other) const { for (int y = this->length - 1; y >= 0; y--) { for (int x = 0; x < this->length; x++) { - bool hasThisPosition = this->positions.contains(Position{x, y}); - bool hasOtherPosition = other.positions.contains(Position{x, y}); + bool hasThisPosition = this->positions.contains(Position(x, y)); + bool hasOtherPosition = other.positions.contains(Position(x, y)); if (hasThisPosition != hasOtherPosition) return hasThisPosition; } } @@ -294,7 +294,7 @@ bool Polyomino::operator==(const Polyomino& other) const { std::ostream& operator<<(std::ostream& os, const Polyomino& polyomino) { for (int y = polyomino.length - 1; y >= 0; y--) { for (int x = 0; x < polyomino.length; x++) { - if (polyomino.positions.contains(Position{x, y})) { + if (polyomino.positions.contains(Position(x, y))) { os << "*"; } else { diff --git a/src/Pieces/Polyomino.h b/src/Pieces/Polyomino.h index c7ed76a..e759f80 100644 --- a/src/Pieces/Polyomino.h +++ b/src/Pieces/Polyomino.h @@ -13,7 +13,7 @@ class Polyomino { private: std::set positions; // the squares composing the polyomino, (0,0) is downleft - int length; // the size of the smallest square box in which the polyomino can fit on any rotation + std::int8_t length; // the size of the smallest square box in which the polyomino can fit on any rotation public: /** @@ -24,7 +24,7 @@ class Polyomino { /** * Creates a polyomino with the specified positions and length, wheter it is actually a polyonimo of this length is not checked */ - Polyomino(const std::set& positions, int length); + Polyomino(const std::set& positions, std::int8_t length); /** * Translates the polyomino to the lowest unsigned values (lower row on y = 0, and left-most column on x = 0) diff --git a/src/Pieces/Position.h b/src/Pieces/Position.h index 710d0ac..43513c5 100644 --- a/src/Pieces/Position.h +++ b/src/Pieces/Position.h @@ -7,8 +7,8 @@ * A position on a 2D grid */ struct Position { - int x; // x position - int y; // y position + std::int8_t x; // x position + std::int8_t y; // y position }; @@ -17,7 +17,7 @@ struct Position { * @return The sums of the coordinates of both positions */ inline Position operator+(const Position& left, const Position& right) { - return Position{left.x + right.x, left.y + right.y}; + return Position(left.x + right.x, left.y + right.y); } /** @@ -34,7 +34,7 @@ inline Position& operator+=(Position& left, const Position& right) { * @return The difference of the coordinate between the left and right position */ inline Position operator-(const Position& left, const Position& right) { - return Position{left.x - right.x, left.y - right.y}; + return Position(left.x - right.x, left.y - right.y); } /** diff --git a/src/TextUI/TextApp.cpp b/src/TextUI/TextApp.cpp index f7b8796..096f52e 100644 --- a/src/TextUI/TextApp.cpp +++ b/src/TextUI/TextApp.cpp @@ -250,9 +250,9 @@ void TextApp::printGame(const Game& game) const { for (int y = maxHeight; y >= 0; y--) { for (int x = 0; x < game.getBoard().getWidth(); x++) { /* BOARD PRINTING */ - bool isActivePieceHere = (game.getActivePiece() != nullptr) && (game.getActivePiece()->getPositions().contains(Position{x, y} - game.getActivePiecePosition())); - bool isGhostPieceHere = (game.getActivePiece() != nullptr) && (game.getActivePiece()->getPositions().contains(Position{x, y} - game.getGhostPiecePosition())); - Block block = (isActivePieceHere || isGhostPieceHere) ? game.getActivePiece()->getBlockType() : game.getBoard().getBlock(Position{x, y}); + bool isActivePieceHere = (game.getActivePiece() != nullptr) && (game.getActivePiece()->getPositions().contains(Position(x, y) - game.getActivePiecePosition())); + bool isGhostPieceHere = (game.getActivePiece() != nullptr) && (game.getActivePiece()->getPositions().contains(Position(x, y) - game.getGhostPiecePosition())); + Block block = (isActivePieceHere || isGhostPieceHere) ? game.getActivePiece()->getBlockType() : game.getBoard().getBlock(Position(x, y)); if (isActivePieceHere || isGhostPieceHere) { std::cout << getConsoleColorCode(block); @@ -294,7 +294,7 @@ void TextApp::printGame(const Game& game) const { } else { for (int i = 0; i < game.getHeldPiece()->getLength(); i++) { - if (game.getHeldPiece()->getPositions().contains(Position{i, printedPieceLineHeight})) { + if (game.getHeldPiece()->getPositions().contains(Position(i, printedPieceLineHeight))) { std::cout << getConsoleColorCode(game.getHeldPiece()->getBlockType()) << "*"; } else { @@ -316,7 +316,7 @@ void TextApp::printGame(const Game& game) const { } else { for (int i = 0; i < game.getNextPieces().at(nextQueuePrintedPiece).getLength(); i++) { - if (game.getNextPieces().at(nextQueuePrintedPiece).getPositions().contains(Position{i, printedPieceLineHeight})) { + if (game.getNextPieces().at(nextQueuePrintedPiece).getPositions().contains(Position(i, printedPieceLineHeight))) { std::cout << getConsoleColorCode(game.getNextPieces().at(nextQueuePrintedPiece).getBlockType()) << "*"; } else { From 46b9b8dd656f4a716562e5826b174dc7558a0f8f Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sat, 19 Jul 2025 23:39:22 +0200 Subject: [PATCH 02/10] begin optimize Generator --- src/Core/GameBoard.cpp | 4 +- .../AppMenus/GamePlayingAppMenu.cpp | 4 +- src/Pieces/Generator.cpp | 2 +- src/Pieces/Piece.cpp | 16 +- src/Pieces/Piece.h | 9 +- src/Pieces/PiecesFiles.cpp | 2 +- src/Pieces/Polyomino.cpp | 144 +++++++++++++----- src/Pieces/Polyomino.h | 36 ++++- 8 files changed, 158 insertions(+), 59 deletions(-) diff --git a/src/Core/GameBoard.cpp b/src/Core/GameBoard.cpp index c57784c..1b79b20 100644 --- a/src/Core/GameBoard.cpp +++ b/src/Core/GameBoard.cpp @@ -345,7 +345,7 @@ std::ostream& operator<<(std::ostream& os, const GameBoard& gameboard) { // print only the position were the active piece is for (int y = gameboard.activePiecePosition.y + gameboard.activePiece->getLength() - 1; y >= gameboard.board.getBaseHeight(); y--) { for (int x = 0; x < gameboard.board.getWidth(); x++) { - bool hasActivePiece = gameboard.activePiece->getPositions().contains(Position(x, y) - gameboard.activePiecePosition); + bool hasActivePiece = gameboard.activePiece->containsSquare(Position(x, y) - gameboard.activePiecePosition); if (hasActivePiece) { os << "*"; } @@ -361,7 +361,7 @@ std::ostream& operator<<(std::ostream& os, const GameBoard& gameboard) { Block pieceBlockType = (gameboard.activePiece == nullptr) ? NOTHING : gameboard.activePiece->getBlockType(); for (int y = gameboard.board.getBaseHeight() - 1; y >= 0; y--) { for (int x = 0; x < gameboard.board.getWidth(); x++) { - bool hasActivePiece = (gameboard.activePiece == nullptr) ? false : gameboard.activePiece->getPositions().contains(Position(x, y) - gameboard.activePiecePosition); + bool hasActivePiece = (gameboard.activePiece == nullptr) ? false : gameboard.activePiece->containsSquare(Position(x, y) - gameboard.activePiecePosition); // the active piece takes visual priority over the board if (hasActivePiece) { diff --git a/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp b/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp index 0245e0d..26c383e 100644 --- a/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp +++ b/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp @@ -196,7 +196,7 @@ void GamePlayingAppMenu::drawFrame() const { for (int y = 0; y < this->game.getNextPieces().at(i).getLength(); y++) { for (int x = 0; x < this->game.getNextPieces().at(i).getLength(); x++) { sf::RectangleShape cell(nextCellSize); - if (this->game.getNextPieces().at(i).getPositions().contains(Position(x, y))) { + if (this->game.getNextPieces().at(i).containsSquare(Position(x, y))) { cell.setFillColor(pieceColor); lowestRank = y; } @@ -223,7 +223,7 @@ void GamePlayingAppMenu::drawFrame() const { for (int y = 0; y < this->game.getHeldPiece()->getLength(); y++) { for (int x = 0; x < this->game.getHeldPiece()->getLength(); x++) { sf::RectangleShape cell(holdCellSize); - if (this->game.getHeldPiece()->getPositions().contains(Position(x, y))) { + if (this->game.getHeldPiece()->containsSquare(Position(x, y))) { cell.setFillColor(color); } else { diff --git a/src/Pieces/Generator.cpp b/src/Pieces/Generator.cpp index 6068e19..4b6e45c 100644 --- a/src/Pieces/Generator.cpp +++ b/src/Pieces/Generator.cpp @@ -29,7 +29,7 @@ std::vector&& Generator::generatePolyominoes(int polyominoSize) { void Generator::generate(int polyominoSize, int lastAddedPositionNumber, int nextAvaibleNumber, std::map candidatePositions) { // recursion stop if (polyominoSize == this->currentTestedShape.size()) { - Polyomino candidate(this->currentTestedShape); + Polyomino candidate(std::move(this->currentTestedShape)); // we sort the rotations of the polyominoes std::vector candidateRotations; diff --git a/src/Pieces/Piece.cpp b/src/Pieces/Piece.cpp index 7023cf7..431c818 100644 --- a/src/Pieces/Piece.cpp +++ b/src/Pieces/Piece.cpp @@ -11,9 +11,9 @@ Piece::Piece(const Polyomino& polyomino, Block blockType) : polyomino(polyomino), - blockType(blockType) { - - this->rotationState = NONE; + blockType(blockType), + rotationState(NONE), + positions(polyomino.getPositions()) { } void Piece::rotate(Rotation rotation) { @@ -25,6 +25,7 @@ void Piece::rotate(Rotation rotation) { this->polyomino.rotateCCW(); this->rotationState += rotation; + this->positions = polyomino.getPositions(); } void Piece::defaultRotation() { @@ -36,10 +37,11 @@ void Piece::defaultRotation() { this->polyomino.rotateCW(); this->rotationState = NONE; + this->positions = polyomino.getPositions(); } -const std::set& Piece::getPositions() const { - return this->polyomino.getPositions(); +const std::vector& Piece::getPositions() const { + return this->positions; } int Piece::getLength() const { @@ -54,3 +56,7 @@ std::ostream& operator<<(std::ostream& os, const Piece& piece) { os << getConsoleColorCode(piece.blockType) << piece.polyomino << getResetConsoleColorCode(); return os; } + +bool Piece::containsSquare(const Position& position) const { + return polyomino.contains(position); +} \ No newline at end of file diff --git a/src/Pieces/Piece.h b/src/Pieces/Piece.h index cd74eb1..99146f5 100644 --- a/src/Pieces/Piece.h +++ b/src/Pieces/Piece.h @@ -16,6 +16,7 @@ class Piece { Polyomino polyomino; // a polyomino representing the piece, (0, 0) is downleft Block blockType; // the block type of the piece Rotation rotationState; // the current rotation of the piece + std::vector positions; // cache positions for easier use (particularly UI) public: /** @@ -36,7 +37,13 @@ class Piece { /** * @return The list of positions of the piece */ - const std::set& getPositions() const; + const std::vector& getPositions() const; + + /** + * @param the position of the square + * @return false if there is a hole + */ + bool containsSquare(const Position& position) const; /** * @return The length of the piece diff --git a/src/Pieces/PiecesFiles.cpp b/src/Pieces/PiecesFiles.cpp index 6cc2fb6..77c701a 100644 --- a/src/Pieces/PiecesFiles.cpp +++ b/src/Pieces/PiecesFiles.cpp @@ -119,7 +119,7 @@ bool PiecesFiles::loadPieces(int polyominoSize, std::vector& pieces, std: } // create piece - Piece readPiece(Polyomino(piecePositions, length), pieceBlock); + Piece readPiece(Polyomino(std::move(piecePositions), length), pieceBlock); nextPieceBlockType(pieceBlock); pieces.push_back(readPiece); diff --git a/src/Pieces/Polyomino.cpp b/src/Pieces/Polyomino.cpp index 0acbb83..3e4fd1c 100644 --- a/src/Pieces/Polyomino.cpp +++ b/src/Pieces/Polyomino.cpp @@ -9,8 +9,7 @@ #include #include - -Polyomino::Polyomino(const std::set& positions) { +Polyomino::Polyomino(std::set&& positions) { int minX = INT_MAX; int maxX = INT_MIN; int minY = INT_MAX; @@ -25,55 +24,90 @@ Polyomino::Polyomino(const std::set& positions) { this->length = std::max(maxX - minX + 1, maxY - minY + 1); // we normalize here instead of calling this->normalize() to reduce the number of calculations for the generation algorithm - std::set newPositions; + Polyomino temp(PolyominoData{}, this->length); for (Position position : positions) { - newPositions.insert(Position(position.x - minX, position.y - minY)); + temp.insert(Position(position.x - minX, position.y - minY)); } - this->positions = std::move(newPositions); + this->positions = std::move(temp.positions); } -Polyomino::Polyomino(const std::set& positions, std::int8_t length) : - positions(positions), +Polyomino::Polyomino(std::set&& positions, std::int8_t length) : positions(), length(length){ + for (Position position : positions) { + insert(position); + } +} + +Polyomino::Polyomino(PolyominoData&& positions) : positions(positions) { + int minX = INT_MAX; + int maxX = INT_MIN; + int minY = INT_MAX; + int maxY = INT_MIN; + + // tout s'appelle positions, osekour ! + std::vector tempPositions = getPositions(); + + for (const Position position : tempPositions) { + if (position.x < minX) minX = position.x; + if (position.x > maxX) maxX = position.x; + if (position.y < minY) minY = position.y; + if (position.y > maxY) maxY = position.y; + } + + this->length = std::max(maxX - minX + 1, maxY - minY + 1); + + // we normalize here instead of calling this->normalize() to reduce the number of calculations for the generation algorithm + Polyomino temp(PolyominoData{}, this->length); + for (Position position : tempPositions) { + temp.insert(Position(position.x - minX, position.y - minY)); + } + this->positions = std::move(temp.positions); +} + +Polyomino::Polyomino(PolyominoData&& positions, std::int8_t length) : + positions(std::move(positions)), length(length) { } void Polyomino::normalize() { int minX = INT_MAX; int minY = INT_MAX; - for (const Position position : this->positions) { + + std::vector tempPositions = getPositions(); + + for (const Position position : tempPositions) { if (position.x < minX) minX = position.x; if (position.y < minY) minY = position.y; } - std::set newPositions; - for (const Position position : this->positions) { - newPositions.insert(Position(position.x - minX, position.y - minY)); + Polyomino temp(PolyominoData{}, this->length); + for (const Position position : tempPositions) { + temp.insert(Position(position.x - minX, position.y - minY)); } - this->positions = std::move(newPositions); + this->positions = std::move(temp.positions); } void Polyomino::rotateCW() { - std::set newPositions; - for (const Position position : this->positions) { - newPositions.insert(Position(position.y, (length - 1) - (position.x))); + Polyomino temp(PolyominoData{}, this->length); + for (const Position position : getPositions()) { + temp.insert(Position(position.y, (length - 1) - (position.x))); } - this->positions = std::move(newPositions); + this->positions = std::move(temp.positions); } void Polyomino::rotate180() { - std::set newPositions; - for (const Position position : this->positions) { - newPositions.insert(Position((length - 1) - (position.x), (length - 1) - (position.y))); + Polyomino temp(PolyominoData{}, this->length); + for (const Position position : getPositions()) { + temp.insert(Position((length - 1) - (position.x), (length - 1) - (position.y))); } - this->positions = std::move(newPositions); + this->positions = std::move(temp.positions); } void Polyomino::rotateCCW() { - std::set newPositions; - for (const Position position : this->positions) { - newPositions.insert(Position((length - 1) - (position.y), position.x)); + Polyomino temp(PolyominoData{}, this->length); + for (const Position position : getPositions()) { + temp.insert(Position((length - 1) - (position.y), position.x)); } - this->positions = std::move(newPositions); + this->positions = std::move(temp.positions); } void Polyomino::goToSpawnPosition() { @@ -89,7 +123,7 @@ void Polyomino::goToSpawnPosition() { } // calculates amount of squares per rows and columns - for (const Position position : this->positions) { + for (const Position position : getPositions()) { linesCompleteness.at(0).at(position.y) += 1; // 0 = bottom to top = no rotation linesCompleteness.at(1).at((length - 1) - position.x) += 1; // 1 = right to left = CW linesCompleteness.at(2).at((length - 1) - position.y) += 1; // 2 = top to bottom = 180 @@ -155,20 +189,22 @@ void Polyomino::goToSpawnPosition() { if (sideToBeOn % 2 == 1) { std::swap(verticalEmptyLines, horizontalEmptyLines); } + + std::vector tempPositions = getPositions(); int minX = INT_MAX; int minY = INT_MAX; - for (const Position position : this->positions) { + for (const Position position : tempPositions) { if (position.x < minX) minX = position.x; if (position.y < minY) minY = position.y; } // center the piece with an up bias - std::set newPositions; - for (const Position position : positions) { - newPositions.insert(Position((position.x - minX) + (verticalEmptyLines / 2), (position.y - minY) + ((horizontalEmptyLines + 1) / 2))); + Polyomino temp(PolyominoData{}, this->length); + for (const Position position : tempPositions) { + temp.insert(Position((position.x - minX) + (verticalEmptyLines / 2), (position.y - minY) + ((horizontalEmptyLines + 1) / 2))); } - this->positions = std::move(newPositions); + this->positions = std::move(temp.positions); } void Polyomino::checkForFlattestSide(const std::vector>& linesCompleteness, bool currentFlattestSides[4], int& sideToBeOn, bool checkLeftSide) const { @@ -216,7 +252,7 @@ bool Polyomino::isConvex() const { bool startedColumn = false; bool completedColumn = false; for (int i = 0; i < this->length; i++) { - if (this->positions.contains(Position(i, j))) { + if (this->contains(Position(i, j))) { if (completedLine) return false; else startedLine = true; } @@ -224,7 +260,7 @@ bool Polyomino::isConvex() const { if (startedLine) completedLine = true; } - if (this->positions.contains(Position(j, i))) { + if (this->contains(Position(j, i))) { if (completedColumn) return false; else startedColumn = true; } @@ -252,7 +288,7 @@ bool Polyomino::hasHole() const { void Polyomino::tryToInsertPosition(std::set& emptyPositions, const Position& candidate) const { if (candidate.x >= this->length || candidate.x < 0 || candidate.y >= this->length || candidate.y < 0) return; - if (this->positions.contains(candidate) || emptyPositions.contains(candidate)) return; + if (this->contains(candidate) || emptyPositions.contains(candidate)) return; // if it's a new empty square, try its neighbors emptyPositions.insert(candidate); @@ -262,10 +298,25 @@ void Polyomino::tryToInsertPosition(std::set& emptyPositions, const Po tryToInsertPosition(emptyPositions, Position(candidate.x - 1, candidate.y)); } -const std::set& Polyomino::getPositions() const { +const PolyominoData& Polyomino::getPositionsData() const { return this->positions; } +std::vector Polyomino::getPositions() const { + std::vector result; + for (int y = 0; y < this->length; y++) { + for (int x = 0; x < this->length; x++) { + int posIndex = y * this->length + x; + int longIndex = posIndex / (sizeof(std::uint64_t) * 8); + int bitIndex = posIndex % (sizeof(std::uint64_t) * 8); + if (this->positions[longIndex] & static_cast(1) << (sizeof(std::uint64_t) * 8 - bitIndex)) { + result.push_back(Position(x, y)); + } + } + } + return result; +} + int Polyomino::getLength() const { return this->length; } @@ -277,13 +328,12 @@ int Polyomino::getPolyominoSize() const { bool Polyomino::operator<(const Polyomino& other) const { if (this->length != other.length) return this->length < other.length; - for (int y = this->length - 1; y >= 0; y--) { - for (int x = 0; x < this->length; x++) { - bool hasThisPosition = this->positions.contains(Position(x, y)); - bool hasOtherPosition = other.positions.contains(Position(x, y)); - if (hasThisPosition != hasOtherPosition) return hasThisPosition; + for (int i = 0; i < this->positions.size(); i++) { + if (this->positions[i] != other.positions[i]) { + return this->positions[i] < other.positions[i]; } } + return false; } @@ -294,7 +344,7 @@ bool Polyomino::operator==(const Polyomino& other) const { std::ostream& operator<<(std::ostream& os, const Polyomino& polyomino) { for (int y = polyomino.length - 1; y >= 0; y--) { for (int x = 0; x < polyomino.length; x++) { - if (polyomino.positions.contains(Position(x, y))) { + if (polyomino.contains(Position(x, y))) { os << "*"; } else { @@ -305,3 +355,17 @@ std::ostream& operator<<(std::ostream& os, const Polyomino& polyomino) { } return os; } + +bool Polyomino::contains(const Position& position) const { + int posIndex = position.y * this->length + position.x; + int longIndex = posIndex / (sizeof(std::uint64_t) * 8); + int bitIndex = posIndex % (sizeof(std::uint64_t) * 8); + return this->positions[longIndex] & static_cast(1) << (sizeof(std::uint64_t) * 8 - bitIndex); +} + +void Polyomino::insert(const Position& position) { + int posIndex = position.y * this->length + position.x; + int longIndex = posIndex / (sizeof(std::uint64_t) * 8); + int bitIndex = posIndex % (sizeof(std::uint64_t) * 8); + this->positions[longIndex] |= static_cast(1) << (sizeof(std::uint64_t) * 8 - bitIndex); +} \ No newline at end of file diff --git a/src/Pieces/Polyomino.h b/src/Pieces/Polyomino.h index e759f80..32eabac 100644 --- a/src/Pieces/Polyomino.h +++ b/src/Pieces/Polyomino.h @@ -5,26 +5,32 @@ #include #include #include +#include +using PolyominoData = std::array; /** * A mathematical object consisting of touching squares on a 2D grid */ class Polyomino { private: - std::set positions; // the squares composing the polyomino, (0,0) is downleft - std::int8_t length; // the size of the smallest square box in which the polyomino can fit on any rotation + PolyominoData positions; // the squares composing the polyomino, stored in binary. MSB is downleft + std::int8_t length; // the size of the smallest square box in which the polyomino can fit on any rotation public: /** * Creates a polyomino with the specified positions and normalizes it, wheter it is actually a polyonimo is not checked */ - Polyomino(const std::set& positions); + Polyomino(PolyominoData&& positions); /** * Creates a polyomino with the specified positions and length, wheter it is actually a polyonimo of this length is not checked */ - Polyomino(const std::set& positions, std::int8_t length); + Polyomino(PolyominoData&& positions, std::int8_t length); + + // this is temporary. They are here for compatibility reasons for now + Polyomino(std::set&& positions); + Polyomino(std::set&& positions, std::int8_t length); /** * Translates the polyomino to the lowest unsigned values (lower row on y = 0, and left-most column on x = 0) @@ -70,7 +76,7 @@ class Polyomino { */ bool hasHole() const; - private : + private: /** * Auxiliary method of hasHole() */ @@ -78,9 +84,14 @@ class Polyomino { public: /** - * @return The positions of the polyomino + * @return The positions data of the polyomino */ - const std::set& getPositions() const; + const PolyominoData& getPositionsData() const; + + /** + * @return The positions of the polyomino (deduces it from the binary representation) + */ + std::vector getPositions() const; /** * @return The length of the polyomino @@ -110,4 +121,15 @@ class Polyomino { * @return A reference to the output stream */ friend std::ostream& operator<<(std::ostream& os, const Polyomino& polyomino); + + /** + * @return True if it contains the position + */ + bool contains(const Position& position) const; + + private: + /** + * @brief Insert a square at the position + */ + void insert(const Position& position); }; From 72e9f420abee98570b7661ca6f7d8fdb05581cd9 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sat, 19 Jul 2025 23:53:27 +0200 Subject: [PATCH 03/10] add missing include --- src/Pieces/Position.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Pieces/Position.h b/src/Pieces/Position.h index 43513c5..5048853 100644 --- a/src/Pieces/Position.h +++ b/src/Pieces/Position.h @@ -1,7 +1,7 @@ #pragma once #include - +#include /** * A position on a 2D grid From a3ef52c7a158fa281c0b9903e3e324aec2bf611f Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sun, 20 Jul 2025 21:08:36 +0200 Subject: [PATCH 04/10] decrease file retrieving time --- src/Pieces/Piece.cpp | 2 +- src/Pieces/Piece.h | 2 +- src/Pieces/PiecesFiles.cpp | 34 ++++++++++++------------- src/Pieces/Polyomino.cpp | 51 ++++++++++---------------------------- src/Pieces/Polyomino.h | 23 +++++++++-------- 5 files changed, 45 insertions(+), 67 deletions(-) diff --git a/src/Pieces/Piece.cpp b/src/Pieces/Piece.cpp index 431c818..bfff2f9 100644 --- a/src/Pieces/Piece.cpp +++ b/src/Pieces/Piece.cpp @@ -9,7 +9,7 @@ #include -Piece::Piece(const Polyomino& polyomino, Block blockType) : +Piece::Piece(Polyomino&& polyomino, Block blockType) : polyomino(polyomino), blockType(blockType), rotationState(NONE), diff --git a/src/Pieces/Piece.h b/src/Pieces/Piece.h index 99146f5..04a7074 100644 --- a/src/Pieces/Piece.h +++ b/src/Pieces/Piece.h @@ -22,7 +22,7 @@ class Piece { /** * Creates a piece with a specified shape and block type */ - Piece(const Polyomino& piece, Block blockType); + Piece(Polyomino&& piece, Block blockType); /** * Rotates the piece in the specified direction diff --git a/src/Pieces/PiecesFiles.cpp b/src/Pieces/PiecesFiles.cpp index 77c701a..f53b644 100644 --- a/src/Pieces/PiecesFiles.cpp +++ b/src/Pieces/PiecesFiles.cpp @@ -50,11 +50,12 @@ bool PiecesFiles::savePieces(int polyominoSize, std::vector& polyomin std::uint8_t infoByte = (isConvex << 7) + (hasHole << 6) + polyomino.getLength(); buffer << infoByte; + const int bitsNeeded = polyomino.getLength() * polyomino.getLength(); + const int longsNeeded = bitsNeeded / 64 + 1; + // write the positions of the piece - std::uint8_t positionByte; - for (const Position position : polyomino.getPositions()) { - positionByte = (position.x << 4) + position.y; - buffer << positionByte; + for (int i = 0; i < longsNeeded; i++) { + buffer << polyomino.getPositionsData()[i]; } } @@ -91,11 +92,11 @@ bool PiecesFiles::loadPieces(int polyominoSize, std::vector& pieces, std: nextPieceBlockType(pieceBlock); } - char convexMask = 0b1000'0000; - char holeMask = 0b0100'0000; - char lengthMask = 0b0011'1111; - char xMask = 0b1111'0000; - char yMask = 0b0000'1111; + constexpr char convexMask = 0b1000'0000; + constexpr char holeMask = 0b0100'0000; + constexpr char lengthMask = 0b0011'1111; + constexpr char xMask = 0b1111'0000; + constexpr char yMask = 0b0000'1111; std::uint8_t infoByte; int i = 0; @@ -108,18 +109,17 @@ bool PiecesFiles::loadPieces(int polyominoSize, std::vector& pieces, std: bool hasHole = (infoByte & holeMask) >> 6; int length = (infoByte & lengthMask); + const int bitsNeeded = length * length; + const int longsNeeded = bitsNeeded / 64 + 1; + // read positions - std::set piecePositions; - std::uint8_t positionByte; - for (int i = 0; i < polyominoSize; i++) { - buffer >> positionByte; - int x = ((unsigned char) positionByte & xMask) >> 4; - int y = positionByte & yMask; - piecePositions.insert(Position(x, y)); + Polyomino::PolyominoData polyominoData{}; + for (int i = 0; i < longsNeeded; i++) { + buffer >> polyominoData[i]; } // create piece - Piece readPiece(Polyomino(std::move(piecePositions), length), pieceBlock); + Piece readPiece(Polyomino(std::move(polyominoData), length), pieceBlock); nextPieceBlockType(pieceBlock); pieces.push_back(readPiece); diff --git a/src/Pieces/Polyomino.cpp b/src/Pieces/Polyomino.cpp index 3e4fd1c..45d9e5d 100644 --- a/src/Pieces/Polyomino.cpp +++ b/src/Pieces/Polyomino.cpp @@ -31,38 +31,6 @@ Polyomino::Polyomino(std::set&& positions) { this->positions = std::move(temp.positions); } -Polyomino::Polyomino(std::set&& positions, std::int8_t length) : positions(), length(length){ - for (Position position : positions) { - insert(position); - } -} - -Polyomino::Polyomino(PolyominoData&& positions) : positions(positions) { - int minX = INT_MAX; - int maxX = INT_MIN; - int minY = INT_MAX; - int maxY = INT_MIN; - - // tout s'appelle positions, osekour ! - std::vector tempPositions = getPositions(); - - for (const Position position : tempPositions) { - if (position.x < minX) minX = position.x; - if (position.x > maxX) maxX = position.x; - if (position.y < minY) minY = position.y; - if (position.y > maxY) maxY = position.y; - } - - this->length = std::max(maxX - minX + 1, maxY - minY + 1); - - // we normalize here instead of calling this->normalize() to reduce the number of calculations for the generation algorithm - Polyomino temp(PolyominoData{}, this->length); - for (Position position : tempPositions) { - temp.insert(Position(position.x - minX, position.y - minY)); - } - this->positions = std::move(temp.positions); -} - Polyomino::Polyomino(PolyominoData&& positions, std::int8_t length) : positions(std::move(positions)), length(length) { @@ -79,7 +47,7 @@ void Polyomino::normalize() { if (position.y < minY) minY = position.y; } - Polyomino temp(PolyominoData{}, this->length); + Polyomino temp({}, this->length); for (const Position position : tempPositions) { temp.insert(Position(position.x - minX, position.y - minY)); } @@ -87,7 +55,7 @@ void Polyomino::normalize() { } void Polyomino::rotateCW() { - Polyomino temp(PolyominoData{}, this->length); + Polyomino temp({}, this->length); for (const Position position : getPositions()) { temp.insert(Position(position.y, (length - 1) - (position.x))); } @@ -95,7 +63,7 @@ void Polyomino::rotateCW() { } void Polyomino::rotate180() { - Polyomino temp(PolyominoData{}, this->length); + Polyomino temp({}, this->length); for (const Position position : getPositions()) { temp.insert(Position((length - 1) - (position.x), (length - 1) - (position.y))); } @@ -103,7 +71,7 @@ void Polyomino::rotate180() { } void Polyomino::rotateCCW() { - Polyomino temp(PolyominoData{}, this->length); + Polyomino temp({}, this->length); for (const Position position : getPositions()) { temp.insert(Position((length - 1) - (position.y), position.x)); } @@ -200,7 +168,7 @@ void Polyomino::goToSpawnPosition() { } // center the piece with an up bias - Polyomino temp(PolyominoData{}, this->length); + Polyomino temp({}, this->length); for (const Position position : tempPositions) { temp.insert(Position((position.x - minX) + (verticalEmptyLines / 2), (position.y - minY) + ((horizontalEmptyLines + 1) / 2))); } @@ -298,7 +266,7 @@ void Polyomino::tryToInsertPosition(std::set& emptyPositions, const Po tryToInsertPosition(emptyPositions, Position(candidate.x - 1, candidate.y)); } -const PolyominoData& Polyomino::getPositionsData() const { +const Polyomino::PolyominoData& Polyomino::getPositionsData() const { return this->positions; } @@ -368,4 +336,11 @@ void Polyomino::insert(const Position& position) { int longIndex = posIndex / (sizeof(std::uint64_t) * 8); int bitIndex = posIndex % (sizeof(std::uint64_t) * 8); this->positions[longIndex] |= static_cast(1) << (sizeof(std::uint64_t) * 8 - bitIndex); +} + +void Polyomino::erase(const Position& position) { + int posIndex = position.y * this->length + position.x; + int longIndex = posIndex / (sizeof(std::uint64_t) * 8); + int bitIndex = posIndex % (sizeof(std::uint64_t) * 8); + this->positions[longIndex] &= ~(static_cast(1) << (sizeof(std::uint64_t) * 8 - bitIndex)); } \ No newline at end of file diff --git a/src/Pieces/Polyomino.h b/src/Pieces/Polyomino.h index 32eabac..c1c2086 100644 --- a/src/Pieces/Polyomino.h +++ b/src/Pieces/Polyomino.h @@ -7,30 +7,29 @@ #include #include -using PolyominoData = std::array; /** * A mathematical object consisting of touching squares on a 2D grid */ class Polyomino { + public: + static const std::size_t POLYOMINO_DATA_SIZE = 4; + using PolyominoData = std::array; + private: PolyominoData positions; // the squares composing the polyomino, stored in binary. MSB is downleft std::int8_t length; // the size of the smallest square box in which the polyomino can fit on any rotation - + public: - /** - * Creates a polyomino with the specified positions and normalizes it, wheter it is actually a polyonimo is not checked - */ - Polyomino(PolyominoData&& positions); - /** * Creates a polyomino with the specified positions and length, wheter it is actually a polyonimo of this length is not checked */ Polyomino(PolyominoData&& positions, std::int8_t length); - // this is temporary. They are here for compatibility reasons for now + /** + * Creates a polyomino with the specified positions and normalizes it, wheter it is actually a polyonimo is not checked + */ Polyomino(std::set&& positions); - Polyomino(std::set&& positions, std::int8_t length); /** * Translates the polyomino to the lowest unsigned values (lower row on y = 0, and left-most column on x = 0) @@ -127,9 +126,13 @@ class Polyomino { */ bool contains(const Position& position) const; - private: /** * @brief Insert a square at the position */ void insert(const Position& position); + + /** + * @brief Removes a square at the position + */ + void erase(const Position& position); }; From dd6da5864235674e764a50adedfcc7cccc82a711 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sun, 20 Jul 2025 21:39:10 +0200 Subject: [PATCH 05/10] decrease file size --- src/Pieces/PiecesFiles.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Pieces/PiecesFiles.cpp b/src/Pieces/PiecesFiles.cpp index f53b644..b6c70cd 100644 --- a/src/Pieces/PiecesFiles.cpp +++ b/src/Pieces/PiecesFiles.cpp @@ -51,11 +51,11 @@ bool PiecesFiles::savePieces(int polyominoSize, std::vector& polyomin buffer << infoByte; const int bitsNeeded = polyomino.getLength() * polyomino.getLength(); - const int longsNeeded = bitsNeeded / 64 + 1; + const int bytesNeeded = bitsNeeded / 8 + 1; // write the positions of the piece - for (int i = 0; i < longsNeeded; i++) { - buffer << polyomino.getPositionsData()[i]; + for (int i = 0; i < bytesNeeded; i++) { + buffer << static_cast(polyomino.getPositionsData()[i/4] >> ((i%4) * 8)); } } @@ -99,6 +99,7 @@ bool PiecesFiles::loadPieces(int polyominoSize, std::vector& pieces, std: constexpr char yMask = 0b0000'1111; std::uint8_t infoByte; + std::uint8_t positionByte; int i = 0; while (!buffer.IsFinished()) { // if (piecesFile.eof()) break; @@ -110,19 +111,20 @@ bool PiecesFiles::loadPieces(int polyominoSize, std::vector& pieces, std: int length = (infoByte & lengthMask); const int bitsNeeded = length * length; - const int longsNeeded = bitsNeeded / 64 + 1; + const int bytesNeeded = bitsNeeded / 8 + 1; // read positions Polyomino::PolyominoData polyominoData{}; - for (int i = 0; i < longsNeeded; i++) { - buffer >> polyominoData[i]; + + for (int j = 0; j < bytesNeeded; j++) { + buffer >> positionByte; + polyominoData[j/4] |= static_cast(positionByte) << ((j%4) * 8); } - // create piece - Piece readPiece(Polyomino(std::move(polyominoData), length), pieceBlock); + pieces.emplace_back(Polyomino(std::move(polyominoData), length), pieceBlock); + nextPieceBlockType(pieceBlock); - - pieces.push_back(readPiece); + if (isConvex) { convexPieces.push_back(i); } From ecc035c972f02ec01ed50739b6597ee4ed698744 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sun, 20 Jul 2025 22:32:33 +0200 Subject: [PATCH 06/10] fix polyominos --- src/Pieces/PiecesFiles.cpp | 10 ++++++++-- src/Pieces/Polyomino.cpp | 8 ++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Pieces/PiecesFiles.cpp b/src/Pieces/PiecesFiles.cpp index b6c70cd..15e7036 100644 --- a/src/Pieces/PiecesFiles.cpp +++ b/src/Pieces/PiecesFiles.cpp @@ -55,7 +55,12 @@ bool PiecesFiles::savePieces(int polyominoSize, std::vector& polyomin // write the positions of the piece for (int i = 0; i < bytesNeeded; i++) { - buffer << static_cast(polyomino.getPositionsData()[i/4] >> ((i%4) * 8)); + buffer << static_cast( + ( + polyomino.getPositionsData()[i / sizeof(std::uint64_t)] >> + (56 - ((i % sizeof(std::uint64_t)) * 8)) + ) + & 0xFF); } } @@ -118,7 +123,8 @@ bool PiecesFiles::loadPieces(int polyominoSize, std::vector& pieces, std: for (int j = 0; j < bytesNeeded; j++) { buffer >> positionByte; - polyominoData[j/4] |= static_cast(positionByte) << ((j%4) * 8); + polyominoData[j / sizeof(std::uint64_t)] |= + (static_cast(positionByte) << (56 - ((j % sizeof(std::uint64_t)) * 8))); } pieces.emplace_back(Polyomino(std::move(polyominoData), length), pieceBlock); diff --git a/src/Pieces/Polyomino.cpp b/src/Pieces/Polyomino.cpp index 45d9e5d..f0e634a 100644 --- a/src/Pieces/Polyomino.cpp +++ b/src/Pieces/Polyomino.cpp @@ -277,7 +277,7 @@ std::vector Polyomino::getPositions() const { int posIndex = y * this->length + x; int longIndex = posIndex / (sizeof(std::uint64_t) * 8); int bitIndex = posIndex % (sizeof(std::uint64_t) * 8); - if (this->positions[longIndex] & static_cast(1) << (sizeof(std::uint64_t) * 8 - bitIndex)) { + if (this->positions[longIndex] & static_cast(1) << (sizeof(std::uint64_t) * 8 - bitIndex - 1)) { result.push_back(Position(x, y)); } } @@ -328,19 +328,19 @@ bool Polyomino::contains(const Position& position) const { int posIndex = position.y * this->length + position.x; int longIndex = posIndex / (sizeof(std::uint64_t) * 8); int bitIndex = posIndex % (sizeof(std::uint64_t) * 8); - return this->positions[longIndex] & static_cast(1) << (sizeof(std::uint64_t) * 8 - bitIndex); + return this->positions[longIndex] & static_cast(1) << (sizeof(std::uint64_t) * 8 - bitIndex - 1); } void Polyomino::insert(const Position& position) { int posIndex = position.y * this->length + position.x; int longIndex = posIndex / (sizeof(std::uint64_t) * 8); int bitIndex = posIndex % (sizeof(std::uint64_t) * 8); - this->positions[longIndex] |= static_cast(1) << (sizeof(std::uint64_t) * 8 - bitIndex); + this->positions[longIndex] |= static_cast(1) << (sizeof(std::uint64_t) * 8 - bitIndex - 1); } void Polyomino::erase(const Position& position) { int posIndex = position.y * this->length + position.x; int longIndex = posIndex / (sizeof(std::uint64_t) * 8); int bitIndex = posIndex % (sizeof(std::uint64_t) * 8); - this->positions[longIndex] &= ~(static_cast(1) << (sizeof(std::uint64_t) * 8 - bitIndex)); + this->positions[longIndex] &= ~(static_cast(1) << (sizeof(std::uint64_t) * 8 - bitIndex - 1)); } \ No newline at end of file From 40951038438a01e5201ae1d0ba306e225428de6f Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sun, 20 Jul 2025 23:02:45 +0200 Subject: [PATCH 07/10] fix some compiler warnings --- src/Core/Board.cpp | 8 ++-- src/Core/Board.h | 4 +- src/Core/PiecesList.cpp | 8 ++-- src/Core/PiecesList.h | 8 ++-- src/Pieces/Generator.cpp | 4 +- src/Pieces/Generator.h | 2 +- src/Pieces/PiecesFiles.cpp | 2 - src/Pieces/Polyomino.cpp | 2 +- src/Utils/AssetManager.cpp | 80 +++++++++++++++++++------------------- src/Utils/AssetManager.h | 14 +++---- xmake.lua | 2 + 11 files changed, 67 insertions(+), 67 deletions(-) diff --git a/src/Core/Board.cpp b/src/Core/Board.cpp index 7006d51..848736d 100644 --- a/src/Core/Board.cpp +++ b/src/Core/Board.cpp @@ -20,10 +20,10 @@ Board::Board(int width, int height) : } void Board::changeBlock(const Position& position, Block block) { - if (position.x < 0 || position.x >= this->width || position.y < 0) return; + if (position.x < 0 || static_cast(position.x) >= this->width || position.y < 0) return; // resize the grid if needed - if (position.y >= this->grid.size()) { + if (static_cast(position.y) >= this->grid.size()) { for (int j = this->grid.size(); j <= position.y; j++) { this->grid.push_back(this->emptyRow); } @@ -34,7 +34,7 @@ void Board::changeBlock(const Position& position, Block block) { void Board::insertRow(int height, int holePosition, Block blockType) { std::vector insertedRow; - for (int i = 0; i < this->width; i++) { + for (unsigned i = 0; i < this->width; i++) { if (i == holePosition) { insertedRow.push_back(NOTHING); } @@ -51,7 +51,7 @@ int Board::clearRows() { int clearedLines = 0; for (int j = this->grid.size() - 1; j >= 0; j--) { bool lineIsFull = true; - int i = 0; + unsigned i = 0; while (lineIsFull && (i < width)) { if (this->grid.at(j).at(i) == NOTHING) { lineIsFull = false; diff --git a/src/Core/Board.h b/src/Core/Board.h index fcd48fa..cc5f107 100644 --- a/src/Core/Board.h +++ b/src/Core/Board.h @@ -13,8 +13,8 @@ class Board { private: std::vector> grid; // the grid, (0,0) is downleft std::vector emptyRow; // an empty row of blocks - int width; // the width of the grid - int height; // the base height of the grid, which can extend indefinitely + unsigned width; // the width of the grid + unsigned height; // the base height of the grid, which can extend indefinitely public: /** diff --git a/src/Core/PiecesList.cpp b/src/Core/PiecesList.cpp index a6bd0d3..614fd11 100644 --- a/src/Core/PiecesList.cpp +++ b/src/Core/PiecesList.cpp @@ -27,12 +27,12 @@ PiecesList::PiecesList() { this->pushBackEmptyVectors(); } -bool PiecesList::loadPieces(int max_size) { +bool PiecesList::loadPieces(unsigned max_size) { if (max_size < 1) return false; if (max_size <= this->highestLoadedSize) return true; PiecesFiles piecesFiles; - for (int i = this->highestLoadedSize + 1; i <= max_size; i++) { + for (unsigned i = this->highestLoadedSize + 1; i <= max_size; i++) { if (!piecesFiles.loadPieces(i, this->loadedPieces.at(i), this->convexPieces.at(i), this->holelessPieces.at(i), this->otherPieces.at(i))) { return false; } @@ -45,14 +45,14 @@ bool PiecesList::loadPieces(int max_size) { return true; } -bool PiecesList::selectPiece(int size, int number) { +bool PiecesList::selectPiece(unsigned size, unsigned number) { if (size < 1 || size > this->highestLoadedSize || number >= this->loadedPieces.at(size).size()) return false; this->selectedPieces.push_back(std::pair(size, number)); return true; } -bool PiecesList::selectAllPieces(int size) { +bool PiecesList::selectAllPieces(unsigned size) { if (size < 1 || size > this->highestLoadedSize) return false; for (int i = 0; i < this->loadedPieces.at(size).size(); i++) { diff --git a/src/Core/PiecesList.h b/src/Core/PiecesList.h index 8c4991f..2150d0c 100644 --- a/src/Core/PiecesList.h +++ b/src/Core/PiecesList.h @@ -13,7 +13,7 @@ */ class PiecesList { private: - int highestLoadedSize; // the highest size of pieces currently loaded + unsigned highestLoadedSize; // the highest size of pieces currently loaded std::vector> loadedPieces; // every loaded pieces by size std::vector> convexPieces; // the list of convex loaded pieces by size std::vector> holelessPieces; // the list of holeless loaded pieces by size @@ -33,19 +33,19 @@ class PiecesList { * Makes the list load all pieces up to the specified size * @return If all pieces up to the specified size are correctly loaded */ - [[nodiscard]] bool loadPieces(int max_size); + [[nodiscard]] bool loadPieces(unsigned max_size); /** * Selects the specified piece * @return If the piece could be selected */ - bool selectPiece(int size, int number); + bool selectPiece(unsigned size, unsigned number); /** * Selects all pieces of the specified size * @return If the pieces could be selected */ - bool selectAllPieces(int size); + bool selectAllPieces(unsigned size); /** * Selects all convex pieces of the specified size diff --git a/src/Pieces/Generator.cpp b/src/Pieces/Generator.cpp index 4b6e45c..dd12183 100644 --- a/src/Pieces/Generator.cpp +++ b/src/Pieces/Generator.cpp @@ -26,7 +26,7 @@ std::vector&& Generator::generatePolyominoes(int polyominoSize) { return std::move(this->validPolyominoes); } -void Generator::generate(int polyominoSize, int lastAddedPositionNumber, int nextAvaibleNumber, std::map candidatePositions) { +void Generator::generate(unsigned polyominoSize, int lastAddedPositionNumber, int nextAvaibleNumber, std::map candidatePositions) { // recursion stop if (polyominoSize == this->currentTestedShape.size()) { Polyomino candidate(std::move(this->currentTestedShape)); @@ -58,7 +58,7 @@ void Generator::generate(int polyominoSize, int lastAddedPositionNumber, int nex } // try adding a square only to positions with a higher number than the last one - for (const auto [key, val] : candidatePositions) { + for (const auto& [key, val] : candidatePositions) { if (val > lastAddedPositionNumber) { this->currentTestedShape.insert(key); this->generate(polyominoSize, val, nextAvaibleNumber, (polyominoSize == this->currentTestedShape.size()) ? std::map() : candidatePositions); diff --git a/src/Pieces/Generator.h b/src/Pieces/Generator.h index 3cdaeac..339c5f3 100644 --- a/src/Pieces/Generator.h +++ b/src/Pieces/Generator.h @@ -31,7 +31,7 @@ class Generator { /** * Generates all one-sided polyominoes of the specified size using the current tested shape */ - void generate(int polyominoSize, int lastAddedPositionNumber, int nextAvaibleNumber, std::map candidatePositions); + void generate(unsigned polyominoSize, int lastAddedPositionNumber, int nextAvaibleNumber, std::map candidatePositions); /** * Checks wheter a candidate position can be added to the current tested shape diff --git a/src/Pieces/PiecesFiles.cpp b/src/Pieces/PiecesFiles.cpp index 15e7036..6357eab 100644 --- a/src/Pieces/PiecesFiles.cpp +++ b/src/Pieces/PiecesFiles.cpp @@ -100,8 +100,6 @@ bool PiecesFiles::loadPieces(int polyominoSize, std::vector& pieces, std: constexpr char convexMask = 0b1000'0000; constexpr char holeMask = 0b0100'0000; constexpr char lengthMask = 0b0011'1111; - constexpr char xMask = 0b1111'0000; - constexpr char yMask = 0b0000'1111; std::uint8_t infoByte; std::uint8_t positionByte; diff --git a/src/Pieces/Polyomino.cpp b/src/Pieces/Polyomino.cpp index f0e634a..c771d2f 100644 --- a/src/Pieces/Polyomino.cpp +++ b/src/Pieces/Polyomino.cpp @@ -296,7 +296,7 @@ int Polyomino::getPolyominoSize() const { bool Polyomino::operator<(const Polyomino& other) const { if (this->length != other.length) return this->length < other.length; - for (int i = 0; i < this->positions.size(); i++) { + for (std::size_t i = 0; i < this->positions.size(); i++) { if (this->positions[i] != other.positions[i]) { return this->positions[i] < other.positions[i]; } diff --git a/src/Utils/AssetManager.cpp b/src/Utils/AssetManager.cpp index 75b2c5d..2656747 100644 --- a/src/Utils/AssetManager.cpp +++ b/src/Utils/AssetManager.cpp @@ -10,30 +10,6 @@ static const unsigned char data_fonts_pressstart_prstartk_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_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_Moveright_png[] = { - #include -}; - static const unsigned char data_images_keybinds_Harddrop_png[] = { #include }; @@ -42,14 +18,38 @@ static const unsigned char data_images_keybinds_Moveleft_png[] = { #include }; -static const unsigned char data_images_keybinds_Hold_png[] = { - #include +static const unsigned char data_images_keybinds_RotateCW_png[] = { + #include +}; + +static const unsigned char data_images_keybinds_RotateCCW_png[] = { + #include }; static const unsigned char data_images_keybinds_Softdrop_png[] = { #include }; +static const unsigned char data_images_keybinds_Moveright_png[] = { + #include +}; + +static const unsigned char data_images_keybinds_Rotate180_png[] = { + #include +}; + +static const unsigned char data_images_keybinds_Hold_png[] = { + #include +}; + +static const unsigned char data_images_keybinds_Rotate0_png[] = { + #include +}; + +static const unsigned char data_images_keybinds_Retry_png[] = { + #include +}; + static const unsigned char data_images_keybinds_Pause_png[] = { #include }; @@ -57,16 +57,16 @@ static const unsigned char data_images_keybinds_Pause_png[] = { static const Asset assets[] = { {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_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_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_RotateCW_png, sizeof(data_images_keybinds_RotateCW_png)}, + {data_images_keybinds_RotateCCW_png, sizeof(data_images_keybinds_RotateCCW_png)}, {data_images_keybinds_Softdrop_png, sizeof(data_images_keybinds_Softdrop_png)}, + {data_images_keybinds_Moveright_png, sizeof(data_images_keybinds_Moveright_png)}, + {data_images_keybinds_Rotate180_png, sizeof(data_images_keybinds_Rotate180_png)}, + {data_images_keybinds_Hold_png, sizeof(data_images_keybinds_Hold_png)}, + {data_images_keybinds_Rotate0_png, sizeof(data_images_keybinds_Rotate0_png)}, + {data_images_keybinds_Retry_png, sizeof(data_images_keybinds_Retry_png)}, {data_images_keybinds_Pause_png, sizeof(data_images_keybinds_Pause_png)}, }; @@ -74,16 +74,16 @@ static const Asset assets[] = { static const std::map assetMap = { {"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/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/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/RotateCW.png", AssetName::data_images_keybinds_RotateCW_png}, + {"data/images/keybinds/RotateCCW.png", AssetName::data_images_keybinds_RotateCCW_png}, {"data/images/keybinds/Softdrop.png", AssetName::data_images_keybinds_Softdrop_png}, + {"data/images/keybinds/Moveright.png", AssetName::data_images_keybinds_Moveright_png}, + {"data/images/keybinds/Rotate180.png", AssetName::data_images_keybinds_Rotate180_png}, + {"data/images/keybinds/Hold.png", AssetName::data_images_keybinds_Hold_png}, + {"data/images/keybinds/Rotate0.png", AssetName::data_images_keybinds_Rotate0_png}, + {"data/images/keybinds/Retry.png", AssetName::data_images_keybinds_Retry_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 e4eacae..0404d69 100644 --- a/src/Utils/AssetManager.h +++ b/src/Utils/AssetManager.h @@ -11,16 +11,16 @@ struct Asset { enum class AssetName { data_fonts_pressstart_prstart_ttf, data_fonts_pressstart_prstartk_ttf, - data_images_keybinds_Rotate180_png, - data_images_keybinds_Rotate0_png, - data_images_keybinds_RotateCCW_png, - data_images_keybinds_Retry_png, - data_images_keybinds_RotateCW_png, - data_images_keybinds_Moveright_png, data_images_keybinds_Harddrop_png, data_images_keybinds_Moveleft_png, - data_images_keybinds_Hold_png, + data_images_keybinds_RotateCW_png, + data_images_keybinds_RotateCCW_png, data_images_keybinds_Softdrop_png, + data_images_keybinds_Moveright_png, + data_images_keybinds_Rotate180_png, + data_images_keybinds_Hold_png, + data_images_keybinds_Rotate0_png, + data_images_keybinds_Retry_png, data_images_keybinds_Pause_png, }; diff --git a/xmake.lua b/xmake.lua index d579661..9b0c35e 100644 --- a/xmake.lua +++ b/xmake.lua @@ -2,6 +2,8 @@ add_rules("mode.debug", "mode.release") includes("xmake/bin2c.lua") +set_warnings("all") + add_requires("sfml 3.0.0", "zlib") set_languages("c++20") From 07ba9619edf48131281bf31c44d79f9425eb27f4 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Mon, 21 Jul 2025 16:06:27 +0200 Subject: [PATCH 08/10] use bitsets --- src/Core/Bag.cpp | 2 +- src/Pieces/Piece.cpp | 9 ++-- src/Pieces/Piece.h | 3 +- src/Pieces/PiecesFiles.cpp | 17 +++--- src/Pieces/Polyomino.cpp | 108 ++++++++++++++++++------------------- src/Pieces/Polyomino.h | 62 ++++++++++++++++++--- xmake.lua | 2 - 7 files changed, 120 insertions(+), 83 deletions(-) diff --git a/src/Core/Bag.cpp b/src/Core/Bag.cpp index 1f5479d..c419300 100644 --- a/src/Core/Bag.cpp +++ b/src/Core/Bag.cpp @@ -35,7 +35,7 @@ Bag::Bag(const std::shared_ptr& piecesList) : } for (const auto& piece : this->selectedPieces) { - int pieceSize = this->piecesList->lookAtPiece(piece).getPositions().size(); + int pieceSize = this->piecesList->lookAtPiece(piece).getPositions().getSize(); this->currentBags.at(pieceSize).push_back(piece); } } diff --git a/src/Pieces/Piece.cpp b/src/Pieces/Piece.cpp index bfff2f9..28fc372 100644 --- a/src/Pieces/Piece.cpp +++ b/src/Pieces/Piece.cpp @@ -12,8 +12,7 @@ Piece::Piece(Polyomino&& polyomino, Block blockType) : polyomino(polyomino), blockType(blockType), - rotationState(NONE), - positions(polyomino.getPositions()) { + rotationState(NONE) { } void Piece::rotate(Rotation rotation) { @@ -25,7 +24,6 @@ void Piece::rotate(Rotation rotation) { this->polyomino.rotateCCW(); this->rotationState += rotation; - this->positions = polyomino.getPositions(); } void Piece::defaultRotation() { @@ -37,11 +35,10 @@ void Piece::defaultRotation() { this->polyomino.rotateCW(); this->rotationState = NONE; - this->positions = polyomino.getPositions(); } -const std::vector& Piece::getPositions() const { - return this->positions; +const Polyomino& Piece::getPositions() const { + return this->polyomino; } int Piece::getLength() const { diff --git a/src/Pieces/Piece.h b/src/Pieces/Piece.h index 04a7074..012d94d 100644 --- a/src/Pieces/Piece.h +++ b/src/Pieces/Piece.h @@ -16,7 +16,6 @@ class Piece { Polyomino polyomino; // a polyomino representing the piece, (0, 0) is downleft Block blockType; // the block type of the piece Rotation rotationState; // the current rotation of the piece - std::vector positions; // cache positions for easier use (particularly UI) public: /** @@ -37,7 +36,7 @@ class Piece { /** * @return The list of positions of the piece */ - const std::vector& getPositions() const; + const Polyomino& getPositions() const; /** * @param the position of the square diff --git a/src/Pieces/PiecesFiles.cpp b/src/Pieces/PiecesFiles.cpp index 6357eab..2881335 100644 --- a/src/Pieces/PiecesFiles.cpp +++ b/src/Pieces/PiecesFiles.cpp @@ -53,14 +53,15 @@ bool PiecesFiles::savePieces(int polyominoSize, std::vector& polyomin const int bitsNeeded = polyomino.getLength() * polyomino.getLength(); const int bytesNeeded = bitsNeeded / 8 + 1; + static const Polyomino::PolyominoData byteMask = 0xFF; + const Polyomino::PolyominoData& polyominoData = polyomino.getPositionsData(); + // write the positions of the piece for (int i = 0; i < bytesNeeded; i++) { - buffer << static_cast( - ( - polyomino.getPositionsData()[i / sizeof(std::uint64_t)] >> - (56 - ((i % sizeof(std::uint64_t)) * 8)) - ) - & 0xFF); + Polyomino::PolyominoData pData = polyominoData >> (i * 8); + unsigned long data = + (pData & byteMask).to_ulong(); + buffer << static_cast(data); } } @@ -121,8 +122,8 @@ bool PiecesFiles::loadPieces(int polyominoSize, std::vector& pieces, std: for (int j = 0; j < bytesNeeded; j++) { buffer >> positionByte; - polyominoData[j / sizeof(std::uint64_t)] |= - (static_cast(positionByte) << (56 - ((j % sizeof(std::uint64_t)) * 8))); + Polyomino::PolyominoData tempByte(positionByte); + polyominoData |= (tempByte << (j * 8)); } pieces.emplace_back(Polyomino(std::move(polyominoData), length), pieceBlock); diff --git a/src/Pieces/Polyomino.cpp b/src/Pieces/Polyomino.cpp index c771d2f..444140c 100644 --- a/src/Pieces/Polyomino.cpp +++ b/src/Pieces/Polyomino.cpp @@ -8,8 +8,9 @@ #include #include #include +#include -Polyomino::Polyomino(std::set&& positions) { +Polyomino::Polyomino(std::set&& positions) : positions(0) { int minX = INT_MAX; int maxX = INT_MIN; int minY = INT_MAX; @@ -24,11 +25,11 @@ Polyomino::Polyomino(std::set&& positions) { this->length = std::max(maxX - minX + 1, maxY - minY + 1); // we normalize here instead of calling this->normalize() to reduce the number of calculations for the generation algorithm - Polyomino temp(PolyominoData{}, this->length); + Polyomino newPolyomino({}, this->length); for (Position position : positions) { - temp.insert(Position(position.x - minX, position.y - minY)); + newPolyomino.insert(Position(position.x - minX, position.y - minY)); } - this->positions = std::move(temp.positions); + this->positions = std::move(newPolyomino.positions); } Polyomino::Polyomino(PolyominoData&& positions, std::int8_t length) : @@ -39,24 +40,21 @@ Polyomino::Polyomino(PolyominoData&& positions, std::int8_t length) : void Polyomino::normalize() { int minX = INT_MAX; int minY = INT_MAX; - - std::vector tempPositions = getPositions(); - - for (const Position position : tempPositions) { + for (const Position position : *this) { if (position.x < minX) minX = position.x; if (position.y < minY) minY = position.y; } - Polyomino temp({}, this->length); - for (const Position position : tempPositions) { - temp.insert(Position(position.x - minX, position.y - minY)); + Polyomino newPolyomino({}, this->length); + for (const Position position : *this) { + newPolyomino.insert(Position(position.x - minX, position.y - minY)); } - this->positions = std::move(temp.positions); + this->positions = std::move(newPolyomino.positions); } void Polyomino::rotateCW() { Polyomino temp({}, this->length); - for (const Position position : getPositions()) { + for (const Position position : *this) { temp.insert(Position(position.y, (length - 1) - (position.x))); } this->positions = std::move(temp.positions); @@ -64,7 +62,7 @@ void Polyomino::rotateCW() { void Polyomino::rotate180() { Polyomino temp({}, this->length); - for (const Position position : getPositions()) { + for (const Position position : *this) { temp.insert(Position((length - 1) - (position.x), (length - 1) - (position.y))); } this->positions = std::move(temp.positions); @@ -72,7 +70,7 @@ void Polyomino::rotate180() { void Polyomino::rotateCCW() { Polyomino temp({}, this->length); - for (const Position position : getPositions()) { + for (const Position position : *this) { temp.insert(Position((length - 1) - (position.y), position.x)); } this->positions = std::move(temp.positions); @@ -91,7 +89,7 @@ void Polyomino::goToSpawnPosition() { } // calculates amount of squares per rows and columns - for (const Position position : getPositions()) { + for (const Position position : *this) { linesCompleteness.at(0).at(position.y) += 1; // 0 = bottom to top = no rotation linesCompleteness.at(1).at((length - 1) - position.x) += 1; // 1 = right to left = CW linesCompleteness.at(2).at((length - 1) - position.y) += 1; // 2 = top to bottom = 180 @@ -158,18 +156,17 @@ void Polyomino::goToSpawnPosition() { std::swap(verticalEmptyLines, horizontalEmptyLines); } - std::vector tempPositions = getPositions(); int minX = INT_MAX; int minY = INT_MAX; - for (const Position position : tempPositions) { + for (const Position position : *this) { if (position.x < minX) minX = position.x; if (position.y < minY) minY = position.y; } // center the piece with an up bias Polyomino temp({}, this->length); - for (const Position position : tempPositions) { + for (const Position position : *this) { temp.insert(Position((position.x - minX) + (verticalEmptyLines / 2), (position.y - minY) + ((horizontalEmptyLines + 1) / 2))); } this->positions = std::move(temp.positions); @@ -242,19 +239,19 @@ bool Polyomino::isConvex() const { bool Polyomino::hasHole() const { // add every empty square on the outer of the box containing the polyomino - std::set emptyPositions; + Polyomino temp({}, this->length); for (int i = 0; i < this->length - 1; i++) { - this->tryToInsertPosition(emptyPositions, Position(i, 0)); // up row - this->tryToInsertPosition(emptyPositions, Position(this->length - 1, i)); // rigth column - this->tryToInsertPosition(emptyPositions, Position(this->length - 1 - i, this->length - 1)); // bottom row - this->tryToInsertPosition(emptyPositions, Position(0, this->length - 1 - i)); // left column + this->tryToInsertPosition(temp, Position(i, 0)); // up row + this->tryToInsertPosition(temp, Position(this->length - 1, i)); // rigth column + this->tryToInsertPosition(temp, Position(this->length - 1 - i, this->length - 1)); // bottom row + this->tryToInsertPosition(temp, Position(0, this->length - 1 - i)); // left column } // if we didn't reached all empty squares in the box then there was some contained within the polyomino, i.e. there was a hole - return (emptyPositions.size() < (this->length * this->length) - this->positions.size()); + return (temp.getSize() < (this->length * this->length) - this->positions.size()); } -void Polyomino::tryToInsertPosition(std::set& emptyPositions, const Position& candidate) const { +void Polyomino::tryToInsertPosition(Polyomino& emptyPositions, const Position& candidate) const { if (candidate.x >= this->length || candidate.x < 0 || candidate.y >= this->length || candidate.y < 0) return; if (this->contains(candidate) || emptyPositions.contains(candidate)) return; @@ -270,21 +267,6 @@ const Polyomino::PolyominoData& Polyomino::getPositionsData() const { return this->positions; } -std::vector Polyomino::getPositions() const { - std::vector result; - for (int y = 0; y < this->length; y++) { - for (int x = 0; x < this->length; x++) { - int posIndex = y * this->length + x; - int longIndex = posIndex / (sizeof(std::uint64_t) * 8); - int bitIndex = posIndex % (sizeof(std::uint64_t) * 8); - if (this->positions[longIndex] & static_cast(1) << (sizeof(std::uint64_t) * 8 - bitIndex - 1)) { - result.push_back(Position(x, y)); - } - } - } - return result; -} - int Polyomino::getLength() const { return this->length; } @@ -296,9 +278,16 @@ int Polyomino::getPolyominoSize() const { bool Polyomino::operator<(const Polyomino& other) const { if (this->length != other.length) return this->length < other.length; - for (std::size_t i = 0; i < this->positions.size(); i++) { - if (this->positions[i] != other.positions[i]) { - return this->positions[i] < other.positions[i]; + assert(other.positions.any() && "The other polyomino is empty !"); + + static const PolyominoData longMask = 0xFFFFFFFFFFFFFFFF; + const int longsNeeded = this->length * this->length / 64 + 1; + + for (int i = 0; i < longsNeeded; i++) { + unsigned long l1 = (this->positions >> (i * sizeof(std::uint64_t)) & longMask).to_ulong(); + unsigned long l2 = (other.positions >> (i * sizeof(std::uint64_t)) & longMask).to_ulong(); + if (l1 != l2) { + return l1 < l2; } } @@ -324,23 +313,30 @@ std::ostream& operator<<(std::ostream& os, const Polyomino& polyomino) { return os; } +int Polyomino::getSize() const { + return positions.count(); +} + bool Polyomino::contains(const Position& position) const { - int posIndex = position.y * this->length + position.x; - int longIndex = posIndex / (sizeof(std::uint64_t) * 8); - int bitIndex = posIndex % (sizeof(std::uint64_t) * 8); - return this->positions[longIndex] & static_cast(1) << (sizeof(std::uint64_t) * 8 - bitIndex - 1); + return this->positions[getBitIndex(position)]; } void Polyomino::insert(const Position& position) { - int posIndex = position.y * this->length + position.x; - int longIndex = posIndex / (sizeof(std::uint64_t) * 8); - int bitIndex = posIndex % (sizeof(std::uint64_t) * 8); - this->positions[longIndex] |= static_cast(1) << (sizeof(std::uint64_t) * 8 - bitIndex - 1); + this->positions.set(getBitIndex(position), true); } void Polyomino::erase(const Position& position) { - int posIndex = position.y * this->length + position.x; - int longIndex = posIndex / (sizeof(std::uint64_t) * 8); - int bitIndex = posIndex % (sizeof(std::uint64_t) * 8); - this->positions[longIndex] &= ~(static_cast(1) << (sizeof(std::uint64_t) * 8 - bitIndex - 1)); + this->positions.set(getBitIndex(position), false); +} + +std::size_t Polyomino::getBitIndex(const Position& position) const { + return position.y * this->length + position.x; +} + +Polyomino::ConstIterator Polyomino::begin() const { + return ConstIterator(*this, positions._Find_first()); +} + +Polyomino::ConstIterator Polyomino::end() const { + return ConstIterator(*this, positions.size()); } \ No newline at end of file diff --git a/src/Pieces/Polyomino.h b/src/Pieces/Polyomino.h index c1c2086..d2f963a 100644 --- a/src/Pieces/Polyomino.h +++ b/src/Pieces/Polyomino.h @@ -6,6 +6,7 @@ #include #include #include +#include /** @@ -13,8 +14,44 @@ */ class Polyomino { public: - static const std::size_t POLYOMINO_DATA_SIZE = 4; - using PolyominoData = std::array; + static const std::size_t MAX_LENGTH = 16; + using PolyominoData = std::bitset; + + class ConstIterator { + public: + using iterator_category = std::forward_iterator_tag; + using difference_type = std::ptrdiff_t; + using value_type = Position; + using const_pointer = const Position*; // or also value_type* + using const_reference = const Position&; // or also value_type& + public: + ConstIterator(const Polyomino& polyomino, std::size_t index) : m_Polyomino(polyomino), m_Index(index) { + updatePosition(); + } + + const_reference operator*() const { return m_Position; } + const_pointer operator->() { return &m_Position; } + + // Prefix increment + ConstIterator& operator++() { + m_Index = m_Polyomino.getPositionsData()._Find_next(m_Index); + updatePosition(); + return *this; + } + + + friend bool operator== (const ConstIterator& a, const ConstIterator& b) { return a.m_Index == b.m_Index; }; + friend bool operator!= (const ConstIterator& a, const ConstIterator& b) { return a.m_Index != b.m_Index; }; + + private: + void updatePosition() { + m_Position = Position(m_Index % m_Polyomino.getLength(), m_Index / m_Polyomino.getLength()); + } + + const Polyomino& m_Polyomino; + std::size_t m_Index; + Position m_Position; + }; private: PolyominoData positions; // the squares composing the polyomino, stored in binary. MSB is downleft @@ -79,7 +116,7 @@ class Polyomino { /** * Auxiliary method of hasHole() */ - void tryToInsertPosition(std::set& emptypositions, const Position& candidate) const; + void tryToInsertPosition(Polyomino& emptypositions, const Position& candidate) const; public: /** @@ -87,16 +124,21 @@ class Polyomino { */ const PolyominoData& getPositionsData() const; - /** - * @return The positions of the polyomino (deduces it from the binary representation) - */ - std::vector getPositions() const; - /** * @return The length of the polyomino */ int getLength() const; + /** + * + */ + int getSize() const; + + /** + * + */ + std::size_t getBitIndex(const Position& position) const; + /** * @return The number of squares in the polyomino */ @@ -135,4 +177,8 @@ class Polyomino { * @brief Removes a square at the position */ void erase(const Position& position); + + ConstIterator begin() const; + + ConstIterator end() const; }; diff --git a/xmake.lua b/xmake.lua index 9b0c35e..d579661 100644 --- a/xmake.lua +++ b/xmake.lua @@ -2,8 +2,6 @@ add_rules("mode.debug", "mode.release") includes("xmake/bin2c.lua") -set_warnings("all") - add_requires("sfml 3.0.0", "zlib") set_languages("c++20") From de63cd22b636567cb3721a8b6052d8320879ea46 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Mon, 28 Jul 2025 09:51:02 +0200 Subject: [PATCH 09/10] optimize vector initialization --- src/Pieces/Polyomino.cpp | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/Pieces/Polyomino.cpp b/src/Pieces/Polyomino.cpp index 444140c..81f0698 100644 --- a/src/Pieces/Polyomino.cpp +++ b/src/Pieces/Polyomino.cpp @@ -78,15 +78,8 @@ void Polyomino::rotateCCW() { void Polyomino::goToSpawnPosition() { // initialize array - std::vector> linesCompleteness; - linesCompleteness.reserve(4); - std::vector empty; - for (int j = 0; j < this->length; j++) { - empty.push_back(0); - } - for (int i = 0; i < 4; i++) { - linesCompleteness.push_back(empty); - } + std::vector empty(this->length, 0); + std::vector> linesCompleteness(4, empty); // calculates amount of squares per rows and columns for (const Position position : *this) { From 5bb908402238252888b4fa50745e02f2e9bca48a Mon Sep 17 00:00:00 2001 From: zulianc Date: Mon, 2 Feb 2026 18:33:45 +0100 Subject: [PATCH 10/10] =?UTF-8?q?m=C3=A0j=20readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 30 +++++++------- src/Utils/AssetManager.cpp | 80 +++++++++++++++++++------------------- src/Utils/AssetManager.h | 14 +++---- 3 files changed, 62 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index d324fa2..7e1e320 100644 --- a/README.md +++ b/README.md @@ -115,21 +115,21 @@ To package the executable manually properly, follow theses steps: | n | Number | Generation | File storing | File retrieving | File size | | -: | -: | :-: | :-: | :-: | -: | -| 1 | 1 | 0s 0.005471ms | 0s 0.14436ms | 0s 0.022223ms | 3 bytes | -| 2 | 1 | 0s 0.006979ms | 0s 0.036624ms | 0s 0.011424ms | 4 bytes | -| 3 | 2 | 0s 0.018718ms | 0s 0.035885ms | 0s 0.013246ms | 9 bytes | -| 4 | 7 | 0s 0.060544ms | 0s 0.056277ms | 0s 0.019395ms | 36 bytes | -| 5 | 18 | 0s 0.220348ms | 0s 0.166593ms | 0s 0.036526ms | 76 bytes | -| 6 | 60 | 0s 0.773924ms | 0s 0.283423ms | 0s 0.063492ms | 186 bytes | -| 7 | 196 | 0s 3.00331ms | 0s 0.827344ms | 0s 0.163653ms | 546 bytes | -| 8 | 704 | 0s 13.142ms | 0s 3.68255ms | 0s 0.630044ms | 1 898 bytes | -| 9 | 2500 | 0s 50.9272ms | 0s 16.1929ms | 0s 2.35157ms | 6889 bytes | -| 10 | 9189 | 0s 204.031ms | 0s 87.1819ms | 0s 10.5841ms | 25302 bytes | -| 11 | 33896 | 0s 832.82ms | 0s 412.466ms | 0s 57.6399ms | 93711 bytes | -| 12 | 126759 | 3s 425.907ms | 1s 982.715ms | 0s 226.816ms | 350325 bytes | -| 13 | 476270 | 14s 570.595ms | 9s 945.511ms | 0s 972.036ms | 1327156 bytes | -| 14 | 1802312 | 56s 394.426ms | 41s 675.672ms | 4s 79.0436ms | 5035148 bytes | -| 15 | 6849777 | 258s 219.666ms | 223s 386.329ms | 16s 483.426ms | 19392417 bytes | +| 1 | 1 | 0s 0.006496ms | 0s 0.18186ms | 0s 0.064298ms | 3 bytes | +| 2 | 1 | 0s 0.003848ms | 0s 0.167537ms | 0s 0.013054ms | 3 bytes | +| 3 | 2 | 0s 0.010133ms | 0s 0.076844ms | 0s 0.011991ms | 6 bytes | +| 4 | 7 | 0s 0.027356ms | 0s 0.050884ms | 0s 0.016362ms | 22 bytes | +| 5 | 18 | 0s 0.082079ms | 0s 0.088215ms | 0s 0.015016ms | 63 bytes | +| 6 | 60 | 0s 0.292599ms | 0s 0.254024ms | 0s 0.034325ms | 201 bytes | +| 7 | 196 | 0s 1.15671ms | 0s 0.424141ms | 0s 0.061736ms | 538 bytes | +| 8 | 704 | 0s 7.32448ms | 0s 2.00197ms | 0s 0.161199ms | 1803 bytes | +| 9 | 2500 | 0s 23.1679ms | 0s 4.30285ms | 0s 0.496256ms | 6291 bytes | +| 10 | 9189 | 0s 76.8277ms | 0s 20.5346ms | 0s 1.99335ms | 22396 bytes | +| 11 | 33896 | 0s 292.303ms | 0s 84.7814ms | 0s 7.11621ms | 79040 bytes | +| 12 | 126759 | 1s 368.505ms | 0s 407.837ms | 0s 33.5164ms | 279497 bytes | +| 13 | 476270 | 5s 963.297ms | 1s 709.14ms | 0s 171.402ms | 1010672 bytes | +| 14 | 1802312 | 25s 319.931ms | 3s 853.451ms | 0s 580.736ms | 3750531 bytes | +| 15 | 6849777 | 102s 886.385ms | 21s 198.445ms | 1s 973.051ms | 14162217 bytes | _File storing includes type checking and sorting all polyominoes before writing them to the file._ The files are compressed, they used to be about 5x as large. diff --git a/src/Utils/AssetManager.cpp b/src/Utils/AssetManager.cpp index 2656747..75b2c5d 100644 --- a/src/Utils/AssetManager.cpp +++ b/src/Utils/AssetManager.cpp @@ -10,6 +10,30 @@ static const unsigned char data_fonts_pressstart_prstartk_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_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_Moveright_png[] = { + #include +}; + static const unsigned char data_images_keybinds_Harddrop_png[] = { #include }; @@ -18,36 +42,12 @@ static const unsigned char data_images_keybinds_Moveleft_png[] = { #include }; -static const unsigned char data_images_keybinds_RotateCW_png[] = { - #include -}; - -static const unsigned char data_images_keybinds_RotateCCW_png[] = { - #include -}; - -static const unsigned char data_images_keybinds_Softdrop_png[] = { - #include -}; - -static const unsigned char data_images_keybinds_Moveright_png[] = { - #include -}; - -static const unsigned char data_images_keybinds_Rotate180_png[] = { - #include -}; - static const unsigned char data_images_keybinds_Hold_png[] = { #include }; -static const unsigned char data_images_keybinds_Rotate0_png[] = { - #include -}; - -static const unsigned char data_images_keybinds_Retry_png[] = { - #include +static const unsigned char data_images_keybinds_Softdrop_png[] = { + #include }; static const unsigned char data_images_keybinds_Pause_png[] = { @@ -57,16 +57,16 @@ static const unsigned char data_images_keybinds_Pause_png[] = { static const Asset assets[] = { {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_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_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_RotateCW_png, sizeof(data_images_keybinds_RotateCW_png)}, - {data_images_keybinds_RotateCCW_png, sizeof(data_images_keybinds_RotateCCW_png)}, - {data_images_keybinds_Softdrop_png, sizeof(data_images_keybinds_Softdrop_png)}, - {data_images_keybinds_Moveright_png, sizeof(data_images_keybinds_Moveright_png)}, - {data_images_keybinds_Rotate180_png, sizeof(data_images_keybinds_Rotate180_png)}, {data_images_keybinds_Hold_png, sizeof(data_images_keybinds_Hold_png)}, - {data_images_keybinds_Rotate0_png, sizeof(data_images_keybinds_Rotate0_png)}, - {data_images_keybinds_Retry_png, sizeof(data_images_keybinds_Retry_png)}, + {data_images_keybinds_Softdrop_png, sizeof(data_images_keybinds_Softdrop_png)}, {data_images_keybinds_Pause_png, sizeof(data_images_keybinds_Pause_png)}, }; @@ -74,16 +74,16 @@ static const Asset assets[] = { static const std::map assetMap = { {"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/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/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/RotateCW.png", AssetName::data_images_keybinds_RotateCW_png}, - {"data/images/keybinds/RotateCCW.png", AssetName::data_images_keybinds_RotateCCW_png}, - {"data/images/keybinds/Softdrop.png", AssetName::data_images_keybinds_Softdrop_png}, - {"data/images/keybinds/Moveright.png", AssetName::data_images_keybinds_Moveright_png}, - {"data/images/keybinds/Rotate180.png", AssetName::data_images_keybinds_Rotate180_png}, {"data/images/keybinds/Hold.png", AssetName::data_images_keybinds_Hold_png}, - {"data/images/keybinds/Rotate0.png", AssetName::data_images_keybinds_Rotate0_png}, - {"data/images/keybinds/Retry.png", AssetName::data_images_keybinds_Retry_png}, + {"data/images/keybinds/Softdrop.png", AssetName::data_images_keybinds_Softdrop_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 0404d69..e4eacae 100644 --- a/src/Utils/AssetManager.h +++ b/src/Utils/AssetManager.h @@ -11,16 +11,16 @@ struct Asset { enum class AssetName { data_fonts_pressstart_prstart_ttf, data_fonts_pressstart_prstartk_ttf, + data_images_keybinds_Rotate180_png, + data_images_keybinds_Rotate0_png, + data_images_keybinds_RotateCCW_png, + data_images_keybinds_Retry_png, + data_images_keybinds_RotateCW_png, + data_images_keybinds_Moveright_png, data_images_keybinds_Harddrop_png, data_images_keybinds_Moveleft_png, - data_images_keybinds_RotateCW_png, - data_images_keybinds_RotateCCW_png, - data_images_keybinds_Softdrop_png, - data_images_keybinds_Moveright_png, - data_images_keybinds_Rotate180_png, data_images_keybinds_Hold_png, - data_images_keybinds_Rotate0_png, - data_images_keybinds_Retry_png, + data_images_keybinds_Softdrop_png, data_images_keybinds_Pause_png, };