]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/bobtoolz/bsploader.cpp
reformat code! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / contrib / bobtoolz / bsploader.cpp
1 #include <globaldefs.h>
2 #include "bsploader.h"
3 #include "dialogs/dialogs-gtk.h"
4 #include "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 const int BSP_IDENT = (('P' << 24) + ('S' << 16) + ('B' << 8) + 'I');
29 const int Q3_BSP_VERSION = 46;
30 const int 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 bool 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
66     length = FileLength(f);
67     buffer = new byte[length + 1];
68     buffer[length] = 0;
69     fread(buffer, 1, length, f);
70     fclose(f);
71
72     *bufferptr = buffer;
73     return true;
74 }
75
76 int LittleLong(int l)
77 {
78     if (GDEF_ARCH_ENDIAN_BIG) {
79         std::reverse(reinterpret_cast<unsigned char *>( &l ), reinterpret_cast<unsigned char *>( &l ) + sizeof(int));
80     }
81     return l;
82 }
83
84 float LittleFloat(float l)
85 {
86     if (GDEF_ARCH_ENDIAN_BIG) {
87         std::reverse(reinterpret_cast<unsigned char *>( &l ), reinterpret_cast<unsigned char *>( &l ) + sizeof(float));
88     }
89     return l;
90 }
91
92 /*
93    =============
94    SwapBlock
95
96    If all values are 32 bits, this can be used to swap everything
97    =============
98  */
99 void SwapBlock(int *block, int sizeOfBlock)
100 {
101     int i;
102
103     sizeOfBlock >>= 2;
104     for (i = 0; i < sizeOfBlock; i++) {
105         block[i] = LittleLong(block[i]);
106     }
107 }
108
109 /*
110    =============
111    SwapBSPFile
112
113    Byte swaps all data in a bsp file.
114    =============
115  */
116 void SwapBSPFile(void)
117 {
118     int i;
119
120     // models
121 //      SwapBlock( (int *)dmodels, nummodels * sizeof( dmodels[0] ) );
122
123     // shaders (don't swap the name)
124 //      for ( i = 0 ; i < numShaders ; i++ ) {
125 //              dshaders[i].contentFlags = LittleLong( dshaders[i].contentFlags );
126 //              dshaders[i].surfaceFlags = LittleLong( dshaders[i].surfaceFlags );
127 //      }
128
129     // planes
130     SwapBlock((int *) dplanes, numplanes * sizeof(dplanes[0]));
131
132     // nodes
133     SwapBlock((int *) dnodes, numnodes * sizeof(dnodes[0]));
134
135     // leafs
136     SwapBlock((int *) dleafs, numleafs * sizeof(dleafs[0]));
137
138     // leaffaces
139     SwapBlock((int *) dleafsurfaces, numleafsurfaces * sizeof(dleafsurfaces[0]));
140
141     // leafbrushes
142     SwapBlock((int *) dleafbrushes, numleafbrushes * sizeof(dleafbrushes[0]));
143
144     // brushes
145     SwapBlock((int *) dbrushes, numbrushes * sizeof(dbrushes[0]));
146
147     // brushsides
148     SwapBlock((int *) dbrushsides, numbrushsides * sizeof(dbrushsides[0]));
149
150     // vis
151     ((int *) &visBytes)[0] = LittleLong(((int *) &visBytes)[0]);
152     ((int *) &visBytes)[1] = LittleLong(((int *) &visBytes)[1]);
153
154     // drawverts (don't swap colors )
155     for (i = 0; i < numDrawVerts; i++) {
156         drawVerts[i].lightmap[0] = LittleFloat(drawVerts[i].lightmap[0]);
157         drawVerts[i].lightmap[1] = LittleFloat(drawVerts[i].lightmap[1]);
158         drawVerts[i].st[0] = LittleFloat(drawVerts[i].st[0]);
159         drawVerts[i].st[1] = LittleFloat(drawVerts[i].st[1]);
160         drawVerts[i].xyz[0] = LittleFloat(drawVerts[i].xyz[0]);
161         drawVerts[i].xyz[1] = LittleFloat(drawVerts[i].xyz[1]);
162         drawVerts[i].xyz[2] = LittleFloat(drawVerts[i].xyz[2]);
163         drawVerts[i].normal[0] = LittleFloat(drawVerts[i].normal[0]);
164         drawVerts[i].normal[1] = LittleFloat(drawVerts[i].normal[1]);
165         drawVerts[i].normal[2] = LittleFloat(drawVerts[i].normal[2]);
166     }
167
168     // drawindexes
169 //      SwapBlock( (int *)drawIndexes, numDrawIndexes * sizeof( drawIndexes[0] ) );
170
171     // drawsurfs
172     SwapBlock((int *) drawSurfaces, numDrawSurfaces * sizeof(drawSurfaces[0]));
173
174     // fogs
175 //      for ( i = 0 ; i < numFogs ; i++ ) {
176 //              dfogs[i].brushNum = LittleLong( dfogs[i].brushNum );
177 //              dfogs[i].visibleSide = LittleLong( dfogs[i].visibleSide );
178 //      }
179 }
180
181 /*
182    =============
183    CopyLump
184    =============
185  */
186 int CopyLump(dheader_t *header, int lump, void **dest, int size)
187 {
188     int length, ofs;
189
190     length = header->lumps[lump].filelen;
191     ofs = header->lumps[lump].fileofs;
192
193     if (length == 0) {
194         return 0;
195     }
196
197     *dest = new byte[length];
198     memcpy(*dest, (byte *) header + ofs, length);
199
200     return length / size;
201 }
202
203 /*
204    =============
205    LoadBSPFile
206    =============
207  */
208 bool LoadBSPFile(const char *filename)
209 {
210     dheader_t *header;
211
212     // load the file header
213     if (!LoadFile(filename, (byte **) &header)) {
214         return false;
215     }
216
217     // swap the header
218     SwapBlock((int *) header, sizeof(*header));
219
220     if (header->ident != BSP_IDENT) {
221         DoMessageBox("Cant find a valid IBSP file", "Error", eMB_OK);
222         return false;
223     }
224     if ((header->version != Q3_BSP_VERSION) &&
225         (header->version != WOLF_BSP_VERSION)) {
226         DoMessageBox("File is incorrect version", "Error", eMB_OK);
227         return false;
228     }
229
230     numbrushsides = CopyLump(header, LUMP_BRUSHES, (void **) &dbrushsides, sizeof(dbrushside_t));
231     numbrushes = CopyLump(header, LUMP_BRUSHES, (void **) &dbrushes, sizeof(dbrush_t));
232     numplanes = CopyLump(header, LUMP_PLANES, (void **) &dplanes, sizeof(dplane_t));
233     numleafs = CopyLump(header, LUMP_LEAFS, (void **) &dleafs, sizeof(dleaf_t));
234     numnodes = CopyLump(header, LUMP_NODES, (void **) &dnodes, sizeof(dnode_t));
235     numDrawVerts = CopyLump(header, LUMP_DRAWVERTS, (void **) &drawVerts, sizeof(qdrawVert_t));
236     numDrawSurfaces = CopyLump(header, LUMP_SURFACES, (void **) &drawSurfaces, sizeof(dsurface_t));
237     numleafsurfaces = CopyLump(header, LUMP_LEAFSURFACES, (void **) &dleafsurfaces, sizeof(int));
238     numVisBytes = CopyLump(header, LUMP_VISIBILITY, (void **) &visBytes, 1);
239     numleafbrushes = CopyLump(header, LUMP_LEAFBRUSHES, (void **) &dleafbrushes, sizeof(int));
240
241     delete header;      // everything has been copied out
242
243     // swap everything
244     SwapBSPFile();
245
246     return true;
247 }
248
249 void FreeBSPData()
250 {
251     if (visBytes) {
252         delete visBytes;
253     }
254     if (dnodes) {
255         delete dnodes;
256     }
257     if (dplanes) {
258         delete dplanes;
259     }
260     if (dleafs) {
261         delete dleafs;
262     }
263     if (drawVerts) {
264         delete drawVerts;
265     }
266     if (drawSurfaces) {
267         delete drawSurfaces;
268     }
269     if (dleafsurfaces) {
270         delete dleafsurfaces;
271     }
272     if (dleafbrushes) {
273         delete dleafbrushes;
274     }
275     if (dbrushes) {
276         delete dbrushes;
277     }
278     if (dbrushsides) {
279         delete dbrushsides;
280     }
281 }