From 1ec833b1f36d741f1ac9ba195ca445da6dd2b2e5 Mon Sep 17 00:00:00 2001 From: David Vereb Date: Thu, 28 Dec 2017 15:13:43 -0500 Subject: [PATCH] Refactored for x,y to be center of object and allow for scale multiplier. Also added code to handle the fact that curses can't print off the left size of the screen. --- main.cpp | 72 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 28 deletions(-) diff --git a/main.cpp b/main.cpp index 3fa17ff..1f7a6d1 100644 --- a/main.cpp +++ b/main.cpp @@ -3,14 +3,14 @@ #include // usleep struct Object { - int x1, y1; // top left - int x2, y2; // bottom right + int x, y; // center + int width, height; // full, not half double scale; - int Left() const { return x1; } - int Right() const { return x2; } - int Top() const { return y1; } - int Bottom() const { return y2; } + 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; } }; bool InBounds(Object bounds, Object object); @@ -25,43 +25,61 @@ int main(int argc, char *argv[]) } Object screen; - screen.x1 = 10; - screen.x2 = 31; - screen.y1 = 10; - screen.y2 = 15; + screen.x = 20; + screen.width = 20; + screen.y = 13; + screen.height = 6; screen.scale = 1.0; const Object initial_screen = screen; Object object; - object.x1 = 1; - object.x2 = 5; - object.y1 = 4; - object.y2 = 8; + object.x = 3; + object.width = 5; + object.y = 6; + object.height = 4; object.scale = 1.0; const Object initial_object = object; + double scale_multiplier = 0.2; + while(true) { screen = initial_screen; object = initial_object; + 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. bool covered_last_frame = false; - for(auto y = 0; y < 14; ++y) + const int num_rows = 14; + for(auto r = 0; r < num_rows; ++r) { - for(auto x = 0; x < 40; ++x) + const int num_columns = 40; + for(auto c = 0; c < num_columns; ++c) { // DRAW clear(); - std::string scr(screen.Right() - screen.Left() + 1, '+'); + int left; + left = screen.Left(); + if(left < 0) + left = 0; + // NOTE: +1 to account for full width + std::string scr(screen.Right() - left + 1, '+'); for(auto row = screen.Top(); row <= screen.Bottom(); ++row) - mvaddstr(row, screen.Left(), scr.c_str()); + mvaddstr(row, left, scr.c_str()); - std::string obj(object.Right() - object.Left() + 1, '*'); + left = object.Left(); + if(left < 0) + left = 0; + // NOTE: +1 to account for full width + std::string obj(object.Right() - left + 1, '*'); for(auto row = object.Top(); row <= object.Bottom(); ++row) - mvaddstr(row, object.Left(), obj.c_str()); + mvaddstr(row, left, obj.c_str()); bool covered = InBounds(screen, object); @@ -69,20 +87,18 @@ int main(int argc, char *argv[]) refresh(); + // When there is a change in "covered" status, freeze to show it: if(covered != covered_last_frame) - usleep(500000); // 1/2 sec + usleep(300000); // 3/10 sec else - usleep(35000); // quick + usleep(5000); // quick (adjust to your PC's performance) covered_last_frame = covered; - ++object.x1; - ++object.x2; + ++object.x; } - ++object.y1; - ++object.y2; - object.x1 -= 40; - object.x2 -= 40; + ++object.y; + object.x -= num_columns; } }