Moved demo stuff into Demo.h and Demo.cpp. Added ability to place pieces from terminal, though positional data hasn't been implemented yet.

This commit is contained in:
David Vereb 2023-04-03 22:13:21 -04:00
parent 0d889306e5
commit 153a80ddbe
5 changed files with 159 additions and 86 deletions

91
Demo.cpp Normal file
View File

@ -0,0 +1,91 @@
#include "Demo.h"
#include "Pieces.h"
#include <curses.h>
#include <set>
#include <string>
#include <vector>
void DemoPieces()
{
std::set rotations = {
Rotation::ROTATION_NONE,
Rotation::ROTATION_90,
Rotation::ROTATION_180,
Rotation::ROTATION_270,
};
for(auto rotation : rotations)
{
clear();
int x, y;
// Always same distance apart:
x = 10;
y = 2;
for(auto i = 0; i < 8; ++i)
{
PieceData data = pieces.at(i);
data = RotatePieceData(data, rotation);
PD_DrawPiece(data, y, x, i);
mvaddstr(y-1, x, "Piece");
mvaddstr(y-1, x + 6, std::to_string(i + 1).c_str());
x += 12;
}
// Always same width apart:
x = 2;
y = 10;
for(auto i = 0; i < 8; ++i)
{
PieceData data = pieces.at(i);
data = RotatePieceData(data, rotation);
PD_DrawPiece(data, y, x, i);
x += PD_PieceWidth(data) * 2 + 2;
}
// Always same height apart, sharing first piece with same-width row:
x = 2;
y = 10;
{
auto data = pieces.at(0);
data = RotatePieceData(data, rotation);
y += PD_PieceHeight(data) + 1;
}
for(auto i = 1; i < 8; ++i) // skip first
{
PieceData data = pieces.at(i);
data = RotatePieceData(data, rotation);
PD_DrawPiece(data, y, x, i);
y += PD_PieceHeight(data) + 1;
}
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);
}

6
Demo.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef APAD_DEMO_H
#define APAD_DEMO_H
void DemoPieces();
#endif

View File

@ -1,6 +1,6 @@
build/apad: main.cpp Pieces.cpp Pieces.h build/apad: main.cpp Pieces.cpp Pieces.h Demo.cpp Demo.h
mkdir -p build mkdir -p build
clang++ -g -O0 -std=c++17 -o $@ main.cpp Pieces.cpp -lncurses clang++ -g -O0 -std=c++17 -o $@ main.cpp Pieces.cpp Demo.cpp -lncurses
clean: clean:
rm build/apad rm build/apad

31
README Normal file
View File

@ -0,0 +1,31 @@
# Grid:
[0 1 2 3 4 5 ]
[6 7 8 9 10 11]
[12 13 14 15 16 17 18]
[19 20 21 22 23 24 25]
[26 27 28 29 30 31 32]
[33 34 35 36 37 38 39]
[40 41 42]
# Order of Operations:
When rendering, always perform the actions in this order:
1. Flips, if set
2. Rotation
# All Piece Placement Format:
Assumes you're placing, in order, all pieces
2 bits for flip, 2 bits for rotation, 6 bits for position, 8 times.
10 * 8 = 80 bits.
# TODO(dev):
# Individual Piece Placement Format:
3 bits for piece number (#0 - #7)
2 bits for flip
(first bit = horizontal flip on)
(second bit = vertical flip on)
2 bits for rotation
(none, 90, 180, 270)
6 bits for position

113
main.cpp
View File

@ -1,3 +1,4 @@
#include "Demo.h"
#include "Pieces.h" #include "Pieces.h"
#include <curses.h> #include <curses.h>
@ -10,8 +11,7 @@
static void finish(int sig); static void finish(int sig);
void init_colors(); void init_colors();
// DEBUG: void PlacePiece(Piece piece, const std::vector<Flip> &flips, Rotation rotation, int y, int x);
void DemoPieces();
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -33,16 +33,33 @@ int main(int argc, char *argv[])
// for each piece passed in, render it: // for each piece passed in, render it:
{ {
int x = 1;
int y = 1; int y = 1;
auto count = 0;
for(auto input = 1; input < argc; ++input) for(auto input = 1; input < argc; ++input)
{ {
try { try {
Piece piece = 0b111 & std::atoi(argv[input]); unsigned long input_val = std::atoi(argv[input]);
PD_DrawPiece(pieces.at(piece), y, 1); Piece piece = 0b111 & input_val;
y += PD_PieceHeight(pieces.at(piece)) + 1; Flip horizontal = (0b1000 & input_val) ? Flip::FLIP_HORIZONTAL : Flip::FLIP_NONE;
Flip vertical = (0b10000 & input_val) ? Flip::FLIP_VERTICAL : Flip::FLIP_NONE;
Rotation rotation = static_cast<Rotation>((0b1100000 & input_val) >> 5);
PlacePiece(piece,
{ horizontal, vertical },
rotation,
y,
x);
// y += PD_PieceHeight(pieces.at(piece)) + 1;
y += 5;
} catch(const std::exception &e) { } catch(const std::exception &e) {
mvaddstr(y, 1, "std::atoi error"); mvaddstr(y, 1, "std::atoi error");
y += 2; y += 3;
}
if(++count > 7)
{
count = 0;
y = 1;
x += 10;
} }
} }
} }
@ -85,86 +102,14 @@ void init_colors()
init_pair(7, COLOR_WHITE, COLOR_BLACK); init_pair(7, COLOR_WHITE, COLOR_BLACK);
} }
void DemoPieces() void PlacePiece(Piece piece, const std::vector<Flip> &flips, Rotation rotation, int y, int x)
{ {
std::set rotations = { PieceData data = pieces.at(piece);
Rotation::ROTATION_NONE,
Rotation::ROTATION_90,
Rotation::ROTATION_180,
Rotation::ROTATION_270,
};
for(auto rotation : rotations)
{
clear();
int x, y; for(Flip flip : flips)
FlipPieceData(data, flip);
// Always same distance apart: data = RotatePieceData(data, rotation);
x = 10;
y = 2;
for(auto i = 0; i < 8; ++i)
{
PieceData data = pieces.at(i);
data = RotatePieceData(data, rotation);
PD_DrawPiece(data, y, x, i);
mvaddstr(y-1, x, "Piece");
mvaddstr(y-1, x + 6, std::to_string(i + 1).c_str());
x += 12;
}
// Always same width apart: PD_DrawPiece(data, y, x, piece);
x = 2;
y = 10;
for(auto i = 0; i < 8; ++i)
{
PieceData data = pieces.at(i);
data = RotatePieceData(data, rotation);
PD_DrawPiece(data, y, x, i);
x += PD_PieceWidth(data) * 2 + 2;
}
// Always same height apart, sharing first piece with same-width row:
x = 2;
y = 10;
{
auto data = pieces.at(0);
data = RotatePieceData(data, rotation);
y += PD_PieceHeight(data) + 1;
}
for(auto i = 1; i < 8; ++i) // skip first
{
PieceData data = pieces.at(i);
data = RotatePieceData(data, rotation);
PD_DrawPiece(data, y, x, i);
y += PD_PieceHeight(data) + 1;
}
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);
} }