summaryrefslogtreecommitdiff
path: root/lib/example/box.c
blob: 3fbedcb7816ea454319c7b97e3fc5fb95100c231 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* box.c 

   Example for drawing 3D grind objects
   
   Part of the swftools package.

   Copyright (c) 2000, 2001 Rainer Böhme <rfxswf@reflex-studio.de>
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


#include <stdio.h>
#include <fcntl.h>
#include <math.h>
#include "../rfxswf.h"

#define BANNER_TEXT     "reflex" // banner is disabled due to new font handling
#define ID_FONT         2000
#define ID_BANNER       2001
#define ID_HIGHLIGHT    2002
#define ID_BUTTON       2003

#define a 200
int sX[] = { a,-a, a,-a, a,-a, a,-a};
int sY[] = { a, a,-a,-a, a, a,-a,-a};
int sZ[] = { a, a, a, a,-a,-a,-a,-a};
#undef a
  
int dX[8];
int dY[8];

int sin_[512],cos_[512];

void calcTables()
{ int i;
  double d;
  for (i=0;i<512;i++)
  { d = ((double)i)/128*3.14159;
    sin_[i] = (int)(sin(d)*256);
    cos_[i] = (int)(cos(d)*256);
  }
}

void ShapeSquare(TAG* t,SHAPE* s,int p1,int p2,int p3,int p4,int dx,int dy)
{ 
  // Hidden-Line-Check
    if (((dX[p2]-dX[p1])*(dY[p3]-dY[p1])-(dX[p3]-dX[p1])*(dY[p2]-dY[p1]))<0) return;

  swf_ShapeSetMove(t,s,dX[p1]+dx,dY[p1]+dy);
  swf_ShapeSetLine(t,s,dX[p2]-dX[p1],dY[p2]-dY[p1]);
  swf_ShapeSetLine(t,s,dX[p3]-dX[p2],dY[p3]-dY[p2]);
  swf_ShapeSetLine(t,s,dX[p4]-dX[p3],dY[p4]-dY[p3]);
  swf_ShapeSetLine(t,s,dX[p1]-dX[p4],dY[p1]-dY[p4]);
}


void mapBox(int xw,int yw,int zw)
// simple trigonometry without using transformation matrices
{ int i;
  int x1,y1,z1,x2,y2,z2,x3,y3,z3;
  int y,x,z;
  
  xw &= 255;
  yw &= 255;
  zw &= 255;

  for (i=0;i<8;i++)
  { x = sX[i];
    y = sY[i];
    z = sZ[i];
                  
    y1 = ( y*cos_[xw]- z*sin_[xw])>>8;
    z1 = ( y*sin_[xw]+ z*cos_[xw])>>8;
    x1 = x;

    x2 = (x1*cos_[yw]+z1*sin_[yw])>>8;
    y2 = y1;

    x3 = (x2*cos_[zw]-y2*sin_[zw])>>8;
    y3 = (x2*sin_[zw]+y2*cos_[zw])>>8;
    z3 = (z1*cos_[yw]-x1*sin_[yw])>>8;

    dX[i] = x3*4000/(z3+950);
    dY[i] = y3*4000/(z3+950);
        
  }
}
               
