2023-04-02 10:09:17 -04:00
|
|
|
#include "Pieces.h"
|
|
|
|
|
|
|
|
#include <curses.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2023-04-02 12:31:01 -04:00
|
|
|
#include <set>
|
2023-04-02 10:09:17 -04:00
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
static void finish(int sig);
|
|
|
|
void init_colors();
|
|
|
|
|
2023-04-02 12:31:01 -04:00
|
|
|
// DEBUG:
|
|
|
|
void DemoPieces();
|
|
|
|
|
2023-04-02 10:09:17 -04:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
initscr(); /* initialize the curses library */
|
|
|
|
keypad(stdscr, TRUE); /* enable keyboard mapping */
|
|
|
|
nonl(); /* tell curses not to do NL->CR/NL on output */
|
|
|
|
cbreak(); /* take input chars one at a time, no wait for \n */
|
|
|
|
echo(); /* echo input - in color */
|
|
|
|
|
|
|
|
if(has_colors())
|
|
|
|
init_colors();
|
|
|
|
|
2023-04-02 12:31:01 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-02 10:09:17 -04:00
|
|
|
int num = 0;
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
int c = getch(); /* refresh, accept single keystroke of input */
|
|
|
|
attrset(COLOR_PAIR(num++ % 8));
|
|
|
|
|
|
|
|
/* process the command keystroke */
|
|
|
|
}
|
|
|
|
|
|
|
|
finish(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE(dev): ALways execute so we don't screw up our terminal.
|
|
|
|
static void finish(int sig)
|
|
|
|
{
|
|
|
|
endwin();
|
2023-04-02 12:31:01 -04:00
|
|
|
std::exit(0);
|
2023-04-02 10:09:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void init_colors()
|
|
|
|
{
|
|
|
|
start_color();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Simple color assignment, often all we need. Color pair 0 cannot
|
|
|
|
* be redefined. This example uses the same value for the color
|
|
|
|
* pair as for the foreground color, though of course that is not
|
|
|
|
* necessary:
|
|
|
|
*/
|
|
|
|
init_pair(1, COLOR_RED, COLOR_BLACK);
|
|
|
|
init_pair(2, COLOR_GREEN, COLOR_BLACK);
|
|
|
|
init_pair(3, COLOR_YELLOW, COLOR_BLACK);
|
|
|
|
init_pair(4, COLOR_BLUE, COLOR_BLACK);
|
|
|
|
init_pair(5, COLOR_CYAN, COLOR_BLACK);
|
|
|
|
init_pair(6, COLOR_MAGENTA, COLOR_BLACK);
|
|
|
|
init_pair(7, COLOR_WHITE, COLOR_BLACK);
|
|
|
|
}
|
2023-04-02 12:31:01 -04:00
|
|
|
|
|
|
|
void DemoPieces()
|
|
|
|
{
|
|
|
|
std::set rotations = {
|
|
|
|
Rotation::ROTATION_NONE,
|
|
|
|
Rotation::ROTATION_90,
|
|
|
|
Rotation::ROTATION_180,
|
|
|
|
Rotation::ROTATION_270,
|
|
|
|
};
|
|
|
|
for(auto rotation : rotations)
|
|
|
|
{
|
|
|
|
clear();
|
2023-04-02 12:41:16 -04:00
|
|
|
|
|
|
|
int x, y;
|
|
|
|
|
|
|
|
// Always same distance apart:
|
|
|
|
x = 10;
|
|
|
|
y = 2;
|
2023-04-02 12:31:01 -04:00
|
|
|
for(auto i = 0; i < 8; ++i)
|
|
|
|
{
|
|
|
|
Piece piece = static_cast<Piece>(i);
|
2023-04-02 12:41:16 -04:00
|
|
|
DrawPiece(piece, y, x, rotation);
|
2023-04-02 12:53:00 -04:00
|
|
|
mvaddstr(y-1, x, "Piece");
|
|
|
|
mvaddstr(y-1, x + 6, std::to_string(i + 1).c_str());
|
2023-04-02 12:31:01 -04:00
|
|
|
x += 12;
|
|
|
|
}
|
2023-04-02 12:41:16 -04:00
|
|
|
|
|
|
|
// Always same width apart:
|
|
|
|
x = 2;
|
|
|
|
y = 10;
|
|
|
|
for(auto i = 0; i < 8; ++i)
|
|
|
|
{
|
|
|
|
Piece piece = static_cast<Piece>(i);
|
|
|
|
DrawPiece(piece, y, x, rotation);
|
|
|
|
x += PieceWidth(piece, rotation) * 2 + 2;
|
|
|
|
}
|
|
|
|
// Always same height apart, sharing first piece with same-width row:
|
|
|
|
x = 2;
|
|
|
|
y = 10 + PieceHeight(0, rotation) + 1;
|
|
|
|
for(auto i = 1; i < 8; ++i) // skip first
|
|
|
|
{
|
|
|
|
Piece piece = static_cast<Piece>(i);
|
|
|
|
DrawPiece(piece, y, x, rotation);
|
|
|
|
y += PieceHeight(piece, rotation) + 1;
|
|
|
|
}
|
|
|
|
|
2023-04-02 12:31:01 -04:00
|
|
|
getch();
|
|
|
|
}
|
|
|
|
move(8, 2);
|
|
|
|
}
|