-
Notifications
You must be signed in to change notification settings - Fork 2
/
Texture.cpp
320 lines (243 loc) · 6.28 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright 2004/2006 Marko Mihovilic
#include "Globals.h"
#include "Kernel.h"
#include "Texture.h"
#include "Targa.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
Texture::Texture(void) :
mCreated(false),
mTexture(NULL),
mBuffer(NULL),
mSize(NULL),
mWidth(NULL),
mHeight(NULL),
mMipmaps(NULL),
mUsage(NULL),
mLevels(NULL)
{
}
Texture::~Texture(void)
{
Destroy();
}
bool Texture::Create(const wchar_t* path,unsigned long width,unsigned long height,unsigned long mipmaps,Usage usage,Format format,Pool pool)
{
ASSERT(!mCreated);
File file;
if(!file.Open(path,File::AccessModeRead,File::ShareModeRead,File::OpenModeExisting))
{
TRACE(L"Texture failed to open file.\n");
return false;
}
unsigned long size = file.GetSize();
if(!size)
{
TRACE(L"Texture file size mismatch.\n");
return false;
}
unsigned char* buffer = new unsigned char[size];
if(!buffer)
{
TRACE(L"Texture failed to allocate file buffer %u bytes.\n",size);
return false;
}
ZeroMemory(buffer,size);
if(file.Read(buffer,size) != size)
{
delete[] buffer;
TRACE(L"Texture file read mismatch %u bytes.\n",size);
return false;
}
file.Close();
if(!Create(buffer,size,width,height,mipmaps,usage,format,pool))
{
delete[] buffer;
return false;
}
delete[] buffer;
return true;
}
bool Texture::Create(const wchar_t* id,const wchar_t* group,unsigned long width,unsigned long height,unsigned long mipmaps,Usage usage,Format format,Pool pool)
{
ASSERT(!mCreated);
HRSRC resource = FindResource(NULL,id,group);
if(!resource)
{
TRACE(L"Failed to find texture resource.\n");
return false;
}
HGLOBAL global = LoadResource(NULL,resource);
if(!global)
{
TRACE(L"Failed to load texture resource.\n");
return false;
}
LPBYTE data = (LPBYTE)LockResource(global);
if(!data)
{
TRACE(L"Failed to lock texture resource.\n");
return false;
}
if(!Create(data,SizeofResource(NULL,resource),width,height,mipmaps,usage,format,pool))
{
UnlockResource(global);
FreeResource(global);
return false;
}
UnlockResource(global);
FreeResource(global);
return true;
}
bool Texture::Create(const unsigned char* data,unsigned long size,unsigned long width,unsigned long height,unsigned long mipmaps,Usage usage,Format format,Pool pool)
{
ASSERT(!mCreated);
if(usage == UsageNone)
mUsage = NULL;
if(usage == UsageDynamic)
mUsage = D3DUSAGE_DYNAMIC;
if(usage == UsageRenderTarget)
mUsage = D3DUSAGE_RENDERTARGET;
if(format == FormatUnknown)
mFormat = D3DFMT_UNKNOWN;
if(format == FormatA8R8G8B8)
mFormat = D3DFMT_A8R8G8B8;
if(format == FormatV8U8)
mFormat = D3DFMT_V8U8;
if(format == FormatDXT5)
mFormat = D3DFMT_DXT5;
if(pool == PoolDefault)
mPool = D3DPOOL_DEFAULT;
if(pool == PoolManaged)
mPool = D3DPOOL_MANAGED;
if(mBuffer)
{
delete[] mBuffer;
mBuffer = NULL;
}
mSize = size;
mBuffer = new unsigned char[mSize];
if(!mBuffer)
{
TRACE(L"Failed to allocate local texture data buffer of %u bytes.\n",mSize);
return false;
}
CopyMemory(mBuffer,data,mSize);
mWidth = width;
mHeight = height;
mMipmaps = mipmaps;
//if(FAILED(GetDynamicD3DX()->D3DXCreateTextureFromFileInMemoryEx(GetKernel()->GetGraphics()->GetDevice(),mBuffer,mSize,mWidth,mHeight,mMipmaps,mUsage,mFormat,mPool,D3DX_DEFAULT,D3DX_DEFAULT,NULL,NULL,NULL,&mTexture)))
//{
// Trace(L"Failed to load texture data.\n");
// return false;
//}
Targa tga;
if(!tga.LoadFromMemory(mBuffer,mSize))
{
TRACE(L"Failed to load texture.\n");
return false;
}
mWidth = tga.GetWidth();
mHeight = tga.GetHeight();
if(FAILED(GetKernel()->GetGraphics()->GetDevice()->CreateTexture(mWidth,mHeight,mLevels,mUsage|D3DUSAGE_DYNAMIC,mFormat,mPool,&mTexture,NULL)))
{
TRACE(L"Failed to create texture.\n");
return false;
}
D3DLOCKED_RECT rect;
if(FAILED(mTexture->LockRect(0,&rect,NULL,D3DLOCK_DISCARD)))
{
TRACE(L"Failed to lock texture.\n");
return false;
}
CopyMemory(rect.pBits,tga.GetData(),tga.GetBitsPerPixel()*tga.GetWidth()*tga.GetHeight()/8);
mTexture->UnlockRect(0);
if(mPool != D3DPOOL_DEFAULT)
SAFEDELETE(mBuffer);
mCreated = true;
return true;
}
bool Texture::Create(unsigned long width,unsigned long height,unsigned long levels,Usage usage,Format format,Pool pool)
{
ASSERT(!mCreated);
if(usage == UsageNone)
mUsage = NULL;
if(usage == UsageDynamic)
mUsage = D3DUSAGE_DYNAMIC;
if(usage == UsageRenderTarget)
mUsage = D3DUSAGE_RENDERTARGET;
if(format == FormatUnknown)
mFormat = D3DFMT_UNKNOWN;
if(format == FormatA8R8G8B8)
mFormat = D3DFMT_A8R8G8B8;
if(format == FormatV8U8)
mFormat = D3DFMT_V8U8;
if(format == FormatDXT5)
mFormat = D3DFMT_DXT5;
if(pool == PoolDefault)
mPool = D3DPOOL_DEFAULT;
if(pool == PoolManaged)
mPool = D3DPOOL_MANAGED;
mWidth = width;
mHeight = height;
mLevels = levels;
if(FAILED(GetKernel()->GetGraphics()->GetDevice()->CreateTexture(mWidth,mHeight,mLevels,mUsage,mFormat,mPool,&mTexture,NULL)))
{
TRACE(L"Failed to create texture.\n");
return false;
}
mCreated = true;
return true;
}
void Texture::Destroy(void)
{
mCreated = false;
SAFERELEASE(mTexture);
SAFEDELETEARRAY(mBuffer);
mSize = NULL;
mWidth = NULL;
mHeight = NULL;
mMipmaps = NULL;
mUsage = NULL;
mLevels = NULL;
}
bool Texture::OnLostDevice(void)
{
if(mPool == D3DPOOL_DEFAULT)
SAFERELEASE(mTexture);
return true;
}
bool Texture::OnResetDevice(void)
{
if(mPool == D3DPOOL_DEFAULT && mBuffer)
{
//if(FAILED(GetDynamicD3DX()->D3DXCreateTextureFromFileInMemoryEx(GetKernel()->GetGraphics()->GetDevice(),mBuffer,mSize,mWidth,mHeight,mMipmaps,mUsage,mFormat,mPool,D3DX_DEFAULT,D3DX_DEFAULT,NULL,NULL,NULL,&mTexture)))
//{
// Trace(L"Failed to recreate texture.\n");
// return false;
//}
Targa tga;
if(!tga.LoadFromMemory(mBuffer,mSize))
{
TRACE(L"Failed to load texture.\n");
return false;
}
mWidth = tga.GetWidth();
mHeight = tga.GetHeight();
if(FAILED(GetKernel()->GetGraphics()->GetDevice()->CreateTexture(mWidth,mHeight,mLevels,mUsage|D3DUSAGE_DYNAMIC,mFormat,mPool,&mTexture,NULL)))
{
TRACE(L"Failed to create texture.\n");
return false;
}
D3DLOCKED_RECT rect;
if(FAILED(mTexture->LockRect(0,&rect,NULL,D3DLOCK_DISCARD)))
{
TRACE(L"Failed to lock texture.\n");
return false;
}
CopyMemory(rect.pBits,tga.GetData(),tga.GetBitsPerPixel()*tga.GetWidth()*tga.GetHeight()/8);
mTexture->UnlockRect(0);
}
return true;
}