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