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.

This commit is contained in:
David Vereb 2017-12-28 15:13:43 -05:00
parent ff8c076044
commit 1ec833b1f3

View File

@ -3,14 +3,14 @@
#include <unistd.h> // usleep #include <unistd.h> // usleep
struct Object { struct Object {
int x1, y1; // top left int x, y; // center
int x2, y2; // bottom right int width, height; // full, not half
double scale; double scale;
int Left() const { return x1; } int Left() const { return x - (width / 2.0) * scale; }
int Right() const { return x2; } int Right() const { return x + (width / 2.0) * scale; }
int Top() const { return y1; } int Top() const { return y - (height / 2.0) * scale; }
int Bottom() const { return y2; } int Bottom() const { return y + (height / 2.0) * scale; }
}; };
bool InBounds(Object bounds, Object object); bool InBounds(Object bounds, Object object);
@ -25,43 +25,61 @@ int main(int argc, char *argv[])
} }
Object screen; Object screen;
screen.x1 = 10; screen.x = 20;
screen.x2 = 31; screen.width = 20;
screen.y1 = 10; screen.y = 13;
screen.y2 = 15; screen.height = 6;
screen.scale = 1.0; screen.scale = 1.0;
const Object initial_screen = screen; const Object initial_screen = screen;
Object object; Object object;
object.x1 = 1; object.x = 3;
object.x2 = 5; object.width = 5;
object.y1 = 4; object.y = 6;
object.y2 = 8; object.height = 4;
object.scale = 1.0; object.scale = 1.0;
const Object initial_object = object; const Object initial_object = object;
double scale_multiplier = 0.2;
while(true) while(true)
{ {
screen = initial_screen; screen = initial_screen;
object = initial_object; 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; 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 // DRAW
clear(); 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) 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) 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); bool covered = InBounds(screen, object);
@ -69,20 +87,18 @@ int main(int argc, char *argv[])
refresh(); refresh();
// When there is a change in "covered" status, freeze to show it:
if(covered != covered_last_frame) if(covered != covered_last_frame)
usleep(500000); // 1/2 sec usleep(300000); // 3/10 sec
else else
usleep(35000); // quick usleep(5000); // quick (adjust to your PC's performance)
covered_last_frame = covered; covered_last_frame = covered;
++object.x1; ++object.x;
++object.x2;
} }
++object.y1; ++object.y;
++object.y2; object.x -= num_columns;
object.x1 -= 40;
object.x2 -= 40;
} }
} }