Moves through a list of points
[cascardo/movie.git] / movie.c
1 /*
2  *  Copyright (C) 2008  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #define WIDTH 800
20 #define HEIGHT 600
21
22 #include <SDL.h>
23 #include <SDL_image.h>
24
25 #define IS_CENTER(cx, cy, x, y) \
26         ((x + WIDTH/2 == cx) && (y + HEIGHT/2 == cy))
27
28 SDL_Rect points[] = {
29   {400, 300, 800, 600},
30   {450, 350, 800, 600}
31 };
32
33 SDL_Rect *
34 GetNextPoint (void)
35 {
36   static SDL_Rect rect = {0, 0, WIDTH, HEIGHT};
37   static int cur = 0;
38   int dx, dy;
39   int next;
40   next = (cur + 1) % (sizeof (points) / sizeof (SDL_Rect));
41   if (IS_CENTER (points[next].x, points[next].y, rect.x, rect.y))
42   {
43     cur = next;
44     next = (cur + 1) % (sizeof (points) / sizeof (SDL_Rect));
45   }
46   dx = (points[next].x > points[cur].x) ? 1 : -1;
47   dy = (points[next].y > points[cur].y) ? 1 : -1;
48   rect.x = (rect.x + dx) % WIDTH;
49   rect.y = (rect.y + dy) % HEIGHT;
50   return &rect;
51 }
52
53 void
54 ShowPoint (SDL_Surface *screen, SDL_Surface *image, SDL_Rect *rect)
55 {
56   SDL_BlitSurface (image, rect, screen, NULL);
57   SDL_UpdateRect (screen, 0, 0, 0, 0);
58 }
59
60 Uint32
61 ShowNext (Uint32 interval, void *data)
62 {
63   SDL_UserEvent event;
64   event.type = SDL_USEREVENT;
65   event.code = 0;
66   SDL_PushEvent ((SDL_Event *) &event);
67   return 33;
68 }
69
70 int
71 main (int argc, char **argv)
72 {
73   SDL_Surface *screen;
74   SDL_Surface *image;
75   SDL_Event event;
76   SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER);
77   screen = SDL_SetVideoMode (800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
78   image = IMG_Load ("/home/cascardo/fotos/debconf.jpg");
79   SDL_AddTimer (0, ShowNext, NULL);
80   while (SDL_WaitEvent (&event))
81   {
82     if (event.type == SDL_KEYDOWN)
83       break;
84     else if (event.type == SDL_USEREVENT)
85       ShowPoint (screen, image, GetNextPoint ());
86   }
87   SDL_FreeSurface (image);
88   SDL_Quit ();
89   return 0;
90 }