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 typedef struct _SPRITE
{
35 IDirect3DTexture9
*texture
;
44 typedef struct ID3DXSpriteImpl
46 ID3DXSprite ID3DXSprite_iface
;
49 IDirect3DDevice9
*device
;
50 IDirect3DVertexDeclaration9
*vdecl
;
51 IDirect3DStateBlock9
*stateblock
;
57 /* Store the relevant caps to prevent multiple GetDeviceCaps calls */
63 int sprite_count
; /* number of sprites to be drawn */
64 int allocated_sprites
; /* number of (pre-)allocated sprites */
67 static inline ID3DXSpriteImpl
*impl_from_ID3DXSprite(ID3DXSprite
*iface
)
69 return CONTAINING_RECORD(iface
, ID3DXSpriteImpl
, ID3DXSprite_iface
);
72 static HRESULT WINAPI
ID3DXSpriteImpl_QueryInterface(ID3DXSprite
*iface
, REFIID riid
, void **out
)
74 TRACE("iface %p, riid %s, out %p.\n", iface
, debugstr_guid(riid
), out
);
76 if (IsEqualGUID(riid
, &IID_ID3DXSprite
)
77 || IsEqualGUID(riid
, &IID_IUnknown
))
79 IUnknown_AddRef(iface
);
84 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
90 static ULONG WINAPI
ID3DXSpriteImpl_AddRef(ID3DXSprite
*iface
)
92 ID3DXSpriteImpl
*This
= impl_from_ID3DXSprite(iface
);
93 ULONG ref
=InterlockedIncrement(&This
->ref
);
94 TRACE("(%p)->(): AddRef from %d\n", This
, ref
-1);
98 static ULONG WINAPI
ID3DXSpriteImpl_Release(ID3DXSprite
*iface
)
100 ID3DXSpriteImpl
*This
= impl_from_ID3DXSprite(iface
);
101 ULONG ref
=InterlockedDecrement(&This
->ref
);
103 TRACE("(%p)->(): ReleaseRef to %d\n", This
, ref
);
108 for(i
=0;i
<This
->sprite_count
;i
++)
109 if(This
->sprites
[i
].texture
)
110 IDirect3DTexture9_Release(This
->sprites
[i
].texture
);
112 HeapFree(GetProcessHeap(), 0, This
->sprites
);
114 if(This
->stateblock
) IDirect3DStateBlock9_Release(This
->stateblock
);
115 if(This
->vdecl
) IDirect3DVertexDeclaration9_Release(This
->vdecl
);
116 if(This
->device
) IDirect3DDevice9_Release(This
->device
);
117 HeapFree(GetProcessHeap(), 0, This
);
122 static HRESULT WINAPI
ID3DXSpriteImpl_GetDevice(struct ID3DXSprite
*iface
, struct IDirect3DDevice9
**device
)
124 ID3DXSpriteImpl
*This
= impl_from_ID3DXSprite(iface
);
126 TRACE("(%p)->(%p)\n", This
, device
);
128 if(device
==NULL
) return D3DERR_INVALIDCALL
;
129 *device
=This
->device
;
130 IDirect3DDevice9_AddRef(This
->device
);
135 static HRESULT WINAPI
ID3DXSpriteImpl_GetTransform(ID3DXSprite
*iface
, D3DXMATRIX
*transform
)
137 ID3DXSpriteImpl
*This
= impl_from_ID3DXSprite(iface
);
139 TRACE("(%p)->(%p)\n", This
, transform
);
141 if(transform
==NULL
) return D3DERR_INVALIDCALL
;
142 *transform
=This
->transform
;
147 static HRESULT WINAPI
ID3DXSpriteImpl_SetTransform(ID3DXSprite
*iface
, CONST D3DXMATRIX
*transform
)
149 ID3DXSpriteImpl
*This
= impl_from_ID3DXSprite(iface
);
151 TRACE("(%p)->(%p)\n", This
, transform
);
153 if(transform
==NULL
) return D3DERR_INVALIDCALL
;
154 This
->transform
=*transform
;
159 static HRESULT WINAPI
ID3DXSpriteImpl_SetWorldViewRH(ID3DXSprite
*iface
, CONST D3DXMATRIX
*world
,
160 CONST D3DXMATRIX
*view
)
162 ID3DXSpriteImpl
*This
= impl_from_ID3DXSprite(iface
);
163 FIXME("(%p)->(%p, %p): stub\n", This
, world
, view
);
167 static HRESULT WINAPI
ID3DXSpriteImpl_SetWorldViewLH(ID3DXSprite
*iface
, CONST D3DXMATRIX
*world
,
168 CONST D3DXMATRIX
*view
)
170 ID3DXSpriteImpl
*This
= impl_from_ID3DXSprite(iface
);
171 FIXME("(%p)->(%p, %p): stub\n", This
, world
, view
);
175 /* Helper function */
176 static void set_states(ID3DXSpriteImpl
*object
)
181 /* Miscellaneous stuff */
182 IDirect3DDevice9_SetVertexShader(object
->device
, NULL
);
183 IDirect3DDevice9_SetPixelShader(object
->device
, NULL
);
184 IDirect3DDevice9_SetNPatchMode(object
->device
, 0.0f
);
187 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_ALPHABLENDENABLE
, TRUE
);
188 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_ALPHAFUNC
, D3DCMP_GREATER
);
189 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_ALPHAREF
, 0x00);
190 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_ALPHATESTENABLE
, object
->alphacmp_caps
);
191 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_BLENDOP
, D3DBLENDOP_ADD
);
192 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_CLIPPING
, TRUE
);
193 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_CLIPPLANEENABLE
, FALSE
);
194 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_COLORWRITEENABLE
, D3DCOLORWRITEENABLE_ALPHA
| D3DCOLORWRITEENABLE_BLUE
|
195 D3DCOLORWRITEENABLE_GREEN
| D3DCOLORWRITEENABLE_RED
);
196 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_CULLMODE
, D3DCULL_NONE
);
197 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_DESTBLEND
, D3DBLEND_INVSRCALPHA
);
198 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_DIFFUSEMATERIALSOURCE
, D3DMCS_COLOR1
);
199 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_ENABLEADAPTIVETESSELLATION
, FALSE
);
200 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_FILLMODE
, D3DFILL_SOLID
);
201 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_FOGENABLE
, FALSE
);
202 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_INDEXEDVERTEXBLENDENABLE
, FALSE
);
203 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_LIGHTING
, FALSE
);
204 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_RANGEFOGENABLE
, FALSE
);
205 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_SEPARATEALPHABLENDENABLE
, FALSE
);
206 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_SHADEMODE
, D3DSHADE_GOURAUD
);
207 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_SPECULARENABLE
, FALSE
);
208 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_SRCBLEND
, D3DBLEND_SRCALPHA
);
209 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_SRGBWRITEENABLE
, FALSE
);
210 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_STENCILENABLE
, FALSE
);
211 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_VERTEXBLEND
, FALSE
);
212 IDirect3DDevice9_SetRenderState(object
->device
, D3DRS_WRAP0
, 0);
214 /* Texture stage states */
215 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_ALPHAARG1
, D3DTA_TEXTURE
);
216 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_ALPHAARG2
, D3DTA_DIFFUSE
);
217 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_ALPHAOP
, D3DTOP_MODULATE
);
218 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_COLORARG1
, D3DTA_TEXTURE
);
219 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_COLORARG2
, D3DTA_DIFFUSE
);
220 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_COLOROP
, D3DTOP_MODULATE
);
221 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_TEXCOORDINDEX
, 0);
222 IDirect3DDevice9_SetTextureStageState(object
->device
, 0, D3DTSS_TEXTURETRANSFORMFLAGS
, D3DTTFF_DISABLE
);
223 IDirect3DDevice9_SetTextureStageState(object
->device
, 1, D3DTSS_ALPHAOP
, D3DTOP_DISABLE
);
224 IDirect3DDevice9_SetTextureStageState(object
->device
, 1, D3DTSS_COLOROP
, D3DTOP_DISABLE
);
227 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_ADDRESSU
, D3DTADDRESS_CLAMP
);
228 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_ADDRESSV
, D3DTADDRESS_CLAMP
);
230 if(object
->texfilter_caps
& D3DPTFILTERCAPS_MAGFANISOTROPIC
)
231 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MAGFILTER
, D3DTEXF_ANISOTROPIC
);
232 else IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MAGFILTER
, D3DTEXF_LINEAR
);
234 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MAXMIPLEVEL
, 0);
235 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MAXANISOTROPY
, object
->maxanisotropy
);
237 if(object
->texfilter_caps
& D3DPTFILTERCAPS_MINFANISOTROPIC
)
238 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MINFILTER
, D3DTEXF_ANISOTROPIC
);
239 else IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MINFILTER
, D3DTEXF_LINEAR
);
241 if(object
->texfilter_caps
& D3DPTFILTERCAPS_MIPFLINEAR
)
242 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MIPFILTER
, D3DTEXF_LINEAR
);
243 else IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MIPFILTER
, D3DTEXF_POINT
);
245 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_MIPMAPLODBIAS
, 0);
246 IDirect3DDevice9_SetSamplerState(object
->device
, 0, D3DSAMP_SRGBTEXTURE
, 0);
249 D3DXMatrixIdentity(&mat
);
250 IDirect3DDevice9_SetTransform(object
->device
, D3DTS_WORLD
, &mat
);
251 IDirect3DDevice9_SetTransform(object
->device
, D3DTS_VIEW
, &object
->view
);
252 IDirect3DDevice9_GetViewport(object
->device
, &vp
);
253 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
);
254 IDirect3DDevice9_SetTransform(object
->device
, D3DTS_PROJECTION
, &mat
);
257 static HRESULT WINAPI
ID3DXSpriteImpl_Begin(ID3DXSprite
*iface
, DWORD flags
)
259 ID3DXSpriteImpl
*This
= impl_from_ID3DXSprite(iface
);
262 TRACE("(%p)->(%#x)\n", This
, flags
);
264 if(flags
>D3DXSPRITE_FLAGLIMIT
|| This
->ready
) return D3DERR_INVALIDCALL
;
266 /* TODO: Implement flags:
267 D3DXSPRITE_BILLBOARD: makes the sprite always face the camera
268 D3DXSPRITE_DONOTMODIFY_RENDERSTATE: name says it all
269 D3DXSPRITE_OBJECTSPACE: do not change device transforms
270 D3DXSPRITE_SORT_DEPTH_BACKTOFRONT: sort by position
271 D3DXSPRITE_SORT_DEPTH_FRONTTOBACK: sort by position
272 D3DXSPRITE_SORT_TEXTURE: sort by texture (so that it doesn't change too often)
274 /* Seems like alpha blending is always enabled, regardless of D3DXSPRITE_ALPHABLEND flag */
275 if(flags
& (D3DXSPRITE_BILLBOARD
|
276 D3DXSPRITE_DONOTMODIFY_RENDERSTATE
| D3DXSPRITE_OBJECTSPACE
|
277 D3DXSPRITE_SORT_DEPTH_BACKTOFRONT
))
278 FIXME("Flags unsupported: %#x\n", flags
);
279 /* These flags should only matter to performances */
280 else if(flags
& (D3DXSPRITE_SORT_DEPTH_FRONTTOBACK
| D3DXSPRITE_SORT_TEXTURE
))
281 TRACE("Flags unsupported: %#x\n", flags
);
283 if(This
->vdecl
==NULL
) {
284 static const D3DVERTEXELEMENT9 elements
[] =
286 { 0, 0, D3DDECLTYPE_FLOAT3
, D3DDECLMETHOD_DEFAULT
, D3DDECLUSAGE_POSITION
, 0 },
287 { 0, 12, D3DDECLTYPE_D3DCOLOR
, D3DDECLMETHOD_DEFAULT
, D3DDECLUSAGE_COLOR
, 0 },
288 { 0, 16, D3DDECLTYPE_FLOAT2
, D3DDECLMETHOD_DEFAULT
, D3DDECLUSAGE_TEXCOORD
, 0 },
291 IDirect3DDevice9_CreateVertexDeclaration(This
->device
, elements
, &This
->vdecl
);
294 if(!(flags
& D3DXSPRITE_DONOTSAVESTATE
)) {
295 if(This
->stateblock
==NULL
) {
296 /* Tell our state block what it must store */
297 hr
=IDirect3DDevice9_BeginStateBlock(This
->device
);
298 if(hr
!=D3D_OK
) return hr
;
302 IDirect3DDevice9_SetVertexDeclaration(This
->device
, This
->vdecl
);
303 IDirect3DDevice9_SetStreamSource(This
->device
, 0, NULL
, 0, sizeof(SPRITEVERTEX
));
304 IDirect3DDevice9_SetIndices(This
->device
, NULL
);
305 IDirect3DDevice9_SetTexture(This
->device
, 0, NULL
);
307 IDirect3DDevice9_EndStateBlock(This
->device
, &This
->stateblock
);
309 IDirect3DStateBlock9_Capture(This
->stateblock
); /* Save current state */
312 /* Apply device state */
321 static HRESULT WINAPI
ID3DXSpriteImpl_Draw(ID3DXSprite
*iface
, IDirect3DTexture9
*texture
,
322 const RECT
*rect
, const D3DXVECTOR3
*center
, const D3DXVECTOR3
*position
, D3DCOLOR color
)
324 ID3DXSpriteImpl
*This
= impl_from_ID3DXSprite(iface
);
325 D3DSURFACE_DESC texdesc
;
327 TRACE("iface %p, texture %p, rect %s, center %p, position %p, color 0x%08x.\n",
328 iface
, texture
, wine_dbgstr_rect(rect
), center
, position
, color
);
330 if(texture
==NULL
) return D3DERR_INVALIDCALL
;
331 if(!This
->ready
) return D3DERR_INVALIDCALL
;
333 if(This
->allocated_sprites
==0) {
334 This
->sprites
=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, 32*sizeof(SPRITE
));
335 This
->allocated_sprites
=32;
336 } else if(This
->allocated_sprites
<=This
->sprite_count
) {
337 This
->allocated_sprites
=This
->allocated_sprites
*3/2;
338 This
->sprites
=HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, This
->sprites
, This
->allocated_sprites
*sizeof(SPRITE
));
340 This
->sprites
[This
->sprite_count
].texture
=texture
;
341 if(!(This
->flags
& D3DXSPRITE_DO_NOT_ADDREF_TEXTURE
))
342 IDirect3DTexture9_AddRef(texture
);
344 /* Reuse the texture desc if possible */
345 if(This
->sprite_count
) {
346 if(This
->sprites
[This
->sprite_count
-1].texture
!=texture
) {
347 IDirect3DTexture9_GetLevelDesc(texture
, 0, &texdesc
);
349 texdesc
.Width
=This
->sprites
[This
->sprite_count
-1].texw
;
350 texdesc
.Height
=This
->sprites
[This
->sprite_count
-1].texh
;
352 } else IDirect3DTexture9_GetLevelDesc(texture
, 0, &texdesc
);
354 This
->sprites
[This
->sprite_count
].texw
=texdesc
.Width
;
355 This
->sprites
[This
->sprite_count
].texh
=texdesc
.Height
;
358 This
->sprites
[This
->sprite_count
].rect
.left
=0;
359 This
->sprites
[This
->sprite_count
].rect
.top
=0;
360 This
->sprites
[This
->sprite_count
].rect
.right
=texdesc
.Width
;
361 This
->sprites
[This
->sprite_count
].rect
.bottom
=texdesc
.Height
;
362 } else This
->sprites
[This
->sprite_count
].rect
=*rect
;
365 This
->sprites
[This
->sprite_count
].center
.x
=0.0f
;
366 This
->sprites
[This
->sprite_count
].center
.y
=0.0f
;
367 This
->sprites
[This
->sprite_count
].center
.z
=0.0f
;
368 } else This
->sprites
[This
->sprite_count
].center
=*center
;
371 This
->sprites
[This
->sprite_count
].pos
.x
=0.0f
;
372 This
->sprites
[This
->sprite_count
].pos
.y
=0.0f
;
373 This
->sprites
[This
->sprite_count
].pos
.z
=0.0f
;
374 } else This
->sprites
[This
->sprite_count
].pos
=*position
;
376 This
->sprites
[This
->sprite_count
].color
=color
;
377 This
->sprites
[This
->sprite_count
].transform
=This
->transform
;
378 This
->sprite_count
++;
383 static HRESULT WINAPI
ID3DXSpriteImpl_Flush(ID3DXSprite
*iface
)
385 ID3DXSpriteImpl
*This
= impl_from_ID3DXSprite(iface
);
386 SPRITEVERTEX
*vertices
;
387 int i
, count
=0, start
;
389 TRACE("(%p)->()\n", This
);
391 if(!This
->ready
) return D3DERR_INVALIDCALL
;
392 if(!This
->sprite_count
) return D3D_OK
;
394 /* TODO: use of a vertex buffer here */
395 vertices
=HeapAlloc(GetProcessHeap(), 0, sizeof(SPRITEVERTEX
)*6*This
->sprite_count
);
397 for(start
=0;start
<This
->sprite_count
;start
+=count
,count
=0) {
399 while(i
<This
->sprite_count
&&
400 (count
==0 || This
->sprites
[i
].texture
==This
->sprites
[i
-1].texture
)) {
401 float spritewidth
=(float)This
->sprites
[i
].rect
.right
-(float)This
->sprites
[i
].rect
.left
;
402 float spriteheight
=(float)This
->sprites
[i
].rect
.bottom
-(float)This
->sprites
[i
].rect
.top
;
404 vertices
[6*i
].pos
.x
= This
->sprites
[i
].pos
.x
- This
->sprites
[i
].center
.x
;
405 vertices
[6*i
].pos
.y
= This
->sprites
[i
].pos
.y
- This
->sprites
[i
].center
.y
;
406 vertices
[6*i
].pos
.z
= This
->sprites
[i
].pos
.z
- This
->sprites
[i
].center
.z
;
407 vertices
[6*i
+1].pos
.x
= spritewidth
+ This
->sprites
[i
].pos
.x
- This
->sprites
[i
].center
.x
;
408 vertices
[6*i
+1].pos
.y
= This
->sprites
[i
].pos
.y
- This
->sprites
[i
].center
.y
;
409 vertices
[6*i
+1].pos
.z
= This
->sprites
[i
].pos
.z
- This
->sprites
[i
].center
.z
;
410 vertices
[6*i
+2].pos
.x
= spritewidth
+ This
->sprites
[i
].pos
.x
- This
->sprites
[i
].center
.x
;
411 vertices
[6*i
+2].pos
.y
= spriteheight
+ This
->sprites
[i
].pos
.y
- This
->sprites
[i
].center
.y
;
412 vertices
[6*i
+2].pos
.z
= This
->sprites
[i
].pos
.z
- This
->sprites
[i
].center
.z
;
413 vertices
[6*i
+3].pos
.x
= This
->sprites
[i
].pos
.x
- This
->sprites
[i
].center
.x
;
414 vertices
[6*i
+3].pos
.y
= spriteheight
+ This
->sprites
[i
].pos
.y
- This
->sprites
[i
].center
.y
;
415 vertices
[6*i
+3].pos
.z
= This
->sprites
[i
].pos
.z
- This
->sprites
[i
].center
.z
;
416 vertices
[6*i
].col
= This
->sprites
[i
].color
;
417 vertices
[6*i
+1].col
= This
->sprites
[i
].color
;
418 vertices
[6*i
+2].col
= This
->sprites
[i
].color
;
419 vertices
[6*i
+3].col
= This
->sprites
[i
].color
;
420 vertices
[6*i
].tex
.x
= (float)This
->sprites
[i
].rect
.left
/ (float)This
->sprites
[i
].texw
;
421 vertices
[6*i
].tex
.y
= (float)This
->sprites
[i
].rect
.top
/ (float)This
->sprites
[i
].texh
;
422 vertices
[6*i
+1].tex
.x
= (float)This
->sprites
[i
].rect
.right
/ (float)This
->sprites
[i
].texw
;
423 vertices
[6*i
+1].tex
.y
= (float)This
->sprites
[i
].rect
.top
/ (float)This
->sprites
[i
].texh
;
424 vertices
[6*i
+2].tex
.x
= (float)This
->sprites
[i
].rect
.right
/ (float)This
->sprites
[i
].texw
;
425 vertices
[6*i
+2].tex
.y
= (float)This
->sprites
[i
].rect
.bottom
/ (float)This
->sprites
[i
].texh
;
426 vertices
[6*i
+3].tex
.x
= (float)This
->sprites
[i
].rect
.left
/ (float)This
->sprites
[i
].texw
;
427 vertices
[6*i
+3].tex
.y
= (float)This
->sprites
[i
].rect
.bottom
/ (float)This
->sprites
[i
].texh
;
429 vertices
[6*i
+4]=vertices
[6*i
];
430 vertices
[6*i
+5]=vertices
[6*i
+2];
432 D3DXVec3TransformCoordArray(&vertices
[6*i
].pos
, sizeof(SPRITEVERTEX
),
433 &vertices
[6*i
].pos
, sizeof(SPRITEVERTEX
),
434 &This
->sprites
[i
].transform
, 6);
439 IDirect3DDevice9_SetTexture(This
->device
, 0, (struct IDirect3DBaseTexture9
*)This
->sprites
[start
].texture
);
440 IDirect3DDevice9_SetVertexDeclaration(This
->device
, This
->vdecl
);
442 IDirect3DDevice9_DrawPrimitiveUP(This
->device
, D3DPT_TRIANGLELIST
, 2*count
, vertices
+6*start
, sizeof(SPRITEVERTEX
));
444 HeapFree(GetProcessHeap(), 0, vertices
);
446 if(!(This
->flags
& D3DXSPRITE_DO_NOT_ADDREF_TEXTURE
))
447 for(i
=0;i
<This
->sprite_count
;i
++)
448 IDirect3DTexture9_Release(This
->sprites
[i
].texture
);
450 This
->sprite_count
=0;
452 /* Flush may be called more than once, so we don't reset This->ready here */
457 static HRESULT WINAPI
ID3DXSpriteImpl_End(ID3DXSprite
*iface
)
459 ID3DXSpriteImpl
*This
= impl_from_ID3DXSprite(iface
);
461 TRACE("(%p)->()\n", This
);
463 if(!This
->ready
) return D3DERR_INVALIDCALL
;
465 ID3DXSprite_Flush(iface
);
467 if(!(This
->flags
& D3DXSPRITE_DONOTSAVESTATE
))
468 if(This
->stateblock
) IDirect3DStateBlock9_Apply(This
->stateblock
); /* Restore old state */
475 static HRESULT WINAPI
ID3DXSpriteImpl_OnLostDevice(ID3DXSprite
*iface
)
477 ID3DXSpriteImpl
*This
= impl_from_ID3DXSprite(iface
);
479 TRACE("(%p)->()\n", This
);
481 if(This
->stateblock
) IDirect3DStateBlock9_Release(This
->stateblock
);
482 if(This
->vdecl
) IDirect3DVertexDeclaration9_Release(This
->vdecl
);
484 This
->stateblock
=NULL
;
486 /* Reset some variables */
487 ID3DXSprite_OnResetDevice(iface
);
492 static HRESULT WINAPI
ID3DXSpriteImpl_OnResetDevice(ID3DXSprite
*iface
)
494 ID3DXSpriteImpl
*This
= impl_from_ID3DXSprite(iface
);
497 TRACE("(%p)->()\n", This
);
499 for(i
=0;i
<This
->sprite_count
;i
++)
500 if(This
->sprites
[i
].texture
)
501 IDirect3DTexture9_Release(This
->sprites
[i
].texture
);
503 This
->sprite_count
=0;
509 /* device objects get restored on Begin */
514 static const ID3DXSpriteVtbl D3DXSprite_Vtbl
=
516 /*** IUnknown methods ***/
517 ID3DXSpriteImpl_QueryInterface
,
518 ID3DXSpriteImpl_AddRef
,
519 ID3DXSpriteImpl_Release
,
520 /*** ID3DXSprite methods ***/
521 ID3DXSpriteImpl_GetDevice
,
522 ID3DXSpriteImpl_GetTransform
,
523 ID3DXSpriteImpl_SetTransform
,
524 ID3DXSpriteImpl_SetWorldViewRH
,
525 ID3DXSpriteImpl_SetWorldViewLH
,
526 ID3DXSpriteImpl_Begin
,
527 ID3DXSpriteImpl_Draw
,
528 ID3DXSpriteImpl_Flush
,
530 ID3DXSpriteImpl_OnLostDevice
,
531 ID3DXSpriteImpl_OnResetDevice
534 HRESULT WINAPI
D3DXCreateSprite(struct IDirect3DDevice9
*device
, struct ID3DXSprite
**sprite
)
536 ID3DXSpriteImpl
*object
;
539 TRACE("(%p, %p)\n", device
, sprite
);
541 if(device
==NULL
|| sprite
==NULL
) return D3DERR_INVALIDCALL
;
543 object
=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(ID3DXSpriteImpl
));
546 return E_OUTOFMEMORY
;
548 object
->ID3DXSprite_iface
.lpVtbl
= &D3DXSprite_Vtbl
;
550 object
->device
=device
;
551 IUnknown_AddRef(device
);
554 object
->stateblock
=NULL
;
556 D3DXMatrixIdentity(&object
->transform
);
557 D3DXMatrixIdentity(&object
->view
);
559 IDirect3DDevice9_GetDeviceCaps(device
, &caps
);
560 object
->texfilter_caps
=caps
.TextureFilterCaps
;
561 object
->maxanisotropy
=caps
.MaxAnisotropy
;
562 object
->alphacmp_caps
=caps
.AlphaCmpCaps
;
564 ID3DXSprite_OnResetDevice(&object
->ID3DXSprite_iface
);
566 object
->sprites
=NULL
;
567 object
->allocated_sprites
=0;
568 *sprite
=&object
->ID3DXSprite_iface
;