766b1a7901a1d9a5a0813bc4d74d51bcdf2c4260
[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 test_square(int width, int height, int num)
58 {
59     int t;
60     gfxline_t* line = malloc(sizeof(gfxline_t)*num);
61     for(t=0;t<num;t++) {
62         line[t].type = t?gfx_lineTo:gfx_moveTo;
63         line[t].x = (lrand48()%width)-width/2;
64         line[t].y = (lrand48()%height)-height/2;
65         line[t].next = &line[t+1];
66     }
67     line[num-1].x = line[0].x;
68     line[num-1].y = line[0].y;
69     line[num-1].next = 0;
70     
71     gfxpoly_t*poly = gfxpoly_fillToPoly(line);
72     gfxpoly_t*poly2 = gfxpoly_process(poly);
73     gfxline_free(line);
74     gfxpoly_destroy(poly);
75     gfxpoly_destroy(poly2);
76 }
77
78 int test2()
79 {
80     test_square(200,5, 1000);
81     test_square(5,200, 1000);
82     test_square(10,10, 200);
83     test_square(500,500, 50);
84     test_square(65536,65536,256);
85 }
86
87 #include "../rfxswf.h"
88 void test3()
89 {
90 #undef N
91 #undef RANGE
92 #define N 20
93 #define RANGE 400
94
95     int i;
96     gfxline_t* line = malloc(sizeof(gfxline_t)*N*2);
97     for(i=0;i<N;i++) {
98         line[i].type = i?gfx_lineTo:gfx_moveTo;
99         line[i].x = lrand48()%RANGE - RANGE/2;
100         line[i].y = lrand48()%RANGE - RANGE/2;
101         line[i].next = &line[i+1];
102         line[i+N].x = lrand48()%RANGE - RANGE/2;
103         line[i+N].y = lrand48()%RANGE - RANGE/2;
104         line[i+N].next = &line[N+i+1];
105     }
106     line[N*2-1].x = line[0].x;
107     line[N*2-1].y = line[0].y;
108     line[N*2-1].next = 0;
109
110     gfxmatrix_t m;
111     memset(&m, 0, sizeof(m));
112
113     SWF swf;
114     memset(&swf, 0, sizeof(SWF));
115     swf.movieSize.xmax = RANGE*20*1.41;
116     swf.movieSize.ymax = RANGE*20*1.41;
117     swf.fileVersion = 9;
118     swf.frameRate = 25*0x100;
119     TAG * tag = swf.firstTag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
120     swf_SetU8(tag, 0);
121     swf_SetU8(tag, 0);
122     swf_SetU8(tag, 0);
123
124     int t;
125     for(t=0;t<360;t++) {
126         m.m00 = cos(t*M_PI/180.0);
127         m.m01 = sin(t*M_PI/180.0);
128         m.m10 = -sin(t*M_PI/180.0);
129         m.m11 = cos(t*M_PI/180.0);
130         m.tx = RANGE*1.41/2;
131         m.ty = RANGE*1.41/2;
132         gfxline_t*l = gfxline_clone(line);
133         gfxline_transform(l, &m);
134         
135         gfxpoly_t*poly = gfxpoly_fillToPoly(l);
136         gfxpoly_t*poly2 = gfxpoly_process(poly);
137
138         tag = swf_InsertTag(tag, ST_DEFINESHAPE);
139         SHAPE* s;
140         swf_ShapeNew(&s);
141         RGBA rgb;
142         rgb.r = rgb.g = 0x00; rgb.b = 0xff;
143         rgb.a = 255;
144         int fs = swf_ShapeAddSolidFillStyle(s,&rgb);
145         int ls = swf_ShapeAddLineStyle(s,20,&rgb);
146         swf_SetU16(tag,t+1);
147         swf_SetRect(tag,&swf.movieSize);
148         swf_SetShapeHeader(tag,s);
149
150 #ifdef FILL
151         swf_ShapeSetAll(tag,s,0,0,0,fs,0);
152         edge_t*e = poly2;
153         while(e) {
154             swf_ShapeSetMove(tag, s, e->a.x*20, e->a.y*20);
155             swf_ShapeSetLine(tag, s, e->b.x*20 - e->a.x*20, e->b.y*20 - e->a.y*20);
156             e = e->next;
157         }
158 #else
159         swf_ShapeSetAll(tag,s,0,0,ls,0,0);
160         edge_t*e = poly2;
161         while(e) {
162             swf_ShapeSetMove(tag, s, e->a.x*20, e->a.y*20);
163             swf_ShapeSetLine(tag, s, e->b.x*20 - e->a.x*20, e->b.y*20 - e->a.y*20);
164             
165             swf_ShapeSetCircle(tag, s, e->a.x*20, e->a.y*20, 5*20, 5*20);
166             swf_ShapeSetCircle(tag, s, e->b.x*20, e->b.y*20, 5*20, 5*20);
167             e = e->next;
168         }
169 #endif
170
171         swf_ShapeSetEnd(tag);
172         swf_ShapeFree(s);
173
174         gfxpoly_destroy(poly);
175         gfxpoly_destroy(poly2);
176
177         gfxline_free(l);
178    
179         if(t) {
180             tag = swf_InsertTag(tag,ST_REMOVEOBJECT2);
181             swf_SetU16(tag, t);
182         }
183         tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
184         swf_ObjectPlace(tag,t+1,t+1,NULL,NULL,NULL);
185
186         tag = swf_InsertTag(tag, ST_SHOWFRAME);
187     }
188     tag = swf_InsertTag(tag, ST_END);
189
190     swf_SaveSWF(&swf, "test.swf");
191 }
192
193 int main()
194 {
195     test2();
196 }