more eol-style
[xonotic/netradiant.git] / contrib / bobtoolz / bsploader.cpp
1 #include "StdAfx.h"
2 #include "./dialogs/dialogs-gtk.h"
3 #include "bsploader.h"
4 #include "../../libs/cmdlib.h"
5
6 int                     numnodes;
7 int                     numplanes;
8 int                     numleafs;
9 int                     numleafsurfaces;
10 int                     numVisBytes;
11 int                     numDrawVerts;
12 int                     numDrawSurfaces;
13 int                     numbrushes;
14 int                     numbrushsides;
15 int                     numleafbrushes;
16
17 byte                        *visBytes =                   NULL;
18 dnode_t                   *dnodes =                         NULL;
19 dplane_t                  *dplanes =                      NULL;
20 dleaf_t                   *dleafs =                         NULL;
21 qdrawVert_t       *drawVerts =            NULL;
22 dsurface_t        *drawSurfaces =               NULL;
23 int                                 *dleafsurfaces =    NULL;
24 dbrush_t                  *dbrushes =                     NULL;
25 dbrushside_t    *dbrushsides =          NULL;
26 int                                 *dleafbrushes =             NULL;
27
28 #define BSP_IDENT       (('P'<<24)+('S'<<16)+('B'<<8)+'I')
29 #define Q3_BSP_VERSION                  46
30 #define WOLF_BSP_VERSION                        47
31
32 /*
33 ================
34 FileLength
35 ================
36 */
37 int FileLength (FILE *f)
38 {
39         int             pos;
40         int             end;
41
42         pos = ftell (f);
43         fseek (f, 0, SEEK_END);
44         end = ftell (f);
45         fseek (f, pos, SEEK_SET);
46
47         return end;
48 }
49
50 /*
51 ==============
52 LoadFile
53 ==============
54 */
55 qboolean    LoadFile( const char *filename, byte **bufferptr)
56 {
57         FILE    *f;
58         int             length;
59         byte    *buffer;
60
61         f = fopen(filename, "rb");
62         if(!f)
63                 return false;
64
65         length = FileLength (f);
66         buffer = new byte[length+1];
67         buffer[length] = 0;
68         fread(buffer, 1, length, f);
69         fclose (f);
70
71         *bufferptr = buffer;
72         return true;
73 }
74
75 /*int    LittleLong (int l)
76 {
77         return l;
78 }
79
80 float   LittleFloat (float l)
81 {
82         return l;
83 }*/
84
85 /*
86 =============
87 SwapBlock
88
89 If all values are 32 bits, this can be used to swap everything
90 =============
91 */
92 void SwapBlock( int *block, int sizeOfBlock ) {
93         int             i;
94
95         sizeOfBlock >>= 2;
96         for ( i = 0 ; i < sizeOfBlock ; i++ ) {
97                 block[i] = LittleLong( block[i] );
98         }
99 }
100
101 /*
102 =============
103 SwapBSPFile
104
105 Byte swaps all data in a bsp file.
106 =============
107 */
108 void SwapBSPFile( void ) {
109         int                             i;
110         
111         // models       
112 //      SwapBlock( (int *)dmodels, nummodels * sizeof( dmodels[0] ) );
113
114         // shaders (don't swap the name)
115 //      for ( i = 0 ; i < numShaders ; i++ ) {
116 //              dshaders[i].contentFlags = LittleLong( dshaders[i].contentFlags );
117 //              dshaders[i].surfaceFlags = LittleLong( dshaders[i].surfaceFlags );
118 //      }
119
120         // planes
121         SwapBlock( (int *)dplanes, numplanes * sizeof( dplanes[0] ) );
122         
123         // nodes
124         SwapBlock( (int *)dnodes, numnodes * sizeof( dnodes[0] ) );
125
126         // leafs
127         SwapBlock( (int *)dleafs, numleafs * sizeof( dleafs[0] ) );
128
129         // leaffaces
130         SwapBlock( (int *)dleafsurfaces, numleafsurfaces * sizeof( dleafsurfaces[0] ) );
131
132         // leafbrushes
133         SwapBlock( (int *)dleafbrushes, numleafbrushes * sizeof( dleafbrushes[0] ) );
134
135         // brushes
136         SwapBlock( (int *)dbrushes, numbrushes * sizeof( dbrushes[0] ) );
137
138         // brushsides
139         SwapBlock( (int *)dbrushsides, numbrushsides * sizeof( dbrushsides[0] ) );
140
141         // vis
142         ((int *)&visBytes)[0] = LittleLong( ((int *)&visBytes)[0] );
143         ((int *)&visBytes)[1] = LittleLong( ((int *)&visBytes)[1] );
144
145         // drawverts (don't swap colors )
146         for ( i = 0 ; i < numDrawVerts ; i++ ) {
147                 drawVerts[i].lightmap[0] = LittleFloat( drawVerts[i].lightmap[0] );
148                 drawVerts[i].lightmap[1] = LittleFloat( drawVerts[i].lightmap[1] );
149                 drawVerts[i].st[0] = LittleFloat( drawVerts[i].st[0] );
150                 drawVerts[i].st[1] = LittleFloat( drawVerts[i].st[1] );
151                 drawVerts[i].xyz[0] = LittleFloat( drawVerts[i].xyz[0] );
152                 drawVerts[i].xyz[1] = LittleFloat( drawVerts[i].xyz[1] );
153                 drawVerts[i].xyz[2] = LittleFloat( drawVerts[i].xyz[2] );
154                 drawVerts[i].normal[0] = LittleFloat( drawVerts[i].normal[0] );
155                 drawVerts[i].normal[1] = LittleFloat( drawVerts[i].normal[1] );
156                 drawVerts[i].normal[2] = LittleFloat( drawVerts[i].normal[2] );
157         }
158
159         // drawindexes
160 //      SwapBlock( (int *)drawIndexes, numDrawIndexes * sizeof( drawIndexes[0] ) );
161
162         // drawsurfs
163         SwapBlock( (int *)drawSurfaces, numDrawSurfaces * sizeof( drawSurfaces[0] ) );
164
165         // fogs
166 //      for ( i = 0 ; i < numFogs ; i++ ) {
167 //              dfogs[i].brushNum = LittleLong( dfogs[i].brushNum );
168 //              dfogs[i].visibleSide = LittleLong( dfogs[i].visibleSide );
169 //      }
170 }
171
172 /*
173 =============
174 CopyLump
175 =============
176 */
177 int CopyLump( dheader_t *header, int lump, void **dest, int size ) {
178         int             length, ofs;
179
180         length = header->lumps[lump].filelen;
181         ofs = header->lumps[lump].fileofs;
182         
183         if(length == 0)
184                 return 0;
185         
186         *dest = new byte[length];
187         memcpy( *dest, (byte *)header + ofs, length );
188
189         return length / size;
190 }
191
192 /*
193 =============
194 LoadBSPFile
195 =============
196 */
197 qboolean        LoadBSPFile( const char *filename ) {
198         dheader_t       *header;
199
200         // load the file header
201         if(!LoadFile (filename, (byte **)&header))
202                 return false;
203
204         // swap the header
205         SwapBlock( (int *)header, sizeof(*header) );
206
207         if ( header->ident != BSP_IDENT ) {
208                 DoMessageBox( "Cant find a valid IBSP file", "Error", MB_OK);
209                 return false;
210         }
211         if ( (header->version != Q3_BSP_VERSION) &&
212                                                                                                                                                         (header->version != WOLF_BSP_VERSION) ) {
213                 DoMessageBox( "File is incorrect version", "Error", MB_OK);
214                 return false;
215         }
216
217         numbrushsides =         CopyLump( header, LUMP_BRUSHES,                 (void**)&dbrushsides,   sizeof(dbrushside_t) );
218         numbrushes =            CopyLump( header, LUMP_BRUSHES,                 (void**)&dbrushes,              sizeof(dbrush_t) );
219         numplanes =                     CopyLump( header, LUMP_PLANES,                  (void**)&dplanes,               sizeof(dplane_t) );
220         numleafs =                      CopyLump( header, LUMP_LEAFS,                   (void**)&dleafs,                sizeof(dleaf_t) );
221         numnodes =                      CopyLump( header, LUMP_NODES,                   (void**)&dnodes,                sizeof(dnode_t) );
222         numDrawVerts =          CopyLump( header, LUMP_DRAWVERTS,               (void**)&drawVerts,             sizeof(qdrawVert_t) );
223         numDrawSurfaces =       CopyLump( header, LUMP_SURFACES,                (void**)&drawSurfaces,  sizeof(dsurface_t) );
224         numleafsurfaces =       CopyLump( header, LUMP_LEAFSURFACES,    (void**)&dleafsurfaces, sizeof(int) );
225         numVisBytes =           CopyLump( header, LUMP_VISIBILITY,              (void**)&visBytes,              1 );
226         numleafbrushes =        CopyLump( header, LUMP_LEAFBRUSHES,             (void**)&dleafbrushes,  sizeof(int) );
227
228         delete header;          // everything has been copied out
229                 
230         // swap everything
231         SwapBSPFile();
232
233         return true;
234 }
235
236 void FreeBSPData()
237 {
238         if(visBytes)
239                 delete visBytes;
240         if(dnodes)
241                 delete dnodes;
242         if(dplanes)
243                 delete dplanes;
244         if(dleafs)
245                 delete dleafs;
246         if(drawVerts)
247                 delete drawVerts;
248         if(drawSurfaces)
249                 delete drawSurfaces;
250         if(dleafsurfaces)
251                 delete dleafsurfaces;
252         if(dleafbrushes)
253                 delete dleafbrushes;
254         if(dbrushes)
255                 delete dbrushes;
256         if(dbrushsides)
257                 delete dbrushsides;
258 }