-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd3dtools.cpp
More file actions
executable file
·383 lines (333 loc) · 7.15 KB
/
d3dtools.cpp
File metadata and controls
executable file
·383 lines (333 loc) · 7.15 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include "d3dtools.h"
COLORREF convertColorRef(COLORREF crSrc, DWORD rBit, DWORD gBit, DWORD bBit) {
DWORD red = GetRValue(crSrc);
DWORD blue = GetBValue(crSrc);
DWORD green = GetGValue(crSrc);
red>>=(8-rBit);
blue>>=(8-bBit);
green>>=(8-gBit);
red<<=(gBit+bBit);
green<<=(bBit);
DWORD dwColor=(red|blue|green);
return dwColor;
}
IDirect3DSurface8* loadBitmap(LPCSTR filename, D3DFORMAT format) {
//Comme d'habitude on va utiliser GDI, donc on commence par créer un DC
HDC hdc = CreateCompatibleDC ( NULL ) ;
//ensuite on peut charger l'image
HBITMAP hbmNew = ( HBITMAP ) LoadImage ( NULL , filename, IMAGE_BITMAP , 0 , 0 , LR_LOADFROMFILE ) ;
//on récupère les dimensions de l'image
BITMAP bmp ;
GetObject ( hbmNew , sizeof ( BITMAP ) , &bmp );
//et on sélectionne l'image dans notre dc
HBITMAP hbmOld = ( HBITMAP ) SelectObject ( hdc , hbmNew ) ;
//nous pouvons maintenant créer l'image surface pour D3D en spécifiante la taille de l'image
IDirect3DSurface8* pSurface ;
if (FAILED(_dd3dInter->CreateImageSurface ( bmp.bmWidth , bmp.bmHeight , format , &pSurface )))
MessageBox(NULL, "erreur", "erreur", MB_OK);
//on récupère les formats pour chaque couleur
DWORD dwRShiftL = 8 - GetFormatRBits ( format ) ;
DWORD dwGShiftL = 8 - GetFormatGBits ( format ) ;
DWORD dwBShiftL = 8 - GetFormatBBits ( format ) ;
DWORD dwRShiftR = GetFormatGBits ( format ) + GetFormatBBits ( format ) ;
DWORD dwGShiftR = GetFormatBBits ( format ) ;
//nous pouvons maintenant calculer les bits par pixel
DWORD dwBytesPerPixel = ( GetFormatXBits ( format ) + GetFormatABits ( format ) + GetFormatRBits ( format ) + GetFormatGBits ( format ) + GetFormatBBits ( format ) ) / 8 ;
//quelques variables...
COLORREF crColor ;
DWORD dwColor ;
DWORD dwRed ;
DWORD dwGreen ;
DWORD dwBlue ;
DWORD dwPosition;
//nous avons tout ce qu'il faut maintenant pour remplir notre surface
D3DLOCKED_RECT lockrect ;
pSurface->LockRect ( &lockrect , NULL , 0 ) ;
BYTE* pbyBuffer = ( BYTE* ) lockrect.pBits ;
//on scanne l'image...
for ( int y = 0 ; y < bmp.bmHeight ; y ++ )
{
for ( int x = 0 ; x < bmp.bmWidth ; x ++ )
{
//récupération du pixel
crColor = GetPixel ( hdc , x , y ) ;
//calcule de sa position
dwPosition = y * lockrect.Pitch + x * dwBytesPerPixel ;
//récupération des couleurs
dwRed = GetRValue ( crColor ) ;
dwGreen = GetGValue ( crColor ) ;
dwBlue = GetBValue ( crColor ) ;
//convertions..
dwRed >>= dwRShiftL ;
dwGreen >>= dwGShiftL ;
dwBlue >>= dwBShiftL ;
dwRed <<=dwRShiftR ;
dwGreen <<=dwGShiftR ;
dwColor = dwRed | dwGreen | dwBlue ;
//nous pouvons enfin copier le pixel
memcpy ( &pbyBuffer [ dwPosition ] , &dwColor , dwBytesPerPixel ) ;
}
}
//unlock la surface
pSurface->UnlockRect ( ) ;
//restoration du DC
SelectObject ( hdc , hbmOld ) ;
//destruction de la bitmap
DeleteObject ( hbmNew ) ;
//destruction du dc
DeleteDC ( hdc ) ;
//retour du pointeur sur la surface créée
return ( pSurface ) ;
}
//fonctions pour récupérer les formats
UINT GetFormatRBits ( D3DFORMAT Format ) //rouge
{
//tests en fonction du format
switch ( Format )
{
case D3DFMT_R8G8B8 ://24 bpp, 8 pour r,g,b
{
return ( 8 ) ;
} break ;
case D3DFMT_A8R8G8B8 ://32 bpp, 8 pour a,r,g,b
{
return ( 8 ) ;
} break ;
case D3DFMT_X8R8G8B8 ://32 bpp, 8 pour r, g, b
{
return ( 8 ) ;
} break ;
case D3DFMT_R5G6B5 ://16bpp, 5 pour r,b, 6 pour g
{
return ( 5 ) ;
} break ;
case D3DFMT_X1R5G5B5 ://16 bpp, 5 pour r,g,b
{
return ( 5 ) ;
} break ;
case D3DFMT_A1R5G5B5 ://16 bpp, 5 pour r,g,b, 1 pour a
{
return ( 5 ) ;
} break ;
case D3DFMT_A4R4G4B4 ://16 bpp, 4 pour a,r,g,b
{
return ( 4 ) ;
} break ;
case D3DFMT_R3G3B2 ://8bpp, 3 pour r,g, 2 pour b
{
return ( 3 ) ;
} break ;
case D3DFMT_A8R3G3B2 ://16bpp, 8 pour a, 3 pour r,g, 2 pour b
{
return ( 3 ) ;
} break ;
case D3DFMT_X4R4G4B4 ://16bpp, 4 pour r,g,b
{
return ( 4 ) ;
} break ;
}
return ( 0 ) ;
}
UINT GetFormatGBits ( D3DFORMAT Format ) //vert
{
switch ( Format )
{
case D3DFMT_R8G8B8 :
{
return ( 8 ) ;
} break ;
case D3DFMT_A8R8G8B8 :
{
return ( 8 ) ;
} break ;
case D3DFMT_X8R8G8B8 :
{
return ( 8 ) ;
} break ;
case D3DFMT_R5G6B5 :
{
return ( 6 ) ;
} break ;
case D3DFMT_X1R5G5B5 :
{
return ( 5 ) ;
} break ;
case D3DFMT_A1R5G5B5 :
{
return ( 5 ) ;
} break ;
case D3DFMT_A4R4G4B4 :
{
return ( 4 ) ;
} break ;
case D3DFMT_R3G3B2 :
{
return ( 3 ) ;
} break ;
case D3DFMT_A8R3G3B2 :
{
return ( 3 ) ;
} break ;
case D3DFMT_X4R4G4B4 :
{
return ( 4 ) ;
} break ;
}
return ( 0 ) ;
}
UINT GetFormatBBits ( D3DFORMAT Format ) //bleu
{
switch ( Format )
{
case D3DFMT_R8G8B8 :
{
return ( 8 ) ;
} break ;
case D3DFMT_A8R8G8B8 :
{
return ( 8 ) ;
} break ;
case D3DFMT_X8R8G8B8 :
{
return ( 8 ) ;
} break ;
case D3DFMT_R5G6B5 :
{
return ( 5 ) ;
} break ;
case D3DFMT_X1R5G5B5 :
{
return ( 5 ) ;
} break ;
case D3DFMT_A1R5G5B5 :
{
return ( 5 ) ;
} break ;
case D3DFMT_A4R4G4B4 :
{
return ( 4 ) ;
} break ;
case D3DFMT_R3G3B2 :
{
return ( 2 ) ;
} break ;
case D3DFMT_A8R3G3B2 :
{
return ( 2 ) ;
} break ;
case D3DFMT_X4R4G4B4 :
{
return ( 4 ) ;
} break ;
}
return ( 0 ) ;
}
UINT GetFormatABits ( D3DFORMAT Format ) //alpha
{
switch ( Format )
{
case D3DFMT_A8R8G8B8 :
{
return ( 8 ) ;
} break ;
case D3DFMT_A1R5G5B5 :
{
return ( 1 ) ;
} break ;
case D3DFMT_A4R4G4B4 :
{
return ( 4 ) ;
} break ;
case D3DFMT_A8 :
{
return ( 8 ) ;
} break ;
case D3DFMT_A8R3G3B2 :
{
return ( 8 ) ;
} break ;
}
return ( 0 ) ;
}
UINT GetFormatDBits ( D3DFORMAT Format ) //depth
{
switch ( Format )
{
case D3DFMT_D16_LOCKABLE :
{
return ( 16 ) ;
} break ;
case D3DFMT_D32 :
{
return ( 32 ) ;
} break ;
case D3DFMT_D15S1 :
{
return ( 15 ) ;
} break ;
case D3DFMT_D24S8 :
{
return ( 24 ) ;
} break ;
case D3DFMT_D16 :
{
return ( 16 ) ;
} break ;
case D3DFMT_D24X8 :
{
return ( 24 ) ;
} break ;
case D3DFMT_D24X4S4 :
{
return ( 24 ) ;
} break ;
}
//not found, return 0
return ( 0 ) ;
}
UINT GetFormatSBits ( D3DFORMAT Format ) //stencil
{
switch ( Format )
{
case D3DFMT_D15S1 :
{
return ( 1 ) ;
} break ;
case D3DFMT_D24S8 :
{
return ( 8 ) ;
} break ;
case D3DFMT_D24X4S4 :
{
return ( 4 ) ;
} break ;
}
return ( 0 ) ;
}
UINT GetFormatXBits ( D3DFORMAT Format )
{
switch ( Format )
{
case D3DFMT_X8R8G8B8 :
{
return ( 8 ) ;
} break ;
case D3DFMT_X1R5G5B5 :
{
return ( 1 ) ;
} break ;
case D3DFMT_X4R4G4B4 :
{
return ( 4 ) ;
} break ;
case D3DFMT_D24X8 :
{
return ( 8 ) ;
} break ;
case D3DFMT_D24X4S4 :
{
return ( 4 ) ;
} break ;
}
return ( 0 ) ;
}