* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#define WIDTH 800
-#define HEIGHT 600
+#define _GNU_SOURCE
+#include <glib.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
-#include <SDL.h>
-#include <SDL_image.h>
+#include "point.h"
-#define IS_CENTER(cx, cy, x, y) \
- ((x + WIDTH/2 == cx) && (y + HEIGHT/2 == cy))
+#define SWAP(x, y) do { \
+ x ^= y; y ^= x; x ^= y; \
+ } while (0)
-SDL_Rect points[] = {
- {400, 300, 800, 600},
- {450, 350, 800, 600}
-};
-
-SDL_Rect *
-GetNextPoint (void)
+void
+InsertLine (GArray *points, Point *src, Point *dst)
{
- static SDL_Rect rect = {0, 0, WIDTH, HEIGHT};
- static int cur = 0;
- int dx, dy;
- int next;
- next = (cur + 1) % (sizeof (points) / sizeof (SDL_Rect));
- if (IS_CENTER (points[next].x, points[next].y, rect.x, rect.y))
+ Point rect;
+ int inc, err, thre, swap;
+ int x1, y1, x2, y2;
+ int x, y;
+ rect.name = src->name;
+ err = 0;
+ swap = 0;
+ x1 = src->x;
+ y1 = src->y;
+ x2 = dst->x;
+ y2 = dst->y;
+ inc = y2 - y1;
+ thre = x2 - x1;
+ if (ABS (inc) > ABS (thre))
{
- cur = next;
- next = (cur + 1) % (sizeof (points) / sizeof (SDL_Rect));
+ SWAP (inc, thre);
+ SWAP (x1, y1);
+ SWAP (x2, y2);
+ swap = 1;
+ }
+ for (y = y1, x = x1; (x2 < x1) ? (x >= x2) : (x <= x2); (x2 < x1) ? x-- : x++)
+ {
+ rect.x = (swap ? y : x);
+ rect.y = (swap ? x : y);
+ g_array_append_val (points, rect);
+ err += ABS (inc);
+ if (err >= ABS (thre))
+ {
+ err -= ABS (thre);
+ y += (inc < 0) ? -1 : 1;
+ }
}
- dx = (points[next].x > points[cur].x) ? 1 : -1;
- dy = (points[next].y > points[cur].y) ? 1 : -1;
- rect.x = (rect.x + dx) % WIDTH;
- rect.y = (rect.y + dy) % HEIGHT;
- return ▭
-}
-
-void
-ShowPoint (SDL_Surface *screen, SDL_Surface *image, SDL_Rect *rect)
-{
- SDL_BlitSurface (image, rect, screen, NULL);
- SDL_UpdateRect (screen, 0, 0, 0, 0);
-}
-
-Uint32
-ShowNext (Uint32 interval, void *data)
-{
- SDL_UserEvent event;
- event.type = SDL_USEREVENT;
- event.code = 0;
- SDL_PushEvent ((SDL_Event *) &event);
- return 33;
}
-int
-main (int argc, char **argv)
+GArray *
+ReadPoints (char *filename)
{
- SDL_Surface *screen;
- SDL_Surface *image;
- SDL_Event event;
- SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER);
- screen = SDL_SetVideoMode (800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
- image = IMG_Load ("/home/cascardo/fotos/debconf.jpg");
- SDL_AddTimer (0, ShowNext, NULL);
- while (SDL_WaitEvent (&event))
+ GArray *points;
+ FILE *file;
+ char *buffer;
+ char *next;
+ size_t len;
+ ssize_t r;
+ int i = 0;
+ Point last;
+ Point rect;
+ file = fopen (filename, "r");
+ points = g_array_new (FALSE, TRUE, sizeof (Point));
+ buffer = NULL;
+ len = 0;
+ rect.x = rect.y = 0;
+ rect.name = NULL;
+ last = rect;
+ while (!feof (file))
{
- if (event.type == SDL_KEYDOWN)
- break;
- else if (event.type == SDL_USEREVENT)
- ShowPoint (screen, image, GetNextPoint ());
+ r = getline (&buffer, &len, file);
+ buffer[r - 1] = '\0';
+ rect.x = strtol (buffer, &next, 0);
+ rect.y = strtol (next+1, &next, 0);
+ strtol (next, &next, 0);
+ if (i > 0)
+ InsertLine (points, &last, &rect);
+ while (isspace (*next)) next++;
+ rect.name = g_strdup (next);
+ last = rect;
+ i++;
}
- SDL_FreeSurface (image);
- SDL_Quit ();
- return 0;
+ fclose (file);
+ return points;
}