Initial revision
[swftools.git] / lib / example / box.c
1
2 // linux/gcc: cc box.c ../rfxswf.c -funsigned-char -o box -lm; cp box /home/www/cgi-bin/box
3
4 #include <stdio.h>
5 #include <fcntl.h>
6 #include <math.h>
7 #include "../rfxswf.h"
8
9 // Box
10
11 #define BANNER_TEXT     "reflex"
12 #define ID_FONT         2000
13 #define ID_BANNER       2001
14 #define ID_HIGHLIGHT    2002
15 #define ID_BUTTON       2003
16
17 #define a 200
18 int sX[] = { a,-a, a,-a, a,-a, a,-a};
19 int sY[] = { a, a,-a,-a, a, a,-a,-a};
20 int sZ[] = { a, a, a, a,-a,-a,-a,-a};
21 #undef a
22   
23 int dX[8];
24 int dY[8];
25
26 int sin_[512],cos_[512];
27
28 void calcTables()
29 { int i;
30   double d;
31   for (i=0;i<512;i++)
32   { d = ((double)i)/128*3.14159;
33     sin_[i] = (int)(sin(d)*256);
34     cos_[i] = (int)(cos(d)*256);
35   }
36 }
37
38 void ShapeSquare(LPTAG t,LPSHAPE s,int p1,int p2,int p3,int p4,int dx,int dy)
39
40   // Hidden-Line-Check
41     if (((dX[p2]-dX[p1])*(dY[p3]-dY[p1])-(dX[p3]-dX[p1])*(dY[p2]-dY[p1]))<0) return;
42
43   ShapeSetMove(t,s,dX[p1]+dx,dY[p1]+dy);
44   ShapeSetLine(t,s,dX[p2]-dX[p1],dY[p2]-dY[p1]);
45   ShapeSetLine(t,s,dX[p3]-dX[p2],dY[p3]-dY[p2]);
46   ShapeSetLine(t,s,dX[p4]-dX[p3],dY[p4]-dY[p3]);
47   ShapeSetLine(t,s,dX[p1]-dX[p4],dY[p1]-dY[p4]);
48 }
49
50
51 void mapBox(int xw,int yw,int zw)
52 { int i;
53   int x1,y1,z1,x2,y2,z2,x3,y3,z3;
54   int y,x,z;
55   
56   xw &= 255;
57   yw &= 255;
58   zw &= 255;
59
60   for (i=0;i<8;i++)
61   { x = sX[i];
62     y = sY[i];
63     z = sZ[i];
64                   
65     y1 = ( y*cos_[xw]- z*sin_[xw])>>8;
66     z1 = ( y*sin_[xw]+ z*cos_[xw])>>8;
67     x1 = x;
68
69     x2 = (x1*cos_[yw]+z1*sin_[yw])>>8;
70     y2 = y1;
71
72     x3 = (x2*cos_[zw]-y2*sin_[zw])>>8;
73     y3 = (x2*sin_[zw]+y2*cos_[zw])>>8;
74     z3 = (z1*cos_[yw]-x1*sin_[yw])>>8;
75
76     dX[i] = x3*4000/(z3+950);
77     dY[i] = y3*4000/(z3+950);
78         
79   }
80 }
81                
82 int main (int argc,char ** argv)
83 { SWF swf;
84   LPTAG t;
85   RGBA rgb;
86   SRECT r;
87   LPSHAPE s;
88   S32 width = 800,height = 800;
89   U8 gbits,abits;
90   LPSWFFONT font;
91   FONTUSAGE use;
92   CXFORM cx1,cx2;
93   MATRIX m;
94   
95   int f,i,j,frame;
96   
97 /*  f = open("Arial.efont",O_RDONLY);
98   if (f>=0)
99   { if (FAILED(FontImport(f,&font)))
100     { fprintf(stderr,"Font import failed\n");
101       close(f);
102       return -1;
103     }
104   }
105   else
106   { fprintf(stderr,"Font not found\n");
107     return -1;
108   }
109   close(f);
110
111   FontSetID(font,ID_FONT);
112   FontInitUsage(&use);
113   FontUse(&use,BANNER_TEXT);
114   FontReduce(font,&use);*/
115   
116   calcTables();
117   
118   memset(&swf,0x00,sizeof(SWF));
119
120   swf.FileVersion    = 4;
121   swf.FrameRate      = 0x4000;
122   swf.MovieSize.xmax = 4*width;
123   swf.MovieSize.ymax = 4*height;
124
125   swf.FirstTag = InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
126   t = swf.FirstTag;
127
128         rgb.r = 0xff;
129         rgb.g = 0xff;
130         rgb.b = 0xff;
131         SetRGB(t,&rgb);
132
133   t = InsertTag(t,ST_DEFINEFONT);
134
135         FontSetDefine(t,font);
136
137   t = InsertTag(t,ST_DEFINEFONTINFO);
138
139         FontSetInfo(t,font);
140
141   t = InsertTag(t,ST_DEFINETEXT);
142
143         SetU16(t,ID_BANNER);            // ID
144         
145         r.xmin = 0;
146         r.ymin = 0;
147         r.xmax = 400;
148         r.ymax = 400;
149         SetRect(t,&r);
150
151         SetMatrix(t,NULL);
152
153         TextCountBits(font,BANNER_TEXT,80,&gbits,&abits);
154         
155         SetU8(t,gbits);
156         SetU8(t,abits);
157
158         rgb.r = 0xc0;
159         rgb.g = 0xc0;
160         rgb.b = 0xc0;
161
162         TextSetInfoRecord(t,font,height/4,&rgb,0,200);
163         TextSetCharRecord(t,font,BANNER_TEXT,80,gbits,abits);
164
165         SetU8(t,0);
166         
167   t = InsertTag(t,ST_DEFINETEXT);
168
169         SetU16(t,ID_HIGHLIGHT);            // ID
170         
171         r.xmin = 0;
172         r.ymin = 0;
173         r.xmax = 800;
174         r.ymax = 400;
175         SetRect(t,&r);
176
177         SetMatrix(t,NULL);
178
179         TextCountBits(font,BANNER_TEXT,80,&gbits,&abits);
180         
181         SetU8(t,gbits);
182         SetU8(t,abits);
183
184         rgb.r = 0x20;
185         rgb.g = 0x20;
186         rgb.b = 0x20;
187
188         TextSetInfoRecord(t,font,height/4,&rgb,0,200);
189         TextSetCharRecord(t,font,BANNER_TEXT,80,gbits,abits);
190
191         SetU8(t,0);
192         
193     t = InsertTag(t,ST_DEFINEBUTTON);
194     
195         GetMatrix(NULL,&m);
196         
197         m.tx = 3*width;
198         m.ty = 7*height/2;
199         
200         SetU16(t,ID_BUTTON); // ID
201         ButtonSetRecord(t,BS_UP,ID_BANNER,1,&m,NULL);
202         ButtonSetRecord(t,BS_DOWN|BS_HIT|BS_OVER,ID_HIGHLIGHT,1,&m,NULL);
203         SetU8(t,0); // End of Button Records
204         SetU8(t,0); // End of Action Records
205     
206       
207     t = InsertTag(t,ST_PLACEOBJECT2);
208
209          ObjectPlace(t,ID_BUTTON,1,NULL,NULL,NULL);
210          
211     GetCXForm(NULL,&cx1,1);
212     GetCXForm(NULL,&cx2,1);
213     
214 //    cx1.a1 = -(0x40*1);
215 //    cx2.a1 = -(0x40*2);
216     
217     cx1.r1 = cx1.g1 = 0x80;
218     cx2.r1 = cx2.g1 = 0xc0;     
219     
220
221         
222   for (frame=0;frame<256;frame+=2)
223   { int id = frame +1;
224     
225     t = InsertTag(t,ST_DEFINESHAPE);
226
227         NewShape(&s);
228         rgb.r = rgb.g = 0x00; rgb.b = 0xff;
229         j = ShapeAddLineStyle(s,40,&rgb);
230
231         SetU16(t,id);  // ID
232
233         r.xmin = 0;
234         r.ymin = 0;
235         r.xmax = 4*width;
236         r.ymax = 4*height;
237         
238         SetRect(t,&r);
239
240         SetShapeStyles(t,s);
241         ShapeCountBits(s,NULL,NULL);
242         SetShapeBits(t,s);
243
244         ShapeSetAll(t,s,0,0,j,0,0);
245         
246         mapBox(frame,frame,frame>>1);
247
248         ShapeSquare(t,s,0,2,3,1,2*width,2*height);
249         ShapeSquare(t,s,4,5,7,6,2*width,2*height);
250         ShapeSquare(t,s,0,4,6,2,2*width,2*height);
251         ShapeSquare(t,s,1,3,7,5,2*width,2*height);
252         ShapeSquare(t,s,0,1,5,4,2*width,2*height);
253         ShapeSquare(t,s,2,6,7,3,2*width,2*height);
254         
255         ShapeSetEnd(t);
256   }
257   
258   for (frame=0;frame<256;frame+=2)
259   { int id = frame +1;
260     int id2 = ((frame-2)&255)+1;
261     int id3 = ((frame-4)&255)+1;
262
263     if (frame)
264     { t = InsertTag(t,ST_REMOVEOBJECT2); SetU16(t,2); // depth
265       t = InsertTag(t,ST_REMOVEOBJECT2); SetU16(t,3); // depth
266       t = InsertTag(t,ST_REMOVEOBJECT2); SetU16(t,4); // depth
267     }
268
269     t = InsertTag(t,ST_PLACEOBJECT2);
270
271         ObjectPlace(t,id,4,NULL,NULL,NULL);
272         
273     t = InsertTag(t,ST_PLACEOBJECT2);
274
275         ObjectPlace(t,id2,3,NULL,&cx1,NULL);
276   
277     t = InsertTag(t,ST_PLACEOBJECT2);
278
279         ObjectPlace(t,id3,2,NULL,&cx2,NULL);
280  
281     
282     t = InsertTag(t,ST_SHOWFRAME);
283   }
284   
285   
286
287   t = InsertTag(t,ST_END);
288  
289 //  WriteCGI(&swf);
290   
291
292   f = open("shape1.swf",O_RDWR|O_CREAT|O_TRUNC);
293   if FAILED(WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
294   close(f);
295
296   FreeTags(&swf);
297
298 #ifdef __NT__
299   system("start ..\\shape1.swf");
300 #endif
301   
302   return 0;
303 }
304
305