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 (5*1000)
33 #include <SDL_image.h>
36 #define SWAP(x, y) do { \
37 x ^= y; y ^= x; x ^= y; \
40 #define ABS(x) ((x) < 0 ? -(x) : (x))
42 #define IS_CENTER(cx, cy, x, y) \
43 ((x + WIDTH/2 == cx) && (y + HEIGHT/2 == cy))
53 ReadPoints (char *filename)
61 file = fopen (filename, "r");
62 fscanf (file, "%d\n", &psize);
63 points = malloc (sizeof (SDL_Rect) * psize);
64 names = malloc (sizeof (char *) * psize);
65 if (points == NULL || names == NULL)
69 for (i = 0; i < psize; i++)
71 r = getline (&buffer, &len, file);
73 points[i].x = strtol (buffer, &next, 0);
74 points[i].y = strtol (next+1, &next, 0);
75 strtol (next, &next, 0);
76 while (isspace (*next)) next++;
77 names[i] = strdup (next);
85 static SDL_Rect rect = {0, 0, WIDTH, HEIGHT};
87 static int inc, err, thre, swap;
88 static int x1, y1, x2, y2;
91 next = (cur + 1) % psize;
92 if ((points[next].x == rect.x && points[next].y == rect.y) || cur == -1)
96 next = (cur + 1) % psize;
105 if (ABS (inc) > ABS (thre))
115 rect.x = (swap ? y : x);
116 rect.y = (swap ? x : y);
117 (x2 < x1) ? x-- : x++;
119 if (err >= ABS (thre))
122 y += (inc < 0) ? -1 : 1;
128 ShowPoint (SDL_Surface *screen, SDL_Surface *image)
130 SDL_BlitSurface (image, NULL, screen, NULL);
131 SDL_UpdateRect (screen, 0, 0, 0, 0);
135 CairoFromSDL (SDL_Surface *surface)
137 return cairo_image_surface_create_for_data (surface->pixels,
139 surface->w, surface->h,
144 CairoToSDL (cairo_surface_t *surface)
146 return SDL_CreateRGBSurfaceFrom (cairo_image_surface_get_data (surface),
147 cairo_image_surface_get_width (surface),
148 cairo_image_surface_get_height (surface),
150 cairo_image_surface_get_stride (surface),
151 0x00ff0000, 0x0000ff00, 0x000000ff,
156 CairoTarget (SDL_Surface *image)
158 static unsigned char *data = NULL;
159 static cairo_surface_t *surface = NULL;
160 static cairo_t *ctx = NULL;
161 cairo_surface_t *source;
162 if (data == NULL && surface == NULL && ctx == NULL)
164 data = malloc (WIDTH * HEIGHT * 4);
165 surface = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_ARGB32,
166 WIDTH, HEIGHT, WIDTH * 4);
167 ctx = cairo_create (surface);
168 cairo_scale (ctx, 2, 2);
170 source = CairoFromSDL (image);
171 cairo_set_source_surface (ctx, source, 0, 0);
173 cairo_surface_destroy (source);
174 return CairoToSDL (surface);
178 GetNextImage (SDL_Surface *image)
185 SDL_Color Yellow = { 255, 255, 0, 0};
186 center = GetNextPoint (), GetNextPoint (), GetNextPoint ();
187 slice = SDL_CreateRGBSurface (SDL_SWSURFACE, WIDTH/2, HEIGHT/2,
189 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
190 center.x -= (WIDTH/2) / 2;
191 center.y -= (HEIGHT/2) / 2;
194 SDL_BlitSurface (image, ¢er, slice, NULL);
195 SDL_UpdateRect (slice, 0, 0, 0, 0);
196 scale = CairoTarget (slice);
197 SDL_FreeSurface (slice);
198 text = TTF_RenderUTF8_Solid (font, "GNU", Yellow);
199 box.w = text->w + HBORDER;
200 box.h = text->h + VBORDER;
201 box.x = (WIDTH - text->w - HBORDER) / 2;
202 box.y = HEIGHT - (text->h + 32 + VBORDER/2);
203 SDL_FillRect (scale, &box, SDL_MapRGB (scale->format, 0, 0, 0));
208 SDL_BlitSurface (text, NULL, scale, &box);
209 SDL_UpdateRect (scale, 0, 0, 0, 0);
210 SDL_FreeSurface (text);
215 ShowNext (Uint32 interval, void *data)
218 event.type = SDL_USEREVENT;
220 SDL_PushEvent ((SDL_Event *) &event);
224 return STOP_INTERVAL;
230 main (int argc, char **argv)
237 ReadPoints ("pro-gnu");
238 SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER);
240 font = TTF_OpenFont ("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf", 48);
241 screen = SDL_SetVideoMode (WIDTH, HEIGHT, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
242 image = IMG_Load ("/home/cascardo/fotos/debconf.jpg");
243 SDL_AddTimer (0, ShowNext, NULL);
244 while (SDL_WaitEvent (&event))
246 if (event.type == SDL_KEYDOWN)
248 else if (event.type == SDL_USEREVENT)
250 slice = GetNextImage (image);
251 ShowPoint (screen, slice);
252 SDL_FreeSurface (slice);
255 SDL_FreeSurface (image);
256 TTF_CloseFont (font);
260 for (i = 0; i < psize; i++)