fixed graphics bug
[swftools.git] / lib / example / sprites.c
1 /* sprites.c
2
3    Demonstration of using different timelines (sprites)
4
5    Remarks:
6    
7    * rfxswf uses a flat tag list
8      All sprite tags between ST_DEFINESPRITE and ST_END
9      will be packed into the data of the ST_DEFINESPRITE
10      by the output routines
11
12    * make sure, that all defining tags are placed
13      outside the sub-timelines (i.e. before ST_DEFINESPRITE)
14
15    * framerate is global (sad but true!)
16
17    Part of the swftools package.
18
19    Copyright (c) 2001 Rainer Böhme <rfxswf@reflex-studio.de>
20  
21    This program is free software; you can redistribute it and/or modify
22    it under the terms of the GNU General Public License as published by
23    the Free Software Foundation; either version 2 of the License, or
24    (at your option) any later version.
25
26    This program is distributed in the hope that it will be useful,
27    but WITHOUT ANY WARRANTY; without even the implied warranty of
28    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29    GNU General Public License for more details.
30
31    You should have received a copy of the GNU General Public License
32    along with this program; if not, write to the Free Software
33    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
34
35 #include <stdio.h>
36 #include <fcntl.h>
37 #include <math.h>
38 #include "../rfxswf.h"
39
40
41 RGBA rgba(U8 r,U8 g,U8 b,U8 a)
42 { RGBA c; c.r = r; c.g = g; c.b = b; c.a = a;
43   return c;
44 }
45
46 U16 last_id;
47 U16 last_depth;
48
49 #define GET_ID (last_id++)
50 #define GET_DEPTH (last_depth++)
51
52 int main(int argc, char ** argv)
53 { SWF swf;
54   RGBA rgb;
55
56   LPTAG t;
57   LPSHAPE s;
58   SRECT r;
59
60   U16 id_circle;
61   U16 id_sprite;
62   U16 id_sprite2;
63
64   int ls,fs,i,j,f;
65
66   U32 width  = 400;
67   U32 height = 400;
68
69   last_id = last_depth = 1;
70
71   memset(&swf,0x00,sizeof(SWF));
72   
73   swf.fileVersion       = 4;
74   swf.frameRate         = 0x1000;
75   swf.movieSize.xmax    = width*20;
76   swf.movieSize.ymax    = height*20;
77
78
79   t = swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
80   
81         rgb.r = rgb.g = rgb.b = rgb.a = 0xff;
82         swf_SetRGB(t,&rgb);
83
84   t = swf_InsertTag(t,ST_DEFINESHAPE);
85
86         swf_ShapeNew(&s);
87         rgb.b = rgb.g = 0x00;
88         ls = swf_ShapeAddLineStyle(s,40,&rgb);
89
90         id_circle = GET_ID;
91         swf_SetU16(t,id_circle);
92
93         r.xmin = 0;
94         r.ymin = 0;
95         r.xmax = width;
96         r.ymax = height;
97         swf_SetRect(t,&r);
98
99         swf_SetShapeHeader(t,s);
100         swf_ShapeSetAll(t,s,0,0,ls,0,0);
101         swf_ShapeSetCircle(t,s,width/2,height/2,width/2,height/2);
102         swf_ShapeSetEnd(t);
103
104     // SPRITE #1 TIMELINE
105         
106     t = swf_InsertTag(t,ST_DEFINESPRITE);
107   
108         id_sprite = GET_ID;
109         swf_SetU16(t,id_sprite);
110         swf_SetU16(t,32); // frame count
111         
112         for (i=0;i<32;i++) // 32 frames
113         { MATRIX m;
114           swf_GetMatrix(NULL,&m);
115           m.tx = width*2+(int)((float)width*2*sin((float)i/16*3.14152));
116           m.ty = width*2+(int)((float)height*2*cos((float)i/16*3.14152));
117           t = swf_InsertTag(t,ST_PLACEOBJECT2);
118               swf_ObjectPlace(t,(i==0)?id_circle:0,1,&m,NULL,NULL);
119           t = swf_InsertTag(t,ST_SHOWFRAME);
120         }
121
122     t = swf_InsertTag(t,ST_END);
123
124     // SPRITE #2 TIMELINE
125
126     t = swf_InsertTag(t,ST_DEFINESPRITE);
127   
128         id_sprite2 = GET_ID;
129         swf_SetU16(t,id_sprite2);
130         swf_SetU16(t,80); // frame count
131         
132         for (i=0;i<80;i++) // 80 frames
133         { MATRIX m;
134           swf_GetMatrix(NULL,&m);
135           m.tx = width*4+(int)((float)width*4*sin((float)i/40*3.14152));
136           m.ty = width*4+(int)((float)height*4*cos((float)i/40*3.14152));
137           t = swf_InsertTag(t,ST_PLACEOBJECT2);
138               swf_ObjectPlace(t,(i==0)?id_sprite:0,1,&m,NULL,NULL);
139           m.sx *= -1;
140           m.tx += 4*width;
141           m.ty += 4*height;
142           t = swf_InsertTag(t,ST_PLACEOBJECT2);
143               swf_ObjectPlace(t,(i==0)?id_sprite:0,2,&m,NULL,NULL);
144           t = swf_InsertTag(t,ST_SHOWFRAME);
145         }
146
147   t = swf_InsertTag(t,ST_END);
148
149   // MAIN MOVIE TIMELINE
150
151   t = swf_InsertTag(t,ST_PLACEOBJECT2);
152
153         swf_ObjectPlace(t,id_sprite2,1,NULL,NULL,NULL);
154         
155   t = swf_InsertTag(t,ST_SHOWFRAME);    // just one frame
156
157   t = swf_InsertTag(t,ST_END);
158
159 //  swf_WriteCGI(&swf);
160
161   f = open("sprites.swf",O_RDWR|O_CREAT|O_TRUNC,0644);
162   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
163   close(f);
164
165   swf_FreeTags(&swf);
166
167 #ifdef __NT__
168   system("start ..\\sprites.swf");
169 #endif
170   
171   return(0);
172 }
173
174