2017-12-28 15:04:10 -05:00
|
|
|
#include <string>
|
|
|
|
#include <curses.h>
|
|
|
|
#include <unistd.h> // usleep
|
|
|
|
|
|
|
|
struct Object {
|
2017-12-28 15:13:43 -05:00
|
|
|
int x, y; // center
|
|
|
|
int width, height; // full, not half
|
2017-12-28 15:04:10 -05:00
|
|
|
double scale;
|
|
|
|
|
2017-12-28 15:13:43 -05:00
|
|
|
int Left() const { return x - (width / 2.0) * scale; }
|
|
|
|
int Right() const { return x + (width / 2.0) * scale; }
|
|
|
|
int Top() const { return y - (height / 2.0) * scale; }
|
|
|
|
int Bottom() const { return y + (height / 2.0) * scale; }
|
2017-12-28 15:04:10 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
2017-12-28 15:13:43 -05:00
|
|
|
screen.x = 20;
|
|
|
|
screen.width = 20;
|
|
|
|
screen.y = 13;
|
|
|
|
screen.height = 6;
|
2017-12-28 15:04:10 -05:00
|
|
|
screen.scale = 1.0;
|
|
|
|
|
|
|
|
const Object initial_screen = screen;
|
|
|
|
|
|
|
|
Object object;
|
2017-12-28 15:13:43 -05:00
|
|
|
object.x = 3;
|
|
|
|
object.width = 5;
|
|
|
|
object.y = 6;
|
|
|
|
object.height = 4;
|
2017-12-28 15:04:10 -05:00
|
|
|
object.scale = 1.0;
|
|
|
|
|
|
|
|
const Object initial_object = object;
|
|
|
|
|
2017-12-28 15:13:43 -05:00
|
|
|
double scale_multiplier = 0.2;
|
|
|
|
|
2017-12-28 15:04:10 -05:00
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
screen = initial_screen;
|
|
|
|
object = initial_object;
|
|
|
|
|
2017-12-28 15:13:43 -05:00
|
|
|
object.scale *= scale_multiplier;
|
|
|
|
scale_multiplier *= 2;
|
|
|
|
|
|
|
|
// NOTE: This causes it to pause on each resize when it is immediately covered.
|
|
|
|
// To avoid this, move this initialization outside of the "while(true)" loop.
|
2017-12-28 15:04:10 -05:00
|
|
|
bool covered_last_frame = false;
|
|
|
|
|
2017-12-28 15:13:43 -05:00
|
|
|
const int num_rows = 14;
|
|
|
|
for(auto r = 0; r < num_rows; ++r)
|
2017-12-28 15:04:10 -05:00
|
|
|
{
|
2017-12-28 15:13:43 -05:00
|
|
|
const int num_columns = 40;
|
|
|
|
for(auto c = 0; c < num_columns; ++c)
|
2017-12-28 15:04:10 -05:00
|
|
|
{
|
|
|
|
// DRAW
|
|
|
|
clear();
|
2017-12-28 15:13:43 -05:00
|
|
|
int left;
|
|
|
|
left = screen.Left();
|
|
|
|
if(left < 0)
|
|
|
|
left = 0;
|
|
|
|
// NOTE: +1 to account for full width
|
|
|
|
std::string scr(screen.Right() - left + 1, '+');
|
2017-12-28 15:04:10 -05:00
|
|
|
for(auto row = screen.Top(); row <= screen.Bottom(); ++row)
|
2017-12-28 15:13:43 -05:00
|
|
|
mvaddstr(row, left, scr.c_str());
|
2017-12-28 15:04:10 -05:00
|
|
|
|
2017-12-28 15:13:43 -05:00
|
|
|
left = object.Left();
|
|
|
|
if(left < 0)
|
|
|
|
left = 0;
|
|
|
|
// NOTE: +1 to account for full width
|
|
|
|
std::string obj(object.Right() - left + 1, '*');
|
2017-12-28 15:04:10 -05:00
|
|
|
for(auto row = object.Top(); row <= object.Bottom(); ++row)
|
2017-12-28 15:13:43 -05:00
|
|
|
mvaddstr(row, left, obj.c_str());
|
2017-12-28 15:04:10 -05:00
|
|
|
|
|
|
|
bool covered = InBounds(screen, object);
|
|
|
|
|
|
|
|
mvaddstr(1, 1, covered ? "Covered: YES" : "Covered: NO");
|
|
|
|
|
|
|
|
refresh();
|
|
|
|
|
2017-12-28 15:13:43 -05:00
|
|
|
// When there is a change in "covered" status, freeze to show it:
|
2017-12-28 15:04:10 -05:00
|
|
|
if(covered != covered_last_frame)
|
2017-12-28 15:13:43 -05:00
|
|
|
usleep(300000); // 3/10 sec
|
2017-12-28 15:04:10 -05:00
|
|
|
else
|
2017-12-28 15:13:43 -05:00
|
|
|
usleep(5000); // quick (adjust to your PC's performance)
|
2017-12-28 15:04:10 -05:00
|
|
|
|
|
|
|
covered_last_frame = covered;
|
|
|
|
|
2017-12-28 15:13:43 -05:00
|
|
|
++object.x;
|
2017-12-28 15:04:10 -05:00
|
|
|
}
|
2017-12-28 15:13:43 -05:00
|
|
|
++object.y;
|
|
|
|
object.x -= num_columns;
|
2017-12-28 15:04:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|