diff --git a/Makefile b/Makefile index ce5bc1e..4f0b447 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ -build/apad: main.cpp +build/apad: main.cpp Pieces.cpp mkdir -p build - clang++ -g -O0 -std=c++17 -o $@ $? -lncurses + clang++ -g -O0 -std=c++17 -o $@ $^ -lncurses clean: rm build/apad diff --git a/Pieces.cpp b/Pieces.cpp new file mode 100644 index 0000000..606c5bd --- /dev/null +++ b/Pieces.cpp @@ -0,0 +1,239 @@ +#include "Pieces.h" + +#include +#include // exit() + +unsigned PieceHeight(const Piece &piece) +{ + switch(piece) + { + case PIECE_ONE: + return 2; + break; + case PIECE_TWO: + return 4; + break; + case PIECE_THREE: + return 4; + break; + case PIECE_FOUR: + return 3; + break; + case PIECE_FIVE: + return 3; + break; + case PIECE_SIX: + return 2; + break; + case PIECE_SEVEN: + return 3; + break; + case PIECE_EIGHT: + return 3; + break; + } + + std::exit(-1); +} + +void DrawPiece(const Piece &piece, int y, int x, Rotation rotation) +{ + attrset(COLOR_PAIR(static_cast(piece) % 8)); + + switch(piece) + { + case PIECE_ONE: + switch(rotation) + { + default: + case ROTATION_NONE: + case ROTATION_180: + mvaddstr(y, x, "[][][]"); + mvaddstr(y + 1, x, "[][][]"); + break; + case ROTATION_90: + case ROTATION_270: + mvaddstr(y, x, "[][]"); + mvaddstr(y + 1, x, "[][]"); + mvaddstr(y + 2, x, "[][]"); + break; + }; + break; + case PIECE_TWO: + switch(rotation) + { + default: + case ROTATION_NONE: + mvaddstr(y, x, "[]"); + mvaddstr(y + 1, x, "[][]"); + mvaddstr(y + 2, x, "[]"); + mvaddstr(y + 3, x, "[]"); + break; + case ROTATION_90: + mvaddstr(y, x, "[][][][]"); + mvaddstr(y + 1, x + 4, "[]"); + break; + case ROTATION_180: + mvaddstr(y, x + 2, "[]"); + mvaddstr(y + 1, x + 2, "[]"); + mvaddstr(y + 2, x, "[][]"); + mvaddstr(y + 3, x + 2, "[]"); + break; + case ROTATION_270: + mvaddstr(y, x + 2, "[]"); + mvaddstr(y + 1, x, "[][][][]"); + break; + }; + break; + case PIECE_THREE: + switch(rotation) + { + default: + case ROTATION_NONE: + mvaddstr(y, x + 2, "[]"); + mvaddstr(y + 1, x + 2, "[]"); + mvaddstr(y + 2, x + 2, "[]"); + mvaddstr(y + 3, x, "[][]"); + break; + case ROTATION_90: + mvaddstr(y, x, "[]"); + mvaddstr(y + 1, x, "[][][][]"); + break; + case ROTATION_180: + mvaddstr(y, x, "[][]"); + mvaddstr(y + 1, x, "[]"); + mvaddstr(y + 2, x, "[]"); + mvaddstr(y + 3, x, "[]"); + break; + case ROTATION_270: + mvaddstr(y, x, "[][][][]"); + mvaddstr(y + 1, x + 6, "[]"); + break; + }; + break; + case PIECE_FOUR: + switch(rotation) + { + default: + case ROTATION_NONE: + mvaddstr(y, x, "[][]"); + mvaddstr(y + 1, x, "[]"); + mvaddstr(y + 2, x, "[][]"); + break; + case ROTATION_90: + mvaddstr(y, x, "[][][]"); + mvaddstr(y + 1, x, "[]"); + mvaddstr(y + 1, x + 4, "[]"); // same line as above + break; + case ROTATION_180: + mvaddstr(y, x, "[][]"); + mvaddstr(y + 1, x + 2, "[]"); + mvaddstr(y + 2, x, "[][]"); + break; + case ROTATION_270: + mvaddstr(y, x, "[]"); + mvaddstr(y, x + 4, "[]"); // same line as above + mvaddstr(y + 1, x, "[][][]"); + break; + }; + break; + case PIECE_FIVE: + switch(rotation) + { + default: + case ROTATION_NONE: + case ROTATION_180: + mvaddstr(y, x, "[][]"); + mvaddstr(y + 1, x + 2, "[]"); + mvaddstr(y + 2, x + 2, "[][]"); + break; + case ROTATION_90: + case ROTATION_270: + mvaddstr(y, x + 4, "[]"); + mvaddstr(y + 1, x, "[][][]"); + mvaddstr(y + 2, x, "[]"); + break; + }; + break; + case PIECE_SIX: + switch(rotation) + { + default: + case ROTATION_NONE: + mvaddstr(y, x, "[][]"); + mvaddstr(y + 1, x + 2, "[][][]"); + break; + case ROTATION_90: + mvaddstr(y, x + 2, "[]"); + mvaddstr(y + 1, x, "[][]"); + mvaddstr(y + 2, x, "[]"); + mvaddstr(y + 3, x, "[]"); + break; + case ROTATION_180: + mvaddstr(y, x, "[][][]"); + mvaddstr(y + 1, x + 4, "[][]"); + break; + case ROTATION_270: + mvaddstr(y, x + 2, "[]"); + mvaddstr(y + 1, x + 2, "[]"); + mvaddstr(y + 2, x, "[][]"); + mvaddstr(y + 3, x, "[]"); + break; + }; + break; + case PIECE_SEVEN: + switch(rotation) + { + default: + case ROTATION_NONE: + mvaddstr(y, x, "[][]"); + mvaddstr(y + 1, x, "[][]"); + mvaddstr(y + 2, x, "[]"); + break; + case ROTATION_90: + mvaddstr(y, x, "[][][]"); + mvaddstr(y + 1, x + 2, "[][]"); + break; + case ROTATION_180: + mvaddstr(y, x + 2, "[]"); + mvaddstr(y + 1, x, "[][]"); + mvaddstr(y + 2, x, "[][]"); + break; + case ROTATION_270: + mvaddstr(y, x, "[][]"); + mvaddstr(y + 1, x, "[][][]"); + break; + }; + break; + case PIECE_EIGHT: + switch(rotation) + { + default: + case ROTATION_NONE: + mvaddstr(y, x + 4, "[]"); + mvaddstr(y + 1, x + 4, "[]"); + mvaddstr(y + 2, x, "[][][]"); + break; + case ROTATION_90: + mvaddstr(y, x, "[]"); + mvaddstr(y + 1, x, "[]"); + mvaddstr(y + 2, x, "[][][]"); + break; + case ROTATION_180: + mvaddstr(y, x, "[][][]"); + mvaddstr(y + 1, x, "[]"); + mvaddstr(y + 2, x, "[]"); + break; + case ROTATION_270: + mvaddstr(y, x, "[][][]"); + mvaddstr(y + 1, x + 4, "[]"); + mvaddstr(y + 2, x + 4, "[]"); + break; + }; + break; + default: + std::exit(-1); + break; + } +} + diff --git a/Pieces.h b/Pieces.h index c6c12a2..82424e4 100644 --- a/Pieces.h +++ b/Pieces.h @@ -3,6 +3,13 @@ #include +enum Rotation { + ROTATION_NONE, + ROTATION_90, + ROTATION_180, + ROTATION_270, +}; + #define NUM_PIECES 8 typedef uint8_t Piece; // only need literally 8, not 8 bits, but hey. @@ -20,7 +27,7 @@ typedef uint8_t Piece; // only need literally 8, not 8 bits, but hey. * [] */ -#define PIECE_TRHEE 0x2 +#define PIECE_THREE 0x2 /* * [] * [] @@ -62,6 +69,8 @@ typedef uint8_t Piece; // only need literally 8, not 8 bits, but hey. * [][][] */ -void DrawPiece(const Piece &piece, int y, int x); +unsigned PieceHeight(const Piece &piece); +void DrawPiece(const Piece &piece, int y, int x, + Rotation rotation = Rotation::ROTATION_NONE); #endif diff --git a/main.cpp b/main.cpp index 008fdf6..b48c8b1 100644 --- a/main.cpp +++ b/main.cpp @@ -3,12 +3,16 @@ #include #include +#include #include #include static void finish(int sig); void init_colors(); +// DEBUG: +void DemoPieces(); + int main(int argc, char *argv[]) { initscr(); /* initialize the curses library */ @@ -20,6 +24,29 @@ int main(int argc, char *argv[]) if(has_colors()) init_colors(); + if(argc == 2 && std::string(argv[1]) == "-d") + { + DemoPieces(); + + finish(0); + } + + // for each piece passed in, render it: + { + int y = 1; + for(auto input = 1; input < argc; ++input) + { + try { + Piece piece = 0b111 & std::atoi(argv[input]); + DrawPiece(piece, y, 1); + y += PieceHeight(piece) + 1; + } catch(const std::exception &e) { + mvaddstr(y, 1, "std::atoi error"); + y += 2; + } + } + } + int num = 0; while(true) { @@ -36,6 +63,7 @@ int main(int argc, char *argv[]) static void finish(int sig) { endwin(); + std::exit(0); } void init_colors() @@ -56,3 +84,26 @@ void init_colors() init_pair(6, COLOR_MAGENTA, COLOR_BLACK); init_pair(7, COLOR_WHITE, COLOR_BLACK); } + +void DemoPieces() +{ + std::set rotations = { + Rotation::ROTATION_NONE, + Rotation::ROTATION_90, + Rotation::ROTATION_180, + Rotation::ROTATION_270, + }; + for(auto rotation : rotations) + { + clear(); + int x = 1; + for(auto i = 0; i < 8; ++i) + { + Piece piece = static_cast(i); + DrawPiece(piece, 2, x, rotation); + x += 12; + } + getch(); + } + move(8, 2); +}