Added ability to flip piece data.
This commit is contained in:
parent
ee82117f19
commit
0d889306e5
48
Pieces.cpp
48
Pieces.cpp
@ -406,6 +406,54 @@ PieceData RotatePieceData(const PieceData &data, Rotation rotation)
|
|||||||
return rtn;
|
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)
|
unsigned PD_PieceHeight(const PieceData &data)
|
||||||
{
|
{
|
||||||
return data.size();
|
return data.size();
|
||||||
|
6
Pieces.h
6
Pieces.h
@ -11,6 +11,11 @@ enum Rotation {
|
|||||||
ROTATION_180,
|
ROTATION_180,
|
||||||
ROTATION_270,
|
ROTATION_270,
|
||||||
};
|
};
|
||||||
|
enum Flip {
|
||||||
|
FLIP_NONE,
|
||||||
|
FLIP_HORIZONTAL,
|
||||||
|
FLIP_VERTICAL,
|
||||||
|
};
|
||||||
|
|
||||||
#define NUM_PIECES 8
|
#define NUM_PIECES 8
|
||||||
typedef uint8_t Piece; // only need literally 8, not 8 bits, but hey.
|
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);
|
Rotation rotation = Rotation::ROTATION_NONE);
|
||||||
|
|
||||||
PieceData RotatePieceData(const PieceData &data, Rotation rotation);
|
PieceData RotatePieceData(const PieceData &data, Rotation rotation);
|
||||||
|
void FlipPieceData(PieceData &data, Flip flip);
|
||||||
unsigned PD_PieceHeight(const PieceData &data);
|
unsigned PD_PieceHeight(const PieceData &data);
|
||||||
unsigned PD_PieceWidth(const PieceData &data);
|
unsigned PD_PieceWidth(const PieceData &data);
|
||||||
void PD_DrawPiece(const PieceData &data, int y, int x, int color = 0);
|
void PD_DrawPiece(const PieceData &data, int y, int x, int color = 0);
|
||||||
|
26
main.cpp
26
main.cpp
@ -140,5 +140,31 @@ void DemoPieces()
|
|||||||
|
|
||||||
getch();
|
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);
|
move(8, 2);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user