aabb/main.cpp

106 lines
2.0 KiB
C++

#include <string>
#include <curses.h>
#include <unistd.h> // usleep
struct Object {
int x1, y1; // top left
int x2, y2; // bottom right
double scale;
int Left() const { return x1; }
int Right() const { return x2; }
int Top() const { return y1; }
int Bottom() const { return y2; }
};
bool InBounds(Object bounds, Object object);
int main(int argc, char *argv[])
{
WINDOW * mainwin;
// Initialize ncurses
if ( (mainwin = initscr()) == NULL ) {
fprintf(stderr, "Error initialising ncurses.\n");
exit(EXIT_FAILURE);
}
Object screen;
screen.x1 = 10;
screen.x2 = 31;
screen.y1 = 10;
screen.y2 = 15;
screen.scale = 1.0;
const Object initial_screen = screen;
Object object;
object.x1 = 1;
object.x2 = 5;
object.y1 = 4;
object.y2 = 8;
object.scale = 1.0;
const Object initial_object = object;
while(true)
{
screen = initial_screen;
object = initial_object;
bool covered_last_frame = false;
for(auto y = 0; y < 14; ++y)
{
for(auto x = 0; x < 40; ++x)
{
// DRAW
clear();
std::string scr(screen.Right() - screen.Left() + 1, '+');
for(auto row = screen.Top(); row <= screen.Bottom(); ++row)
mvaddstr(row, screen.Left(), scr.c_str());
std::string obj(object.Right() - object.Left() + 1, '*');
for(auto row = object.Top(); row <= object.Bottom(); ++row)
mvaddstr(row, object.Left(), obj.c_str());
bool covered = InBounds(screen, object);
mvaddstr(1, 1, covered ? "Covered: YES" : "Covered: NO");
refresh();
if(covered != covered_last_frame)
usleep(500000); // 1/2 sec
else
usleep(35000); // quick
covered_last_frame = covered;
++object.x1;
++object.x2;
}
++object.y1;
++object.y2;
object.x1 -= 40;
object.x2 -= 40;
}
}
// destroy ncurses
delwin(mainwin);
endwin();
refresh();
return 0;
}
bool InBounds(Object bounds, Object object)
{
if(object.Right() < bounds.Left()
|| object.Left() > bounds.Right()
|| object.Bottom() < bounds.Top()
|| object.Top() > bounds.Bottom())
return false;
return true;
}