new function list_deep_free
[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 program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
23
24 #include <stdio.h>
25 #include <fcntl.h>
26 #include <math.h>
27 #include "../rfxswf.h"
28
29 TAG* t;
30
31 #define ID_BUTTON 31
32
33 int useDefineButton2 = 0; // set this to 1 to use DefineButton2 Tags
34                           // instead of DefineButton1
35                
36 int main (int argc,char ** argv)
37 { SWF swf;
38   RGBA rgb;
39   SRECT r;
40   SHAPE* s;
41   ActionTAG*a1,*a2,*a3, *actiontoset;
42   S32 width=300,height = 300;
43   
44   int f,i,ls1,fs1;
45   int count;
46
47   memset(&swf,0x00,sizeof(SWF));        // set global movie parameters
48
49   swf.fileVersion    = 4;               // make flash 4 compatible swf
50   swf.frameRate      = 0x1900;          // about 0x19 frames per second
51   
52   swf.movieSize.xmax = 20*width;        // flash units: 1 pixel = 20 units
53   swf.movieSize.ymax = 20*height;
54
55   swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
56   t = swf.firstTag;
57   rgb.r = 0xff;
58   rgb.g = 0xff;
59   rgb.b = 0xff;
60   swf_SetRGB(t,&rgb);
61
62   for(count=1;count<4;count++)
63   {
64       t = swf_InsertTag(t,ST_DEFINESHAPE);
65       swf_ShapeNew(&s);               // create new shape instance
66       rgb.r = rgb.b = rgb.g = 0x00;           
67       ls1 = swf_ShapeAddLineStyle(s,40,&rgb);
68       rgb.r = count*0x40; rgb.b = 0xff;
69       fs1 = swf_ShapeAddSolidFillStyle(s,&rgb);
70       swf_SetU16(t,33+count);                // now set character ID
71       r.xmin = 0;
72       r.ymin = 0;
73       r.xmax = 20*width;
74       r.ymax = 20*height;
75       swf_SetRect(t,&r);              // set shape bounds
76       swf_SetShapeHeader(t,s);        // write all styles to tag
77       swf_ShapeSetAll(t,s,0,0,ls1,fs1,0); // move to (0,0), select linestyle ls1 and no fillstyle
78       swf_ShapeSetCircle(t,s,width*10,height*10,width*10,height*10);
79       swf_ShapeSetEnd(t);                 // finish drawing
80       swf_ShapeFree(s);                   // clean shape structure (which isn't needed anymore after writing the tag)
81   }
82
83   a1 = action_SetTarget(0, "movie");
84   a1 = action_NextFrame(a1);
85   a1 = action_SetTarget(a1, "");
86   a1 = action_End(a1);
87  
88     //a2 = action_GetUrl(a2, "test.swf","_level0"); // load a swf
89     //a2 = action_GetUrl(a2, "index.html","_this"); // load html in new window
90     a2 = action_GetUrl(0, "http://www.quiss.org/swftools/index.html","_parent"); // load html in this window
91     a2 = action_End(a2);
92
93     a3 = action_GotoFrame(0,33);
94     a3 = action_End(a3);
95
96   actiontoset = a2;  
97
98   if(!useDefineButton2)
99   {
100       t = swf_InsertTag(t,ST_DEFINEBUTTON);
101       swf_SetU16(t,31); //id
102       swf_ButtonSetRecord(t,BS_UP|BS_HIT,34,1,NULL,NULL);
103       swf_ButtonSetRecord(t,BS_OVER,35,1,NULL,NULL);
104       swf_ButtonSetRecord(t,BS_DOWN,36,1,NULL,NULL);
105       swf_SetU8(t,0); // end of button records
106       
107       swf_ActionSet(t,actiontoset);
108   }
109   else
110   {
111       t = swf_InsertTag(t,ST_DEFINEBUTTON2);
112       swf_SetU16(t,ID_BUTTON); //id
113       swf_ButtonSetFlags(t, 0); //menu=no
114       swf_ButtonSetRecord(t,BS_UP|BS_HIT,34,1,NULL,NULL);
115       swf_ButtonSetRecord(t,BS_OVER,35,1,NULL,NULL);
116       swf_ButtonSetRecord(t,BS_DOWN,36,1,NULL,NULL);
117       swf_SetU8(t,0); // end of button records
118
119       swf_ButtonSetCondition(t, BC_OVERDOWN_OVERUP);
120        swf_ActionSet(t,actiontoset);
121        
122       swf_ButtonPostProcess(t, 1); // don't forget!
123   }
124
125   swf_ActionFree(a1);
126   swf_ActionFree(a2);
127   swf_ActionFree(a3);
128
129   t = swf_InsertTag(t,ST_PLACEOBJECT2);
130   swf_ObjectPlace(t, ID_BUTTON, 2,0,0,0);
131
132   t = swf_InsertTag(t,ST_SHOWFRAME);
133   t = swf_InsertTag(t,ST_END);
134
135   f = open("buttontest.swf",O_WRONLY|O_CREAT|O_BINARY, 0644);
136   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
137   close(f);
138
139   swf_FreeTags(&swf);                       // cleanup
140   return 0;
141 }
142
143