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)
31 #include <SDL_image.h>
34 #define SWAP(x, y) do { \
35 x ^= y; y ^= x; x ^= y; \
38 #define ABS(x) ((x) < 0 ? -(x) : (x))
40 #define IS_CENTER(cx, cy, x, y) \
41 ((x + WIDTH/2 == cx) && (y + HEIGHT/2 == cy))
50 ReadPoints (char *filename)
57 file = fopen (filename, "r");
58 fscanf (file, "%d\n", &psize);
59 points = malloc (sizeof (SDL_Rect) * psize);
64 for (i = 0; i < psize; i++)
66 getline (&buffer, &len, file);
67 points[i].x = strtol (buffer, &next, 0);
68 points[i].y = strtol (next+1, NULL, 0);
76 static SDL_Rect rect = {0, 0, WIDTH, HEIGHT};
78 static int inc, err, thre, swap;
79 static int x1, y1, x2, y2;
82 next = (cur + 1) % psize;
83 if ((points[next].x == rect.x && points[next].y == rect.y) || cur == -1)
87 next = (cur + 1) % psize;
96 if (ABS (inc) > ABS (thre))
106 rect.x = (swap ? y : x);
107 rect.y = (swap ? x : y);
108 (x2 < x1) ? x-- : x++;
110 if (err >= ABS (thre))
113 y += (inc < 0) ? -1 : 1;
119 ShowPoint (SDL_Surface *screen, SDL_Surface *image)
121 SDL_BlitSurface (image, NULL, screen, NULL);
122 SDL_UpdateRect (screen, 0, 0, 0, 0);
126 CairoFromSDL (SDL_Surface *surface)
128 return cairo_image_surface_create_for_data (surface->pixels,
130 surface->w, surface->h,
135 CairoToSDL (cairo_surface_t *surface)
137 return SDL_CreateRGBSurfaceFrom (cairo_image_surface_get_data (surface),
138 cairo_image_surface_get_width (surface),
139 cairo_image_surface_get_height (surface),
141 cairo_image_surface_get_stride (surface),
142 0x00ff0000, 0x0000ff00, 0x000000ff,
147 CairoTarget (SDL_Surface *image)
149 static unsigned char *data = NULL;
150 static cairo_surface_t *surface = NULL;
151 static cairo_t *ctx = NULL;
152 cairo_surface_t *source;
153 if (data == NULL && surface == NULL && ctx == NULL)
155 data = malloc (WIDTH * HEIGHT * 4);
156 surface = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_ARGB32,
157 WIDTH, HEIGHT, WIDTH * 4);
158 ctx = cairo_create (surface);
159 cairo_scale (ctx, 2, 2);
161 source = CairoFromSDL (image);
162 cairo_set_source_surface (ctx, source, 0, 0);
164 cairo_surface_destroy (source);
165 return CairoToSDL (surface);
169 GetNextImage (SDL_Surface *image)
176 SDL_Color Yellow = { 255, 255, 0, 0};
177 center = GetNextPoint (), GetNextPoint (), GetNextPoint ();
178 slice = SDL_CreateRGBSurface (SDL_SWSURFACE, WIDTH/2, HEIGHT/2,
180 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
181 center.x -= (WIDTH/2) / 2;
182 center.y -= (HEIGHT/2) / 2;
185 SDL_BlitSurface (image, ¢er, slice, NULL);
186 SDL_UpdateRect (slice, 0, 0, 0, 0);
187 scale = CairoTarget (slice);
188 SDL_FreeSurface (slice);
189 text = TTF_RenderUTF8_Solid (font, "GNU", Yellow);
190 box.w = text->w + HBORDER;
191 box.h = text->h + VBORDER;
192 box.x = (WIDTH - text->w - HBORDER) / 2;
193 box.y = HEIGHT - (text->h + 32 + VBORDER/2);
194 SDL_FillRect (scale, &box, SDL_MapRGB (scale->format, 0, 0, 0));
199 SDL_BlitSurface (text, NULL, scale, &box);
200 SDL_UpdateRect (scale, 0, 0, 0, 0);
201 SDL_FreeSurface (text);
206 ShowNext (Uint32 interval, void *data)
209 event.type = SDL_USEREVENT;
211 SDL_PushEvent ((SDL_Event *) &event);
215 return STOP_INTERVAL;
221 main (int argc, char **argv)
227 ReadPoints ("pro-gnu");
228 SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER);
230 font = TTF_OpenFont ("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf", 48);
231 screen = SDL_SetVideoMode (WIDTH, HEIGHT, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
232 image = IMG_Load ("/home/cascardo/fotos/debconf.jpg");
233 SDL_AddTimer (0, ShowNext, NULL);
234 while (SDL_WaitEvent (&event))
236 if (event.type == SDL_KEYDOWN)
238 else if (event.type == SDL_USEREVENT)
240 slice = GetNextImage (image);
241 ShowPoint (screen, slice);
242 SDL_FreeSurface (slice);
245 SDL_FreeSurface (image);
246 TTF_CloseFont (font);