Added #define-switch for "watermarking" option:
[swftools.git] / swfs / simple_viewer.c
1 /* simple_viewer.c
2
3    Creates the swf file simple_viewer.swf. 
4    I'm not much of an designer. Suggestions and improvements concerning
5    this file or simple_viewer.swf are highly welcome. -mk
6    
7    Part of the swftools package.
8
9    Copyright (c) 2000, 2001 Matthias Kramm <kramm@quiss.org>
10  
11    This file is distributed under the GPL, see file COPYING for details 
12 */
13
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <math.h>
17 #include "rfxswf.h"
18
19 TAG* tag;
20
21 int button_sizex = 20; //button size in pixels
22 int button_sizey = 20; 
23
24 RGBA button_colors[6]=
25 {{0,0x00,0x00,0x80}, //Idle      left
26  {0,0x00,0x00,0x80}, //   "      right
27  {0,0x20,0x20,0xc0}, //MouseOver left
28  {0,0x20,0x20,0xc0}, //   "      right
29  {0,0x10,0x10,0xff}, //Down      left
30  {0,0x10,0x10,0xff}};//   "      right
31
32 int useDefineButton2 = 0; // set this to 1 to use DefineButton2 Tags
33                           // instead of DefineButton1
34 #define SUBTITLES 1
35 //#define USE_WATERMARK 1   // set this flag to print-protect your swfs
36
37 #define ID_WATERMARK 64
38
39 int main (int argc,char ** argv)
40 { SWF swf;
41   RGBA rgb;
42   SRECT r;
43   LPSHAPE s;
44   MATRIX m;
45   ActionTAG*a1,*a2,*a3;
46   S32 width=1024,height = 768;
47   
48   int f,i,ls1,fs1;
49   int count;
50   int t;
51
52   memset(&swf,0x00,sizeof(SWF));        // set global movie parameters
53
54   swf.fileVersion    = 4;               // make flash 4 compatible swf
55   swf.frameRate      = 0x1900;          // about 0x19 frames per second
56   
57   swf.movieSize.xmax = 20*width;        // flash units: 1 pixel = 20 units
58   swf.movieSize.ymax = 20*height;
59
60   swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
61   tag = swf.firstTag;
62   rgb.r = 0xff;
63   rgb.g = 0xff;
64   rgb.b = 0xff;
65   swf_SetRGB(tag,&rgb);
66  
67   // initialize matrix
68   m.sx = 65536; //scale
69   m.sy = 65536;
70   m.r0 = 0; //rotate
71   m.r1 = 0;
72   m.tx = 0; //move
73   m.ty = 0;
74
75   /* the "viewport" object will be replaced by swfcombine
76      with the object to browse. It is placed at the
77      upper left corner (0,0), by setting m.tx and m.ty
78      to 0. Therefore, the buttons are "in" the viewport,
79      not above it*/
80   tag = swf_InsertTag(tag,ST_DEFINESPRITE);
81   swf_SetU16(tag, 23); //id
82   swf_SetU16(tag, 0); //frames
83   tag = swf_InsertTag(tag,ST_END);
84   tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
85   m.tx = 0; //move
86   m.ty = 0;
87   swf_ObjectPlace(tag, 23, 1,&m,0,"viewport");
88     
89 #ifdef USE_WATERMARK
90
91   // Insert Alpha watermark to avoid printing
92
93   tag = swf_InsertTag(tag,ST_DEFINESHAPE3);
94   swf_ShapeNew(&s);
95   rgb.r = rgb.g = rgb.b = 0xff;
96   rgb.a = 0x08;
97   fs1 = swf_ShapeAddSolidFillStyle(s,&rgb);
98   swf_SetU16(tag,ID_WATERMARK);
99   r.xmin = r.ymin = 0;
100   r.xmax = 20*width;
101   r.ymax = 20*height;
102   swf_SetRect(tag,&r); // cover whole viewport
103   swf_SetShapeHeader(tag,s);
104   swf_ShapeSetAll(tag,s,0,0,0,fs1,0);
105   swf_ShapeSetLine(tag,s,20*width,0);
106   swf_ShapeSetLine(tag,s,0,20*height);
107   swf_ShapeSetLine(tag,s,-20*width,0);
108   swf_ShapeSetLine(tag,s,0,-20*height);
109   swf_ShapeSetEnd(tag);
110   swf_ShapeFree(s);  
111
112 #endif // USE_WATERMARK
113
114   for(count=0;count<6;count++)
115   {
116       tag = swf_InsertTag(tag,ST_DEFINESHAPE);
117       swf_ShapeNew(&s);               // create new shape instance
118       rgb.r = rgb.b = rgb.g = 0x00;           
119       ls1 = swf_ShapeAddLineStyle(s,40,&rgb);
120       rgb = button_colors[count];
121       fs1 = swf_ShapeAddSolidFillStyle(s,&rgb);
122       swf_SetU16(tag,32+count);                // now set character ID
123       r.xmin = 0;
124       r.ymin = 0;
125       r.xmax = 20*width;
126       r.ymax = 20*height;
127       swf_SetRect(tag,&r);              // set shape bounds
128       swf_SetShapeHeader(tag,s);        // write all styles to tag
129       if(count&1)
130       {
131           swf_ShapeSetAll(tag,s,0,0,ls1,fs1,0); // move to (0,0), select linestyle ls1 and no fillstyle
132           /* SetLine coordinates are relative.
133              It's important that the start and end points match, otherwise
134              the Macromedia Flash player will crash. */
135           swf_ShapeSetLine(tag,s,20*button_sizex,20*button_sizey);
136           swf_ShapeSetLine(tag,s,-20*button_sizex,20*button_sizey);
137           swf_ShapeSetLine(tag,s,0,-40*button_sizey);
138       } else {
139           swf_ShapeSetAll(tag,s,20*button_sizex,0,ls1,fs1,0);
140           swf_ShapeSetLine(tag,s,-20*button_sizex,20*button_sizey);
141           swf_ShapeSetLine(tag,s,20*button_sizex,20*button_sizey);
142           swf_ShapeSetLine(tag,s,0,-40*button_sizey);
143       }
144       swf_ShapeSetEnd(tag);   // finish drawing
145       swf_ShapeFree(s);   // clean shape structure (which isn't needed anymore after writing the tag)
146   }
147
148     a1 = action_SetTarget(0, "viewport");
149     a1 = action_PreviousFrame(a1);
150     a1 = action_SetTarget(a1, "");
151     a1 = action_End(a1);
152
153     a2 = action_SetTarget(0, "viewport");
154     a2 = action_NextFrame(a2);
155     a2 = action_SetTarget(a2,"");
156     a2 = action_End(a2);
157
158     a3 = action_SetTarget(0,"viewport");
159     a3 = action_Stop(a3);
160     a3 = action_SetTarget(a3,"");
161 #ifdef SUBTITLES
162     a3 = action_PushString(a3,"/:subtitle");
163     a3 = action_PushString(a3,""); //reset variable
164     a3 = action_SetVariable(a3);
165 #endif
166     a3 = action_End(a3);
167
168   for(t=0;t<2;t++)
169   {
170       if(!useDefineButton2)
171       {
172           tag = swf_InsertTag(tag,ST_DEFINEBUTTON);
173           swf_SetU16(tag,30+t); //id
174           swf_ButtonSetRecord(tag,BS_UP|BS_HIT,32+t,1,NULL,NULL);
175           swf_ButtonSetRecord(tag,BS_OVER,34+t,1,NULL,NULL);
176           swf_ButtonSetRecord(tag,BS_DOWN,36+t,1,NULL,NULL);
177           swf_SetU8(tag,0); // end of button records
178          
179           if(t)
180             swf_ActionSet(tag,a2);
181           else
182             swf_ActionSet(tag,a1);
183       }
184       else
185       {
186           tag = swf_InsertTag(tag,ST_DEFINEBUTTON2);
187           swf_SetU16(tag,30+t); //id
188           swf_ButtonSetFlags(tag, 0); //menu=no
189           swf_ButtonSetRecord(tag,BS_UP|BS_HIT,32+t,1,NULL,NULL);
190           swf_ButtonSetRecord(tag,BS_OVER,34+t,1,NULL,NULL);
191           swf_ButtonSetRecord(tag,BS_DOWN,36+t,1,NULL,NULL);
192           swf_SetU8(tag,0); // end of button records
193
194           swf_ButtonSetCondition(tag, BC_OVERDOWN_OVERUP);
195           if(t)
196             swf_ActionSet(tag,a2);
197           else
198             swf_ActionSet(tag,a1);
199            
200           swf_ButtonPostProcess(tag, 1); // don't forget!
201       }
202   }
203
204   tag = swf_InsertTag(tag,ST_DOACTION);
205   swf_ActionSet(tag,a3);
206
207   m.tx = 0; //move
208   m.ty = 0;
209   tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
210   swf_ObjectPlace(tag, 30, 3,&m,0,0);
211   m.tx = button_sizex*30;
212   tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
213   swf_ObjectPlace(tag, 31, 4,&m,0,0);
214
215 #ifdef USE_WATERMARK
216
217   // place watermark
218
219   tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
220   swf_ObjectPlace(tag, ID_WATERMARK, 2, &m, 0,0);
221
222 #endif // USE_WATERMARK
223   
224   swf_ActionFree(a1);
225   swf_ActionFree(a2);
226   swf_ActionFree(a3);
227
228 #ifdef SUBTITLES
229   tag = swf_InsertTag(tag,ST_DEFINEFONT2); {
230       U8 data[] = {0x90, 0x00, 0x0f, 0x54, 0x69, 0x6d, 0x65, 0x73, 
231                    0x20, 0x4e, 0x65, 0x77, 0x20, 0x52, 0x6f, 0x6d, 
232                    0x61, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
233                    0x00, 0x00, 0x00, 0x00};
234       swf_SetU16(tag, 0x76); //id
235       swf_SetBlock(tag, data, sizeof(data));
236   }
237   tag = swf_InsertTag(tag,ST_DEFINEEDITTEXT); {
238       EditTextLayout layout;
239       layout.align = 0;
240       layout.leftmargin = 0;
241       layout.rightmargin = 0;
242       layout.indent = 0;
243       layout.leading = 0;
244
245       swf_SetU16(tag, 0x77);//id
246       r.xmin = button_sizex*60;
247       r.xmax = r.xmin+ 826*20;
248       r.ymin = 0;
249       r.ymax = button_sizey*80;
250       rgb.r = rgb.g = rgb.b = 0;
251       rgb.a = 255;
252       swf_SetEditText(tag, ET_MULTILINE|ET_READONLY, r, 0, &rgb, 0, 0x76, button_sizey*40, &layout, "/:subtitle");
253       m.tx = m.ty = 0;
254   }
255   tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
256   swf_ObjectPlace(tag, 0x77, 5,&m,0,0);
257 #endif
258
259   tag = swf_InsertTag(tag,ST_SHOWFRAME);
260   tag = swf_InsertTag(tag,ST_END);
261   
262
263   f = open("simple_viewer.swf",O_WRONLY|O_CREAT|O_TRUNC, 0644);
264   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
265   close(f);
266
267   swf_FreeTags(&swf);                       // cleanup
268   return 0;
269 }
270
271