[D3DX9_36]
[reactos.git] / reactos / dll / directx / wine / d3dx9_36 / sprite.c
1 /*
2 * Copyright (C) 2008 Tony Wasserka
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 *
18 */
19
20 #include "d3dx9_36_private.h"
21
22 /* the combination of all possible D3DXSPRITE flags */
23 #define D3DXSPRITE_FLAGLIMIT 511
24
25 struct sprite_vertex
26 {
27 D3DXVECTOR3 pos;
28 DWORD col;
29 D3DXVECTOR2 tex;
30 };
31
32 struct sprite
33 {
34 IDirect3DTexture9 *texture;
35 UINT texw, texh;
36 RECT rect;
37 D3DXVECTOR3 center;
38 D3DXVECTOR3 pos;
39 D3DCOLOR color;
40 D3DXMATRIX transform;
41 };
42
43 struct d3dx9_sprite
44 {
45 ID3DXSprite ID3DXSprite_iface;
46 LONG ref;
47
48 IDirect3DDevice9 *device;
49 IDirect3DVertexDeclaration9 *vdecl;
50 IDirect3DStateBlock9 *stateblock;
51 D3DXMATRIX transform;
52 D3DXMATRIX view;
53 DWORD flags;
54 BOOL ready;
55
56 /* Store the relevant caps to prevent multiple GetDeviceCaps calls */
57 DWORD texfilter_caps;
58 DWORD maxanisotropy;
59 DWORD alphacmp_caps;
60
61 struct sprite *sprites;
62 int sprite_count; /* number of sprites to be drawn */
63 int allocated_sprites; /* number of (pre-)allocated sprites */
64 };
65
66 static inline struct d3dx9_sprite *impl_from_ID3DXSprite(ID3DXSprite *iface)
67 {
68 return CONTAINING_RECORD(iface, struct d3dx9_sprite, ID3DXSprite_iface);
69 }
70
71 static HRESULT WINAPI d3dx9_sprite_QueryInterface(ID3DXSprite *iface, REFIID riid, void **out)
72 {
73 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
74
75 if (IsEqualGUID(riid, &IID_ID3DXSprite)
76 || IsEqualGUID(riid, &IID_IUnknown))
77 {
78 IUnknown_AddRef(iface);
79 *out = iface;
80 return S_OK;
81 }
82
83 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
84
85 *out = NULL;
86 return E_NOINTERFACE;
87 }
88
89 static ULONG WINAPI d3dx9_sprite_AddRef(ID3DXSprite *iface)
90 {
91 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
92 ULONG refcount = InterlockedIncrement(&sprite->ref);
93
94 TRACE("%p increasing refcount to %u.\n", sprite, refcount);
95
96 return refcount;
97 }
98
99 static ULONG WINAPI d3dx9_sprite_Release(ID3DXSprite *iface)
100 {
101 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
102 ULONG refcount = InterlockedDecrement(&sprite->ref);
103
104 TRACE("%p decreasing refcount to %u.\n", sprite, refcount);
105
106 if (!refcount)
107 {
108 if (sprite->sprites)
109 {
110 int i;
111
112 for (i = 0; i < sprite->sprite_count; ++i)
113 {
114 if (sprite->sprites[i].texture)
115 IDirect3DTexture9_Release(sprite->sprites[i].texture);
116 }
117
118 HeapFree(GetProcessHeap(), 0, sprite->sprites);
119 }
120
121 if (sprite->stateblock)
122 IDirect3DStateBlock9_Release(sprite->stateblock);
123 if (sprite->vdecl)
124 IDirect3DVertexDeclaration9_Release(sprite->vdecl);
125 if (sprite->device)
126 IDirect3DDevice9_Release(sprite->device);
127 HeapFree(GetProcessHeap(), 0, sprite);
128 }
129
130 return refcount;
131 }
132
133 static HRESULT WINAPI d3dx9_sprite_GetDevice(struct ID3DXSprite *iface, struct IDirect3DDevice9 **device)
134 {
135 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
136
137 TRACE("iface %p, device %p.\n", iface, device);
138
139 if (!device)
140 return D3DERR_INVALIDCALL;
141 *device = sprite->device;
142 IDirect3DDevice9_AddRef(sprite->device);
143
144 return D3D_OK;
145 }
146
147 static HRESULT WINAPI d3dx9_sprite_GetTransform(ID3DXSprite *iface, D3DXMATRIX *transform)
148 {
149 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
150
151 TRACE("iface %p, transform %p.\n", iface, transform);
152
153 if (!transform)
154 return D3DERR_INVALIDCALL;
155 *transform = sprite->transform;
156
157 return D3D_OK;
158 }
159
160 static HRESULT WINAPI d3dx9_sprite_SetTransform(ID3DXSprite *iface, const D3DXMATRIX *transform)
161 {
162 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
163
164 TRACE("iface %p, transform %p.\n", iface, transform);
165
166 if (!transform)
167 return D3DERR_INVALIDCALL;
168 sprite->transform = *transform;
169
170 return D3D_OK;
171 }
172
173 static HRESULT WINAPI d3dx9_sprite_SetWorldViewRH(ID3DXSprite *iface,
174 const D3DXMATRIX *world, const D3DXMATRIX *view)
175 {
176 FIXME("iface %p, world %p, view %p stub!\n", iface, world, view);
177
178 return E_NOTIMPL;
179 }
180
181 static HRESULT WINAPI d3dx9_sprite_SetWorldViewLH(ID3DXSprite *iface,
182 const D3DXMATRIX *world, const D3DXMATRIX *view)
183 {
184 FIXME("iface %p, world %p, view %p stub!\n", iface, world, view);
185
186 return E_NOTIMPL;
187 }
188
189 /* Helper function */
190 static void set_states(struct d3dx9_sprite *object)
191 {
192 D3DXMATRIX mat;
193 D3DVIEWPORT9 vp;
194
195 /* Miscellaneous stuff */
196 IDirect3DDevice9_SetVertexShader(object->device, NULL);
197 IDirect3DDevice9_SetPixelShader(object->device, NULL);
198 IDirect3DDevice9_SetNPatchMode(object->device, 0.0f);
199
200 /* Render states */
201 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHABLENDENABLE, TRUE);
202 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHAFUNC, D3DCMP_GREATER);
203 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHAREF, 0x00);
204 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHATESTENABLE, object->alphacmp_caps);
205 IDirect3DDevice9_SetRenderState(object->device, D3DRS_BLENDOP, D3DBLENDOP_ADD);
206 IDirect3DDevice9_SetRenderState(object->device, D3DRS_CLIPPING, TRUE);
207 IDirect3DDevice9_SetRenderState(object->device, D3DRS_CLIPPLANEENABLE, FALSE);
208 IDirect3DDevice9_SetRenderState(object->device, D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_BLUE |
209 D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_RED);
210 IDirect3DDevice9_SetRenderState(object->device, D3DRS_CULLMODE, D3DCULL_NONE);
211 IDirect3DDevice9_SetRenderState(object->device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
212 IDirect3DDevice9_SetRenderState(object->device, D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1);
213 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ENABLEADAPTIVETESSELLATION, FALSE);
214 IDirect3DDevice9_SetRenderState(object->device, D3DRS_FILLMODE, D3DFILL_SOLID);
215 IDirect3DDevice9_SetRenderState(object->device, D3DRS_FOGENABLE, FALSE);
216 IDirect3DDevice9_SetRenderState(object->device, D3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
217 IDirect3DDevice9_SetRenderState(object->device, D3DRS_LIGHTING, FALSE);
218 IDirect3DDevice9_SetRenderState(object->device, D3DRS_RANGEFOGENABLE, FALSE);
219 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
220 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
221 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SPECULARENABLE, FALSE);
222 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
223 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SRGBWRITEENABLE, FALSE);
224 IDirect3DDevice9_SetRenderState(object->device, D3DRS_STENCILENABLE, FALSE);
225 IDirect3DDevice9_SetRenderState(object->device, D3DRS_VERTEXBLEND, FALSE);
226 IDirect3DDevice9_SetRenderState(object->device, D3DRS_WRAP0, 0);
227
228 /* Texture stage states */
229 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
230 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
231 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
232 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
233 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
234 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_COLOROP, D3DTOP_MODULATE);
235 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_TEXCOORDINDEX, 0);
236 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
237 IDirect3DDevice9_SetTextureStageState(object->device, 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
238 IDirect3DDevice9_SetTextureStageState(object->device, 1, D3DTSS_COLOROP, D3DTOP_DISABLE);
239
240 /* Sampler states */
241 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
242 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
243
244 if(object->texfilter_caps & D3DPTFILTERCAPS_MAGFANISOTROPIC)
245 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC);
246 else IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
247
248 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAXMIPLEVEL, 0);
249 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAXANISOTROPY, object->maxanisotropy);
250
251 if(object->texfilter_caps & D3DPTFILTERCAPS_MINFANISOTROPIC)
252 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC);
253 else IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
254
255 if(object->texfilter_caps & D3DPTFILTERCAPS_MIPFLINEAR)
256 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
257 else IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
258
259 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MIPMAPLODBIAS, 0);
260 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_SRGBTEXTURE, 0);
261
262 /* Matrices */
263 D3DXMatrixIdentity(&mat);
264 IDirect3DDevice9_SetTransform(object->device, D3DTS_WORLD, &mat);
265 IDirect3DDevice9_SetTransform(object->device, D3DTS_VIEW, &object->view);
266 IDirect3DDevice9_GetViewport(object->device, &vp);
267 D3DXMatrixOrthoOffCenterLH(&mat, vp.X+0.5f, (float)vp.Width+vp.X+0.5f, (float)vp.Height+vp.Y+0.5f, vp.Y+0.5f, vp.MinZ, vp.MaxZ);
268 IDirect3DDevice9_SetTransform(object->device, D3DTS_PROJECTION, &mat);
269 }
270
271 static HRESULT WINAPI d3dx9_sprite_Begin(ID3DXSprite *iface, DWORD flags)
272 {
273 struct d3dx9_sprite *This = impl_from_ID3DXSprite(iface);
274 HRESULT hr;
275
276 TRACE("iface %p, flags %#x.\n", iface, flags);
277
278 if(flags>D3DXSPRITE_FLAGLIMIT || This->ready) return D3DERR_INVALIDCALL;
279
280 /* TODO: Implement flags:
281 D3DXSPRITE_BILLBOARD: makes the sprite always face the camera
282 D3DXSPRITE_DONOTMODIFY_RENDERSTATE: name says it all
283 D3DXSPRITE_OBJECTSPACE: do not change device transforms
284 D3DXSPRITE_SORT_DEPTH_BACKTOFRONT: sort by position
285 D3DXSPRITE_SORT_DEPTH_FRONTTOBACK: sort by position
286 D3DXSPRITE_SORT_TEXTURE: sort by texture (so that it doesn't change too often)
287 */
288 /* Seems like alpha blending is always enabled, regardless of D3DXSPRITE_ALPHABLEND flag */
289 if(flags & (D3DXSPRITE_BILLBOARD |
290 D3DXSPRITE_DONOTMODIFY_RENDERSTATE | D3DXSPRITE_OBJECTSPACE |
291 D3DXSPRITE_SORT_DEPTH_BACKTOFRONT))
292 FIXME("Flags unsupported: %#x\n", flags);
293 /* These flags should only matter to performance */
294 else if(flags & (D3DXSPRITE_SORT_DEPTH_FRONTTOBACK | D3DXSPRITE_SORT_TEXTURE))
295 TRACE("Flags unsupported: %#x\n", flags);
296
297 if(This->vdecl==NULL) {
298 static const D3DVERTEXELEMENT9 elements[] =
299 {
300 { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
301 { 0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
302 { 0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
303 D3DDECL_END()
304 };
305 IDirect3DDevice9_CreateVertexDeclaration(This->device, elements, &This->vdecl);
306 }
307
308 if(!(flags & D3DXSPRITE_DONOTSAVESTATE)) {
309 if(This->stateblock==NULL) {
310 /* Tell our state block what it must store */
311 hr=IDirect3DDevice9_BeginStateBlock(This->device);
312 if(hr!=D3D_OK) return hr;
313
314 set_states(This);
315
316 IDirect3DDevice9_SetVertexDeclaration(This->device, This->vdecl);
317 IDirect3DDevice9_SetStreamSource(This->device, 0, NULL, 0, sizeof(struct sprite_vertex));
318 IDirect3DDevice9_SetIndices(This->device, NULL);
319 IDirect3DDevice9_SetTexture(This->device, 0, NULL);
320
321 IDirect3DDevice9_EndStateBlock(This->device, &This->stateblock);
322 }
323 IDirect3DStateBlock9_Capture(This->stateblock); /* Save current state */
324 }
325
326 /* Apply device state */
327 set_states(This);
328
329 This->flags=flags;
330 This->ready=TRUE;
331
332 return D3D_OK;
333 }
334
335 static HRESULT WINAPI d3dx9_sprite_Draw(ID3DXSprite *iface, IDirect3DTexture9 *texture,
336 const RECT *rect, const D3DXVECTOR3 *center, const D3DXVECTOR3 *position, D3DCOLOR color)
337 {
338 struct d3dx9_sprite *This = impl_from_ID3DXSprite(iface);
339 D3DSURFACE_DESC texdesc;
340
341 TRACE("iface %p, texture %p, rect %s, center %p, position %p, color 0x%08x.\n",
342 iface, texture, wine_dbgstr_rect(rect), center, position, color);
343
344 if(texture==NULL) return D3DERR_INVALIDCALL;
345 if(!This->ready) return D3DERR_INVALIDCALL;
346
347 if (!This->allocated_sprites)
348 {
349 This->sprites = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 32 * sizeof(*This->sprites));
350 This->allocated_sprites = 32;
351 }
352 else if (This->allocated_sprites <= This->sprite_count)
353 {
354 This->allocated_sprites += This->allocated_sprites / 2;
355 This->sprites = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
356 This->sprites, This->allocated_sprites * sizeof(*This->sprites));
357 }
358 This->sprites[This->sprite_count].texture=texture;
359 if(!(This->flags & D3DXSPRITE_DO_NOT_ADDREF_TEXTURE))
360 IDirect3DTexture9_AddRef(texture);
361
362 /* Reuse the texture desc if possible */
363 if(This->sprite_count) {
364 if(This->sprites[This->sprite_count-1].texture!=texture) {
365 IDirect3DTexture9_GetLevelDesc(texture, 0, &texdesc);
366 } else {
367 texdesc.Width=This->sprites[This->sprite_count-1].texw;
368 texdesc.Height=This->sprites[This->sprite_count-1].texh;
369 }
370 } else IDirect3DTexture9_GetLevelDesc(texture, 0, &texdesc);
371
372 This->sprites[This->sprite_count].texw=texdesc.Width;
373 This->sprites[This->sprite_count].texh=texdesc.Height;
374
375 if(rect==NULL) {
376 This->sprites[This->sprite_count].rect.left=0;
377 This->sprites[This->sprite_count].rect.top=0;
378 This->sprites[This->sprite_count].rect.right=texdesc.Width;
379 This->sprites[This->sprite_count].rect.bottom=texdesc.Height;
380 } else This->sprites[This->sprite_count].rect=*rect;
381
382 if(center==NULL) {
383 This->sprites[This->sprite_count].center.x=0.0f;
384 This->sprites[This->sprite_count].center.y=0.0f;
385 This->sprites[This->sprite_count].center.z=0.0f;
386 } else This->sprites[This->sprite_count].center=*center;
387
388 if(position==NULL) {
389 This->sprites[This->sprite_count].pos.x=0.0f;
390 This->sprites[This->sprite_count].pos.y=0.0f;
391 This->sprites[This->sprite_count].pos.z=0.0f;
392 } else This->sprites[This->sprite_count].pos=*position;
393
394 This->sprites[This->sprite_count].color=color;
395 This->sprites[This->sprite_count].transform=This->transform;
396 This->sprite_count++;
397
398 return D3D_OK;
399 }
400
401 static HRESULT WINAPI d3dx9_sprite_Flush(ID3DXSprite *iface)
402 {
403 struct d3dx9_sprite *This = impl_from_ID3DXSprite(iface);
404 struct sprite_vertex *vertices;
405 int i, count=0, start;
406
407 TRACE("iface %p.\n", iface);
408
409 if(!This->ready) return D3DERR_INVALIDCALL;
410 if(!This->sprite_count) return D3D_OK;
411
412 /* TODO: use of a vertex buffer here */
413 vertices = HeapAlloc(GetProcessHeap(), 0, sizeof(*vertices) * 6 * This->sprite_count);
414
415 for(start=0;start<This->sprite_count;start+=count,count=0) {
416 i=start;
417 while(i<This->sprite_count &&
418 (count==0 || This->sprites[i].texture==This->sprites[i-1].texture)) {
419 float spritewidth=(float)This->sprites[i].rect.right-(float)This->sprites[i].rect.left;
420 float spriteheight=(float)This->sprites[i].rect.bottom-(float)This->sprites[i].rect.top;
421
422 vertices[6*i ].pos.x = This->sprites[i].pos.x - This->sprites[i].center.x;
423 vertices[6*i ].pos.y = This->sprites[i].pos.y - This->sprites[i].center.y;
424 vertices[6*i ].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
425 vertices[6*i+1].pos.x = spritewidth + This->sprites[i].pos.x - This->sprites[i].center.x;
426 vertices[6*i+1].pos.y = This->sprites[i].pos.y - This->sprites[i].center.y;
427 vertices[6*i+1].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
428 vertices[6*i+2].pos.x = spritewidth + This->sprites[i].pos.x - This->sprites[i].center.x;
429 vertices[6*i+2].pos.y = spriteheight + This->sprites[i].pos.y - This->sprites[i].center.y;
430 vertices[6*i+2].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
431 vertices[6*i+3].pos.x = This->sprites[i].pos.x - This->sprites[i].center.x;
432 vertices[6*i+3].pos.y = spriteheight + This->sprites[i].pos.y - This->sprites[i].center.y;
433 vertices[6*i+3].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
434 vertices[6*i ].col = This->sprites[i].color;
435 vertices[6*i+1].col = This->sprites[i].color;
436 vertices[6*i+2].col = This->sprites[i].color;
437 vertices[6*i+3].col = This->sprites[i].color;
438 vertices[6*i ].tex.x = (float)This->sprites[i].rect.left / (float)This->sprites[i].texw;
439 vertices[6*i ].tex.y = (float)This->sprites[i].rect.top / (float)This->sprites[i].texh;
440 vertices[6*i+1].tex.x = (float)This->sprites[i].rect.right / (float)This->sprites[i].texw;
441 vertices[6*i+1].tex.y = (float)This->sprites[i].rect.top / (float)This->sprites[i].texh;
442 vertices[6*i+2].tex.x = (float)This->sprites[i].rect.right / (float)This->sprites[i].texw;
443 vertices[6*i+2].tex.y = (float)This->sprites[i].rect.bottom / (float)This->sprites[i].texh;
444 vertices[6*i+3].tex.x = (float)This->sprites[i].rect.left / (float)This->sprites[i].texw;
445 vertices[6*i+3].tex.y = (float)This->sprites[i].rect.bottom / (float)This->sprites[i].texh;
446
447 vertices[6*i+4]=vertices[6*i];
448 vertices[6*i+5]=vertices[6*i+2];
449
450 D3DXVec3TransformCoordArray(&vertices[6 * i].pos, sizeof(*vertices),
451 &vertices[6 * i].pos, sizeof(*vertices), &This->sprites[i].transform, 6);
452 count++;
453 i++;
454 }
455
456 IDirect3DDevice9_SetTexture(This->device, 0, (struct IDirect3DBaseTexture9 *)This->sprites[start].texture);
457 IDirect3DDevice9_SetVertexDeclaration(This->device, This->vdecl);
458
459 IDirect3DDevice9_DrawPrimitiveUP(This->device, D3DPT_TRIANGLELIST,
460 2 * count, vertices + 6 * start, sizeof(*vertices));
461 }
462 HeapFree(GetProcessHeap(), 0, vertices);
463
464 if(!(This->flags & D3DXSPRITE_DO_NOT_ADDREF_TEXTURE))
465 for(i=0;i<This->sprite_count;i++)
466 IDirect3DTexture9_Release(This->sprites[i].texture);
467
468 This->sprite_count=0;
469
470 /* Flush may be called more than once, so we don't reset This->ready here */
471
472 return D3D_OK;
473 }
474
475 static HRESULT WINAPI d3dx9_sprite_End(ID3DXSprite *iface)
476 {
477 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
478
479 TRACE("iface %p.\n", iface);
480
481 if (!sprite->ready)
482 return D3DERR_INVALIDCALL;
483
484 ID3DXSprite_Flush(iface);
485
486 if (sprite->stateblock && !(sprite->flags & D3DXSPRITE_DONOTSAVESTATE))
487 IDirect3DStateBlock9_Apply(sprite->stateblock); /* Restore old state */
488
489 sprite->ready = FALSE;
490
491 return D3D_OK;
492 }
493
494 static HRESULT WINAPI d3dx9_sprite_OnLostDevice(ID3DXSprite *iface)
495 {
496 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
497
498 TRACE("iface %p.\n", iface);
499
500 if (sprite->stateblock)
501 IDirect3DStateBlock9_Release(sprite->stateblock);
502 if (sprite->vdecl)
503 IDirect3DVertexDeclaration9_Release(sprite->vdecl);
504 sprite->vdecl = NULL;
505 sprite->stateblock = NULL;
506
507 /* Reset some variables */
508 ID3DXSprite_OnResetDevice(iface);
509
510 return D3D_OK;
511 }
512
513 static HRESULT WINAPI d3dx9_sprite_OnResetDevice(ID3DXSprite *iface)
514 {
515 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
516 int i;
517
518 TRACE("iface %p.\n", iface);
519
520 for (i = 0; i < sprite->sprite_count; ++i)
521 {
522 if (sprite->sprites[i].texture)
523 IDirect3DTexture9_Release(sprite->sprites[i].texture);
524 }
525
526 sprite->sprite_count = 0;
527 sprite->flags = 0;
528 sprite->ready = FALSE;
529
530 /* keep matrices */
531 /* device objects get restored on Begin */
532
533 return D3D_OK;
534 }
535
536 static const ID3DXSpriteVtbl d3dx9_sprite_vtbl =
537 {
538 d3dx9_sprite_QueryInterface,
539 d3dx9_sprite_AddRef,
540 d3dx9_sprite_Release,
541 d3dx9_sprite_GetDevice,
542 d3dx9_sprite_GetTransform,
543 d3dx9_sprite_SetTransform,
544 d3dx9_sprite_SetWorldViewRH,
545 d3dx9_sprite_SetWorldViewLH,
546 d3dx9_sprite_Begin,
547 d3dx9_sprite_Draw,
548 d3dx9_sprite_Flush,
549 d3dx9_sprite_End,
550 d3dx9_sprite_OnLostDevice,
551 d3dx9_sprite_OnResetDevice,
552 };
553
554 HRESULT WINAPI D3DXCreateSprite(struct IDirect3DDevice9 *device, struct ID3DXSprite **sprite)
555 {
556 struct d3dx9_sprite *object;
557 D3DCAPS9 caps;
558
559 TRACE("device %p, sprite %p.\n", device, sprite);
560
561 if(device==NULL || sprite==NULL) return D3DERR_INVALIDCALL;
562
563 if (!(object=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
564 {
565 *sprite = NULL;
566 return E_OUTOFMEMORY;
567 }
568 object->ID3DXSprite_iface.lpVtbl = &d3dx9_sprite_vtbl;
569 object->ref=1;
570 object->device=device;
571 IUnknown_AddRef(device);
572
573 object->vdecl=NULL;
574 object->stateblock=NULL;
575
576 D3DXMatrixIdentity(&object->transform);
577 D3DXMatrixIdentity(&object->view);
578
579 IDirect3DDevice9_GetDeviceCaps(device, &caps);
580 object->texfilter_caps=caps.TextureFilterCaps;
581 object->maxanisotropy=caps.MaxAnisotropy;
582 object->alphacmp_caps=caps.AlphaCmpCaps;
583
584 ID3DXSprite_OnResetDevice(&object->ID3DXSprite_iface);
585
586 object->sprites=NULL;
587 object->allocated_sprites=0;
588 *sprite=&object->ID3DXSprite_iface;
589
590 return D3D_OK;
591 }