Added Point struct and cleaned up line generation
[cascardo/movie.git] / movie.c
1 /*
2  *  Copyright (C) 2008  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <glib.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include "point.h"
25
26 #define SWAP(x, y) do { \
27         x ^= y; y ^= x; x ^= y; \
28         } while (0)
29
30 void
31 InsertLine (GArray *points, Point *src, Point *dst)
32 {
33   Point rect;
34   int inc, err, thre, swap;
35   int x1, y1, x2, y2;
36   int x, y;
37   rect.name = src->name;
38   err = 0;
39   swap = 0;
40   x1 = src->x;
41   y1 = src->y;
42   x2 = dst->x;
43   y2 = dst->y;
44   inc = y2 - y1;
45   thre = x2 - x1;
46   if (ABS (inc) > ABS (thre))
47   {
48     SWAP (inc, thre);
49     SWAP (x1, y1);
50     SWAP (x2, y2);
51     swap = 1;
52   }
53   for (y = y1, x = x1; (x2 < x1) ? (x >= x2) : (x <= x2); (x2 < x1) ? x-- : x++)
54   {
55     rect.x = (swap ? y : x);
56     rect.y = (swap ? x : y);
57     g_array_append_val (points, rect);
58     err += ABS (inc);
59     if (err >= ABS (thre))
60     {
61       err -= ABS (thre);
62       y += (inc < 0) ? -1 : 1;
63     }
64   }
65 }
66
67 GArray *
68 ReadPoints (char *filename)
69 {
70   GArray *points;
71   FILE *file;
72   char *buffer;
73   char *next;
74   size_t len;
75   ssize_t r;
76   int i = 0;
77   Point last;
78   Point rect;
79   file = fopen (filename, "r");
80   points = g_array_new (FALSE, TRUE, sizeof (SDL_Rect));
81   names = g_ptr_array_new ();
82   buffer = NULL;
83   len = 0;
84   rect.x = rect.y = 0;
85   rect.name = NULL;
86   last = rect;
87   while (!feof (file))
88   {
89     r = getline (&buffer, &len, file);
90     buffer[r - 1] = '\0';
91     rect.x = strtol (buffer, &next, 0);
92     rect.y = strtol (next+1, &next, 0);
93     strtol (next, &next, 0);
94     if (i > 0)
95       InsertLine (points, &last, &rect);
96     while (isspace (*next)) next++;
97     rect.name = g_strdup (next);
98     last = rect;
99     i++;
100   }
101   fclose (file);
102   return points;
103 }