* updated examples to new rfxswf name conventions
[swftools.git] / lib / example / transtest.c
1 /* transtest.c
2
3    Example for transforming a textured triangle
4    
5    Part of the swftools package.
6
7    Copyright (c) 2001 Rainer Böhme <rfxswf@reflex-studio.de>
8  
9    This file is distributed under the GPL, see file COPYING for details 
10
11 */
12
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include <math.h>
16 #include "../rfxswf.h"
17
18 #define QUALITY 80
19 #define ID_BITMAP       2004
20 #define ID_SHAPE        2005
21
22   
23 int main (int argc,char ** argv)
24 { SWF swf;
25   LPTAG t;
26   RGBA rgb;
27   SRECT r;
28   LPSHAPE s;
29   S32 width = 800,height = 800;
30   int fs,ls; // line & fillstyle
31   LPJPEGBITS jpeg;
32   MATRIX m;  
33   
34   int f,i,j,frame;
35   
36   memset(&swf,0x00,sizeof(SWF));
37
38   swf.fileVersion    = 4;
39   swf.frameRate      = 0x4000;
40   swf.movieSize.xmax = 4*width;
41   swf.movieSize.ymax = 4*height;
42
43   swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
44   t = swf.firstTag;
45
46         rgb.r = 0xff;
47         rgb.g = 0xff;
48         rgb.b = 0xff;
49         swf_SetRGB(t,&rgb);
50
51     t = swf_InsertTag(t,ST_DEFINEBITSJPEG2);
52
53          swf_SetU16(t,ID_BITMAP);
54          
55          if (FAILED(swf_SetJPEGBits(t,"texture.jpg",QUALITY)))
56          { fprintf(stderr,"Error: texture.jpg (256x256) not found in work directory.\n");
57            exit(1);
58          }
59          
60     t = swf_InsertTag(t,ST_DEFINESHAPE);
61     
62          swf_ShapeNew(&s);
63          rgb.b = rgb.g = rgb.r = 0x00;
64          ls = 0;
65          rgb.b = 0xff;
66   
67          fs = swf_ShapeAddBitmapFillStyle(s,NULL,ID_BITMAP,0);
68     
69          swf_SetU16(t,ID_SHAPE);    // ID   
70          
71          r.xmin = 0;
72          r.ymin = 0;
73          r.xmax = 2*width;
74          r.ymax = 2*height;
75          
76          swf_SetRect(t,&r);
77
78          swf_SetShapeHeader(t,s);
79          
80          swf_ShapeSetAll(t,s,0,0,ls,fs,0);
81          swf_ShapeSetLine(t,s,width,0);
82          swf_ShapeSetLine(t,s,-width,height);
83          swf_ShapeSetLine(t,s,0,-height);
84          swf_ShapeSetEnd(t);
85
86          swf_ShapeFree(s);
87     
88   for (frame=0;frame<64;frame++)
89   {
90
91     /* Test procedure for swf_MatrixJoin
92
93     MATRIX m1,m2;
94
95     // set m1 to left rotation
96      
97     m1.sy = m1.sx = (int)(cos(((float)(frame))/32*3.141)*0x10000);
98     m1.r0 = (int)(sin(((float)(frame))/32*3.141)*0x10000);
99     m1.r1 = -m1.r0;
100     m1.tx = width+frame*4; m1.ty = height;
101
102     // set m2 to right rotation
103
104     m2.sy = m2.sx = (int)(cos(((float)(64-frame))/32*3.141)*0x10000);
105     m2.r0 = (int)(sin(((float)(64-frame))/32*3.141)*0x10000);
106     m2.r1 = -m2.r0;
107     m2.tx = width; m2.ty = height;
108
109     // joining m1 and m2 should lead to no transformation
110
111     swf_MatrixJoin(&m,&m1,&m2);
112
113     */
114
115     int dx0 = width;    // constants of shape 
116     int dy0 = width;
117
118     int px0 = 2*width;  // destination of mapping
119     int py0 = 2*width;
120
121     int px1 = 3*width;
122     int py1 = 2*width-frame*4;
123
124     int px2 = 2*width-frame*8;
125     int py2 = 3*width;
126
127     swf_MatrixMapTriangle(&m,dx0,dy0,px0,py0,px1,py1,px2,py2);
128
129     t = swf_InsertTag(t,ST_PLACEOBJECT2);
130
131         if (!frame)
132          swf_ObjectPlace(t,ID_SHAPE,1,&m,NULL,NULL);
133         else
134           swf_ObjectMove(t,1,&m,NULL);
135  
136     t = swf_InsertTag(t,ST_SHOWFRAME);
137   }
138   
139   
140   t = swf_InsertTag(t,ST_END);
141   
142 //  swf_WriteCGI(&swf);
143
144   f = open("transtest.swf",O_RDWR|O_CREAT|O_TRUNC,0644);
145   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
146   close(f);
147
148   swf_FreeTags(&swf);
149
150 #ifdef __NT__
151   system("start ..\\transtest.swf");
152 #endif
153   
154   return 0;
155 }
156
157