e8c140c48d339312c7b6361997085bc66356e7fd
[swftools.git] / lib / gfxpoly / test.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <memory.h>
4 #include <math.h>
5 #include "../gfxtools.h"
6 #include "poly.h"
7 #include "convert.h"
8
9 gfxline_t*mkstar(int x1, int y1, int x2, int y2)
10 {
11     gfxline_t*l=0,*line = 0;
12     int x;
13     for(x=x1;x<=x2;x+=50) {
14         l = rfx_calloc(sizeof(gfxline_t));
15         l->type = gfx_moveTo;
16         l->x = x;l->y = y1;
17         line = gfxline_append(line, l);
18
19         l = rfx_calloc(sizeof(gfxline_t));
20         l->type = gfx_lineTo;
21         l->x = x2-x;l->y = y2;
22         line = gfxline_append(line, l);
23     }
24     return line;
25 }
26
27 int test1()
28 {
29     gfxline_t*box1 = gfxline_makerectangle(50,50,150,150);
30     // put box2 and box3 on top of each other *snicker*
31     gfxline_t*box2 = gfxline_makerectangle(100,100,200,200);
32     gfxline_t*box3 = gfxline_makerectangle(100,100,200,200);
33     gfxline_t*star = mkstar(50,50, 150,150);
34     gfxline_t*b = 0;
35     b = gfxline_append(b, box1);
36     b = gfxline_append(b, box2);
37     b = gfxline_append(b, box3);
38     b = gfxline_append(b, star);
39
40     gfxmatrix_t matrix;
41     memset(&matrix, 0, sizeof(gfxmatrix_t));
42     double ua=0.1;
43     matrix.m00=cos(ua);matrix.m10=sin(ua);
44     matrix.m01=-sin(ua);matrix.m11=cos(ua);
45
46     gfxline_transform(b, &matrix);
47     gfxpoly_t*poly = gfxpoly_fillToPoly(b);
48     gfxline_free(box1);
49     gfxline_free(box2);
50     gfxline_free(box3);
51     gfxline_free(star);
52
53     gfxpoly_dump(poly);
54     gfxpoly_process(poly);
55 }
56
57 int test2()
58 {
59 #define N 50
60 #define RANGE 150
61     int t;
62     gfxline_t* line = malloc(sizeof(gfxline_t)*N);
63     for(t=0;t<N;t++) {
64         line[t].type = t?gfx_lineTo:gfx_moveTo;
65         line[t].x = (lrand48()%RANGE)-RANGE/2;
66         line[t].y = (lrand48()%RANGE)-RANGE/2;
67         line[t].next = &line[t+1];
68     }
69     line[N-1].x = line[0].x;
70     line[N-1].y = line[0].y;
71     line[N-1].next = 0;
72     
73     gfxpoly_t*poly = gfxpoly_fillToPoly(line);
74     gfxpoly_t*poly2 = gfxpoly_process(poly);
75     gfxline_free(line);
76     gfxpoly_destroy(poly);
77     gfxpoly_destroy(poly2);
78 }
79
80 #include "../rfxswf.h"
81 void test3()
82 {
83 #undef N
84 #undef RANGE
85 #define N 250
86 #define RANGE 150
87
88     int i;
89     gfxline_t* line = malloc(sizeof(gfxline_t)*N);
90     for(i=0;i<N;i++) {
91         line[i].type = i?gfx_lineTo:gfx_moveTo;
92         line[i].x = lrand48()%RANGE - RANGE/2;
93         line[i].y = lrand48()%RANGE - RANGE/2;
94         line[i].next = &line[i+1];
95     }
96     line[N-1].x = line[0].x;
97     line[N-1].y = line[0].y;
98     line[N-1].next = 0;
99
100     gfxmatrix_t m;
101     memset(&m, 0, sizeof(m));
102
103     SWF swf;
104     memset(&swf, 0, sizeof(SWF));
105     swf.movieSize.xmax = RANGE*20*1.41;
106     swf.movieSize.ymax = RANGE*20*1.41;
107     swf.fileVersion = 9;
108     swf.frameRate = 25*0x100;
109     TAG * tag = swf.firstTag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
110     swf_SetU8(tag, 0);
111     swf_SetU8(tag, 0);
112     swf_SetU8(tag, 0);
113
114     int t;
115     for(t=0;t<360;t++) {
116         m.m00 = cos(t*M_PI/180.0);
117         m.m01 = sin(t*M_PI/180.0);
118         m.m10 = -sin(t*M_PI/180.0);
119         m.m11 = cos(t*M_PI/180.0);
120         m.tx = RANGE*1.41/2;
121         m.ty = RANGE*1.41/2;
122         gfxline_t*l = gfxline_clone(line);
123         gfxline_transform(l, &m);
124         
125         gfxpoly_t*poly = gfxpoly_fillToPoly(l);
126         gfxpoly_t*poly2 = gfxpoly_process(poly);
127
128         tag = swf_InsertTag(tag, ST_DEFINESHAPE);
129         SHAPE* s;
130         swf_ShapeNew(&s);
131         RGBA rgb;
132         rgb.r = rgb.g = 0x00; rgb.b = 0xff;
133         rgb.a = 255;
134         int j = swf_ShapeAddSolidFillStyle(s,&rgb);
135         swf_SetU16(tag,t+1);
136         swf_SetRect(tag,&swf.movieSize);
137         swf_SetShapeHeader(tag,s);
138         swf_ShapeSetAll(tag,s,0,0,0,j,0);
139         edge_t*e = poly2;
140         while(e) {
141             swf_ShapeSetMove(tag, s, e->a.x*20, e->a.y*20);
142             swf_ShapeSetLine(tag, s, e->b.x*20 - e->a.x*20, e->b.y*20 - e->a.y*20);
143             e = e->next;
144         }
145         swf_ShapeSetEnd(tag);
146         swf_ShapeFree(s);
147
148         gfxpoly_destroy(poly);
149         gfxpoly_destroy(poly2);
150
151         gfxline_free(l);
152    
153         if(t) {
154             tag = swf_InsertTag(tag,ST_REMOVEOBJECT2);
155             swf_SetU16(tag, t);
156         }
157         tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
158         swf_ObjectPlace(tag,t+1,t+1,NULL,NULL,NULL);
159
160         tag = swf_InsertTag(tag, ST_SHOWFRAME);
161     }
162     tag = swf_InsertTag(tag, ST_END);
163
164     swf_SaveSWF(&swf, "test.swf");
165 }
166
167 int main()
168 {
169     test3();
170 }