Initial Commit. Working example. Needs refactored to store object position by CENTER to make SCALE easier to implement.

This commit is contained in:
David Vereb 2017-12-28 15:04:10 -05:00
commit ff8c076044
3 changed files with 110 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*~
a.out

2
Makefile Normal file
View File

@ -0,0 +1,2 @@
a.out: main.cpp
clang++ --std=c++11 main.cpp -lncurses

105
main.cpp Normal file
View File

@ -0,0 +1,105 @@
#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;
}