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