Read the points from a file.
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Tue, 12 Aug 2008 07:48:34 +0000 (04:48 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Tue, 12 Aug 2008 07:52:33 +0000 (04:52 -0300)
movie.c

diff --git a/movie.c b/movie.c
index 4184b82..17cf90f 100644 (file)
--- a/movie.c
+++ b/movie.c
@@ -19,6 +19,8 @@
 #define WIDTH 800
 #define HEIGHT 600
 
+#include <stdio.h>
+#include <stdlib.h>
 #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 *points;
+int psize;
+
+void
+ReadPoints (char *filename)
+{
+  FILE *file;
+  char *buffer;
+  char *next;
+  size_t len;
+  int i;
+  file = fopen (filename, "r");
+  fscanf (file, "%d\n", &psize);
+  points = malloc (sizeof (SDL_Rect) * psize);
+  if (points == NULL)
+    abort ();
+  buffer = NULL;
+  len = 0;
+  for (i = 0; i < psize; i++)
+  {
+    getline (&buffer, &len, file);
+    points[i].x = strtol (buffer, &next, 0);
+    points[i].y = strtol (next+1, NULL, 0);
+  }
+  fclose (file);
+}
 
 SDL_Rect *
 GetNextPoint (void)
@@ -45,11 +69,11 @@ GetNextPoint (void)
   static int x1, y1, x2, y2;
   static int x, y;
   int next;
-  next = (cur + 1) % (sizeof (points) / sizeof (SDL_Rect));
+  next = (cur + 1) % psize;
   if (IS_CENTER (points[next].x, points[next].y, rect.x, rect.y) || cur == -1)
   {
     cur = next;
-    next = (cur + 1) % (sizeof (points) / sizeof (SDL_Rect));
+    next = (cur + 1) % psize;
     err = 0;
     swap = 0;
     x1 = points[cur].x;
@@ -103,6 +127,7 @@ main (int argc, char **argv)
   SDL_Surface *screen;
   SDL_Surface *image;
   SDL_Event event;
+  ReadPoints ("pro-gnu");
   SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER);
   screen = SDL_SetVideoMode (800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
   image = IMG_Load ("/home/cascardo/fotos/debconf.jpg");
@@ -116,5 +141,6 @@ main (int argc, char **argv)
   }
   SDL_FreeSurface (image);
   SDL_Quit ();
+  free (points);
   return 0;
 }