bugfix: ST_DEFINEBUTTON2 and CXForms
[swftools.git] / lib / example / buttontest.c
1 /* buttontest.c
2
3    Example implementation for creating a button with rfxswf
4    
5    Part of the swftools package.
6
7    Copyright (c) 2000, 2001 Matthias Kramm <kramm@quiss.org>
8                             Rainer Böhme <rfxswf@reflex-studio.de>
9  
10    This file is distributed under the GPL, see file COPYING for details 
11 */
12
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include <math.h>
16 #include "../rfxswf.h"
17
18 TAG* t;
19
20 #define ID_BUTTON 31
21
22 int useDefineButton2 = 0; // set this to 1 to use DefineButton2 Tags
23                           // instead of DefineButton1
24                
25 int main (int argc,char ** argv)
26 { SWF swf;
27   RGBA rgb;
28   SRECT r;
29   LPSHAPE s;
30   ActionTAG*a1,*a2,*a3, *actiontoset;
31   S32 width=300,height = 300;
32   
33   int f,i,ls1,fs1;
34   int count;
35
36   memset(&swf,0x00,sizeof(SWF));        // set global movie parameters
37
38   swf.fileVersion    = 4;               // make flash 4 compatible swf
39   swf.frameRate      = 0x1900;          // about 0x19 frames per second
40   
41   swf.movieSize.xmax = 20*width;        // flash units: 1 pixel = 20 units
42   swf.movieSize.ymax = 20*height;
43
44   swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
45   t = swf.firstTag;
46   rgb.r = 0xff;
47   rgb.g = 0xff;
48   rgb.b = 0xff;
49   swf_SetRGB(t,&rgb);
50
51   for(count=1;count<4;count++)
52   {
53       t = swf_InsertTag(t,ST_DEFINESHAPE);
54       swf_ShapeNew(&s);               // create new shape instance
55       rgb.r = rgb.b = rgb.g = 0x00;           
56       ls1 = swf_ShapeAddLineStyle(s,40,&rgb);
57       rgb.r = count*0x40; rgb.b = 0xff;
58       fs1 = swf_ShapeAddSolidFillStyle(s,&rgb);
59       swf_SetU16(t,33+count);                // now set character ID
60       r.xmin = 0;
61       r.ymin = 0;
62       r.xmax = 20*width;
63       r.ymax = 20*height;
64       swf_SetRect(t,&r);              // set shape bounds
65       swf_SetShapeHeader(t,s);        // write all styles to tag
66       swf_ShapeSetAll(t,s,0,0,ls1,fs1,0); // move to (0,0), select linestyle ls1 and no fillstyle
67       swf_ShapeSetCircle(t,s,width*10,height*10,width*10,height*10);
68       swf_ShapeSetEnd(t);                 // finish drawing
69       swf_ShapeFree(s);                   // clean shape structure (which isn't needed anymore after writing the tag)
70   }
71
72   a1 = swf_ActionStart(t);
73     action_SetTarget("movie");
74     action_NextFrame();
75     action_SetTarget("");
76     action_End();
77   swf_ActionEnd();
78  
79   a2 = swf_ActionStart(t);
80     //action_GetUrl("test.swf","_level0"); // load a swf
81     //action_GetUrl("index.html","_this"); // load html in new window
82     action_GetUrl("http://www.quiss.org/swftools/index.html","_parent"); // load html in this window
83     action_End();
84   swf_ActionEnd();
85
86   a3 = swf_ActionStart(t);
87     action_GotoFrame(33);
88     action_End();
89   swf_ActionEnd();
90
91   actiontoset = a2;  
92
93   if(!useDefineButton2)
94   {
95       t = swf_InsertTag(t,ST_DEFINEBUTTON);
96       swf_SetU16(t,31); //id
97       swf_ButtonSetRecord(t,BS_UP|BS_HIT,34,1,NULL,NULL);
98       swf_ButtonSetRecord(t,BS_OVER,35,1,NULL,NULL);
99       swf_ButtonSetRecord(t,BS_DOWN,36,1,NULL,NULL);
100       swf_SetU8(t,0); // end of button records
101       
102       swf_SetActions(t,actiontoset);
103   }
104   else
105   {
106       t = swf_InsertTag(t,ST_DEFINEBUTTON2);
107       swf_SetU16(t,ID_BUTTON); //id
108       swf_ButtonSetFlags(t, 0); //menu=no
109       swf_ButtonSetRecord(t,BS_UP|BS_HIT,34,1,NULL,NULL);
110       swf_ButtonSetRecord(t,BS_OVER,35,1,NULL,NULL);
111       swf_ButtonSetRecord(t,BS_DOWN,36,1,NULL,NULL);
112       swf_SetU8(t,0); // end of button records
113
114       swf_ButtonSetCondition(t, BC_OVERDOWN_OVERUP);
115        swf_SetActions(t,actiontoset);
116        
117       swf_ButtonPostProcess(t, 1); // don't forget!
118   }
119
120   // FIXME: Free Action Tag lists
121
122   t = swf_InsertTag(t,ST_PLACEOBJECT2);
123   swf_ObjectPlace(t, ID_BUTTON, 2,0,0,0);
124
125   t = swf_InsertTag(t,ST_SHOWFRAME);
126   t = swf_InsertTag(t,ST_END);
127
128   f = open("buttontest.swf",O_WRONLY|O_CREAT, 0644);
129   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
130   close(f);
131
132   swf_FreeTags(&swf);                       // cleanup
133   return 0;
134 }
135
136