From 6568397392b0f65e2c68b8b8a417e27cc952aac5 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Tue, 12 Aug 2008 02:28:03 -0300 Subject: [PATCH] Moves through a list of points Given a list of points, it will loop over them and show each point centered on the screen, moving in a straight line between them. However, it only works with lines with a slope of one. --- movie.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/movie.c b/movie.c index f6a20b9..42310e6 100644 --- a/movie.c +++ b/movie.c @@ -22,12 +22,31 @@ #include #include +#define IS_CENTER(cx, cy, x, y) \ + ((x + WIDTH/2 == cx) && (y + HEIGHT/2 == cy)) + +SDL_Rect points[] = { + {400, 300, 800, 600}, + {450, 350, 800, 600} +}; + SDL_Rect * GetNextPoint (void) { static SDL_Rect rect = {0, 0, WIDTH, HEIGHT}; - rect.x = (rect.x++) % WIDTH; - rect.y = (rect.y++) % HEIGHT; + static int cur = 0; + int dx, dy; + int next; + next = (cur + 1) % (sizeof (points) / sizeof (SDL_Rect)); + if (IS_CENTER (points[next].x, points[next].y, rect.x, rect.y)) + { + cur = next; + next = (cur + 1) % (sizeof (points) / sizeof (SDL_Rect)); + } + dx = (points[next].x > points[cur].x) ? 1 : -1; + dy = (points[next].y > points[cur].y) ? 1 : -1; + rect.x = (rect.x + dx) % WIDTH; + rect.y = (rect.y + dy) % HEIGHT; return ▭ } -- 2.20.1