Initial revision
[swftools.git] / lib / example / texbox.c
1
2 // linux/gcc: cc texbox.c ../rfxswf.c -funsigned-char -o texbox -lm -ljpeg; cp texbox /home/www/cgi-bin/texbox
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 QUALITY 80
13 #define ID_FONT         2000
14 #define ID_BANNER       2001
15 #define ID_HIGHLIGHT    2002
16 #define ID_BUTTON       2003
17 #define ID_BITMAP       2004
18 #define ID_SHAPE        2005
19 #define ID_SHAPE2       2006
20
21 #define a 200
22 int sX[] = { a,-a, a,-a, a,-a, a,-a};
23 int sY[] = { a, a,-a,-a, a, a,-a,-a};
24 int sZ[] = { a, a, a, a,-a,-a,-a,-a};
25 #undef a
26
27 #define PRECISION 16
28
29 int SHADE = 1;
30
31 int dX[8];
32 int dY[8];
33 int square_visible[6]  = {0,0,0,0,0,0};
34 int square_depth[6][2] = {{2,3},{4,5},{6,7},{8,9},{10,11},{12,13}};
35 int square_light[6]    = {0,0,64,64,-64,-64};
36
37 int sin_[512],cos_[512];
38
39 void calcTables()       
40 { int i;
41   double d;
42   for (i=0;i<512;i++)
43   { d = ((double)i)/128*3.14159;
44     sin_[i] = (int)(sin(d)*(1<<PRECISION));
45     cos_[i] = (int)(cos(d)*(1<<PRECISION));
46   }
47 }
48
49 LPTAG MapSquare(LPTAG t,int dx0,int dy0,int p1,int p2,int p3,int p4,int dx,int dy,int square)
50 { MATRIX m;
51   CXFORM cx;
52
53   if (SHADE)
54   { GetCXForm(NULL,&cx,1);
55     cx.r1 = cx.b1 = cx.g1 = square_light[square];
56   }
57         
58   if (((dX[p2]-dX[p1])*(dY[p3]-dY[p1])-(dX[p3]-dX[p1])*(dY[p2]-dY[p1]))<0) 
59   { if (square_visible[square])
60     { t = InsertTag(t,ST_REMOVEOBJECT2); SetU16(t,square_depth[square][0]);
61       t = InsertTag(t,ST_REMOVEOBJECT2); SetU16(t,square_depth[square][1]);
62     }
63     square_visible[square] = 0;
64     return t; 
65   }
66
67   t = InsertTag(t,ST_PLACEOBJECT2);
68   
69   MatrixMapTriangle(&m,dx0,dy0,dX[p1]+dx,dY[p1]+dy,
70                         dX[p2]+dx,dY[p2]+dy,
71                  dX[p4]+dx,dY[p4]+dy);
72
73   if (!square_visible[square])
74     ObjectPlace(t,ID_SHAPE,square_depth[square][0],&m,SHADE?&cx:NULL,NULL);
75   else
76     ObjectMove(t,square_depth[square][0],&m,SHADE?&cx:NULL);
77
78   t = InsertTag(t,ST_PLACEOBJECT2);
79   
80   MatrixMapTriangle(&m,dx0,dy0,dX[p3]+dx,dY[p3]+dy,
81                         dX[p4]+dx,dY[p4]+dy,
82                  dX[p2]+dx,dY[p2]+dy);
83
84   if (!square_visible[square])
85     ObjectPlace(t,ID_SHAPE2,square_depth[square][1],&m,SHADE?&cx:NULL,NULL);
86   else
87     ObjectMove(t,square_depth[square][1],&m,SHADE?&cx:NULL);
88
89   square_visible[square] = 1;
90  
91   return t;
92 }
93
94 void mapBox(int xw,int yw,int zw)
95 { int i;
96   int x1,y1,z1,x2,y2,z2,x3,y3,z3;
97   int y,x,z;
98   
99   xw &= 255;
100   yw &= 255;
101   zw &= 255;
102
103   for (i=0;i<8;i++)
104   { x = sX[i];
105     y = sY[i];
106     z = sZ[i];
107                   
108     y1 = ( y*cos_[xw]- z*sin_[xw])>>PRECISION;
109     z1 = ( y*sin_[xw]+ z*cos_[xw])>>PRECISION;
110     x1 = x;
111
112     x2 = (x1*cos_[yw]+z1*sin_[yw])>>PRECISION;
113     y2 = y1;
114
115     x3 = (x2*cos_[zw]-y2*sin_[zw])>>PRECISION;
116     y3 = (x2*sin_[zw]+y2*cos_[zw])>>PRECISION;
117     z3 = (z1*cos_[yw]-x1*sin_[yw])>>PRECISION;
118
119     dX[i] = x3*4000/(z3+950);
120     dY[i] = y3*4000/(z3+950);
121         
122   }
123 }
124
125 void mapLights(int xw,int yw,int zw)
126 { int i;
127   int x1,y1,z1,x2,y2,z2,x3,y3,z3;
128
129   int y[] = {0,0,0,0,256,-256};
130   int x[] = {0,0,256,-256,0,0};
131   int z[] = {256,-256,0,0,0,0};
132
133   int lz = 256; // lightvector
134   int lx = 200, ly = 200;
135
136   for (i=0;i<6;i++)
137   { y1 = (y[i]*cos_[xw]-z[i]*sin_[xw])>>PRECISION;
138     z1 = (y[i]*sin_[xw]+z[i]*cos_[xw])>>PRECISION;
139     x1 = x[i];
140
141     x2 = (x1*cos_[yw]+z1*sin_[yw])>>PRECISION;
142     y2 = y1;
143     z2 = (z1*cos_[yw]-x1*sin_[yw])>>PRECISION;
144
145     x3 = (x2*cos_[zw]-y2*sin_[zw])>>PRECISION;
146     y3 = (x2*sin_[zw]+y2*cos_[zw])>>PRECISION;
147     z3 = z2;
148
149     square_light[i] = -128-((x3*lx + y3*ly + z3*lz)>>9);
150   }
151 }
152
153
154 int main (int argc,char ** argv)
155 { SWF swf;
156   LPTAG t;
157   RGBA rgb;
158   SRECT r;
159   LPSHAPE s;
160   S32 width = 800,height = 800;
161   U8 gbits,abits;
162   int fs,ls; // line & fillstyle
163   LPSWFFONT font;
164   FONTUSAGE use;
165   LPJPEGBITS jpeg;
166   MATRIX m;  
167   
168   int f,i,j,frame;
169   
170   f = open("Arial.efont",O_RDONLY);
171   if (f>=0)
172   { if (FAILED(FontImport(f,&font)))
173     { fprintf(stderr,"Font import failed\n");
174       close(f);
175       return -1;
176     }
177   }
178   else
179   { fprintf(stderr,"Font not found\n");
180     return -1;
181   }
182   close(f);
183
184   FontSetID(font,ID_FONT);
185   FontInitUsage(&use);
186   FontUse(&use,BANNER_TEXT);
187   FontReduce(font,&use);
188   
189   calcTables();
190   
191   memset(&swf,0x00,sizeof(SWF));
192
193   swf.FileVersion    = 4;
194   swf.FrameRate      = 0x4000;
195   swf.MovieSize.xmax = 4*width;
196   swf.MovieSize.ymax = 4*height;
197
198   swf.FirstTag = InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
199   t = swf.FirstTag;
200
201         rgb.r = 0xff;
202         rgb.g = 0xff;
203         rgb.b = 0xff;
204         SetRGB(t,&rgb);
205
206   t = InsertTag(t,ST_DEFINEFONT);
207
208         FontSetDefine(t,font);
209
210   t = InsertTag(t,ST_DEFINEFONTINFO);
211
212         FontSetInfo(t,font);
213
214   t = InsertTag(t,ST_DEFINETEXT);
215
216         SetU16(t,ID_BANNER);            // ID
217         
218         r.xmin = 0;
219         r.ymin = 0;
220         r.xmax = 400;
221         r.ymax = 400;
222         SetRect(t,&r);
223
224         SetMatrix(t,NULL);
225
226         TextCountBits(font,BANNER_TEXT,80,&gbits,&abits);
227         
228         SetU8(t,gbits);
229         SetU8(t,abits);
230
231         rgb.r = 0xc0;
232         rgb.g = 0xc0;
233         rgb.b = 0xc0;
234
235         TextSetInfoRecord(t,font,height/4,&rgb,0,200);
236         TextSetCharRecord(t,font,BANNER_TEXT,80,gbits,abits);
237
238         SetU8(t,0);
239         
240   t = InsertTag(t,ST_DEFINETEXT);
241
242         SetU16(t,ID_HIGHLIGHT);            // ID
243         
244         r.xmin = 0; r.ymin = 0; r.xmax = 800; r.ymax = 400;
245         SetRect(t,&r);
246
247         SetMatrix(t,NULL);
248         TextCountBits(font,BANNER_TEXT,80,&gbits,&abits);
249         
250         SetU8(t,gbits);
251         SetU8(t,abits);
252
253         rgb.r = 0x20;
254         rgb.g = 0x20;
255         rgb.b = 0x20;
256
257         TextSetInfoRecord(t,font,height/4,&rgb,0,200);
258         TextSetCharRecord(t,font,BANNER_TEXT,80,gbits,abits);
259
260         SetU8(t,0);
261         
262     t = InsertTag(t,ST_DEFINEBUTTON);
263     
264         GetMatrix(NULL,&m);
265         
266         m.tx = 3*width;
267         m.ty = 7*height/2;
268         
269         SetU16(t,ID_BUTTON); // ID
270         ButtonSetRecord(t,BS_UP,ID_BANNER,1,&m,NULL);
271         ButtonSetRecord(t,BS_DOWN|BS_HIT|BS_OVER,ID_HIGHLIGHT,1,&m,NULL);
272         SetU8(t,0); // End of Button Records
273         SetU8(t,0); // End of Action Records
274     
275       
276     t = InsertTag(t,ST_PLACEOBJECT2);
277
278          ObjectPlace(t,ID_BUTTON,1,NULL,NULL,NULL);
279
280     t = InsertTag(t,ST_DEFINEBITSJPEG2);
281
282          SetU16(t,ID_BITMAP);
283          SetJPEGBits(t,"eye.jpg",QUALITY);
284          
285     t = InsertTag(t,ST_DEFINESHAPE);
286     
287          NewShape(&s);
288          rgb.b = rgb.g = rgb.r = 0x00;
289          ls = ShapeAddLineStyle(s,10,&rgb);     
290          rgb.b = 0xff;
291
292          m.tx = m.ty = 0;
293          m.r0 = m.r1 = 0;
294          m.sx = m.sy = width<<8;
295   
296          fs = ShapeAddBitmapFillStyle(s,&m,ID_BITMAP,0);
297     
298          SetU16(t,ID_SHAPE);    // ID   
299          
300          r.xmin = 0; r.ymin = 0;
301          r.xmax = 2*width; r.ymax = 2*height;
302          SetRect(t,&r);
303
304          SetShapeHeader(t,s);
305          ShapeSetAll(t,s,width,0,0,fs,0);
306          ShapeSetLine(t,s,-width,height);
307          ShapeSetStyle(t,s,ls,fs,0);
308          ShapeSetLine(t,s,0,-height);
309          ShapeSetLine(t,s,width,0);
310          ShapeSetEnd(t);
311
312          ShapeFree(s);
313          
314     t = InsertTag(t,ST_DEFINESHAPE);
315     
316          NewShape(&s);
317          rgb.b = rgb.g = rgb.r = 0x00;
318          ls = ShapeAddLineStyle(s,10,&rgb);     
319          rgb.b = 0xff;
320
321          m.tx = m.ty = 0;
322          m.r0 = m.r1 = 0;
323          m.sx = m.sy = -(width<<8);
324   
325          fs = ShapeAddBitmapFillStyle(s,&m,ID_BITMAP,0);
326     
327          SetU16(t,ID_SHAPE2);   // ID   
328          
329          r.xmin = 0; r.ymin = 0;
330          r.xmax = 2*width; r.ymax = 2*height;
331          SetRect(t,&r);
332
333          SetShapeHeader(t,s);
334          ShapeSetAll(t,s,width,0,0,fs,0);
335          ShapeSetLine(t,s,-width,height);
336          ShapeSetStyle(t,s,ls,fs,0);
337          ShapeSetLine(t,s,0,-height);
338          ShapeSetLine(t,s,width,0);
339          ShapeSetEnd(t);
340
341          ShapeFree(s);
342    
343   for (frame=0;frame<256;frame++)
344   { int dc = 3; // whitespace correction
345
346     mapBox(frame<<1,frame<<1,frame);
347     if (SHADE) mapLights(frame<<1,frame<<1,frame);
348     
349     t = MapSquare(t,width-dc,height-dc,0,2,3,1,2*width,2*height,0);
350     t = MapSquare(t,width-dc,height-dc,4,5,7,6,2*width,2*height,1);
351     t = MapSquare(t,width-dc,height-dc,0,4,6,2,2*width,2*height,2);
352     t = MapSquare(t,width-dc,height-dc,1,3,7,5,2*width,2*height,3);
353     t = MapSquare(t,width-dc,height-dc,0,1,5,4,2*width,2*height,4);
354     t = MapSquare(t,width-dc,height-dc,2,6,7,3,2*width,2*height,5);
355         
356     t = InsertTag(t,ST_SHOWFRAME);
357   }
358   
359   
360   t = InsertTag(t,ST_END);
361   
362   WriteCGI(&swf);
363   FreeTags(&swf);
364   return 0;
365 }
366
367