5 * Copyright (C) 2008 Tony Wasserka
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "d3dx9_private.h"
25 #endif /* __REACTOS__ */
27 WINE_DEFAULT_DEBUG_CHANNEL(d3dx
);
29 /* the combination of all possible D3DXSPRITE flags */
30 #define D3DXSPRITE_FLAGLIMIT 511
41 IDirect3DTexture9
*texture
;
52 ID3DXSprite ID3DXSprite_iface
;
55 IDirect3DDevice9
*device
;
56 IDirect3DVertexDeclaration9
*vdecl
;
57 IDirect3DStateBlock9
*stateblock
;
63 /* Store the relevant caps to prevent multiple GetDeviceCaps calls */
68 struct sprite
*sprites
;
69 int sprite_count
; /* number of sprites to be drawn */
70 int allocated_sprites
; /* number of (pre-)allocated sprites */
73 static inline struct d3dx9_sprite
*impl_from_ID3DXSprite(ID3DXSprite
*iface
)
75 return CONTAINING_RECORD(iface
, struct d3dx9_sprite
, ID3DXSprite_iface
);
78 static HRESULT WINAPI
d3dx9_sprite_QueryInterface(ID3DXSprite
*iface
, REFIID riid
, void **out
)
80 TRACE("iface %p, riid %s, out %p.\n", iface
, debugstr_guid(riid
), out
);
82 if (IsEqualGUID(riid
, &IID_ID3DXSprite
)
83 || IsEqualGUID(riid
, &IID_IUnknown
))
85 IUnknown_AddRef(iface
);
90 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
96 static ULONG WINAPI
d3dx9_sprite_AddRef(ID3DXSprite
*iface
)
98 struct d3dx9_sprite
*sprite
= impl_from_ID3DXSprite(iface
);
99 ULONG refcount
= InterlockedIncrement(&sprite
->ref
);
101 TRACE("%p increasing refcount to %u.\n", sprite
, refcount
);
106 static ULONG WINAPI
d3dx9_sprite_Release(ID3DXSprite
*iface
)
108 struct d3dx9_sprite
*sprite
= impl_from_ID3DXSprite(iface
);
109 ULONG refcount
= InterlockedDecrement(&sprite
->ref
);
111 TRACE("%p decreasing refcount to %u.\n", sprite
, refcount
);
119 if (!(sprite
->flags
& D3DXSPRITE_DO_NOT_ADDREF_TEXTURE
))
121 for (i
= 0; i
< sprite
->sprite_count
; ++i
)
123 if (sprite
->sprites
[i
].texture
)
124 IDirect3DTexture9_Release(sprite
->sprites
[i
].texture
);
128 HeapFree(GetProcessHeap(), 0, sprite
->sprites
);
131 if (sprite
->stateblock
)
132 IDirect3DStateBlock9_Release(sprite
->stateblock
);
134 IDirect3DVertexDeclaration9_Release(sprite
->vdecl
);
136 IDirect3DDevice9_Release(sprite
->device
);
137 HeapFree(GetProcessHeap(), 0, sprite
);
143 static HRESULT WINAPI
d3dx9_sprite_GetDevice(struct ID3DXSprite
*iface
, struct IDirect3DDevice9
**device
)
145 struct d3dx9_sprite
*sprite
= impl_from_ID3DXSprite(iface
);
147 TRACE("iface %p, device %p.\n", iface
, device
);
150 return D3DERR_INVALIDCALL
;
151 *device
= sprite
->device
;
152 IDirect3DDevice9_AddRef(sprite
->device
);
157 static HRESULT WINAPI
d3dx9_sprite_GetTransform(ID3DXSprite
*iface
, D3DXMATRIX
*transform
)
159 struct d3dx9_sprite
*sprite
= impl_from_ID3DXSprite(iface
);
161 TRACE("iface %p, transform %p.\n", iface
, transform
);
164 return D3DERR_INVALIDCALL
;
165 *transform
= sprite
->transform
;
170 static HRESULT WINAPI
d3dx9_sprite_SetTransform(ID3DXSprite
*iface
, const D3DXMATRIX
*transform
)
172 struct d3dx9_sprite
*sprite
= impl_from_ID3DXSprite(iface
);
174 TRACE("iface %p, transform %p.\n", iface
, transform
);
177 return D3DERR_INVALIDCALL
;
178 sprite
->transform
= *transform
;
183 static HRESULT WINAPI
d3dx9_sprite_SetWorldViewRH(ID3DXSprite
*iface
,
184 const D3DXMATRIX
*world
, const D3DXMATRIX
*view
)
186 FIXME("iface %p, world %p, view %p stub!\n", iface
, world
, view
);
191 static HRESULT WINAPI
d3dx9_sprite_SetWorldViewLH(ID3DXSprite
*iface
,
192 const D3DXMATRIX
*world
, const D3DXMATRIX
*view
)
194 FIXME("iface %p, world %p, view %p stub!\n", iface
, world
, view
);
199 /* Helper function */
200 static void set_states(struct d3dx9_sprite
*object
)
205 /* Miscellaneous stuff */
206 IDirect3DDevice9_SetVertexShader(object
->device
, NULL
);
207 IDirect3DDevice9_SetPixelShader(object
->device
, NULL
);
208 IDirect3DDevice9_SetNPatchMode(object
->device
, 0.0f
);
211 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_ALPHABLENDENABLE
, TRUE
);
212 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_ALPHAFUNC
, D3DCMP_GREATER
);
213 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_ALPHAREF
, 0x00);
214 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_ALPHATESTENABLE
, object
->alphacmp_caps
);
215 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_BLENDOP
, D3DBLENDOP_ADD
);
216 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_CLIPPING
, TRUE
);
217 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_CLIPPLANEENABLE
, 0);
218 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_COLORWRITEENABLE
, D3DCOLORWRITEENABLE_ALPHA
| D3DCOLORWRITEENABLE_BLUE
|
219 D3DCOLORWRITEENABLE_GREEN
| D3DCOLORWRITEENABLE_RED
);
220 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_CULLMODE
, D3DCULL_NONE
);
221 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_DESTBLEND
, D3DBLEND_INVSRCALPHA
);
222 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_DIFFUSEMATERIALSOURCE
, D3DMCS_COLOR1
);
223 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_ENABLEADAPTIVETESSELLATION
, FALSE
);
224 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_FILLMODE
, D3DFILL_SOLID
);
225 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_FOGENABLE
, FALSE
);
226 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_INDEXEDVERTEXBLENDENABLE
, FALSE
);
227 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_LIGHTING
, FALSE
);
228 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_RANGEFOGENABLE
, FALSE
);
229 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_SEPARATEALPHABLENDENABLE
, FALSE
);
230 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_SHADEMODE
, D3DSHADE_GOURAUD
);
231 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_SPECULARENABLE
, FALSE
);
232 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_SRCBLEND
, D3DBLEND_SRCALPHA
);
233 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_SRGBWRITEENABLE
, FALSE
);
234 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_STENCILENABLE
, FALSE
);
235 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_VERTEXBLEND
, FALSE
);
236 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_WRAP0
, 0);
238 /* Texture stage states */
239 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_ALPHAARG1
, D3DTA_TEXTURE
);
240 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_ALPHAARG2
, D3DTA_DIFFUSE
);
241 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_ALPHAOP
, D3DTOP_MODULATE
);
242 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_COLORARG1
, D3DTA_TEXTURE
);
243 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_COLORARG2
, D3DTA_DIFFUSE
);
244 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_COLOROP
, D3DTOP_MODULATE
);
245 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_TEXCOORDINDEX
, 0);
246 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_TEXTURETRANSFORMFLAGS
, D3DTTFF_DISABLE
);
247 IDirect3DDevice9_SetTextureStageState(object
->device
, 1, D3DTSS_ALPHAOP
, D3DTOP_DISABLE
);
248 IDirect3DDevice9_SetTextureStageState(object
->device
, 1, D3DTSS_COLOROP
, D3DTOP_DISABLE
);
251 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_ADDRESSU
, D3DTADDRESS_CLAMP
);
252 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_ADDRESSV
, D3DTADDRESS_CLAMP
);
254 if(object
->texfilter_caps
& D3DPTFILTERCAPS_MAGFANISOTROPIC
)
255 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MAGFILTER
, D3DTEXF_ANISOTROPIC
);
256 else IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MAGFILTER
, D3DTEXF_LINEAR
);
258 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MAXMIPLEVEL
, 0);
259 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MAXANISOTROPY
, object
->maxanisotropy
);
261 if(object
->texfilter_caps
& D3DPTFILTERCAPS_MINFANISOTROPIC
)
262 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MINFILTER
, D3DTEXF_ANISOTROPIC
);
263 else IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MINFILTER
, D3DTEXF_LINEAR
);
265 if(object
->texfilter_caps
& D3DPTFILTERCAPS_MIPFLINEAR
)
266 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MIPFILTER
, D3DTEXF_LINEAR
);
267 else IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MIPFILTER
, D3DTEXF_POINT
);
269 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MIPMAPLODBIAS
, 0);
270 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_SRGBTEXTURE
, 0);
273 D3DXMatrixIdentity(&mat
);
274 IDirect3DDevice9_SetTransform(object
->device
, D3DTS_WORLD
, &mat
);
275 IDirect3DDevice9_SetTransform(object
->device
, D3DTS_VIEW
, &object
->view
);
276 IDirect3DDevice9_GetViewport(object
->device
, &vp
);
277 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
);
278 IDirect3DDevice9_SetTransform(object
->device
, D3DTS_PROJECTION
, &mat
);
281 static HRESULT WINAPI
d3dx9_sprite_Begin(ID3DXSprite
*iface
, DWORD flags
)
283 struct d3dx9_sprite
*This
= impl_from_ID3DXSprite(iface
);
286 TRACE("iface %p, flags %#x.\n", iface
, flags
);
288 if(flags
>D3DXSPRITE_FLAGLIMIT
|| This
->ready
) return D3DERR_INVALIDCALL
;
290 /* TODO: Implement flags:
291 D3DXSPRITE_BILLBOARD: makes the sprite always face the camera
292 D3DXSPRITE_DONOTMODIFY_RENDERSTATE: name says it all
293 D3DXSPRITE_OBJECTSPACE: do not change device transforms
294 D3DXSPRITE_SORT_DEPTH_BACKTOFRONT: sort by position
295 D3DXSPRITE_SORT_DEPTH_FRONTTOBACK: sort by position
296 D3DXSPRITE_SORT_TEXTURE: sort by texture (so that it doesn't change too often)
298 /* Seems like alpha blending is always enabled, regardless of D3DXSPRITE_ALPHABLEND flag */
299 if(flags
& (D3DXSPRITE_BILLBOARD
|
300 D3DXSPRITE_DONOTMODIFY_RENDERSTATE
| D3DXSPRITE_OBJECTSPACE
|
301 D3DXSPRITE_SORT_DEPTH_BACKTOFRONT
))
302 FIXME("Flags unsupported: %#x\n", flags
);
303 /* These flags should only matter to performance */
304 else if(flags
& (D3DXSPRITE_SORT_DEPTH_FRONTTOBACK
| D3DXSPRITE_SORT_TEXTURE
))
305 TRACE("Flags unsupported: %#x\n", flags
);
307 if(This
->vdecl
==NULL
) {
308 static const D3DVERTEXELEMENT9 elements
[] =
310 { 0, 0, D3DDECLTYPE_FLOAT3
, D3DDECLMETHOD_DEFAULT
, D3DDECLUSAGE_POSITION
, 0 },
311 { 0, 12, D3DDECLTYPE_D3DCOLOR
, D3DDECLMETHOD_DEFAULT
, D3DDECLUSAGE_COLOR
, 0 },
312 { 0, 16, D3DDECLTYPE_FLOAT2
, D3DDECLMETHOD_DEFAULT
, D3DDECLUSAGE_TEXCOORD
, 0 },
315 IDirect3DDevice9_CreateVertexDeclaration(This
->device
, elements
, &This
->vdecl
);
318 if(!(flags
& D3DXSPRITE_DONOTSAVESTATE
)) {
319 if(This
->stateblock
==NULL
) {
320 /* Tell our state block what it must store */
321 hr
=IDirect3DDevice9_BeginStateBlock(This
->device
);
322 if(hr
!=D3D_OK
) return hr
;
326 IDirect3DDevice9_SetVertexDeclaration(This
->device
, This
->vdecl
);
327 IDirect3DDevice9_SetStreamSource(This
->device
, 0, NULL
, 0, sizeof(struct sprite_vertex
));
328 IDirect3DDevice9_SetIndices(This
->device
, NULL
);
329 IDirect3DDevice9_SetTexture(This
->device
, 0, NULL
);
331 IDirect3DDevice9_EndStateBlock(This
->device
, &This
->stateblock
);
333 IDirect3DStateBlock9_Capture(This
->stateblock
); /* Save current state */
336 /* Apply device state */
345 static HRESULT WINAPI
d3dx9_sprite_Draw(ID3DXSprite
*iface
, IDirect3DTexture9
*texture
,
346 const RECT
*rect
, const D3DXVECTOR3
*center
, const D3DXVECTOR3
*position
, D3DCOLOR color
)
348 struct d3dx9_sprite
*This
= impl_from_ID3DXSprite(iface
);
349 struct sprite
*new_sprites
;
350 D3DSURFACE_DESC texdesc
;
352 TRACE("iface %p, texture %p, rect %s, center %p, position %p, color 0x%08x.\n",
353 iface
, texture
, wine_dbgstr_rect(rect
), center
, position
, color
);
355 if(texture
==NULL
) return D3DERR_INVALIDCALL
;
356 if(!This
->ready
) return D3DERR_INVALIDCALL
;
358 if (!This
->allocated_sprites
)
360 This
->sprites
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, 32 * sizeof(*This
->sprites
));
361 This
->allocated_sprites
= 32;
363 else if (This
->allocated_sprites
<= This
->sprite_count
)
365 new_sprites
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
366 This
->sprites
, This
->allocated_sprites
* 2 * sizeof(*This
->sprites
));
368 return E_OUTOFMEMORY
;
369 This
->sprites
= new_sprites
;
370 This
->allocated_sprites
*= 2;
372 This
->sprites
[This
->sprite_count
].texture
=texture
;
373 if(!(This
->flags
& D3DXSPRITE_DO_NOT_ADDREF_TEXTURE
))
374 IDirect3DTexture9_AddRef(texture
);
376 /* Reuse the texture desc if possible */
377 if(This
->sprite_count
) {
378 if(This
->sprites
[This
->sprite_count
-1].texture
!=texture
) {
379 IDirect3DTexture9_GetLevelDesc(texture
, 0, &texdesc
);
381 texdesc
.Width
=This
->sprites
[This
->sprite_count
-1].texw
;
382 texdesc
.Height
=This
->sprites
[This
->sprite_count
-1].texh
;
384 } else IDirect3DTexture9_GetLevelDesc(texture
, 0, &texdesc
);
386 This
->sprites
[This
->sprite_count
].texw
=texdesc
.Width
;
387 This
->sprites
[This
->sprite_count
].texh
=texdesc
.Height
;
390 This
->sprites
[This
->sprite_count
].rect
= *rect
;
392 SetRect(&This
->sprites
[This
->sprite_count
].rect
, 0, 0, texdesc
.Width
, texdesc
.Height
);
395 This
->sprites
[This
->sprite_count
].center
.x
=0.0f
;
396 This
->sprites
[This
->sprite_count
].center
.y
=0.0f
;
397 This
->sprites
[This
->sprite_count
].center
.z
=0.0f
;
398 } else This
->sprites
[This
->sprite_count
].center
=*center
;
401 This
->sprites
[This
->sprite_count
].pos
.x
=0.0f
;
402 This
->sprites
[This
->sprite_count
].pos
.y
=0.0f
;
403 This
->sprites
[This
->sprite_count
].pos
.z
=0.0f
;
404 } else This
->sprites
[This
->sprite_count
].pos
=*position
;
406 This
->sprites
[This
->sprite_count
].color
=color
;
407 This
->sprites
[This
->sprite_count
].transform
=This
->transform
;
408 This
->sprite_count
++;
413 static HRESULT WINAPI
d3dx9_sprite_Flush(ID3DXSprite
*iface
)
415 struct d3dx9_sprite
*This
= impl_from_ID3DXSprite(iface
);
416 struct sprite_vertex
*vertices
;
417 int i
, count
=0, start
;
419 TRACE("iface %p.\n", iface
);
421 if(!This
->ready
) return D3DERR_INVALIDCALL
;
422 if(!This
->sprite_count
) return D3D_OK
;
424 /* TODO: use of a vertex buffer here */
425 vertices
= HeapAlloc(GetProcessHeap(), 0, sizeof(*vertices
) * 6 * This
->sprite_count
);
427 for(start
=0;start
<This
->sprite_count
;start
+=count
,count
=0) {
429 while(i
<This
->sprite_count
&&
430 (count
==0 || This
->sprites
[i
].texture
==This
->sprites
[i
-1].texture
)) {
431 float spritewidth
=(float)This
->sprites
[i
].rect
.right
-(float)This
->sprites
[i
].rect
.left
;
432 float spriteheight
=(float)This
->sprites
[i
].rect
.bottom
-(float)This
->sprites
[i
].rect
.top
;
434 vertices
[6*i
].pos
.x
= This
->sprites
[i
].pos
.x
- This
->sprites
[i
].center
.x
;
435 vertices
[6*i
].pos
.y
= This
->sprites
[i
].pos
.y
- This
->sprites
[i
].center
.y
;
436 vertices
[6*i
].pos
.z
= This
->sprites
[i
].pos
.z
- This
->sprites
[i
].center
.z
;
437 vertices
[6*i
+1].pos
.x
= spritewidth
+ This
->sprites
[i
].pos
.x
- This
->sprites
[i
].center
.x
;
438 vertices
[6*i
+1].pos
.y
= This
->sprites
[i
].pos
.y
- This
->sprites
[i
].center
.y
;
439 vertices
[6*i
+1].pos
.z
= This
->sprites
[i
].pos
.z
- This
->sprites
[i
].center
.z
;
440 vertices
[6*i
+2].pos
.x
= spritewidth
+ This
->sprites
[i
].pos
.x
- This
->sprites
[i
].center
.x
;
441 vertices
[6*i
+2].pos
.y
= spriteheight
+ This
->sprites
[i
].pos
.y
- This
->sprites
[i
].center
.y
;
442 vertices
[6*i
+2].pos
.z
= This
->sprites
[i
].pos
.z
- This
->sprites
[i
].center
.z
;
443 vertices
[6*i
+3].pos
.x
= This
->sprites
[i
].pos
.x
- This
->sprites
[i
].center
.x
;
444 vertices
[6*i
+3].pos
.y
= spriteheight
+ This
->sprites
[i
].pos
.y
- This
->sprites
[i
].center
.y
;
445 vertices
[6*i
+3].pos
.z
= This
->sprites
[i
].pos
.z
- This
->sprites
[i
].center
.z
;
446 vertices
[6*i
].col
= This
->sprites
[i
].color
;
447 vertices
[6*i
+1].col
= This
->sprites
[i
].color
;
448 vertices
[6*i
+2].col
= This
->sprites
[i
].color
;
449 vertices
[6*i
+3].col
= This
->sprites
[i
].color
;
450 vertices
[6*i
].tex
.x
= (float)This
->sprites
[i
].rect
.left
/ (float)This
->sprites
[i
].texw
;
451 vertices
[6*i
].tex
.y
= (float)This
->sprites
[i
].rect
.top
/ (float)This
->sprites
[i
].texh
;
452 vertices
[6*i
+1].tex
.x
= (float)This
->sprites
[i
].rect
.right
/ (float)This
->sprites
[i
].texw
;
453 vertices
[6*i
+1].tex
.y
= (float)This
->sprites
[i
].rect
.top
/ (float)This
->sprites
[i
].texh
;
454 vertices
[6*i
+2].tex
.x
= (float)This
->sprites
[i
].rect
.right
/ (float)This
->sprites
[i
].texw
;
455 vertices
[6*i
+2].tex
.y
= (float)This
->sprites
[i
].rect
.bottom
/ (float)This
->sprites
[i
].texh
;
456 vertices
[6*i
+3].tex
.x
= (float)This
->sprites
[i
].rect
.left
/ (float)This
->sprites
[i
].texw
;
457 vertices
[6*i
+3].tex
.y
= (float)This
->sprites
[i
].rect
.bottom
/ (float)This
->sprites
[i
].texh
;
459 vertices
[6*i
+4]=vertices
[6*i
];
460 vertices
[6*i
+5]=vertices
[6*i
+2];
462 D3DXVec3TransformCoordArray(&vertices
[6 * i
].pos
, sizeof(*vertices
),
463 &vertices
[6 * i
].pos
, sizeof(*vertices
), &This
->sprites
[i
].transform
, 6);
468 IDirect3DDevice9_SetTexture(This
->device
, 0, (struct IDirect3DBaseTexture9
*)This
->sprites
[start
].texture
);
469 IDirect3DDevice9_SetVertexDeclaration(This
->device
, This
->vdecl
);
471 IDirect3DDevice9_DrawPrimitiveUP(This
->device
, D3DPT_TRIANGLELIST
,
472 2 * count
, vertices
+ 6 * start
, sizeof(*vertices
));
474 HeapFree(GetProcessHeap(), 0, vertices
);
476 if(!(This
->flags
& D3DXSPRITE_DO_NOT_ADDREF_TEXTURE
))
477 for(i
=0;i
<This
->sprite_count
;i
++)
478 IDirect3DTexture9_Release(This
->sprites
[i
].texture
);
480 This
->sprite_count
=0;
482 /* Flush may be called more than once, so we don't reset This->ready here */
487 static HRESULT WINAPI
d3dx9_sprite_End(ID3DXSprite
*iface
)
489 struct d3dx9_sprite
*sprite
= impl_from_ID3DXSprite(iface
);
491 TRACE("iface %p.\n", iface
);
494 return D3DERR_INVALIDCALL
;
496 ID3DXSprite_Flush(iface
);
498 if (sprite
->stateblock
&& !(sprite
->flags
& D3DXSPRITE_DONOTSAVESTATE
))
499 IDirect3DStateBlock9_Apply(sprite
->stateblock
); /* Restore old state */
501 sprite
->ready
= FALSE
;
506 static HRESULT WINAPI
d3dx9_sprite_OnLostDevice(ID3DXSprite
*iface
)
508 struct d3dx9_sprite
*sprite
= impl_from_ID3DXSprite(iface
);
510 TRACE("iface %p.\n", iface
);
512 if (sprite
->stateblock
)
513 IDirect3DStateBlock9_Release(sprite
->stateblock
);
515 IDirect3DVertexDeclaration9_Release(sprite
->vdecl
);
516 sprite
->vdecl
= NULL
;
517 sprite
->stateblock
= NULL
;
519 /* Reset some variables */
520 ID3DXSprite_OnResetDevice(iface
);
525 static HRESULT WINAPI
d3dx9_sprite_OnResetDevice(ID3DXSprite
*iface
)
527 struct d3dx9_sprite
*sprite
= impl_from_ID3DXSprite(iface
);
530 TRACE("iface %p.\n", iface
);
532 if (!(sprite
->flags
& D3DXSPRITE_DO_NOT_ADDREF_TEXTURE
))
534 for (i
= 0; i
< sprite
->sprite_count
; ++i
)
536 if (sprite
->sprites
[i
].texture
)
537 IDirect3DTexture9_Release(sprite
->sprites
[i
].texture
);
541 sprite
->sprite_count
= 0;
543 sprite
->ready
= FALSE
;
546 /* device objects get restored on Begin */
551 static const ID3DXSpriteVtbl d3dx9_sprite_vtbl
=
553 d3dx9_sprite_QueryInterface
,
555 d3dx9_sprite_Release
,
556 d3dx9_sprite_GetDevice
,
557 d3dx9_sprite_GetTransform
,
558 d3dx9_sprite_SetTransform
,
559 d3dx9_sprite_SetWorldViewRH
,
560 d3dx9_sprite_SetWorldViewLH
,
565 d3dx9_sprite_OnLostDevice
,
566 d3dx9_sprite_OnResetDevice
,
569 HRESULT WINAPI
D3DXCreateSprite(struct IDirect3DDevice9
*device
, struct ID3DXSprite
**sprite
)
571 struct d3dx9_sprite
*object
;
574 TRACE("device %p, sprite %p.\n", device
, sprite
);
576 if(device
==NULL
|| sprite
==NULL
) return D3DERR_INVALIDCALL
;
578 if (!(object
=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
581 return E_OUTOFMEMORY
;
583 object
->ID3DXSprite_iface
.lpVtbl
= &d3dx9_sprite_vtbl
;
585 object
->device
=device
;
586 IUnknown_AddRef(device
);
589 object
->stateblock
=NULL
;
591 D3DXMatrixIdentity(&object
->transform
);
592 D3DXMatrixIdentity(&object
->view
);
594 IDirect3DDevice9_GetDeviceCaps(device
, &caps
);
595 object
->texfilter_caps
=caps
.TextureFilterCaps
;
596 object
->maxanisotropy
=caps
.MaxAnisotropy
;
597 object
->alphacmp_caps
=caps
.AlphaCmpCaps
;
599 ID3DXSprite_OnResetDevice(&object
->ID3DXSprite_iface
);
601 object
->sprites
=NULL
;
602 object
->allocated_sprites
=0;
603 *sprite
=&object
->ID3DXSprite_iface
;