From 153a80ddbed61ba3f5c41f3a2ff2902d019e9bb3 Mon Sep 17 00:00:00 2001 From: David Vereb Date: Mon, 3 Apr 2023 22:13:21 -0400 Subject: [PATCH] Moved demo stuff into Demo.h and Demo.cpp. Added ability to place pieces from terminal, though positional data hasn't been implemented yet. --- Demo.cpp | 91 ++++++++++++++++++++++++++++++++++++++++++++ Demo.h | 6 +++ Makefile | 4 +- README | 31 +++++++++++++++ main.cpp | 113 ++++++++++++++----------------------------------------- 5 files changed, 159 insertions(+), 86 deletions(-) create mode 100644 Demo.cpp create mode 100644 Demo.h create mode 100644 README diff --git a/Demo.cpp b/Demo.cpp new file mode 100644 index 0000000..6ad8d1b --- /dev/null +++ b/Demo.cpp @@ -0,0 +1,91 @@ +#include "Demo.h" +#include "Pieces.h" + +#include +#include +#include +#include + +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 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); +} diff --git a/Demo.h b/Demo.h new file mode 100644 index 0000000..38c1022 --- /dev/null +++ b/Demo.h @@ -0,0 +1,6 @@ +#ifndef APAD_DEMO_H +#define APAD_DEMO_H + +void DemoPieces(); + +#endif diff --git a/Makefile b/Makefile index 7ce8de7..52acccd 100644 --- a/Makefile +++ b/Makefile @@ -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 - 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: rm build/apad diff --git a/README b/README new file mode 100644 index 0000000..a549e8b --- /dev/null +++ b/README @@ -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 diff --git a/main.cpp b/main.cpp index 07ecf40..c8eb24d 100644 --- a/main.cpp +++ b/main.cpp @@ -1,3 +1,4 @@ +#include "Demo.h" #include "Pieces.h" #include @@ -10,8 +11,7 @@ static void finish(int sig); void init_colors(); -// DEBUG: -void DemoPieces(); +void PlacePiece(Piece piece, const std::vector &flips, Rotation rotation, int y, int x); int main(int argc, char *argv[]) { @@ -33,16 +33,33 @@ int main(int argc, char *argv[]) // for each piece passed in, render it: { + int x = 1; int y = 1; + auto count = 0; for(auto input = 1; input < argc; ++input) { try { - Piece piece = 0b111 & std::atoi(argv[input]); - PD_DrawPiece(pieces.at(piece), y, 1); - y += PD_PieceHeight(pieces.at(piece)) + 1; + unsigned long input_val = std::atoi(argv[input]); + Piece piece = 0b111 & input_val; + 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((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) { 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); } -void DemoPieces() +void PlacePiece(Piece piece, const std::vector &flips, Rotation rotation, int y, int x) { - std::set rotations = { - Rotation::ROTATION_NONE, - Rotation::ROTATION_90, - Rotation::ROTATION_180, - Rotation::ROTATION_270, - }; - for(auto rotation : rotations) - { - clear(); + PieceData data = pieces.at(piece); - int x, y; + for(Flip flip : flips) + FlipPieceData(data, flip); - // 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; - } + data = RotatePieceData(data, rotation); - // 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 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); + PD_DrawPiece(data, y, x, piece); }