From ff8c0760444a966f87d712ea9fc82705dce00c99 Mon Sep 17 00:00:00 2001 From: David Vereb Date: Thu, 28 Dec 2017 15:04:10 -0500 Subject: [PATCH] Initial Commit. Working example. Needs refactored to store object position by CENTER to make SCALE easier to implement. --- .gitignore | 3 ++ Makefile | 2 + main.cpp | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6f3d0df --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*~ +a.out + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7cb21e7 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +a.out: main.cpp + clang++ --std=c++11 main.cpp -lncurses diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..3fa17ff --- /dev/null +++ b/main.cpp @@ -0,0 +1,105 @@ +#include +#include +#include // 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; +}