Added ability to flip piece data.

This commit is contained in:
David Vereb 2023-04-03 21:22:44 -04:00
parent ee82117f19
commit 0d889306e5
3 changed files with 80 additions and 0 deletions

View File

@ -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();

View File

@ -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);

View File

@ -140,5 +140,31 @@ void DemoPieces()
getch();
}
std::vector<Flip> flips = {
Flip::FLIP_NONE,
Flip::FLIP_HORIZONTAL,
Flip::FLIP_HORIZONTAL,
Flip::FLIP_VERTICAL,
Flip::FLIP_VERTICAL,
};
std::vector<PieceData> 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);
}