-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtexture.cpp
189 lines (140 loc) · 3.31 KB
/
texture.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
#include "map.h"
#include "WAD3.h"
////////////////////////////////////////////////////////////////////
// Texture member functions
////////////////////////////////////////////////////////////////////
Texture* Texture::GetTexture ( char *pacTexture_, LPVOID lpView_, DWORD dwFileSize_, Texture::eGT &rResult_ )
{
rResult_ = eGT::GT_ERROR;
//
// Check if texture already exists
//
if ( stricmp ( name, pacTexture_ ) == 0 )
{
rResult_ = eGT::GT_FOUND;
return this;
}
if ( !IsLast ( ) )
{
return m_pNext->GetTexture ( pacTexture_, lpView_, dwFileSize_, rResult_ );
}
//
// Load texture information
//
Texture *pTexture = new Texture;
LPWAD3_HEADER lpHeader = NULL;
LPWAD3_LUMP lpLump = NULL;
LPWAD3_MIP lpMip = NULL;
DWORD dwNumLumps = 0;
DWORD dwTableOffset = 0;
DWORD dwFilePos = 0;
DWORD dwPaletteOffset = 0;
WORD wPaletteSize = 0;
DWORD dwWidth = 0;
DWORD dwHeight = 0;
// Make sure it's at least big enough to manipulate the header
if (dwFileSize_ < sizeof(WAD3_HEADER))
{
CorruptWAD3( "WAD3 file is malformed.", lpView_);
delete pTexture;
return NULL;
}
lpHeader = (LPWAD3_HEADER)lpView_;
if (lpHeader->identification != WAD3_ID)
{
CorruptWAD3( "Invalid WAD3 header id.", lpView_);
delete pTexture;
return NULL;
}
dwNumLumps = lpHeader->numlumps;
dwTableOffset = lpHeader->infotableofs;
// Make sure our table is really there
if ( ((dwNumLumps * sizeof(WAD3_LUMP)) + dwTableOffset) > dwFileSize_)
{
CorruptWAD3( "WAD3 file is malformed.", lpView_);
delete pTexture;
return NULL;
}
// Point at the first table entry
lpLump = (LPWAD3_LUMP)((LPBYTE)lpView_ + dwTableOffset);
bool bFound = false;
DWORD j = 0;
while ( ( !bFound ) && ( j < dwNumLumps ) )
{
if ( lpLump->type == WAD3_TYPE_MIP)
{
if ( stricmp ( lpLump->name, pacTexture_ ) == 0 )
{
// Find out where the MIP actually is
dwFilePos = lpLump->filepos;
// Make sure it's in bounds
if ( dwFilePos >= dwFileSize_ )
{
CorruptWAD3( "Invalid lump entry; filepos is malformed.", lpView_ );
delete pTexture;
return NULL;
}
// Point at the mip
lpMip = ( LPWAD3_MIP )( ( LPBYTE )lpView_ + dwFilePos );
strcpy ( pTexture->name, pacTexture_ );
pTexture->m_iWidth = lpMip->width;
pTexture->m_iHeight = lpMip->height;
bFound = true;
}
}
j++;
lpLump++;
}
if ( !bFound )
{
delete pTexture;
return NULL;
}
m_pNext = pTexture;
rResult_ = eGT::GT_LOADED;
return pTexture;
}
Texture::Texture ( )
{
memset ( name, 0, MAX_TEXTURE_LENGTH + 1 );
m_pNext = NULL;
m_iHeight = 0;
m_iWidth = 0;
}
Texture::~Texture ( )
{
if ( !IsLast ( ) )
{
delete m_pNext;
m_pNext = NULL;
}
}
void Texture::SetNext ( Texture *pTexture_ )
{
if ( IsLast ( ) )
{
m_pNext = pTexture_;
return;
}
//
// Insert the given list
//
if ( pTexture_ != NULL )
{
Texture *pTexture = pTexture_;
while ( !pTexture->IsLast ( ) )
{
pTexture = pTexture->GetNext ( );
}
pTexture->SetNext ( m_pNext );
}
m_pNext = pTexture_;
}
bool Texture::IsLast ( ) const
{
if ( m_pNext == NULL )
{
return true;
}
return false;
}