60378568faf382b41ad5b705f8855e8230ced0aa
[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 file is distributed under the GPL, see file COPYING for details 
22
23 */
24
25 #include <stdio.h>
26 #include <fcntl.h>
27 #include <math.h>
28 #include "../rfxswf.h"
29
30
31 RGBA rgba(U8 r,U8 g,U8 b,U8 a)
32 { RGBA c; c.r = r; c.g = g; c.b = b; c.a = a;
33   return c;
34 }
35
36 U16 last_id;
37 U16 last_depth;
38
39 #define GET_ID (last_id++)
40 #define GET_DEPTH (last_depth++)
41
42 int main(int argc, char ** argv)
43 { SWF swf;
44   RGBA rgb;
45
46   LPTAG t;
47   LPSHAPE s;
48   SRECT r;
49
50   U16 id_circle;
51   U16 id_sprite;
52   U16 id_sprite2;
53
54   int ls,fs,i,j,f;
55
56   U32 width  = 400;
57   U32 height = 400;
58
59   last_id = last_depth = 1;
60
61   memset(&swf,0x00,sizeof(SWF));
62   
63   swf.fileVersion       = 4;
64   swf.frameRate         = 0x1000;
65   swf.movieSize.xmax    = width*20;
66   swf.movieSize.ymax    = height*20;
67
68
69   t = swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
70   
71         rgb.r = rgb.g = rgb.b = rgb.a = 0xff;
72         swf_SetRGB(t,&rgb);
73
74   t = swf_InsertTag(t,ST_DEFINESHAPE);
75
76         swf_ShapeNew(&s);
77         rgb.b = rgb.g = 0x00;
78         ls = swf_ShapeAddLineStyle(s,40,&rgb);
79
80         id_circle = GET_ID;
81         swf_SetU16(t,id_circle);
82
83         r.xmin = 0;
84         r.ymin = 0;
85         r.xmax = width;
86         r.ymax = height;
87         swf_SetRect(t,&r);
88
89         swf_SetShapeHeader(t,s);
90         swf_ShapeSetAll(t,s,0,0,ls,0,0);
91         swf_ShapeSetCircle(t,s,width/2,height/2,width/2,height/2);
92         swf_ShapeSetEnd(t);
93
94     // SPRITE #1 TIMELINE
95         
96     t = swf_InsertTag(t,ST_DEFINESPRITE);
97   
98         id_sprite = GET_ID;
99         swf_SetU16(t,id_sprite);
100         swf_SetU16(t,32); // frame count
101         
102         for (i=0;i<32;i++) // 32 frames
103         { MATRIX m;
104           swf_GetMatrix(NULL,&m);
105           m.tx = width*2+(int)((float)width*2*sin((float)i/16*3.14152));
106           m.ty = width*2+(int)((float)height*2*cos((float)i/16*3.14152));
107           t = swf_InsertTag(t,ST_PLACEOBJECT2);
108               swf_ObjectPlace(t,(i==0)?id_circle:0,1,&m,NULL,NULL);
109           t = swf_InsertTag(t,ST_SHOWFRAME);
110         }
111
112     t = swf_InsertTag(t,ST_END);
113
114     // SPRITE #2 TIMELINE
115
116     t = swf_InsertTag(t,ST_DEFINESPRITE);
117   
118         id_sprite2 = GET_ID;
119         swf_SetU16(t,id_sprite2);
120         swf_SetU16(t,80); // frame count
121         
122         for (i=0;i<80;i++) // 80 frames
123         { MATRIX m;
124           swf_GetMatrix(NULL,&m);
125           m.tx = width*4+(int)((float)width*4*sin((float)i/40*3.14152));
126           m.ty = width*4+(int)((float)height*4*cos((float)i/40*3.14152));
127           t = swf_InsertTag(t,ST_PLACEOBJECT2);
128               swf_ObjectPlace(t,(i==0)?id_sprite:0,1,&m,NULL,NULL);
129           m.sx *= -1;
130           m.tx += 4*width;
131           m.ty += 4*height;
132           t = swf_InsertTag(t,ST_PLACEOBJECT2);
133               swf_ObjectPlace(t,(i==0)?id_sprite:0,2,&m,NULL,NULL);
134           t = swf_InsertTag(t,ST_SHOWFRAME);
135         }
136
137   t = swf_InsertTag(t,ST_END);
138
139   // MAIN MOVIE TIMELINE
140
141   t = swf_InsertTag(t,ST_PLACEOBJECT2);
142
143         swf_ObjectPlace(t,id_sprite2,1,NULL,NULL,NULL);
144         
145   t = swf_InsertTag(t,ST_SHOWFRAME);    // just one frame
146
147   t = swf_InsertTag(t,ST_END);
148
149 //  swf_WriteCGI(&swf);
150
151   f = open("sprites.swf",O_RDWR|O_CREAT|O_TRUNC,0644);
152   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
153   close(f);
154
155   swf_FreeTags(&swf);
156
157 #ifdef __NT__
158   system("start ..\\sprites.swf");
159 #endif
160   
161   return(0);
162 }
163
164