upgraded ttf2pt1 to 3.4.3.
[swftools.git] / lib / example / box.c
1 /* box.c
2
3    Example for drawing 3D grind objects
4    
5    Part of the swftools package.
6
7    Copyright (c) 2000, 2001 Rainer Böhme <rfxswf@reflex-studio.de>
8  
9    This file is distributed under the GPL, see file COPYING for details 
10
11 */
12
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include <math.h>
16 #include "../rfxswf.h"
17
18 #define BANNER_TEXT     "reflex" // banner is disabled due to new font handling
19 #define ID_FONT         2000
20 #define ID_BANNER       2001
21 #define ID_HIGHLIGHT    2002
22 #define ID_BUTTON       2003
23
24 #define a 200
25 int sX[] = { a,-a, a,-a, a,-a, a,-a};
26 int sY[] = { a, a,-a,-a, a, a,-a,-a};
27 int sZ[] = { a, a, a, a,-a,-a,-a,-a};
28 #undef a
29   
30 int dX[8];
31 int dY[8];
32
33 int sin_[512],cos_[512];
34
35 void calcTables()
36 { int i;
37   double d;
38   for (i=0;i<512;i++)
39   { d = ((double)i)/128*3.14159;
40     sin_[i] = (int)(sin(d)*256);
41     cos_[i] = (int)(cos(d)*256);
42   }
43 }
44
45 void ShapeSquare(LPTAG t,LPSHAPE s,int p1,int p2,int p3,int p4,int dx,int dy)
46
47   // Hidden-Line-Check
48     if (((dX[p2]-dX[p1])*(dY[p3]-dY[p1])-(dX[p3]-dX[p1])*(dY[p2]-dY[p1]))<0) return;
49
50   swf_ShapeSetMove(t,s,dX[p1]+dx,dY[p1]+dy);
51   swf_ShapeSetLine(t,s,dX[p2]-dX[p1],dY[p2]-dY[p1]);
52   swf_ShapeSetLine(t,s,dX[p3]-dX[p2],dY[p3]-dY[p2]);
53   swf_ShapeSetLine(t,s,dX[p4]-dX[p3],dY[p4]-dY[p3]);
54   swf_ShapeSetLine(t,s,dX[p1]-dX[p4],dY[p1]-dY[p4]);
55 }
56
57
58 void mapBox(int xw,int yw,int zw)
59 // simple trigonometry without using transformation matrices
60 { int i;
61   int x1,y1,z1,x2,y2,z2,x3,y3,z3;
62   int y,x,z;
63   
64   xw &= 255;
65   yw &= 255;
66   zw &= 255;
67
68   for (i=0;i<8;i++)
69   { x = sX[i];
70     y = sY[i];
71     z = sZ[i];
72                   
73     y1 = ( y*cos_[xw]- z*sin_[xw])>>8;
74     z1 = ( y*sin_[xw]+ z*cos_[xw])>>8;
75     x1 = x;
76
77     x2 = (x1*cos_[yw]+z1*sin_[yw])>>8;
78     y2 = y1;
79
80     x3 = (x2*cos_[zw]-y2*sin_[zw])>>8;
81     y3 = (x2*sin_[zw]+y2*cos_[zw])>>8;
82     z3 = (z1*cos_[yw]-x1*sin_[yw])>>8;
83
84     dX[i] = x3*4000/(z3+950);
85     dY[i] = y3*4000/(z3+950);
86         
87   }
88 }
89                
90 int main (int argc,char ** argv)
91 { SWF swf;
92   LPTAG t;
93   RGBA rgb;
94   SRECT r;
95   LPSHAPE s;
96   S32 width = 800,height = 800;
97   U8 gbits,abits;
98   CXFORM cx1,cx2;
99   MATRIX m;
100   
101   int f,i,j,frame;
102   
103 /*  f = open("Arial.efont",O_RDONLY);
104   if (f>=0)
105   { if (FAILED(swf_FontImport(f,&font)))
106     { fprintf(stderr,"Font import failed\n");
107       close(f);
108       return -1;
109     }
110   }
111   else
112   { fprintf(stderr,"Font not found\n");
113     return -1;
114   }
115   close(f);
116
117   swf_FontSetID(font,ID_FONT);
118   swf_FontInitUsage(&use);
119   swf_FontUse(&use,BANNER_TEXT);
120   swf_FontReduce(font,&use);
121 */
122   
123   calcTables();
124   
125   memset(&swf,0x00,sizeof(SWF));
126
127   swf.fileVersion    = 4;
128   swf.frameRate      = 0x4000;
129   swf.movieSize.xmax = 4*width;
130   swf.movieSize.ymax = 4*height;
131
132   swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
133   t = swf.firstTag;
134
135         rgb.r = 0xff;
136         rgb.g = 0xff;
137         rgb.b = 0xff;
138         swf_SetRGB(t,&rgb);
139         
140 /* This part can't be used because of missing fonts. Nevertheless it shows, how
141    rfxswf handles fonts, fontusages (in order to include only the used glyphs
142    into the output swf) and how to make a simple button
143
144
145   t = swf_InsertTag(t,ST_DEFINEFONT);
146
147         swf_FontSetDefine(t,font);
148
149   t = swf_InsertTag(t,ST_DEFINEFONTINFO);
150
151         swf_FontSetInfo(t,font);
152
153   t = swf_InsertTag(t,ST_DEFINETEXT);
154
155         swf_SetU16(t,ID_BANNER);            // ID
156         
157         r.xmin = 0;
158         r.ymin = 0;
159         r.xmax = 400;
160         r.ymax = 400;
161         
162         swf_SetRect(t,&r);
163
164         swf_SetMatrix(t,NULL);
165
166         swf_TextCountBits(font,BANNER_TEXT,80,&gbits,&abits);
167         
168         swf_SetU8(t,gbits);
169         swf_SetU8(t,abits);
170
171         rgb.r = 0xc0;
172         rgb.g = 0xc0;
173         rgb.b = 0xc0;
174
175         swf_TextSetInfoRecord(t,font,height/4,&rgb,0,200);
176         swf_TextSetCharRecord(t,font,BANNER_TEXT,80,gbits,abits);
177
178         swf_SetU8(t,0);
179         
180   t = swf_InsertTag(t,ST_DEFINETEXT);
181
182         swf_SetU16(t,ID_HIGHLIGHT);            // ID
183         
184         r.xmin = 0;
185         r.ymin = 0;
186         r.xmax = 800;
187         r.ymax = 400;
188         
189         swf_SetRect(t,&r);
190
191         swf_SetMatrix(t,NULL);
192
193         swf_TextCountBits(font,BANNER_TEXT,80,&gbits,&abits);
194         
195         swf_SetU8(t,gbits);
196         swf_SetU8(t,abits);
197
198         rgb.r = 0x20;
199         rgb.g = 0x20;
200         rgb.b = 0x20;
201
202         swf_TextSetInfoRecord(t,font,height/4,&rgb,0,200);
203         swf_TextSetCharRecord(t,font,BANNER_TEXT,80,gbits,abits);
204
205         swf_SetU8(t,0);
206         
207     t = swf_InsertTag(t,ST_DEFINEBUTTON);
208     
209         swf_GetMatrix(NULL,&m);
210         
211         m.tx = 3*width;
212         m.ty = 7*height/2;
213         
214         swf_SetU16(t,ID_BUTTON); // ID
215         swf_ButtonSetRecord(t,BS_UP,ID_BANNER,1,&m,NULL);
216         swf_ButtonSetRecord(t,BS_DOWN|BS_HIT|BS_OVER,ID_HIGHLIGHT,1,&m,NULL);
217         swf_SetU8(t,0); // End of Button Records
218         swf_SetU8(t,0); // End of Action Records
219     
220       
221     t = swf_InsertTag(t,ST_PLACEOBJECT2);
222
223          swf_ObjectPlace(t,ID_BUTTON,1,NULL,NULL,NULL);
224          
225 */
226         
227   for (frame=0;frame<256;frame+=2)
228   { int id = frame +1;
229     
230     t = swf_InsertTag(t,ST_DEFINESHAPE);
231
232         swf_ShapeNew(&s);
233         rgb.r = rgb.g = 0x00; rgb.b = 0xff;
234         j = swf_ShapeAddLineStyle(s,40,&rgb);
235
236         swf_SetU16(t,id);  // ID
237
238         r.xmin = 0;
239         r.ymin = 0;
240         r.xmax = 4*width;
241         r.ymax = 4*height;
242         
243         swf_SetRect(t,&r);
244         swf_SetShapeHeader(t,s);
245         swf_ShapeSetAll(t,s,0,0,j,0,0);
246         
247         mapBox(frame,frame,frame>>1);
248
249         ShapeSquare(t,s,0,2,3,1,2*width,2*height);
250         ShapeSquare(t,s,4,5,7,6,2*width,2*height);
251         ShapeSquare(t,s,0,4,6,2,2*width,2*height);
252         ShapeSquare(t,s,1,3,7,5,2*width,2*height);
253         ShapeSquare(t,s,0,1,5,4,2*width,2*height);
254         ShapeSquare(t,s,2,6,7,3,2*width,2*height);
255         
256         swf_ShapeSetEnd(t);
257         swf_ShapeFree(s);
258   }
259   
260   swf_GetCXForm(NULL,&cx1,1);  // get default ColorTransforms
261   swf_GetCXForm(NULL,&cx2,1);
262     
263   cx1.r1 = cx1.g1 = 0x80;  // set alpha for blur effect
264   cx2.r1 = cx2.g1 = 0xc0;     
265     
266   for (frame=0;frame<256;frame+=2)
267   { int id = frame +1;
268     int id2 = ((frame-2)&255)+1;
269     int id3 = ((frame-4)&255)+1;
270
271     if (frame)
272     { t = swf_InsertTag(t,ST_REMOVEOBJECT2); swf_SetU16(t,2); // depth
273       t = swf_InsertTag(t,ST_REMOVEOBJECT2); swf_SetU16(t,3); // depth
274       t = swf_InsertTag(t,ST_REMOVEOBJECT2); swf_SetU16(t,4); // depth
275     }
276
277     t = swf_InsertTag(t,ST_PLACEOBJECT2);
278
279         swf_ObjectPlace(t,id,4,NULL,NULL,NULL);
280         
281     t = swf_InsertTag(t,ST_PLACEOBJECT2);
282
283         swf_ObjectPlace(t,id2,3,NULL,&cx1,NULL);
284   
285     t = swf_InsertTag(t,ST_PLACEOBJECT2);
286
287         swf_ObjectPlace(t,id3,2,NULL,&cx2,NULL);
288   
289     t = swf_InsertTag(t,ST_SHOWFRAME);
290   }
291   
292   t = swf_InsertTag(t,ST_END);
293  
294 //  swf_WriteCGI(&swf);
295  
296
297   f = open("box.swf",O_RDWR|O_CREAT|O_TRUNC,0644);
298   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
299   close(f);
300
301   swf_FreeTags(&swf);
302
303 #ifdef __NT__
304   system("start ..\\box.swf");
305 #endif
306   
307   return 0;
308 }
309
310