2 * Copyright (C) 2008 Tony Wasserka
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.
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.
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
20 #include "wine/debug.h"
21 #include "d3dx9_36_private.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(d3dx
);
25 /* the combination of all possible D3DXSPRITE flags */
26 #define D3DXSPRITE_FLAGLIMIT 511
28 typedef struct _SPRITEVERTEX
{
34 static HRESULT WINAPI
ID3DXSpriteImpl_QueryInterface(LPD3DXSPRITE iface
, REFIID riid
, LPVOID
*object
)
36 ID3DXSpriteImpl
*This
=(ID3DXSpriteImpl
*)iface
;
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
);
44 WARN("(%p)->(%s, %p): not found\n", iface
, debugstr_guid(riid
), *object
);
48 static ULONG WINAPI
ID3DXSpriteImpl_AddRef(LPD3DXSPRITE iface
)
50 ID3DXSpriteImpl
*This
=(ID3DXSpriteImpl
*)iface
;
51 ULONG ref
=InterlockedIncrement(&This
->ref
);
52 TRACE("(%p): AddRef from %d\n", This
, ref
-1);
56 static ULONG WINAPI
ID3DXSpriteImpl_Release(LPD3DXSPRITE iface
)
58 ID3DXSpriteImpl
*This
=(ID3DXSpriteImpl
*)iface
;
59 ULONG ref
=InterlockedDecrement(&This
->ref
);
60 TRACE("(%p): ReleaseRef to %d\n", This
, ref
);
65 for(i
=0;i
<This
->sprite_count
;i
++)
66 if(This
->sprites
[i
].texture
)
67 IDirect3DTexture9_Release(This
->sprites
[i
].texture
);
69 HeapFree(GetProcessHeap(), 0, This
->sprites
);
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
);
79 static HRESULT WINAPI
ID3DXSpriteImpl_GetDevice(LPD3DXSPRITE iface
, LPDIRECT3DDEVICE9
*device
)
81 ID3DXSpriteImpl
*This
=(ID3DXSpriteImpl
*)iface
;
82 TRACE("(%p): relay\n", This
);
84 if(device
==NULL
) return D3DERR_INVALIDCALL
;
86 IDirect3DDevice9_AddRef(This
->device
);
91 static HRESULT WINAPI
ID3DXSpriteImpl_GetTransform(LPD3DXSPRITE iface
, D3DXMATRIX
*transform
)
93 ID3DXSpriteImpl
*This
=(ID3DXSpriteImpl
*)iface
;
94 TRACE("(%p)\n", This
);
96 if(transform
==NULL
) return D3DERR_INVALIDCALL
;
97 *transform
=This
->transform
;
102 static HRESULT WINAPI
ID3DXSpriteImpl_SetTransform(LPD3DXSPRITE iface
, CONST D3DXMATRIX
*transform
)
104 ID3DXSpriteImpl
*This
=(ID3DXSpriteImpl
*)iface
;
105 TRACE("(%p)\n", This
);
107 if(transform
==NULL
) return D3DERR_INVALIDCALL
;
108 This
->transform
=*transform
;
113 static HRESULT WINAPI
ID3DXSpriteImpl_SetWorldViewRH(LPD3DXSPRITE iface
, CONST D3DXMATRIX
*world
, CONST D3DXMATRIX
*view
)
115 ID3DXSpriteImpl
*This
=(ID3DXSpriteImpl
*)iface
;
116 FIXME("(%p): stub\n", This
);
120 static HRESULT WINAPI
ID3DXSpriteImpl_SetWorldViewLH(LPD3DXSPRITE iface
, CONST D3DXMATRIX
*world
, CONST D3DXMATRIX
*view
)
122 ID3DXSpriteImpl
*This
=(ID3DXSpriteImpl
*)iface
;
123 FIXME("(%p): stub\n", This
);
127 /* Helper function */
128 static void set_states(ID3DXSpriteImpl
*object
)
133 /* Miscelaneous stuff */
134 IDirect3DDevice9_SetVertexShader(object
->device
, NULL
);
135 IDirect3DDevice9_SetPixelShader(object
->device
, NULL
);
136 IDirect3DDevice9_SetNPatchMode(object
->device
, 0.0f
);
139 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_ALPHABLENDENABLE
, TRUE
);
140 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_ALPHAFUNC
, D3DCMP_GREATER
);
141 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_ALPHAREF
, 0x00);
142 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_ALPHATESTENABLE
, object
->alphacmp_caps
);
143 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_BLENDOP
, D3DBLENDOP_ADD
);
144 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_CLIPPING
, TRUE
);
145 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_CLIPPLANEENABLE
, FALSE
);
146 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_COLORWRITEENABLE
, D3DCOLORWRITEENABLE_ALPHA
| D3DCOLORWRITEENABLE_BLUE
|
147 D3DCOLORWRITEENABLE_GREEN
| D3DCOLORWRITEENABLE_RED
);
148 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_CULLMODE
, D3DCULL_NONE
);
149 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_DESTBLEND
, D3DBLEND_INVSRCALPHA
);
150 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_DIFFUSEMATERIALSOURCE
, D3DMCS_COLOR1
);
151 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_ENABLEADAPTIVETESSELLATION
, FALSE
);
152 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_FILLMODE
, D3DFILL_SOLID
);
153 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_FOGENABLE
, FALSE
);
154 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_INDEXEDVERTEXBLENDENABLE
, FALSE
);
155 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_LIGHTING
, FALSE
);
156 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_RANGEFOGENABLE
, FALSE
);
157 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_SEPARATEALPHABLENDENABLE
, FALSE
);
158 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_SHADEMODE
, D3DSHADE_GOURAUD
);
159 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_SPECULARENABLE
, FALSE
);
160 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_SRCBLEND
, D3DBLEND_SRCALPHA
);
161 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_SRGBWRITEENABLE
, FALSE
);
162 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_STENCILENABLE
, FALSE
);
163 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_VERTEXBLEND
, FALSE
);
164 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_WRAP0
, 0);
166 /* Texture stage states */
167 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_ALPHAARG1
, D3DTA_TEXTURE
);
168 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_ALPHAARG2
, D3DTA_DIFFUSE
);
169 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_ALPHAOP
, D3DTOP_MODULATE
);
170 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_COLORARG1
, D3DTA_TEXTURE
);
171 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_COLORARG2
, D3DTA_DIFFUSE
);
172 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_COLOROP
, D3DTOP_MODULATE
);
173 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_TEXCOORDINDEX
, 0);
174 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_TEXTURETRANSFORMFLAGS
, D3DTTFF_DISABLE
);
175 IDirect3DDevice9_SetTextureStageState(object
->device
, 1, D3DTSS_ALPHAOP
, D3DTOP_DISABLE
);
176 IDirect3DDevice9_SetTextureStageState(object
->device
, 1, D3DTSS_COLOROP
, D3DTOP_DISABLE
);
179 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_ADDRESSU
, D3DTADDRESS_CLAMP
);
180 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_ADDRESSV
, D3DTADDRESS_CLAMP
);
182 if(object
->texfilter_caps
& D3DPTFILTERCAPS_MAGFANISOTROPIC
)
183 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MAGFILTER
, D3DTEXF_ANISOTROPIC
);
184 else IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MAGFILTER
, D3DTEXF_LINEAR
);
186 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MAXMIPLEVEL
, 0);
187 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MAXANISOTROPY
, object
->maxanisotropy
);
189 if(object
->texfilter_caps
& D3DPTFILTERCAPS_MINFANISOTROPIC
)
190 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MINFILTER
, D3DTEXF_ANISOTROPIC
);
191 else IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MINFILTER
, D3DTEXF_LINEAR
);
193 if(object
->texfilter_caps
& D3DPTFILTERCAPS_MIPFLINEAR
)
194 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MIPFILTER
, D3DTEXF_LINEAR
);
195 else IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MIPFILTER
, D3DTEXF_POINT
);
197 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MIPMAPLODBIAS
, 0);
198 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_SRGBTEXTURE
, 0);
201 D3DXMatrixIdentity(&mat
);
202 IDirect3DDevice9_SetTransform(object
->device
, D3DTS_WORLD
, &mat
);
203 IDirect3DDevice9_SetTransform(object
->device
, D3DTS_VIEW
, &mat
);
204 IDirect3DDevice9_GetViewport(object
->device
, &vp
);
205 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
);
206 IDirect3DDevice9_SetTransform(object
->device
, D3DTS_PROJECTION
, &mat
);
209 static HRESULT WINAPI
ID3DXSpriteImpl_Begin(LPD3DXSPRITE iface
, DWORD flags
)
211 ID3DXSpriteImpl
*This
=(ID3DXSpriteImpl
*)iface
;
213 TRACE("(%p): relay\n", This
);
215 if(flags
>D3DXSPRITE_FLAGLIMIT
|| This
->ready
) return D3DERR_INVALIDCALL
;
217 /* TODO: Implement flags:
218 D3DXSPRITE_ALPHABLEND: enables alpha blending
219 D3DXSPRITE_BILLBOARD: makes the sprite always face the camera
220 D3DXSPRITE_DONOTMODIFY_RENDERSTATE: name says it all
221 D3DXSPRITE_OBJECTSPACE: do not change device transforms
222 D3DXSPRITE_SORT_DEPTH_BACKTOFRONT: sort by position
223 D3DXSPRITE_SORT_DEPTH_FRONTTOBACK: sort by position
224 D3DXSPRITE_SORT_TEXTURE: sort by texture (so that it doesn't change too often)
226 if(This
->vdecl
==NULL
) {
227 static const D3DVERTEXELEMENT9 elements
[] =
229 { 0, 0, D3DDECLTYPE_FLOAT3
, D3DDECLMETHOD_DEFAULT
, D3DDECLUSAGE_POSITION
, 0 },
230 { 0, 12, D3DDECLTYPE_D3DCOLOR
, D3DDECLMETHOD_DEFAULT
, D3DDECLUSAGE_COLOR
, 0 },
231 { 0, 16, D3DDECLTYPE_FLOAT2
, D3DDECLMETHOD_DEFAULT
, D3DDECLUSAGE_TEXCOORD
, 0 },
234 IDirect3DDevice9_CreateVertexDeclaration(This
->device
, elements
, &This
->vdecl
);
237 if(!(flags
& D3DXSPRITE_DONOTSAVESTATE
)) {
238 if(This
->stateblock
==NULL
) {
239 /* Tell our state block what it must store */
240 hr
=IDirect3DDevice9_BeginStateBlock(This
->device
);
241 if(hr
!=D3D_OK
) return hr
;
245 IDirect3DDevice9_SetVertexDeclaration(This
->device
, This
->vdecl
);
246 IDirect3DDevice9_SetStreamSource(This
->device
, 0, NULL
, 0, sizeof(SPRITEVERTEX
));
247 IDirect3DDevice9_SetIndices(This
->device
, NULL
);
248 IDirect3DDevice9_SetTexture(This
->device
, 0, NULL
);
250 IDirect3DDevice9_EndStateBlock(This
->device
, &This
->stateblock
);
252 IDirect3DStateBlock9_Capture(This
->stateblock
); /* Save current state */
255 /* Apply device state */
258 D3DXMatrixIdentity(&This
->transform
);
259 D3DXMatrixIdentity(&This
->view
);
267 static HRESULT WINAPI
ID3DXSpriteImpl_Draw(LPD3DXSPRITE iface
, LPDIRECT3DTEXTURE9 texture
, CONST RECT
*rect
, CONST D3DXVECTOR3
*center
,
268 CONST D3DXVECTOR3
*position
, D3DCOLOR color
)
270 ID3DXSpriteImpl
*This
=(ID3DXSpriteImpl
*)iface
;
271 D3DSURFACE_DESC texdesc
;
272 TRACE("(%p): relay\n", This
);
274 if(texture
==NULL
) return D3DERR_INVALIDCALL
;
275 if(!This
->ready
) return D3DERR_INVALIDCALL
;
277 if(This
->allocated_sprites
==0) {
278 This
->sprites
=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, 32*sizeof(SPRITE
));
279 This
->allocated_sprites
=32;
280 } else if(This
->allocated_sprites
<=This
->sprite_count
) {
281 This
->allocated_sprites
=This
->allocated_sprites
*3/2;
282 This
->sprites
=HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, This
->sprites
, This
->allocated_sprites
*sizeof(SPRITE
));
284 This
->sprites
[This
->sprite_count
].texture
=texture
;
285 if(!(This
->flags
& D3DXSPRITE_DO_NOT_ADDREF_TEXTURE
))
286 IDirect3DTexture9_AddRef(texture
);
288 /* Reuse the texture desc if possible */
289 if(This
->sprite_count
) {
290 if(This
->sprites
[This
->sprite_count
-1].texture
!=texture
) {
291 IDirect3DTexture9_GetLevelDesc(texture
, 0, &texdesc
);
293 texdesc
.Width
=This
->sprites
[This
->sprite_count
-1].texw
;
294 texdesc
.Height
=This
->sprites
[This
->sprite_count
-1].texh
;
296 } else IDirect3DTexture9_GetLevelDesc(texture
, 0, &texdesc
);
298 This
->sprites
[This
->sprite_count
].texw
=texdesc
.Width
;
299 This
->sprites
[This
->sprite_count
].texh
=texdesc
.Height
;
302 This
->sprites
[This
->sprite_count
].rect
.left
=0;
303 This
->sprites
[This
->sprite_count
].rect
.top
=0;
304 This
->sprites
[This
->sprite_count
].rect
.right
=texdesc
.Width
;
305 This
->sprites
[This
->sprite_count
].rect
.bottom
=texdesc
.Height
;
306 } else This
->sprites
[This
->sprite_count
].rect
=*rect
;
309 This
->sprites
[This
->sprite_count
].center
.x
=0.0f
;
310 This
->sprites
[This
->sprite_count
].center
.y
=0.0f
;
311 This
->sprites
[This
->sprite_count
].center
.z
=0.0f
;
312 } else This
->sprites
[This
->sprite_count
].center
=*center
;
315 This
->sprites
[This
->sprite_count
].pos
.x
=0.0f
;
316 This
->sprites
[This
->sprite_count
].pos
.y
=0.0f
;
317 This
->sprites
[This
->sprite_count
].pos
.z
=0.0f
;
318 } else This
->sprites
[This
->sprite_count
].pos
=*position
;
320 This
->sprites
[This
->sprite_count
].color
=color
;
321 This
->sprite_count
++;
326 static HRESULT WINAPI
ID3DXSpriteImpl_Flush(LPD3DXSPRITE iface
)
328 ID3DXSpriteImpl
*This
=(ID3DXSpriteImpl
*)iface
;
329 SPRITEVERTEX
*vertices
;
331 TRACE("(%p): relay\n", This
);
333 if(!This
->ready
) return D3DERR_INVALIDCALL
;
334 if(!This
->sprite_count
) return D3D_OK
;
336 /* TODO: use of a vertex buffer here */
337 vertices
=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(SPRITEVERTEX
)*4*This
->sprite_count
);
339 for(i
=0;i
<This
->sprite_count
;i
++) {
340 float spritewidth
=(float)This
->sprites
[i
].rect
.right
-(float)This
->sprites
[i
].rect
.left
;
341 float spriteheight
=(float)This
->sprites
[i
].rect
.bottom
-(float)This
->sprites
[i
].rect
.top
;
343 vertices
[4*i
].pos
.x
= This
->sprites
[i
].pos
.x
- This
->sprites
[i
].center
.x
;
344 vertices
[4*i
].pos
.y
= This
->sprites
[i
].pos
.y
- This
->sprites
[i
].center
.y
;
345 vertices
[4*i
].pos
.z
= This
->sprites
[i
].pos
.z
- This
->sprites
[i
].center
.z
;
346 vertices
[4*i
+1].pos
.x
= spritewidth
+ This
->sprites
[i
].pos
.x
- This
->sprites
[i
].center
.x
;
347 vertices
[4*i
+1].pos
.y
= This
->sprites
[i
].pos
.y
- This
->sprites
[i
].center
.y
;
348 vertices
[4*i
+1].pos
.z
= This
->sprites
[i
].pos
.z
- This
->sprites
[i
].center
.z
;
349 vertices
[4*i
+2].pos
.x
= spritewidth
+ This
->sprites
[i
].pos
.x
- This
->sprites
[i
].center
.x
;
350 vertices
[4*i
+2].pos
.y
= spriteheight
+ This
->sprites
[i
].pos
.y
- This
->sprites
[i
].center
.y
;
351 vertices
[4*i
+2].pos
.z
= This
->sprites
[i
].pos
.z
- This
->sprites
[i
].center
.z
;
352 vertices
[4*i
+3].pos
.x
= This
->sprites
[i
].pos
.x
- This
->sprites
[i
].center
.x
;
353 vertices
[4*i
+3].pos
.y
= spriteheight
+ This
->sprites
[i
].pos
.y
- This
->sprites
[i
].center
.y
;
354 vertices
[4*i
+3].pos
.z
= This
->sprites
[i
].pos
.z
- This
->sprites
[i
].center
.z
;
355 vertices
[4*i
].col
= This
->sprites
[i
].color
;
356 vertices
[4*i
+1].col
= This
->sprites
[i
].color
;
357 vertices
[4*i
+2].col
= This
->sprites
[i
].color
;
358 vertices
[4*i
+3].col
= This
->sprites
[i
].color
;
359 vertices
[4*i
].tex
.x
= (float)This
->sprites
[i
].rect
.left
/ (float)This
->sprites
[i
].texw
;
360 vertices
[4*i
].tex
.y
= (float)This
->sprites
[i
].rect
.top
/ (float)This
->sprites
[i
].texh
;
361 vertices
[4*i
+1].tex
.x
= (float)This
->sprites
[i
].rect
.right
/ (float)This
->sprites
[i
].texw
;
362 vertices
[4*i
+1].tex
.y
= (float)This
->sprites
[i
].rect
.top
/ (float)This
->sprites
[i
].texh
;
363 vertices
[4*i
+2].tex
.x
= (float)This
->sprites
[i
].rect
.right
/ (float)This
->sprites
[i
].texw
;
364 vertices
[4*i
+2].tex
.y
= (float)This
->sprites
[i
].rect
.bottom
/ (float)This
->sprites
[i
].texh
;
365 vertices
[4*i
+3].tex
.x
= (float)This
->sprites
[i
].rect
.left
/ (float)This
->sprites
[i
].texw
;
366 vertices
[4*i
+3].tex
.y
= (float)This
->sprites
[i
].rect
.bottom
/ (float)This
->sprites
[i
].texh
;
369 D3DXVec3TransformCoordArray(&vertices
[0].pos
, sizeof(SPRITEVERTEX
), &vertices
[0].pos
, sizeof(SPRITEVERTEX
), &This
->transform
, 4*This
->sprite_count
);
370 D3DXVec3TransformCoordArray(&vertices
[0].pos
, sizeof(SPRITEVERTEX
), &vertices
[0].pos
, sizeof(SPRITEVERTEX
), &This
->view
, 4*This
->sprite_count
);
372 IDirect3DDevice9_SetVertexDeclaration(This
->device
, This
->vdecl
);
374 for(i
=0;i
<This
->sprite_count
;i
++) {
376 IDirect3DDevice9_SetTexture(This
->device
, 0, (LPDIRECT3DBASETEXTURE9
)(This
->sprites
[i
].texture
));
377 else if(This
->sprites
[i
].texture
!=This
->sprites
[i
-1].texture
)
378 IDirect3DDevice9_SetTexture(This
->device
, 0, (LPDIRECT3DBASETEXTURE9
)(This
->sprites
[i
].texture
));
380 IDirect3DDevice9_DrawPrimitiveUP(This
->device
, D3DPT_TRIANGLEFAN
, 2, vertices
+4*i
, sizeof(SPRITEVERTEX
));
382 HeapFree(GetProcessHeap(), 0, vertices
);
384 if(!(This
->flags
& D3DXSPRITE_DO_NOT_ADDREF_TEXTURE
))
385 for(i
=0;i
<This
->sprite_count
;i
++)
386 IDirect3DTexture9_Release(This
->sprites
[i
].texture
);
388 This
->sprite_count
=0;
390 /* Flush may be called more than once, so we don't reset This->ready here */
395 static HRESULT WINAPI
ID3DXSpriteImpl_End(LPD3DXSPRITE iface
)
397 ID3DXSpriteImpl
*This
=(ID3DXSpriteImpl
*)iface
;
398 TRACE("(%p): relay\n", This
);
400 if(!This
->ready
) return D3DERR_INVALIDCALL
;
402 ID3DXSprite_Flush(iface
);
404 if(!(This
->flags
& D3DXSPRITE_DONOTSAVESTATE
))
405 if(This
->stateblock
) IDirect3DStateBlock9_Apply(This
->stateblock
); /* Restore old state */
412 static HRESULT WINAPI
ID3DXSpriteImpl_OnLostDevice(LPD3DXSPRITE iface
)
414 ID3DXSpriteImpl
*This
=(ID3DXSpriteImpl
*)iface
;
415 TRACE("(%p)\n", This
);
417 if(This
->stateblock
) IDirect3DStateBlock9_Release(This
->stateblock
);
418 if(This
->vdecl
) IDirect3DVertexDeclaration9_Release(This
->vdecl
);
420 This
->stateblock
=NULL
;
422 /* Reset some variables */
423 ID3DXSprite_OnResetDevice(iface
);
428 static HRESULT WINAPI
ID3DXSpriteImpl_OnResetDevice(LPD3DXSPRITE iface
)
430 ID3DXSpriteImpl
*This
=(ID3DXSpriteImpl
*)iface
;
433 TRACE("(%p)\n", This
);
435 for(i
=0;i
<This
->sprite_count
;i
++)
436 if(This
->sprites
[i
].texture
)
437 IDirect3DTexture9_Release(This
->sprites
[i
].texture
);
439 This
->sprite_count
=0;
445 /* device objects get restored on Begin */
450 static const ID3DXSpriteVtbl D3DXSprite_Vtbl
=
452 /*** IUnknown methods ***/
453 ID3DXSpriteImpl_QueryInterface
,
454 ID3DXSpriteImpl_AddRef
,
455 ID3DXSpriteImpl_Release
,
456 /*** ID3DXSprite methods ***/
457 ID3DXSpriteImpl_GetDevice
,
458 ID3DXSpriteImpl_GetTransform
,
459 ID3DXSpriteImpl_SetTransform
,
460 ID3DXSpriteImpl_SetWorldViewRH
,
461 ID3DXSpriteImpl_SetWorldViewLH
,
462 ID3DXSpriteImpl_Begin
,
463 ID3DXSpriteImpl_Draw
,
464 ID3DXSpriteImpl_Flush
,
466 ID3DXSpriteImpl_OnLostDevice
,
467 ID3DXSpriteImpl_OnResetDevice
470 HRESULT WINAPI
D3DXCreateSprite(LPDIRECT3DDEVICE9 device
, LPD3DXSPRITE
*sprite
)
472 ID3DXSpriteImpl
*object
;
474 TRACE("(void): relay\n");
476 if(device
==NULL
|| sprite
==NULL
) return D3DERR_INVALIDCALL
;
478 object
=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(ID3DXSpriteImpl
));
481 return E_OUTOFMEMORY
;
483 object
->lpVtbl
=&D3DXSprite_Vtbl
;
485 object
->device
=device
;
486 IUnknown_AddRef(device
);
489 object
->stateblock
=NULL
;
491 D3DXMatrixIdentity(&object
->transform
);
492 D3DXMatrixIdentity(&object
->view
);
494 IDirect3DDevice9_GetDeviceCaps(device
, &caps
);
495 object
->texfilter_caps
=caps
.TextureFilterCaps
;
496 object
->maxanisotropy
=caps
.MaxAnisotropy
;
497 object
->alphacmp_caps
=caps
.AlphaCmpCaps
;
499 ID3DXSprite_OnResetDevice((ID3DXSprite
*)object
);
501 object
->sprites
=NULL
;
502 object
->allocated_sprites
=0;
503 *sprite
=(ID3DXSprite
*)object
;