Use Bresenham to draw the line between faces
[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 SWAP(x, y) do { \
26         x ^= y; y ^= x; x ^= y; \
27         } while (0)
28
29 #define ABS(x) ((x) < 0 ? -(x) : (x))
30
31 #define IS_CENTER(cx, cy, x, y) \
32         ((x + WIDTH/2 == cx) && (y + HEIGHT/2 == cy))
33
34 SDL_Rect points[] = {
35   {400, 300, 800, 600},
36   {450, 350, 800, 600}
37 };
38
39 SDL_Rect *
40 GetNextPoint (void)
41 {
42   static SDL_Rect rect = {0, 0, WIDTH, HEIGHT};
43   static int cur = -1;
44   static int inc, err, thre, swap;
45   static int x1, y1, x2, y2;
46   static int x, y;
47   int next;
48   next = (cur + 1) % (sizeof (points) / sizeof (SDL_Rect));
49   if (IS_CENTER (points[next].x, points[next].y, rect.x, rect.y) || cur == -1)
50   {
51     cur = next;
52     next = (cur + 1) % (sizeof (points) / sizeof (SDL_Rect));
53     err = 0;
54     swap = 0;
55     x1 = points[cur].x;
56     y1 = points[cur].y;
57     x2 = points[next].x;
58     y2 = points[next].y;
59     inc = y2 - y1;
60     thre = x2 - x1;
61     if (ABS (inc) > ABS (thre))
62     {
63       SWAP (inc, thre);
64       SWAP (x1, y1);
65       SWAP (x2, y2);
66       swap = 1;
67     }
68     x = x1;
69     y = y1;
70   }
71   rect.x = (swap ? y : x) - WIDTH/2;
72   rect.y = (swap ? x : y) - HEIGHT/2;
73   (x2 < x1) ? x-- : x++;
74   err += ABS (inc);
75   if (err >= ABS (thre))
76   {
77     err -= ABS (thre);
78     y += (inc < 0) ? -1 : 1;
79   }
80   return &rect;
81 }
82
83 void
84 ShowPoint (SDL_Surface *screen, SDL_Surface *image, SDL_Rect *rect)
85 {
86   SDL_BlitSurface (image, rect, screen, NULL);
87   SDL_UpdateRect (screen, 0, 0, 0, 0);
88 }
89
90 Uint32
91 ShowNext (Uint32 interval, void *data)
92 {
93   SDL_UserEvent event;
94   event.type = SDL_USEREVENT;
95   event.code = 0;
96   SDL_PushEvent ((SDL_Event *) &event);
97   return 33;
98 }
99
100 int
101 main (int argc, char **argv)
102 {
103   SDL_Surface *screen;
104   SDL_Surface *image;
105   SDL_Event event;
106   SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER);
107   screen = SDL_SetVideoMode (800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
108   image = IMG_Load ("/home/cascardo/fotos/debconf.jpg");
109   SDL_AddTimer (0, ShowNext, NULL);
110   while (SDL_WaitEvent (&event))
111   {
112     if (event.type == SDL_KEYDOWN)
113       break;
114     else if (event.type == SDL_USEREVENT)
115       ShowPoint (screen, image, GetNextPoint ());
116   }
117   SDL_FreeSurface (image);
118   SDL_Quit ();
119   return 0;
120 }