made rfxswf actionscript routines threadsafe. ;)
[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
36 int main (int argc,char ** argv)
37 { SWF swf;
38   RGBA rgb;
39   SRECT r;
40   LPSHAPE s;
41   MATRIX m;
42   ActionTAG*a1,*a2,*a3;
43   S32 width=1024,height = 768;
44   
45   int f,i,ls1,fs1;
46   int count;
47   int t;
48
49   memset(&swf,0x00,sizeof(SWF));        // set global movie parameters
50
51   swf.fileVersion    = 4;               // make flash 4 compatible swf
52   swf.frameRate      = 0x1900;          // about 0x19 frames per second
53   
54   swf.movieSize.xmax = 20*width;        // flash units: 1 pixel = 20 units
55   swf.movieSize.ymax = 20*height;
56
57   swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
58   tag = swf.firstTag;
59   rgb.r = 0xff;
60   rgb.g = 0xff;
61   rgb.b = 0xff;
62   swf_SetRGB(tag,&rgb);
63  
64   // initialize matrix
65   m.sx = 65536; //scale
66   m.sy = 65536;
67   m.r0 = 0; //rotate
68   m.r1 = 0;
69   m.tx = 0; //move
70   m.ty = 0;
71
72   /* the "viewport" object will be replaced by swfcombine
73      with the object to browse. It is placed at the
74      upper left corner (0,0), by setting m.tx and m.ty
75      to 0. Therefore, the buttons are "in" the viewport,
76      not above it*/
77   tag = swf_InsertTag(tag,ST_DEFINESPRITE);
78   swf_SetU16(tag, 23); //id
79   swf_SetU16(tag, 0); //frames
80   tag = swf_InsertTag(tag,ST_END);
81   tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
82   m.tx = 0; //move
83   m.ty = 0;
84   swf_ObjectPlace(tag, 23, 1,&m,0,"viewport");
85
86   for(count=0;count<6;count++)
87   {
88       tag = swf_InsertTag(tag,ST_DEFINESHAPE);
89       swf_ShapeNew(&s);               // create new shape instance
90       rgb.r = rgb.b = rgb.g = 0x00;           
91       ls1 = swf_ShapeAddLineStyle(s,40,&rgb);
92       rgb = button_colors[count];
93       fs1 = swf_ShapeAddSolidFillStyle(s,&rgb);
94       swf_SetU16(tag,32+count);                // now set character ID
95       r.xmin = 0;
96       r.ymin = 0;
97       r.xmax = 20*width;
98       r.ymax = 20*height;
99       swf_SetRect(tag,&r);              // set shape bounds
100       swf_SetShapeHeader(tag,s);        // write all styles to tag
101       if(count&1)
102       {
103           swf_ShapeSetAll(tag,s,0,0,ls1,fs1,0); // move to (0,0), select linestyle ls1 and no fillstyle
104           /* SetLine coordinates are relative.
105              It's important that the start and end points match, otherwise
106              the Macromedia Flash player will crash. */
107           swf_ShapeSetLine(tag,s,20*button_sizex,20*button_sizey);
108           swf_ShapeSetLine(tag,s,-20*button_sizex,20*button_sizey);
109           swf_ShapeSetLine(tag,s,0,-40*button_sizey);
110       } else {
111           swf_ShapeSetAll(tag,s,20*button_sizex,0,ls1,fs1,0);
112           swf_ShapeSetLine(tag,s,-20*button_sizex,20*button_sizey);
113           swf_ShapeSetLine(tag,s,20*button_sizex,20*button_sizey);
114           swf_ShapeSetLine(tag,s,0,-40*button_sizey);
115       }
116       swf_ShapeSetEnd(tag);   // finish drawing
117       swf_ShapeFree(s);   // clean shape structure (which isn't needed anymore after writing the tag)
118   }
119
120     a1 = action_SetTarget(0, "viewport");
121     a1 = action_PreviousFrame(a1);
122     a1 = action_SetTarget(a1, "");
123     a1 = action_End(a1);
124
125     a2 = action_SetTarget(0, "viewport");
126     a2 = action_NextFrame(a2);
127     a2 = action_SetTarget(a2,"");
128     a2 = action_End(a2);
129
130     a3 = action_SetTarget(0,"viewport");
131     a3 = action_Stop(a3);
132     a3 = action_SetTarget(a3,"");
133 #ifdef SUBTITLES
134     a3 = action_PushString(a3,"/:subtitle");
135     a3 = action_PushString(a3,""); //reset variable
136     a3 = action_SetVariable(a3);
137 #endif
138     a3 = action_End(a3);
139
140   for(t=0;t<2;t++)
141   {
142       if(!useDefineButton2)
143       {
144           tag = swf_InsertTag(tag,ST_DEFINEBUTTON);
145           swf_SetU16(tag,30+t); //id
146           swf_ButtonSetRecord(tag,BS_UP|BS_HIT,32+t,1,NULL,NULL);
147           swf_ButtonSetRecord(tag,BS_OVER,34+t,1,NULL,NULL);
148           swf_ButtonSetRecord(tag,BS_DOWN,36+t,1,NULL,NULL);
149           swf_SetU8(tag,0); // end of button records
150          
151           if(t)
152             swf_ActionSet(tag,a2);
153           else
154             swf_ActionSet(tag,a1);
155       }
156       else
157       {
158           tag = swf_InsertTag(tag,ST_DEFINEBUTTON2);
159           swf_SetU16(tag,30+t); //id
160           swf_ButtonSetFlags(tag, 0); //menu=no
161           swf_ButtonSetRecord(tag,BS_UP|BS_HIT,32+t,1,NULL,NULL);
162           swf_ButtonSetRecord(tag,BS_OVER,34+t,1,NULL,NULL);
163           swf_ButtonSetRecord(tag,BS_DOWN,36+t,1,NULL,NULL);
164           swf_SetU8(tag,0); // end of button records
165
166           swf_ButtonSetCondition(tag, BC_OVERDOWN_OVERUP);
167           if(t)
168             swf_ActionSet(tag,a2);
169           else
170             swf_ActionSet(tag,a1);
171            
172           swf_ButtonPostProcess(tag, 1); // don't forget!
173       }
174   }
175
176   tag = swf_InsertTag(tag,ST_DOACTION);
177   swf_ActionSet(tag,a3);
178
179   m.tx = 0; //move
180   m.ty = 0;
181   tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
182   swf_ObjectPlace(tag, 30, 2,&m,0,0);
183   m.tx = button_sizex*30;
184   tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
185   swf_ObjectPlace(tag, 31, 3,&m,0,0);
186   
187   swf_ActionFree(a1);
188   swf_ActionFree(a2);
189   swf_ActionFree(a3);
190
191 #ifdef SUBTITLES
192   tag = swf_InsertTag(tag,ST_DEFINEFONT2); {
193       U8 data[] = {0x90, 0x00, 0x0f, 0x54, 0x69, 0x6d, 0x65, 0x73, 
194                    0x20, 0x4e, 0x65, 0x77, 0x20, 0x52, 0x6f, 0x6d, 
195                    0x61, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
196                    0x00, 0x00, 0x00, 0x00};
197       swf_SetU16(tag, 0x76); //id
198       swf_SetBlock(tag, data, sizeof(data));
199   }
200   tag = swf_InsertTag(tag,ST_DEFINEEDITTEXT); {
201       EditTextLayout layout;
202       layout.align = 0;
203       layout.leftmargin = 0;
204       layout.rightmargin = 0;
205       layout.indent = 0;
206       layout.leading = 0;
207
208       swf_SetU16(tag, 0x77);//id
209       r.xmin = button_sizex*60;
210       r.xmax = r.xmin+ 826*20;
211       r.ymin = 0;
212       r.ymax = button_sizey*80;
213       rgb.r = rgb.g = rgb.b = 0;
214       rgb.a = 255;
215       swf_SetEditText(tag, ET_MULTILINE|ET_READONLY, r, 0, &rgb, 0, 0x76, button_sizey*40, &layout, "/:subtitle");
216       m.tx = m.ty = 0;
217   }
218   tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
219   swf_ObjectPlace(tag, 0x77, 4,&m,0,0);
220 #endif
221
222   tag = swf_InsertTag(tag,ST_SHOWFRAME);
223   tag = swf_InsertTag(tag,ST_END);
224   
225
226   f = open("simple_viewer.swf",O_WRONLY|O_CREAT|O_TRUNC, 0644);
227   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
228   close(f);
229
230   swf_FreeTags(&swf);                       // cleanup
231   return 0;
232 }
233
234