shell32: Don't crash on NULL cmdgroup in DefView OleCommandTarget.
[wine] / dlls / 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 "wine/debug.h"
21 #include "d3dx9_36_private.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
24
25 /* the combination of all possible D3DXSPRITE flags */
26 #define D3DXSPRITE_FLAGLIMIT 511
27
28 typedef struct _SPRITEVERTEX {
29     D3DXVECTOR3 pos;
30     DWORD col;
31     D3DXVECTOR2 tex;
32 } SPRITEVERTEX;
33
34 static HRESULT WINAPI ID3DXSpriteImpl_QueryInterface(LPD3DXSPRITE iface, REFIID riid, LPVOID *object)
35 {
36     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
37
38     TRACE("(%p): QueryInterface from %s\n", This, debugstr_guid(riid));
39     if(IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ID3DXSprite)) {
40         IUnknown_AddRef(iface);
41         *object=This;
42         return S_OK;
43     }
44     WARN("(%p)->(%s, %p): not found\n", iface, debugstr_guid(riid), *object);
45     return E_NOINTERFACE;
46 }
47
48 static ULONG WINAPI ID3DXSpriteImpl_AddRef(LPD3DXSPRITE iface)
49 {
50     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
51     ULONG ref=InterlockedIncrement(&This->ref);
52     TRACE("(%p)->(): AddRef from %d\n", This, ref-1);
53     return ref;
54 }
55
56 static ULONG WINAPI ID3DXSpriteImpl_Release(LPD3DXSPRITE iface)
57 {
58     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
59     ULONG ref=InterlockedDecrement(&This->ref);
60
61     TRACE("(%p)->(): ReleaseRef to %d\n", This, ref);
62
63     if(ref==0) {
64         if(This->sprites) {
65             int i;
66             for(i=0;i<This->sprite_count;i++)
67                 if(This->sprites[i].texture)
68                     IDirect3DTexture9_Release(This->sprites[i].texture);
69
70             HeapFree(GetProcessHeap(), 0, This->sprites);
71         }
72         if(This->stateblock) IDirect3DStateBlock9_Release(This->stateblock);
73         if(This->vdecl) IDirect3DVertexDeclaration9_Release(This->vdecl);
74         if(This->device) IDirect3DDevice9_Release(This->device);
75         HeapFree(GetProcessHeap(), 0, This);
76     }
77     return ref;
78 }
79
80 static HRESULT WINAPI ID3DXSpriteImpl_GetDevice(LPD3DXSPRITE iface, LPDIRECT3DDEVICE9 *device)
81 {
82     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
83
84     TRACE("(%p)->(%p): relay\n", This, device);
85
86     if(device==NULL) return D3DERR_INVALIDCALL;
87     *device=This->device;
88     IDirect3DDevice9_AddRef(This->device);
89
90     return D3D_OK;
91 }
92
93 static HRESULT WINAPI ID3DXSpriteImpl_GetTransform(LPD3DXSPRITE iface, D3DXMATRIX *transform)
94 {
95     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
96
97     TRACE("(%p)->(%p)\n", This, transform);
98
99     if(transform==NULL) return D3DERR_INVALIDCALL;
100     *transform=This->transform;
101
102     return D3D_OK;
103 }
104
105 static HRESULT WINAPI ID3DXSpriteImpl_SetTransform(LPD3DXSPRITE iface, CONST D3DXMATRIX *transform)
106 {
107     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
108
109     TRACE("(%p)->(%p)\n", This, transform);
110
111     if(transform==NULL) return D3DERR_INVALIDCALL;
112     This->transform=*transform;
113
114     return D3D_OK;
115 }
116
117 static HRESULT WINAPI ID3DXSpriteImpl_SetWorldViewRH(LPD3DXSPRITE iface, CONST D3DXMATRIX *world, CONST D3DXMATRIX *view)
118 {
119     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
120     FIXME("(%p)->(%p, %p): stub\n", This, world, view);
121     return E_NOTIMPL;
122 }
123
124 static HRESULT WINAPI ID3DXSpriteImpl_SetWorldViewLH(LPD3DXSPRITE iface, CONST D3DXMATRIX *world, CONST D3DXMATRIX *view)
125 {
126     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
127     FIXME("(%p)->(%p, %p): stub\n", This, world, view);
128     return E_NOTIMPL;
129 }
130
131 /* Helper function */
132 static void set_states(ID3DXSpriteImpl *object)
133 {
134     D3DXMATRIX mat;
135     D3DVIEWPORT9 vp;
136
137     /* Miscelaneous stuff */
138     IDirect3DDevice9_SetVertexShader(object->device, NULL);
139     IDirect3DDevice9_SetPixelShader(object->device, NULL);
140     IDirect3DDevice9_SetNPatchMode(object->device, 0.0f);
141
142     /* Render states */
143     IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHABLENDENABLE, TRUE);
144     IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHAFUNC, D3DCMP_GREATER);
145     IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHAREF, 0x00);
146     IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHATESTENABLE, object->alphacmp_caps);
147     IDirect3DDevice9_SetRenderState(object->device, D3DRS_BLENDOP, D3DBLENDOP_ADD);
148     IDirect3DDevice9_SetRenderState(object->device, D3DRS_CLIPPING, TRUE);
149     IDirect3DDevice9_SetRenderState(object->device, D3DRS_CLIPPLANEENABLE, FALSE);
150     IDirect3DDevice9_SetRenderState(object->device, D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_BLUE |
151                                     D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_RED);
152     IDirect3DDevice9_SetRenderState(object->device, D3DRS_CULLMODE, D3DCULL_NONE);
153     IDirect3DDevice9_SetRenderState(object->device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
154     IDirect3DDevice9_SetRenderState(object->device, D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1);
155     IDirect3DDevice9_SetRenderState(object->device, D3DRS_ENABLEADAPTIVETESSELLATION, FALSE);
156     IDirect3DDevice9_SetRenderState(object->device, D3DRS_FILLMODE, D3DFILL_SOLID);
157     IDirect3DDevice9_SetRenderState(object->device, D3DRS_FOGENABLE, FALSE);
158     IDirect3DDevice9_SetRenderState(object->device, D3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
159     IDirect3DDevice9_SetRenderState(object->device, D3DRS_LIGHTING, FALSE);
160     IDirect3DDevice9_SetRenderState(object->device, D3DRS_RANGEFOGENABLE, FALSE);
161     IDirect3DDevice9_SetRenderState(object->device, D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
162     IDirect3DDevice9_SetRenderState(object->device, D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
163     IDirect3DDevice9_SetRenderState(object->device, D3DRS_SPECULARENABLE, FALSE);
164     IDirect3DDevice9_SetRenderState(object->device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
165     IDirect3DDevice9_SetRenderState(object->device, D3DRS_SRGBWRITEENABLE, FALSE);
166     IDirect3DDevice9_SetRenderState(object->device, D3DRS_STENCILENABLE, FALSE);
167     IDirect3DDevice9_SetRenderState(object->device, D3DRS_VERTEXBLEND, FALSE);
168     IDirect3DDevice9_SetRenderState(object->device, D3DRS_WRAP0, 0);
169
170     /* Texture stage states */
171     IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
172     IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
173     IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
174     IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
175     IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
176     IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_COLOROP, D3DTOP_MODULATE);
177     IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_TEXCOORDINDEX, 0);
178     IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
179     IDirect3DDevice9_SetTextureStageState(object->device, 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
180     IDirect3DDevice9_SetTextureStageState(object->device, 1, D3DTSS_COLOROP, D3DTOP_DISABLE);
181
182     /* Sampler states */
183     IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
184     IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
185
186     if(object->texfilter_caps & D3DPTFILTERCAPS_MAGFANISOTROPIC)
187         IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC);
188     else IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
189
190     IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAXMIPLEVEL, 0);
191     IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAXANISOTROPY, object->maxanisotropy);
192
193     if(object->texfilter_caps & D3DPTFILTERCAPS_MINFANISOTROPIC)
194         IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC);
195     else IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
196
197     if(object->texfilter_caps & D3DPTFILTERCAPS_MIPFLINEAR)
198         IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
199     else IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
200
201     IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MIPMAPLODBIAS, 0);
202     IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_SRGBTEXTURE, 0);
203
204     /* Matrices */
205     D3DXMatrixIdentity(&mat);
206     IDirect3DDevice9_SetTransform(object->device, D3DTS_WORLD, &mat);
207     IDirect3DDevice9_SetTransform(object->device, D3DTS_VIEW, &object->view);
208     IDirect3DDevice9_GetViewport(object->device, &vp);
209     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);
210     IDirect3DDevice9_SetTransform(object->device, D3DTS_PROJECTION, &mat);
211 }
212
213 static HRESULT WINAPI ID3DXSpriteImpl_Begin(LPD3DXSPRITE iface, DWORD flags)
214 {
215     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
216     HRESULT hr;
217     TRACE("(%p): relay\n", This);
218
219     if(flags>D3DXSPRITE_FLAGLIMIT || This->ready) return D3DERR_INVALIDCALL;
220
221 /* TODO: Implement flags:
222 D3DXSPRITE_BILLBOARD: makes the sprite always face the camera
223 D3DXSPRITE_DONOTMODIFY_RENDERSTATE: name says it all
224 D3DXSPRITE_OBJECTSPACE: do not change device transforms
225 D3DXSPRITE_SORT_DEPTH_BACKTOFRONT: sort by position
226 D3DXSPRITE_SORT_DEPTH_FRONTTOBACK: sort by position
227 D3DXSPRITE_SORT_TEXTURE: sort by texture (so that it doesn't change too often)
228 */
229 /* Seems like alpha blending is always enabled, regardless of D3DXSPRITE_ALPHABLEND flag */
230     if(flags & (D3DXSPRITE_BILLBOARD |
231                 D3DXSPRITE_DONOTMODIFY_RENDERSTATE | D3DXSPRITE_OBJECTSPACE |
232                 D3DXSPRITE_SORT_DEPTH_BACKTOFRONT))
233         FIXME("Flags unsupported: %#x\n", flags);
234     /* These flags should only matter to performances */
235     else if(flags & (D3DXSPRITE_SORT_DEPTH_FRONTTOBACK | D3DXSPRITE_SORT_TEXTURE))
236         TRACE("Flags unsupported: %#x\n", flags);
237
238     if(This->vdecl==NULL) {
239         static const D3DVERTEXELEMENT9 elements[] =
240         {
241             { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
242             { 0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
243             { 0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
244             D3DDECL_END()
245         };
246         IDirect3DDevice9_CreateVertexDeclaration(This->device, elements, &This->vdecl);
247     }
248
249     if(!(flags & D3DXSPRITE_DONOTSAVESTATE)) {
250         if(This->stateblock==NULL) {
251             /* Tell our state block what it must store */
252             hr=IDirect3DDevice9_BeginStateBlock(This->device);
253             if(hr!=D3D_OK) return hr;
254
255             set_states(This);
256
257             IDirect3DDevice9_SetVertexDeclaration(This->device, This->vdecl);
258             IDirect3DDevice9_SetStreamSource(This->device, 0, NULL, 0, sizeof(SPRITEVERTEX));
259             IDirect3DDevice9_SetIndices(This->device, NULL);
260             IDirect3DDevice9_SetTexture(This->device, 0, NULL);
261
262             IDirect3DDevice9_EndStateBlock(This->device, &This->stateblock);
263         }
264         IDirect3DStateBlock9_Capture(This->stateblock); /* Save current state */
265     }
266
267     /* Apply device state */
268     set_states(This);
269
270     This->flags=flags;
271     This->ready=TRUE;
272
273     return D3D_OK;
274 }
275
276 static HRESULT WINAPI ID3DXSpriteImpl_Draw(LPD3DXSPRITE iface, LPDIRECT3DTEXTURE9 texture, CONST RECT *rect, CONST D3DXVECTOR3 *center,
277                                            CONST D3DXVECTOR3 *position, D3DCOLOR color)
278 {
279     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
280     D3DSURFACE_DESC texdesc;
281
282     TRACE("(%p)->(%p, %p, %p, %p, %#x): relay\n", This, texture, rect, center, position, color);
283
284     if(texture==NULL) return D3DERR_INVALIDCALL;
285     if(!This->ready) return D3DERR_INVALIDCALL;
286
287     if(This->allocated_sprites==0) {
288         This->sprites=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 32*sizeof(SPRITE));
289         This->allocated_sprites=32;
290     } else if(This->allocated_sprites<=This->sprite_count) {
291         This->allocated_sprites=This->allocated_sprites*3/2;
292         This->sprites=HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->sprites, This->allocated_sprites*sizeof(SPRITE));
293     }
294     This->sprites[This->sprite_count].texture=texture;
295     if(!(This->flags & D3DXSPRITE_DO_NOT_ADDREF_TEXTURE))
296         IDirect3DTexture9_AddRef(texture);
297
298     /* Reuse the texture desc if possible */
299     if(This->sprite_count) {
300         if(This->sprites[This->sprite_count-1].texture!=texture) {
301             IDirect3DTexture9_GetLevelDesc(texture, 0, &texdesc);
302         } else {
303             texdesc.Width=This->sprites[This->sprite_count-1].texw;
304             texdesc.Height=This->sprites[This->sprite_count-1].texh;
305         }
306     } else IDirect3DTexture9_GetLevelDesc(texture, 0, &texdesc);
307
308     This->sprites[This->sprite_count].texw=texdesc.Width;
309     This->sprites[This->sprite_count].texh=texdesc.Height;
310
311     if(rect==NULL) {
312         This->sprites[This->sprite_count].rect.left=0;
313         This->sprites[This->sprite_count].rect.top=0;
314         This->sprites[This->sprite_count].rect.right=texdesc.Width;
315         This->sprites[This->sprite_count].rect.bottom=texdesc.Height;
316     } else This->sprites[This->sprite_count].rect=*rect;
317
318     if(center==NULL) {
319         This->sprites[This->sprite_count].center.x=0.0f;
320         This->sprites[This->sprite_count].center.y=0.0f;
321         This->sprites[This->sprite_count].center.z=0.0f;
322     } else This->sprites[This->sprite_count].center=*center;
323
324     if(position==NULL) {
325         This->sprites[This->sprite_count].pos.x=0.0f;
326         This->sprites[This->sprite_count].pos.y=0.0f;
327         This->sprites[This->sprite_count].pos.z=0.0f;
328     } else This->sprites[This->sprite_count].pos=*position;
329
330     This->sprites[This->sprite_count].color=color;
331     This->sprites[This->sprite_count].transform=This->transform;
332     This->sprite_count++;
333
334     return D3D_OK;
335 }
336
337 static HRESULT WINAPI ID3DXSpriteImpl_Flush(LPD3DXSPRITE iface)
338 {
339     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
340     SPRITEVERTEX *vertices;
341     int i, count=0, start;
342     TRACE("(%p)->(): relay\n", This);
343
344     if(!This->ready) return D3DERR_INVALIDCALL;
345     if(!This->sprite_count) return D3D_OK;
346
347 /* TODO: use of a vertex buffer here */
348     vertices=HeapAlloc(GetProcessHeap(), 0, sizeof(SPRITEVERTEX)*6*This->sprite_count);
349
350     for(start=0;start<This->sprite_count;start+=count,count=0) {
351         i=start;
352         while(i<This->sprite_count &&
353               (count==0 || This->sprites[i].texture==This->sprites[i-1].texture)) {
354             float spritewidth=(float)This->sprites[i].rect.right-(float)This->sprites[i].rect.left;
355             float spriteheight=(float)This->sprites[i].rect.bottom-(float)This->sprites[i].rect.top;
356
357             vertices[6*i  ].pos.x = This->sprites[i].pos.x - This->sprites[i].center.x;
358             vertices[6*i  ].pos.y = This->sprites[i].pos.y - This->sprites[i].center.y;
359             vertices[6*i  ].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
360             vertices[6*i+1].pos.x = spritewidth + This->sprites[i].pos.x - This->sprites[i].center.x;
361             vertices[6*i+1].pos.y = This->sprites[i].pos.y - This->sprites[i].center.y;
362             vertices[6*i+1].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
363             vertices[6*i+2].pos.x = spritewidth + This->sprites[i].pos.x - This->sprites[i].center.x;
364             vertices[6*i+2].pos.y = spriteheight + This->sprites[i].pos.y - This->sprites[i].center.y;
365             vertices[6*i+2].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
366             vertices[6*i+3].pos.x = This->sprites[i].pos.x - This->sprites[i].center.x;
367             vertices[6*i+3].pos.y = spriteheight + This->sprites[i].pos.y - This->sprites[i].center.y;
368             vertices[6*i+3].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
369             vertices[6*i  ].col   = This->sprites[i].color;
370             vertices[6*i+1].col   = This->sprites[i].color;
371             vertices[6*i+2].col   = This->sprites[i].color;
372             vertices[6*i+3].col   = This->sprites[i].color;
373             vertices[6*i  ].tex.x = (float)This->sprites[i].rect.left / (float)This->sprites[i].texw;
374             vertices[6*i  ].tex.y = (float)This->sprites[i].rect.top / (float)This->sprites[i].texh;
375             vertices[6*i+1].tex.x = (float)This->sprites[i].rect.right / (float)This->sprites[i].texw;
376             vertices[6*i+1].tex.y = (float)This->sprites[i].rect.top / (float)This->sprites[i].texh;
377             vertices[6*i+2].tex.x = (float)This->sprites[i].rect.right / (float)This->sprites[i].texw;
378             vertices[6*i+2].tex.y = (float)This->sprites[i].rect.bottom / (float)This->sprites[i].texh;
379             vertices[6*i+3].tex.x = (float)This->sprites[i].rect.left / (float)This->sprites[i].texw;
380             vertices[6*i+3].tex.y = (float)This->sprites[i].rect.bottom / (float)This->sprites[i].texh;
381
382             vertices[6*i+4]=vertices[6*i];
383             vertices[6*i+5]=vertices[6*i+2];
384
385             D3DXVec3TransformCoordArray(&vertices[6*i].pos, sizeof(SPRITEVERTEX),
386                                         &vertices[6*i].pos, sizeof(SPRITEVERTEX),
387                                         &This->sprites[i].transform, 6);
388             count++;
389             i++;
390         }
391
392         IDirect3DDevice9_SetTexture(This->device, 0, (LPDIRECT3DBASETEXTURE9)(This->sprites[start].texture));
393         IDirect3DDevice9_SetVertexDeclaration(This->device, This->vdecl);
394
395         IDirect3DDevice9_DrawPrimitiveUP(This->device, D3DPT_TRIANGLELIST, 2*count, vertices+6*start, sizeof(SPRITEVERTEX));
396     }
397     HeapFree(GetProcessHeap(), 0, vertices);
398
399     if(!(This->flags & D3DXSPRITE_DO_NOT_ADDREF_TEXTURE))
400         for(i=0;i<This->sprite_count;i++)
401             IDirect3DTexture9_Release(This->sprites[i].texture);
402
403     This->sprite_count=0;
404
405     /* Flush may be called more than once, so we don't reset This->ready here */
406
407     return D3D_OK;
408 }
409
410 static HRESULT WINAPI ID3DXSpriteImpl_End(LPD3DXSPRITE iface)
411 {
412     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
413
414     TRACE("(%p)->(): relay\n", This);
415
416     if(!This->ready) return D3DERR_INVALIDCALL;
417
418     ID3DXSprite_Flush(iface);
419
420     if(!(This->flags & D3DXSPRITE_DONOTSAVESTATE))
421         if(This->stateblock) IDirect3DStateBlock9_Apply(This->stateblock); /* Restore old state */
422
423     This->ready=FALSE;
424
425     return D3D_OK;
426 }
427
428 static HRESULT WINAPI ID3DXSpriteImpl_OnLostDevice(LPD3DXSPRITE iface)
429 {
430     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
431
432     TRACE("(%p)->()\n", This);
433
434     if(This->stateblock) IDirect3DStateBlock9_Release(This->stateblock);
435     if(This->vdecl) IDirect3DVertexDeclaration9_Release(This->vdecl);
436     This->vdecl=NULL;
437     This->stateblock=NULL;
438
439     /* Reset some variables */
440     ID3DXSprite_OnResetDevice(iface);
441
442     return D3D_OK;
443 }
444
445 static HRESULT WINAPI ID3DXSpriteImpl_OnResetDevice(LPD3DXSPRITE iface)
446 {
447     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
448     int i;
449
450     TRACE("(%p)->()\n", This);
451
452     for(i=0;i<This->sprite_count;i++)
453         if(This->sprites[i].texture)
454             IDirect3DTexture9_Release(This->sprites[i].texture);
455
456     This->sprite_count=0;
457
458     This->flags=0;
459     This->ready=FALSE;
460
461     /* keep matrices */
462     /* device objects get restored on Begin */
463
464     return D3D_OK;
465 }
466
467 static const ID3DXSpriteVtbl D3DXSprite_Vtbl =
468 {
469     /*** IUnknown methods ***/
470     ID3DXSpriteImpl_QueryInterface,
471     ID3DXSpriteImpl_AddRef,
472     ID3DXSpriteImpl_Release,
473     /*** ID3DXSprite methods ***/
474     ID3DXSpriteImpl_GetDevice,
475     ID3DXSpriteImpl_GetTransform,
476     ID3DXSpriteImpl_SetTransform,
477     ID3DXSpriteImpl_SetWorldViewRH,
478     ID3DXSpriteImpl_SetWorldViewLH,
479     ID3DXSpriteImpl_Begin,
480     ID3DXSpriteImpl_Draw,
481     ID3DXSpriteImpl_Flush,
482     ID3DXSpriteImpl_End,
483     ID3DXSpriteImpl_OnLostDevice,
484     ID3DXSpriteImpl_OnResetDevice
485 };
486
487 HRESULT WINAPI D3DXCreateSprite(LPDIRECT3DDEVICE9 device, LPD3DXSPRITE *sprite)
488 {
489     ID3DXSpriteImpl *object;
490     D3DCAPS9 caps;
491
492     TRACE("(%p, %p): relay\n", device, sprite);
493
494     if(device==NULL || sprite==NULL) return D3DERR_INVALIDCALL;
495
496     object=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ID3DXSpriteImpl));
497     if(object==NULL) {
498         *sprite=NULL;
499         return E_OUTOFMEMORY;
500     }
501     object->lpVtbl=&D3DXSprite_Vtbl;
502     object->ref=1;
503     object->device=device;
504     IUnknown_AddRef(device);
505
506     object->vdecl=NULL;
507     object->stateblock=NULL;
508
509     D3DXMatrixIdentity(&object->transform);
510     D3DXMatrixIdentity(&object->view);
511
512     IDirect3DDevice9_GetDeviceCaps(device, &caps);
513     object->texfilter_caps=caps.TextureFilterCaps;
514     object->maxanisotropy=caps.MaxAnisotropy;
515     object->alphacmp_caps=caps.AlphaCmpCaps;
516
517     ID3DXSprite_OnResetDevice((ID3DXSprite*)object);
518
519     object->sprites=NULL;
520     object->allocated_sprites=0;
521     *sprite=(ID3DXSprite*)object;
522
523     return D3D_OK;
524 }