From 0d889306e5788c408891457416bf2f725d7fc5d2 Mon Sep 17 00:00:00 2001 From: David Vereb Date: Mon, 3 Apr 2023 21:22:44 -0400 Subject: [PATCH] Added ability to flip piece data. --- Pieces.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ Pieces.h | 6 ++++++ main.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) diff --git a/Pieces.cpp b/Pieces.cpp index f596cef..1fe4318 100644 --- a/Pieces.cpp +++ b/Pieces.cpp @@ -406,6 +406,54 @@ PieceData RotatePieceData(const PieceData &data, Rotation rotation) return rtn; } +void FlipPieceData(PieceData &data, Flip flip) +{ + if(!data.size()) + return; + if(!data[0].size()) + return; + + switch(flip) + { + default: + case Flip::FLIP_NONE: + return; + case Flip::FLIP_HORIZONTAL: + // Go through EVERY row: + for(size_t row = 0; row < data.size(); ++row) + { + // And swap the front half with the back half + // [1][2][3][2][1] + // ^ ^ ^ ^ + // | +-----+ | <-- second + // | | + // +-----------+ <-- first + for(size_t col = 0; col < data[0].size() / 2; ++col) + { + // for each row, swap columns + bool temp = data[row][col]; + data[row][col] = data[row][data[0].size() - col - 1]; + data[row][data[0].size() - col - 1] = temp; + } + } + break; + case Flip::FLIP_VERTICAL: + // Go through EVERY col: + for(size_t col = 0; col < data[0].size(); ++col) + { + // And swap the top half with the bottom half + for(size_t row = 0; row < data.size() / 2; ++row) + { + // for each row, swap columns + bool temp = data[row][col]; + data[row][col] = data[data.size() - row - 1][col]; + data[data.size() - row - 1][col] = temp; + } + } + break; + }; +} + unsigned PD_PieceHeight(const PieceData &data) { return data.size(); diff --git a/Pieces.h b/Pieces.h index 794365b..5a6e04b 100644 --- a/Pieces.h +++ b/Pieces.h @@ -11,6 +11,11 @@ enum Rotation { ROTATION_180, ROTATION_270, }; +enum Flip { + FLIP_NONE, + FLIP_HORIZONTAL, + FLIP_VERTICAL, +}; #define NUM_PIECES 8 typedef uint8_t Piece; // only need literally 8, not 8 bits, but hey. @@ -148,6 +153,7 @@ void DrawPiece(const Piece &piece, int y, int x, Rotation rotation = Rotation::ROTATION_NONE); PieceData RotatePieceData(const PieceData &data, Rotation rotation); +void FlipPieceData(PieceData &data, Flip flip); unsigned PD_PieceHeight(const PieceData &data); unsigned PD_PieceWidth(const PieceData &data); void PD_DrawPiece(const PieceData &data, int y, int x, int color = 0); diff --git a/main.cpp b/main.cpp index 7c9aef4..07ecf40 100644 --- a/main.cpp +++ b/main.cpp @@ -140,5 +140,31 @@ void DemoPieces() getch(); } + + std::vector flips = { + Flip::FLIP_NONE, + Flip::FLIP_HORIZONTAL, + Flip::FLIP_HORIZONTAL, + Flip::FLIP_VERTICAL, + Flip::FLIP_VERTICAL, + }; + + std::vector piece_data_to_flip; + for(size_t p = 0; p < pieces.size(); ++p) + piece_data_to_flip.push_back(pieces.at(p)); + for(const auto &flip : flips) + { + clear(); + int y = 2; + int i = 0; + for(auto &data : piece_data_to_flip) + { + FlipPieceData(data, flip); + PD_DrawPiece(data, y, 2, i++); + y += PD_PieceHeight(data) + 2; + } + getch(); + } + move(8, 2); }