From: Thadeu Lima de Souza Cascardo Date: Tue, 12 Aug 2008 07:48:34 +0000 (-0300) Subject: Read the points from a file. X-Git-Tag: cairo~4 X-Git-Url: http://git.cascardo.eti.br/?p=cascardo%2Fmovie.git;a=commitdiff_plain;h=80e5a1ecfdcf19607b736d01cc2bb8b59288c08b Read the points from a file. --- diff --git a/movie.c b/movie.c index 4184b82..17cf90f 100644 --- a/movie.c +++ b/movie.c @@ -19,6 +19,8 @@ #define WIDTH 800 #define HEIGHT 600 +#include +#include #include #include @@ -31,10 +33,32 @@ #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; }