int main (int argc,char ** argv)
{ SWF swf;
  TAG* t;
  RGBA rgb;
  SRECT r;
  SHAPE* s;
  S32 width = 800,height = 800;
  U8 gbits,abits;
  CXFORM cx1,cx2;
  MATRIX m;
  
  int f,i,j,frame;
  
/*  f = open("Arial.efont",O_RDONLY);
  if (f>=0)
  { if (FAILED(swf_FontImport(f,&font)))
    { fprintf(stderr,"Font import failed\n");
      close(f);
      return -1;
    }
  }
  else
  { fprintf(stderr,"Font not found\n");
    return -1;
  }
  close(f);

  swf_FontSetID(font,ID_FONT);
  swf_FontInitUsage(&use);
  swf_FontUse(&use,BANNER_TEXT);
  swf_FontReduce(font,&use);
*/
  
  calcTables();
  
  memset(&swf,0x00,sizeof(SWF));

  swf.fileVersion    = 4;
  swf.frameRate      = 0x4000;
  swf.movieSize.xmax = 4*width;
  swf.movieSize.ymax = 4*height;

  swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
  t = swf.firstTag;

        rgb.r = 0xff;
        rgb.g = 0xff;
        rgb.b = 0xff;
        swf_SetRGB(t,&rgb);
        
/* This part can't be used because of missing fonts. Nevertheless it shows, how
   rfxswf handles fonts, fontusages (in order to include only the used glyphs
   into the output swf) and how to make a simple button


  t = swf_InsertTag(t,ST_DEFINEFONT);

        swf_FontSetDefine(t,font);

  t = swf_InsertTag(t,ST_DEFINEFONTINFO);

        swf_FontSetInfo(t,font);

  t = swf_InsertTag(t,ST_DEFINETEXT);

        swf_SetU16(t,ID_BANNER);            // ID
        
        r.xmin = 0;
        r.ymin = 0;
        r.xmax = 400;
        r.ymax = 400;
        
        swf_SetRect(t,&r);

        swf_SetMatrix(t,NULL);

        swf_TextCountBits(font,BANNER_TEXT,80,&gbits,&abits);
        
        swf_SetU8(t,gbits);
        swf_SetU8(t,abits);

        rgb.r = 0xc0;
        rgb.g = 0xc0;
        rgb.b = 0xc0;

        swf_TextSetInfoRecord(t,font,height/4,&rgb,0,200);
        swf_TextSetCharRecord(t,font,BANNER_TEXT,80,gbits,abits);

        swf_SetU8(t,0);
        
  t = swf_InsertTag(t,ST_DEFINETEXT);

        swf_SetU16(t,ID_HIGHLIGHT);            // ID
        
        r.xmin = 0;
        r.ymin = 0;
        r.xmax = 800;
        r.ymax = 400;
        
        swf_SetRect(t,&r);

        swf_SetMatrix(t,NULL);

        swf_TextCountBits(font,BANNER_TEXT,80,&gbits,&abits);
        
        swf_SetU8(t,gbits);
        swf_SetU8(t,abits);

        rgb.r = 0x20;
        rgb.g = 0x20;
        rgb.b = 0x20;

        swf_TextSetInfoRecord(t,font,height/4,&rgb,0,200);
        swf_TextSetCharRecord(t,font,BANNER_TEXT,80,gbits,abits);

        swf_SetU8(t,0);
        
    t = swf_InsertTag(t,ST_DEFINEBUTTON);
    
        swf_GetMatrix(NULL,&m);
        
        m.tx = 3*width;
        m.ty = 7*height/2;
        
        swf_SetU16(t,ID_BUTTON); // ID
        swf_ButtonSetRecord(t,BS_UP,ID_BANNER,1,&m,NULL);
        swf_ButtonSetRecord(t,BS_DOWN|BS_HIT|BS_OVER,ID_HIGHLIGHT,1,&m,NULL);
        swf_SetU8(t,0); // End of Button Records
        swf_SetU8(t,0); // End of Action Records
    
      
    t = swf_InsertTag(t,ST_PLACEOBJECT2);

         swf_ObjectPlace(t,ID_BUTTON,1,NULL,NULL,NULL);
         
*/
        
  for (frame=0;frame<256;frame+=2)
  { int id = frame +1;
    
    t = swf_InsertTag(t,ST_DEFINESHAPE);

        swf_ShapeNew(&s);
        rgb.r = rgb.g = 0x00; rgb.b = 0xff;
        j = swf_ShapeAddLineStyle(s,40,&rgb);

        swf_SetU16(t,id);  // ID

        r.xmin = 0;
        r.ymin = 0;
        r.xmax = 4*width;
        r.ymax = 4*height;
        
        swf_SetRect(t,&r);
        swf_SetShapeHeader(t,s);
        swf_ShapeSetAll(t,s,0,0,j,0,0);
        
        mapBox(frame,frame,frame>>1);

        ShapeSquare(t,s,0,2,3,1,2*width,2*height);
        ShapeSquare(t,s,4,5,7,6,2*width,2*height);
        ShapeSquare(t,s,0,4,6,2,2*width,2*height);
        ShapeSquare(t,s,1,3,7,5,2*width,2*height);
        ShapeSquare(t,s,0,1,5,4,2*width,2*height);
        ShapeSquare(t,s,2,6,7,3,2*width,2*height);
        
        swf_ShapeSetEnd(t);
        swf_ShapeFree(s);
  }
  
  swf_GetCXForm(NULL,&cx1,1);  // get default ColorTransforms
  swf_GetCXForm(NULL,&cx2,1);
    
  cx1.r1 = cx1.g1 = 0x80;  // set alpha for blur effect
  cx2.r1 = cx2.g1 = 0xc0;     
    
  for (frame=0;frame<256;frame+=2)
  { int id = frame +1;
    int id2 = ((frame-2)&255)+1;
    int id3 = ((frame-4)&255)+1;

    if (frame)
    { t = swf_InsertTag(t,ST_REMOVEOBJECT2); swf_SetU16(t,2); // depth
      t = swf_InsertTag(t,ST_REMOVEOBJECT2); swf_SetU16(t,3); // depth
      t = swf_InsertTag(t,ST_REMOVEOBJECT2); swf_SetU16(t,4); // depth
    }

    t = swf_InsertTag(t,ST_PLACEOBJECT2);

        swf_ObjectPlace(t,id,4,NULL,NULL,NULL);
        
    t = swf_InsertTag(t,ST_PLACEOBJECT2);

        swf_ObjectPlace(t,id2,3,NULL,&cx1,NULL);
  
    t = swf_InsertTag(t,ST_PLACEOBJECT2);

        swf_ObjectPlace(t,id3,2,NULL,&cx2,NULL);
  
    t = swf_InsertTag(t,ST_SHOWFRAME);
  }
  
  t = swf_InsertTag(t,ST_END);
 
//  swf_WriteCGI(&swf);
 

  f = open("box.swf",O_RDWR|O_CREAT|O_TRUNC|O_BINARY,0644);
  if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
  close(f);

  swf_FreeTags(&swf);

#ifdef __NT__
  system("start ..\\box.swf");
#endif
  
  return 0;
}