Initialize libbullet git in 2.0_beta.
[platform/upstream/libbullet.git] / Extras / iff / iffCreateTest / main.cpp
1 /*
2 Test read and write IFF-85, Interchange Format File
3 Copyright (c) 2008 Erwin Coumans  http://bulletphysics.com
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose, 
8 including commercial applications, and to alter it and redistribute it freely, 
9 subject to the following restrictions:
10
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15
16 /*
17 * IFF Support routines for writing IFF-85 files.          12/02/85
18 * (IFF is Interchange Format File.)
19 * By Jerry Morrison and Steve Shaw, Electronic Arts.
20 * This software is in the public domain.
21
22 */
23
24 #include <stdio.h>
25
26 #include "iff.h"
27
28 IFFP MyReadICat(GroupContext *parent) 
29 {
30         printf("Found and skipped a CAT\n");
31         return 0;
32 }
33
34 IFFP MySkipGroup( GroupContext * )
35 {
36         printf("Found and skipped a LIST\n");
37         return 0;
38 }
39
40 typedef UBYTE Masking;          /* Choice of masking technique.*/
41 typedef UBYTE Compression;      /* Choice of compression algorithm applied to
42
43                                                                 /* A BitMapHeader is stored in a BMHD chunk. */
44 typedef struct {
45
46         UWORD w, h;                 /* raster width & height in pixels */
47
48         WORD  x, y;                 /* position for this image */
49         UBYTE nPlanes;              /* # source bitplanes */
50         Masking masking;            /* masking technique */
51         Compression compression;    /* compression algoithm */
52         UBYTE pad1;                 /* UNUSED.  For consistency, put 0 here.*/
53         UWORD transparentColor;     /* transparent "color number" */
54         UBYTE xAspect, yAspect;     /* aspect ratio, a rational number x/y */
55         WORD  pageWidth, pageHeight;  /* source "page" size in pixels */
56         
57 } BitMapHeader;
58
59
60 #define bufSz 512
61 BYTE bodyBuffer[bufSz];
62
63 static void btSwap(char* a, char* b)
64 {
65         char tmp = *a;
66         *a = *b;
67         *b = tmp;
68 };
69
70 #define ID_ILBM MakeID('I','L','B','M')
71 #define ID_BMHD MakeID('B','M','H','D')
72 #define ID_CMAP MakeID('C','M','A','P')
73 #define ID_BODY MakeID('B','O','D','Y')
74
75 #define ID_DYNAWORLD MakeID('B','T','D','W')
76 #define ID_RIGIDBODY MakeID('B','T','R','B')
77 #define ID_SID MakeID('S','I','D',' ')
78 #define ID_MASS MakeID('M','A','S','S')
79 #define ID_SHAPE MakeID('S','H','A','P')
80
81
82 #define ID_COLOBJ MakeID('C','O','B','J')
83 #define ID_CUBE MakeID('C','U','B','E')
84 #define ID_DIMENSIONS MakeID('D','I','M','E')
85
86
87 IFFP MyProcessGroup(GroupContext *parent)
88 {
89         /*compilerBug register*/ IFFP iffp;
90         GroupContext rigidbodyContext;
91
92         BitMapHeader bmHeader;
93         bool foundBMHD = false;
94
95
96         if (parent->subtype != ID_ILBM)
97                 return(IFF_OKAY); /* just continue scaning the file */
98
99         iffp = OpenRGroup(parent, &rigidbodyContext);
100         CheckIFFP();
101
102         do {
103                 iffp = GetFChunkHdr(&rigidbodyContext);
104                 if (iffp == ID_BMHD) {
105                         printf("found ID_BMHD\n");
106                         foundBMHD = true;
107
108                         iffp = IFFReadBytes(&rigidbodyContext, (BYTE *)&bmHeader, (long)sizeof(BitMapHeader));
109                         //do endian swap
110                         bmHeader.w = endianSwap16(bmHeader.w);
111                         bmHeader.h = endianSwap16(bmHeader.h);
112                         bmHeader.pageWidth = endianSwap16(bmHeader.pageWidth);
113                         bmHeader.pageHeight = endianSwap16(bmHeader.pageHeight);
114                 }
115
116                 else if (iffp == ID_CMAP) {
117                         printf("found ID_CMAP\n");
118
119                         //      ilbmFrame.nColorRegs = maxColorReg;  /* we have room for this many */
120                         //       iffp = GetCMAP(
121                         //        &rigidbodyContext, (WORD *)&ilbmFrame.colorMap, &ilbmFrame.nColorRegs);
122                 }
123
124                 else if (iffp == ID_BODY) 
125                 {
126                         printf("found ID_BODY\n");
127                         if (!foundBMHD)
128                                 return BAD_FORM;
129                         //     if (!ilbmFrame.foundBMHD)  return(BAD_FORM);   /* No BMHD chunk! */
130
131                         int moreBytes = ChunkMoreBytes(&rigidbodyContext);
132                         while (moreBytes>0)
133                         {
134                                 int curRead = moreBytes > bufSz? bufSz : moreBytes;
135                                 //read
136                                 iffp = IFFReadBytes(&rigidbodyContext, bodyBuffer, curRead);
137                                 moreBytes -= curRead;
138
139                         }
140                         printf("remaining=%d\n",moreBytes);
141                         if (iffp == IFF_OKAY) 
142                                 iffp = IFF_DONE;      /* Eureka */
143
144
145
146                         //         nPlanes = MIN(ilbmFrame.bmHdr.nPlanes, EXDepth);
147                 }
148
149                 else if (iffp == END_MARK)
150                         iffp = BAD_FORM;
151
152         } while (iffp >= IFF_OKAY);  /* loop if valid ID of ignored chunk or a
153                                                                  * subroutine returned IFF_OKAY (no errors).*/
154
155         if (iffp != IFF_DONE)  return(iffp);
156
157         /* If we get this far, there were no errors. */
158         CloseRGroup(&rigidbodyContext);
159         return(iffp);
160 }
161
162
163
164 #define CkErr(expression)  {if (ifferr == IFF_OKAY) ifferr = (expression);}
165
166
167 int main(int argc, char* argv[])
168 {
169         FILE* file = 0;
170
171         {
172                 //Create and write an IFF file from scratch
173                 file = fopen("test.iff","wb");
174
175                 GroupContext fileContext;
176                 GroupContext catContext;
177
178                 IFFP ifferr=0;
179
180                 ifferr = OpenWIFF(file, &fileContext, szNotYetKnown) ;
181                 //ifferr = StartWGroup(&fileContext, CAT, szNotYetKnown, ID_DYNAWORLD, &catContext);
182                 ifferr = StartWGroup(&fileContext, LIST, szNotYetKnown, ID_DYNAWORLD, &catContext);
183
184                 {
185                         GroupContext rigidbodyPropContext;
186                         ifferr = StartWGroup(&catContext, PROP, szNotYetKnown, ID_MASS, &rigidbodyPropContext);
187                         float mass = 0.1f;
188                         PutCk(&rigidbodyPropContext, ID_MASS, 4,(char*)&mass);
189                         ifferr = EndWGroup(&rigidbodyPropContext) ;
190
191                         for (int i=0;i<3;i++)
192                         {
193                                 GroupContext rigidbodyContext;
194                                 ifferr = StartWGroup(&catContext, FORM, szNotYetKnown, ID_RIGIDBODY, &rigidbodyContext);
195                                 char sidbuffer[]="rb1";
196                                 
197                                 float dimensions[3] = {2,2,2};
198                                 PutCk(&rigidbodyContext, ID_SID, 3,sidbuffer);
199                                 {
200                                         GroupContext shapeContext;
201                                         ifferr = StartWGroup(&rigidbodyContext, FORM, szNotYetKnown, ID_SHAPE, &shapeContext);
202                                         PutCk(&shapeContext, ID_CUBE, 4,(char*)&mass);
203                                         PutCk(&shapeContext, ID_DIMENSIONS, sizeof(dimensions),(char*)&dimensions);
204                                         ifferr = EndWGroup(&shapeContext) ;
205                                 }
206                                 ifferr = EndWGroup(&rigidbodyContext) ;
207                         }
208                         
209                 }
210                 ifferr = EndWGroup(&catContext) ;
211                 ifferr = CloseWGroup(&fileContext);
212
213                 fclose(file);
214         }
215
216         {
217                 //show a very simple way to skim through an ILBM or general IFF file
218                 //for more verbose feedback, use iffcheck.c
219                 IFFP result;
220                 //file = fopen("pe_3000_fall.iff","rb");
221                 file = fopen("test.iff","rb");
222
223                 ClientFrame clientFrame;
224
225                 clientFrame.getList = MySkipGroup;
226                 clientFrame.getProp = MySkipGroup;
227                 clientFrame.getForm = MyProcessGroup;
228                 clientFrame.getCat  = MyReadICat ;
229
230                 result = ReadIFF(file,&clientFrame);
231                 fclose(file);
232         }
233
234
235         return 0;
236 }