Moves through a list of points
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Tue, 12 Aug 2008 05:28:03 +0000 (02:28 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Tue, 12 Aug 2008 07:50:47 +0000 (04:50 -0300)
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

diff --git a/movie.c b/movie.c
index f6a20b9..42310e6 100644 (file)
--- a/movie.c
+++ b/movie.c
 #include <SDL.h>
 #include <SDL_image.h>
 
+#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 &rect;
 }