2 * Copyright (C) 2008 Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
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.
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.
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.
24 #define STOP_INTERVAL (2*1000)
26 #define FRAME_INTERVAL (1000/(FPS))
34 #include <SDL_image.h>
37 #define SWAP(x, y) do { \
38 x ^= y; y ^= x; x ^= y; \
41 #define ABS(x) ((x) < 0 ? -(x) : (x))
43 #define IS_CENTER(cx, cy, x, y) \
44 ((x + WIDTH/2 == cx) && (y + HEIGHT/2 == cy))
55 ReadPoints (char *filename)
63 file = fopen (filename, "r");
64 fscanf (file, "%d\n", &psize);
65 points = malloc (sizeof (SDL_Rect) * psize);
66 names = malloc (sizeof (char *) * psize);
67 if (points == NULL || names == NULL)
71 for (i = 0; i < psize; i++)
73 r = getline (&buffer, &len, file);
75 points[i].x = strtol (buffer, &next, 0);
76 points[i].y = strtol (next+1, &next, 0);
77 strtol (next, &next, 0);
78 while (isspace (*next)) next++;
79 names[i] = strdup (next);
87 static SDL_Rect rect = {0, 0, WIDTH, HEIGHT};
88 static int inc, err, thre, swap;
89 static int x1, y1, x2, y2;
92 next = (cur + 1) % psize;
93 if ((points[next].x == rect.x && points[next].y == rect.y) || cur == -1)
97 next = (cur + 1) % psize;
106 if (ABS (inc) > ABS (thre))
116 rect.x = (swap ? y : x);
117 rect.y = (swap ? x : y);
118 (x2 < x1) ? x-- : x++;
120 if (err >= ABS (thre))
123 y += (inc < 0) ? -1 : 1;
129 ShowPoint (SDL_Surface *screen, SDL_Surface *image)
131 SDL_BlitSurface (image, NULL, screen, NULL);
132 SDL_UpdateRect (screen, 0, 0, 0, 0);
136 CairoFromSDL (SDL_Surface *surface)
138 return cairo_image_surface_create_for_data (surface->pixels,
140 surface->w, surface->h,
145 CairoToSDL (cairo_surface_t *surface)
147 return SDL_CreateRGBSurfaceFrom (cairo_image_surface_get_data (surface),
148 cairo_image_surface_get_width (surface),
149 cairo_image_surface_get_height (surface),
151 cairo_image_surface_get_stride (surface),
152 0x00ff0000, 0x0000ff00, 0x000000ff,
157 CairoTarget (SDL_Surface *image)
159 static unsigned char *data = NULL;
160 static cairo_surface_t *surface = NULL;
161 static cairo_t *ctx = NULL;
162 cairo_surface_t *source;
163 if (data == NULL && surface == NULL && ctx == NULL)
165 data = malloc (WIDTH * HEIGHT * 4);
166 surface = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_ARGB32,
167 WIDTH, HEIGHT, WIDTH * 4);
168 ctx = cairo_create (surface);
169 cairo_scale (ctx, 4, 4);
171 source = CairoFromSDL (image);
172 cairo_set_source_surface (ctx, source, 0, 0);
174 cairo_surface_destroy (source);
175 return CairoToSDL (surface);
179 GetNextImage (SDL_Surface *image)
186 SDL_Color Yellow = { 255, 255, 0, 0};
187 center = GetNextPoint ();
188 slice = SDL_CreateRGBSurface (SDL_SWSURFACE, WIDTH/4, HEIGHT/4,
190 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
191 center.x -= (WIDTH/2) / 4;
192 center.y -= (HEIGHT/2) / 4;
195 SDL_BlitSurface (image, ¢er, slice, NULL);
196 SDL_UpdateRect (slice, 0, 0, 0, 0);
197 scale = CairoTarget (slice);
198 SDL_FreeSurface (slice);
201 text = TTF_RenderUTF8_Solid (font, names[cur], Yellow);
202 box.w = text->w + HBORDER;
203 box.h = text->h + VBORDER;
204 box.x = (WIDTH - text->w - HBORDER) / 2;
205 box.y = HEIGHT - (text->h + 32 + VBORDER/2);
206 SDL_FillRect (scale, &box, SDL_MapRGB (scale->format, 0, 0, 0));
211 SDL_BlitSurface (text, NULL, scale, &box);
212 SDL_FreeSurface (text);
214 SDL_UpdateRect (scale, 0, 0, 0, 0);
219 main (int argc, char **argv)
226 ReadPoints ("pro-gnu");
227 SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER);
229 font = TTF_OpenFont ("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf", 48);
230 screen = SDL_SetVideoMode (WIDTH, HEIGHT, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
231 image = IMG_Load ("/home/cascardo/fotos/debconf.jpg");
232 last = SDL_GetTicks ();
235 now = SDL_GetTicks ();
237 while (!stop && now > last + FRAME_INTERVAL)
239 last += FRAME_INTERVAL;
242 slice = GetNextImage (image);
243 ShowPoint (screen, slice);
244 SDL_FreeSurface (slice);
248 SDL_Delay (STOP_INTERVAL);
249 last = SDL_GetTicks ();
253 SDL_Delay (FRAME_INTERVAL - (now - last));
256 SDL_FreeSurface (image);
257 TTF_CloseFont (font);
261 for (i = 0; i < psize; i++)