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.
26 #define STOP_INTERVAL (2*1000)
28 #define FRAME_INTERVAL (1000/(FPS))
36 #include <SDL_image.h>
39 #define SWAP(x, y) do { \
40 x ^= y; y ^= x; x ^= y; \
43 #define ABS(x) ((x) < 0 ? -(x) : (x))
45 #define IS_CENTER(cx, cy, x, y) \
46 ((x + WIDTH/2 == cx) && (y + HEIGHT/2 == cy))
57 ReadPoints (char *filename)
65 file = fopen (filename, "r");
66 fscanf (file, "%d\n", &psize);
67 points = malloc (sizeof (SDL_Rect) * psize);
68 names = malloc (sizeof (char *) * psize);
69 if (points == NULL || names == NULL)
73 for (i = 0; i < psize; i++)
75 r = getline (&buffer, &len, file);
77 points[i].x = strtol (buffer, &next, 0);
78 points[i].y = strtol (next+1, &next, 0);
79 strtol (next, &next, 0);
80 while (isspace (*next)) next++;
81 names[i] = strdup (next);
89 static SDL_Rect rect = {0, 0, WIDTH, HEIGHT};
90 static int inc, err, thre, swap;
91 static int x1, y1, x2, y2;
94 next = (cur + 1) % psize;
95 if ((points[next].x == rect.x && points[next].y == rect.y) || cur == -1)
99 next = (cur + 1) % psize;
108 if (ABS (inc) > ABS (thre))
118 rect.x = (swap ? y : x);
119 rect.y = (swap ? x : y);
120 (x2 < x1) ? x-- : x++;
122 if (err >= ABS (thre))
125 y += (inc < 0) ? -1 : 1;
131 ShowPoint (SDL_Surface *screen, SDL_Surface *image)
133 SDL_BlitSurface (image, NULL, screen, NULL);
134 SDL_UpdateRect (screen, 0, 0, 0, 0);
138 CairoFromSDL (SDL_Surface *surface)
140 return cairo_image_surface_create_for_data (surface->pixels,
142 surface->w, surface->h,
147 CairoToSDL (cairo_surface_t *surface)
149 return SDL_CreateRGBSurfaceFrom (cairo_image_surface_get_data (surface),
150 cairo_image_surface_get_width (surface),
151 cairo_image_surface_get_height (surface),
153 cairo_image_surface_get_stride (surface),
154 0x00ff0000, 0x0000ff00, 0x000000ff,
159 CairoTarget (SDL_Surface *image)
161 static unsigned char *data = NULL;
162 static cairo_surface_t *surface = NULL;
163 static cairo_t *ctx = NULL;
164 cairo_surface_t *source;
165 static double rx = 1.0, ry = 1.0;
166 if (data == NULL && surface == NULL && ctx == NULL)
168 data = malloc (WIDTH * HEIGHT * 4);
169 surface = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_ARGB32,
170 WIDTH, HEIGHT, WIDTH * 4);
171 ctx = cairo_create (surface);
173 cairo_scale (ctx, 1.0/rx, 1.0/ry);
174 rx = (double) WIDTH / (double) image->w;
175 ry = (double) HEIGHT / (double) image->h;
176 cairo_scale (ctx, rx, ry);
177 source = CairoFromSDL (image);
178 cairo_set_source_surface (ctx, source, 0, 0);
180 cairo_surface_destroy (source);
181 return CairoToSDL (surface);
185 GetNextImage (SDL_Surface *image)
192 SDL_Color Yellow = { 255, 255, 0, 0};
193 static double rx = RX, ry = RY, factor = 0.5;
204 center = GetNextPoint ();
205 slice = SDL_CreateRGBSurface (SDL_SWSURFACE, WIDTH*rx, HEIGHT*ry,
207 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
208 center.x -= (WIDTH/2) * rx;
209 center.y -= (HEIGHT/2) * ry;
212 SDL_BlitSurface (image, ¢er, slice, NULL);
213 SDL_UpdateRect (slice, 0, 0, 0, 0);
214 scale = CairoTarget (slice);
215 SDL_FreeSurface (slice);
218 text = TTF_RenderUTF8_Solid (font, names[cur], Yellow);
219 box.w = text->w + HBORDER;
220 box.h = text->h + VBORDER;
221 box.x = (WIDTH - text->w - HBORDER) / 2;
222 box.y = HEIGHT - (text->h + 32 + VBORDER/2);
223 SDL_FillRect (scale, &box, SDL_MapRGB (scale->format, 0, 0, 0));
228 SDL_BlitSurface (text, NULL, scale, &box);
229 SDL_FreeSurface (text);
231 SDL_UpdateRect (scale, 0, 0, 0, 0);
236 main (int argc, char **argv)
244 ReadPoints ("pro-gnu");
245 SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER);
247 font = TTF_OpenFont ("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf", 48);
248 screen = SDL_SetVideoMode (WIDTH, HEIGHT, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
249 image = IMG_Load ("/home/cascardo/fotos/debconf.jpg");
250 last = SDL_GetTicks ();
253 if (SDL_PollEvent (&event))
255 if (event.type == SDL_KEYDOWN)
258 now = SDL_GetTicks ();
260 while (!stop && now > last + FRAME_INTERVAL)
262 last += FRAME_INTERVAL;
265 slice = GetNextImage (image);
266 ShowPoint (screen, slice);
267 SDL_FreeSurface (slice);
271 SDL_Delay (STOP_INTERVAL);
272 last = SDL_GetTicks ();
276 SDL_Delay (FRAME_INTERVAL - (now - last));
279 SDL_FreeSurface (image);
280 TTF_CloseFont (font);
284 for (i = 0; i < psize; i++)