-
Notifications
You must be signed in to change notification settings - Fork 6
/
CollBsp.cpp
221 lines (180 loc) · 6.11 KB
/
CollBsp.cpp
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
// CollBsp1.cpp: implementation of the CCollBsp class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "sparkedit.h"
#include "CollBsp.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCollBsp::CCollBsp()
{
m_pMapFile = NULL;
m_BspMagic = 0;
m_Magic = 0;
m_pNodes = NULL;
m_pPlanes = NULL;
m_pLeaves = NULL;
m_pBsp2D_Ref = NULL;
m_pBsp2D_Nodes = NULL;
m_pSurfaces = NULL;
m_pEdges = NULL;
m_pVertices = NULL;
}
CCollBsp::~CCollBsp()
{
}
void CCollBsp::Initialize(CFile *pMapFile, UINT bspmagic, UINT magic)
{
m_pMapFile = pMapFile;
m_BspMagic = bspmagic;
m_Magic = magic;
}
void CCollBsp::LoadCollisionBsp(UINT CollHdrOffset)
{
m_pMapFile->Seek(CollHdrOffset, 0);
m_pMapFile->Read(&m_CollHeader, sizeof(m_CollHeader));
m_CollHeader.Nodes.Offset -= m_BspMagic;
m_CollHeader.Planes.Offset -= m_BspMagic;
m_CollHeader.Leaves.Offset -= m_BspMagic;
m_CollHeader.Bsp2dRef.Offset -= m_BspMagic;
m_CollHeader.Bsp2dNodes.Offset -= m_BspMagic;
m_CollHeader.Surfaces.Offset -= m_BspMagic;
m_CollHeader.Edges.Offset -= m_BspMagic;
m_CollHeader.Vertices.Offset -= m_BspMagic;
if(m_CollHeader.Nodes.Count > 0)
{
m_pNodes = new COLL_NODE[m_CollHeader.Nodes.Count];
m_pMapFile->Seek(m_CollHeader.Nodes.Offset, 0);
m_pMapFile->Read(m_pNodes, sizeof(COLL_NODE)*m_CollHeader.Nodes.Count);
}
if(m_CollHeader.Planes.Count > 0)
{
m_pPlanes = new COLL_PLANE[m_CollHeader.Planes.Count];
m_pMapFile->Seek(m_CollHeader.Planes.Offset, 0);
m_pMapFile->Read(m_pPlanes, sizeof(COLL_PLANE)*m_CollHeader.Planes.Count);
}
if(m_CollHeader.Leaves.Count > 0)
{
m_pLeaves = new COLL_LEAF[m_CollHeader.Leaves.Count];
m_pMapFile->Seek(m_CollHeader.Leaves.Offset, 0);
m_pMapFile->Read(m_pLeaves, sizeof(COLL_LEAF)*m_CollHeader.Leaves.Count);
}
if(m_CollHeader.Bsp2dRef.Count > 0)
{
m_pBsp2D_Ref = new COLL_BSP_2D_REF[m_CollHeader.Bsp2dRef.Count];
m_pMapFile->Seek(m_CollHeader.Bsp2dRef.Offset, 0);
m_pMapFile->Read(m_pBsp2D_Ref, sizeof(COLL_BSP_2D_REF)*m_CollHeader.Bsp2dRef.Count);
}
if(m_CollHeader.Bsp2dNodes.Count > 0)
{
m_pBsp2D_Nodes = new COLL_BSP_2D_NODES[m_CollHeader.Bsp2dNodes.Count];
m_pMapFile->Seek(m_CollHeader.Bsp2dNodes.Offset, 0);
m_pMapFile->Read(m_pBsp2D_Nodes, sizeof(COLL_BSP_2D_NODES)*m_CollHeader.Bsp2dNodes.Count);
}
if(m_CollHeader.Surfaces.Count > 0)
{
m_pSurfaces = new COLL_SURFACES[m_CollHeader.Surfaces.Count];
m_pMapFile->Seek(m_CollHeader.Surfaces.Offset, 0);
m_pMapFile->Read(m_pSurfaces, sizeof(COLL_SURFACES)*m_CollHeader.Surfaces.Count);
}
if(m_CollHeader.Edges.Count > 0)
{
m_pEdges = new COLL_EDGES[m_CollHeader.Edges.Count];
m_pMapFile->Seek(m_CollHeader.Edges.Offset, 0);
m_pMapFile->Read(m_pEdges, sizeof(COLL_EDGES)*m_CollHeader.Edges.Count);
}
if(m_CollHeader.Vertices.Count > 0)
{
m_pVertices = new COLL_VERTEX[m_CollHeader.Vertices.Count];
m_pMapFile->Seek(m_CollHeader.Vertices.Offset, 0);
m_pMapFile->Read(m_pVertices, sizeof(COLL_VERTEX)*m_CollHeader.Vertices.Count);
}
}
void CCollBsp::Cleanup()
{
if(m_pNodes)
delete [] m_pNodes;
if(m_pPlanes)
delete [] m_pPlanes;
if(m_pLeaves)
delete [] m_pLeaves;
if(m_pBsp2D_Ref)
delete [] m_pBsp2D_Ref;
if(m_pBsp2D_Nodes)
delete [] m_pBsp2D_Nodes;
if(m_pSurfaces)
delete [] m_pSurfaces;
if(m_pEdges)
delete [] m_pEdges;
if(m_pVertices)
delete [] m_pVertices;
m_pNodes = NULL;
m_pPlanes = NULL;
m_pLeaves = NULL;
m_pBsp2D_Ref = NULL;
m_pBsp2D_Nodes = NULL;
m_pSurfaces = NULL;
m_pEdges = NULL;
m_pVertices = NULL;
}
UINT CCollBsp::WriteCollisionBsp(CFile *pFile, UINT start_offset, UINT new_magic)
{
UINT bytes_written = 0;
UINT block_size;
COLLISION_BSP_HEADER new_hdr;
memcpy(&new_hdr, &m_CollHeader, sizeof(COLLISION_BSP_HEADER));
//write header
pFile->Seek(start_offset, 0);
pFile->Write(&new_hdr, sizeof(COLLISION_BSP_HEADER));
bytes_written += sizeof(COLLISION_BSP_HEADER);
//write nodes
new_hdr.Nodes.Offset = bytes_written + start_offset + new_magic;
block_size = sizeof(COLL_NODE)*m_CollHeader.Nodes.Count;
pFile->Write(m_pNodes, block_size);
bytes_written += block_size;
//write planes
new_hdr.Planes.Offset = bytes_written + start_offset + new_magic;
block_size = sizeof(COLL_PLANE)*m_CollHeader.Planes.Count;
pFile->Write(m_pPlanes, block_size);
bytes_written += block_size;
//write leaves
new_hdr.Leaves.Offset = bytes_written + start_offset + new_magic;
block_size = sizeof(COLL_LEAF)*m_CollHeader.Leaves.Count;
pFile->Write(m_pLeaves, block_size);
bytes_written += block_size;
//write bsp 2d ref
new_hdr.Bsp2dRef.Offset = bytes_written + start_offset + new_magic;
block_size = sizeof(COLL_BSP_2D_REF)*m_CollHeader.Bsp2dRef.Count;
pFile->Write(m_pBsp2D_Ref, block_size);
bytes_written += block_size;
//write bsp 2d nodes
new_hdr.Bsp2dNodes.Offset = bytes_written + start_offset + new_magic;
block_size = sizeof(COLL_BSP_2D_NODES)*m_CollHeader.Bsp2dNodes.Count;
pFile->Write(m_pBsp2D_Nodes, block_size);
bytes_written += block_size;
//write surfaces
new_hdr.Surfaces.Offset = bytes_written + start_offset + new_magic;
block_size = sizeof(COLL_SURFACES)*m_CollHeader.Surfaces.Count;
pFile->Write(m_pSurfaces, block_size);
bytes_written += block_size;
//write edges
new_hdr.Edges.Offset = bytes_written + start_offset + new_magic;
block_size = sizeof(COLL_EDGES)*m_CollHeader.Edges.Count;
pFile->Write(m_pEdges, block_size);
bytes_written += block_size;
//write vertices
new_hdr.Vertices.Offset = bytes_written + start_offset + new_magic;
block_size = sizeof(COLL_VERTEX)*m_CollHeader.Vertices.Count;
pFile->Write(m_pVertices, block_size);
bytes_written += block_size;
//update collision header with new offsets
pFile->Seek(start_offset, 0);
pFile->Write(&new_hdr, sizeof(COLLISION_BSP_HEADER));
return(bytes_written);
}