directmanipulation: Return S_OK form viewport_SetViewportOptions stub.
[wine/zf.git] / dlls / d3dx9_36 / sprite.c
blob9ef7f9c47e06b06cd82028c0e38f0e558c7bf71d
1 /*
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
21 #include "d3dx9_private.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
25 /* the combination of all possible D3DXSPRITE flags */
26 #define D3DXSPRITE_FLAGLIMIT 511
28 struct sprite_vertex
30 D3DXVECTOR3 pos;
31 DWORD col;
32 D3DXVECTOR2 tex;
35 struct sprite
37 IDirect3DTexture9 *texture;
38 UINT texw, texh;
39 RECT rect;
40 D3DXVECTOR3 center;
41 D3DXVECTOR3 pos;
42 D3DCOLOR color;
43 D3DXMATRIX transform;
46 struct d3dx9_sprite
48 ID3DXSprite ID3DXSprite_iface;
49 LONG ref;
51 IDirect3DDevice9 *device;
52 IDirect3DVertexDeclaration9 *vdecl;
53 IDirect3DStateBlock9 *stateblock;
54 D3DXMATRIX transform;
55 D3DXMATRIX view;
56 DWORD flags;
57 BOOL ready;
59 /* Store the relevant caps to prevent multiple GetDeviceCaps calls */
60 DWORD texfilter_caps;
61 DWORD maxanisotropy;
62 DWORD alphacmp_caps;
64 struct sprite *sprites;
65 int sprite_count; /* number of sprites to be drawn */
66 int allocated_sprites; /* number of (pre-)allocated sprites */
69 static inline struct d3dx9_sprite *impl_from_ID3DXSprite(ID3DXSprite *iface)
71 return CONTAINING_RECORD(iface, struct d3dx9_sprite, ID3DXSprite_iface);
74 static HRESULT WINAPI d3dx9_sprite_QueryInterface(ID3DXSprite *iface, REFIID riid, void **out)
76 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
78 if (IsEqualGUID(riid, &IID_ID3DXSprite)
79 || IsEqualGUID(riid, &IID_IUnknown))
81 IUnknown_AddRef(iface);
82 *out = iface;
83 return S_OK;
86 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
88 *out = NULL;
89 return E_NOINTERFACE;
92 static ULONG WINAPI d3dx9_sprite_AddRef(ID3DXSprite *iface)
94 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
95 ULONG refcount = InterlockedIncrement(&sprite->ref);
97 TRACE("%p increasing refcount to %u.\n", sprite, refcount);
99 return refcount;
102 static ULONG WINAPI d3dx9_sprite_Release(ID3DXSprite *iface)
104 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
105 ULONG refcount = InterlockedDecrement(&sprite->ref);
107 TRACE("%p decreasing refcount to %u.\n", sprite, refcount);
109 if (!refcount)
111 if (sprite->sprites)
113 int i;
115 for (i = 0; i < sprite->sprite_count; ++i)
117 if (sprite->sprites[i].texture)
118 IDirect3DTexture9_Release(sprite->sprites[i].texture);
121 HeapFree(GetProcessHeap(), 0, sprite->sprites);
124 if (sprite->stateblock)
125 IDirect3DStateBlock9_Release(sprite->stateblock);
126 if (sprite->vdecl)
127 IDirect3DVertexDeclaration9_Release(sprite->vdecl);
128 if (sprite->device)
129 IDirect3DDevice9_Release(sprite->device);
130 HeapFree(GetProcessHeap(), 0, sprite);
133 return refcount;
136 static HRESULT WINAPI d3dx9_sprite_GetDevice(struct ID3DXSprite *iface, struct IDirect3DDevice9 **device)
138 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
140 TRACE("iface %p, device %p.\n", iface, device);
142 if (!device)
143 return D3DERR_INVALIDCALL;
144 *device = sprite->device;
145 IDirect3DDevice9_AddRef(sprite->device);
147 return D3D_OK;
150 static HRESULT WINAPI d3dx9_sprite_GetTransform(ID3DXSprite *iface, D3DXMATRIX *transform)
152 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
154 TRACE("iface %p, transform %p.\n", iface, transform);
156 if (!transform)
157 return D3DERR_INVALIDCALL;
158 *transform = sprite->transform;
160 return D3D_OK;
163 static HRESULT WINAPI d3dx9_sprite_SetTransform(ID3DXSprite *iface, const D3DXMATRIX *transform)
165 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
167 TRACE("iface %p, transform %p.\n", iface, transform);
169 if (!transform)
170 return D3DERR_INVALIDCALL;
171 sprite->transform = *transform;
173 return D3D_OK;
176 static HRESULT WINAPI d3dx9_sprite_SetWorldViewRH(ID3DXSprite *iface,
177 const D3DXMATRIX *world, const D3DXMATRIX *view)
179 FIXME("iface %p, world %p, view %p stub!\n", iface, world, view);
181 return E_NOTIMPL;
184 static HRESULT WINAPI d3dx9_sprite_SetWorldViewLH(ID3DXSprite *iface,
185 const D3DXMATRIX *world, const D3DXMATRIX *view)
187 FIXME("iface %p, world %p, view %p stub!\n", iface, world, view);
189 return E_NOTIMPL;
192 /* Helper function */
193 static void set_states(struct d3dx9_sprite *object)
195 D3DXMATRIX mat;
196 D3DVIEWPORT9 vp;
198 /* Miscellaneous stuff */
199 IDirect3DDevice9_SetVertexShader(object->device, NULL);
200 IDirect3DDevice9_SetPixelShader(object->device, NULL);
201 IDirect3DDevice9_SetNPatchMode(object->device, 0.0f);
203 /* Render states */
204 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHABLENDENABLE, TRUE);
205 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHAFUNC, D3DCMP_GREATER);
206 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHAREF, 0x00);
207 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHATESTENABLE, object->alphacmp_caps);
208 IDirect3DDevice9_SetRenderState(object->device, D3DRS_BLENDOP, D3DBLENDOP_ADD);
209 IDirect3DDevice9_SetRenderState(object->device, D3DRS_CLIPPING, TRUE);
210 IDirect3DDevice9_SetRenderState(object->device, D3DRS_CLIPPLANEENABLE, 0);
211 IDirect3DDevice9_SetRenderState(object->device, D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_BLUE |
212 D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_RED);
213 IDirect3DDevice9_SetRenderState(object->device, D3DRS_CULLMODE, D3DCULL_NONE);
214 IDirect3DDevice9_SetRenderState(object->device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
215 IDirect3DDevice9_SetRenderState(object->device, D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1);
216 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ENABLEADAPTIVETESSELLATION, FALSE);
217 IDirect3DDevice9_SetRenderState(object->device, D3DRS_FILLMODE, D3DFILL_SOLID);
218 IDirect3DDevice9_SetRenderState(object->device, D3DRS_FOGENABLE, FALSE);
219 IDirect3DDevice9_SetRenderState(object->device, D3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
220 IDirect3DDevice9_SetRenderState(object->device, D3DRS_LIGHTING, FALSE);
221 IDirect3DDevice9_SetRenderState(object->device, D3DRS_RANGEFOGENABLE, FALSE);
222 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
223 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
224 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SPECULARENABLE, FALSE);
225 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
226 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SRGBWRITEENABLE, FALSE);
227 IDirect3DDevice9_SetRenderState(object->device, D3DRS_STENCILENABLE, FALSE);
228 IDirect3DDevice9_SetRenderState(object->device, D3DRS_VERTEXBLEND, FALSE);
229 IDirect3DDevice9_SetRenderState(object->device, D3DRS_WRAP0, 0);
231 /* Texture stage states */
232 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
233 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
234 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
235 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
236 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
237 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_COLOROP, D3DTOP_MODULATE);
238 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_TEXCOORDINDEX, 0);
239 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
240 IDirect3DDevice9_SetTextureStageState(object->device, 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
241 IDirect3DDevice9_SetTextureStageState(object->device, 1, D3DTSS_COLOROP, D3DTOP_DISABLE);
243 /* Sampler states */
244 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
245 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
247 if(object->texfilter_caps & D3DPTFILTERCAPS_MAGFANISOTROPIC)
248 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC);
249 else IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
251 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAXMIPLEVEL, 0);
252 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAXANISOTROPY, object->maxanisotropy);
254 if(object->texfilter_caps & D3DPTFILTERCAPS_MINFANISOTROPIC)
255 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC);
256 else IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
258 if(object->texfilter_caps & D3DPTFILTERCAPS_MIPFLINEAR)
259 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
260 else IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
262 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MIPMAPLODBIAS, 0);
263 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_SRGBTEXTURE, 0);
265 /* Matrices */
266 D3DXMatrixIdentity(&mat);
267 IDirect3DDevice9_SetTransform(object->device, D3DTS_WORLD, &mat);
268 IDirect3DDevice9_SetTransform(object->device, D3DTS_VIEW, &object->view);
269 IDirect3DDevice9_GetViewport(object->device, &vp);
270 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);
271 IDirect3DDevice9_SetTransform(object->device, D3DTS_PROJECTION, &mat);
274 static HRESULT WINAPI d3dx9_sprite_Begin(ID3DXSprite *iface, DWORD flags)
276 struct d3dx9_sprite *This = impl_from_ID3DXSprite(iface);
277 HRESULT hr;
279 TRACE("iface %p, flags %#x.\n", iface, flags);
281 if(flags>D3DXSPRITE_FLAGLIMIT || This->ready) return D3DERR_INVALIDCALL;
283 /* TODO: Implement flags:
284 D3DXSPRITE_BILLBOARD: makes the sprite always face the camera
285 D3DXSPRITE_DONOTMODIFY_RENDERSTATE: name says it all
286 D3DXSPRITE_OBJECTSPACE: do not change device transforms
287 D3DXSPRITE_SORT_DEPTH_BACKTOFRONT: sort by position
288 D3DXSPRITE_SORT_DEPTH_FRONTTOBACK: sort by position
289 D3DXSPRITE_SORT_TEXTURE: sort by texture (so that it doesn't change too often)
291 /* Seems like alpha blending is always enabled, regardless of D3DXSPRITE_ALPHABLEND flag */
292 if(flags & (D3DXSPRITE_BILLBOARD |
293 D3DXSPRITE_DONOTMODIFY_RENDERSTATE | D3DXSPRITE_OBJECTSPACE |
294 D3DXSPRITE_SORT_DEPTH_BACKTOFRONT))
295 FIXME("Flags unsupported: %#x\n", flags);
296 /* These flags should only matter to performance */
297 else if(flags & (D3DXSPRITE_SORT_DEPTH_FRONTTOBACK | D3DXSPRITE_SORT_TEXTURE))
298 TRACE("Flags unsupported: %#x\n", flags);
300 if(This->vdecl==NULL) {
301 static const D3DVERTEXELEMENT9 elements[] =
303 { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
304 { 0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
305 { 0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
306 D3DDECL_END()
308 IDirect3DDevice9_CreateVertexDeclaration(This->device, elements, &This->vdecl);
311 if(!(flags & D3DXSPRITE_DONOTSAVESTATE)) {
312 if(This->stateblock==NULL) {
313 /* Tell our state block what it must store */
314 hr=IDirect3DDevice9_BeginStateBlock(This->device);
315 if(hr!=D3D_OK) return hr;
317 set_states(This);
319 IDirect3DDevice9_SetVertexDeclaration(This->device, This->vdecl);
320 IDirect3DDevice9_SetStreamSource(This->device, 0, NULL, 0, sizeof(struct sprite_vertex));
321 IDirect3DDevice9_SetIndices(This->device, NULL);
322 IDirect3DDevice9_SetTexture(This->device, 0, NULL);
324 IDirect3DDevice9_EndStateBlock(This->device, &This->stateblock);
326 IDirect3DStateBlock9_Capture(This->stateblock); /* Save current state */
329 /* Apply device state */
330 set_states(This);
332 This->flags=flags;
333 This->ready=TRUE;
335 return D3D_OK;
338 static HRESULT WINAPI d3dx9_sprite_Draw(ID3DXSprite *iface, IDirect3DTexture9 *texture,
339 const RECT *rect, const D3DXVECTOR3 *center, const D3DXVECTOR3 *position, D3DCOLOR color)
341 struct d3dx9_sprite *This = impl_from_ID3DXSprite(iface);
342 D3DSURFACE_DESC texdesc;
344 TRACE("iface %p, texture %p, rect %s, center %p, position %p, color 0x%08x.\n",
345 iface, texture, wine_dbgstr_rect(rect), center, position, color);
347 if(texture==NULL) return D3DERR_INVALIDCALL;
348 if(!This->ready) return D3DERR_INVALIDCALL;
350 if (!This->allocated_sprites)
352 This->sprites = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 32 * sizeof(*This->sprites));
353 This->allocated_sprites = 32;
355 else if (This->allocated_sprites <= This->sprite_count)
357 This->allocated_sprites += This->allocated_sprites / 2;
358 This->sprites = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
359 This->sprites, This->allocated_sprites * sizeof(*This->sprites));
361 This->sprites[This->sprite_count].texture=texture;
362 if(!(This->flags & D3DXSPRITE_DO_NOT_ADDREF_TEXTURE))
363 IDirect3DTexture9_AddRef(texture);
365 /* Reuse the texture desc if possible */
366 if(This->sprite_count) {
367 if(This->sprites[This->sprite_count-1].texture!=texture) {
368 IDirect3DTexture9_GetLevelDesc(texture, 0, &texdesc);
369 } else {
370 texdesc.Width=This->sprites[This->sprite_count-1].texw;
371 texdesc.Height=This->sprites[This->sprite_count-1].texh;
373 } else IDirect3DTexture9_GetLevelDesc(texture, 0, &texdesc);
375 This->sprites[This->sprite_count].texw=texdesc.Width;
376 This->sprites[This->sprite_count].texh=texdesc.Height;
378 if (rect)
379 This->sprites[This->sprite_count].rect = *rect;
380 else
381 SetRect(&This->sprites[This->sprite_count].rect, 0, 0, texdesc.Width, texdesc.Height);
383 if(center==NULL) {
384 This->sprites[This->sprite_count].center.x=0.0f;
385 This->sprites[This->sprite_count].center.y=0.0f;
386 This->sprites[This->sprite_count].center.z=0.0f;
387 } else This->sprites[This->sprite_count].center=*center;
389 if(position==NULL) {
390 This->sprites[This->sprite_count].pos.x=0.0f;
391 This->sprites[This->sprite_count].pos.y=0.0f;
392 This->sprites[This->sprite_count].pos.z=0.0f;
393 } else This->sprites[This->sprite_count].pos=*position;
395 This->sprites[This->sprite_count].color=color;
396 This->sprites[This->sprite_count].transform=This->transform;
397 This->sprite_count++;
399 return D3D_OK;
402 static HRESULT WINAPI d3dx9_sprite_Flush(ID3DXSprite *iface)
404 struct d3dx9_sprite *This = impl_from_ID3DXSprite(iface);
405 struct sprite_vertex *vertices;
406 int i, count=0, start;
408 TRACE("iface %p.\n", iface);
410 if(!This->ready) return D3DERR_INVALIDCALL;
411 if(!This->sprite_count) return D3D_OK;
413 /* TODO: use of a vertex buffer here */
414 vertices = HeapAlloc(GetProcessHeap(), 0, sizeof(*vertices) * 6 * This->sprite_count);
416 for(start=0;start<This->sprite_count;start+=count,count=0) {
417 i=start;
418 while(i<This->sprite_count &&
419 (count==0 || This->sprites[i].texture==This->sprites[i-1].texture)) {
420 float spritewidth=(float)This->sprites[i].rect.right-(float)This->sprites[i].rect.left;
421 float spriteheight=(float)This->sprites[i].rect.bottom-(float)This->sprites[i].rect.top;
423 vertices[6*i ].pos.x = This->sprites[i].pos.x - This->sprites[i].center.x;
424 vertices[6*i ].pos.y = This->sprites[i].pos.y - This->sprites[i].center.y;
425 vertices[6*i ].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
426 vertices[6*i+1].pos.x = spritewidth + This->sprites[i].pos.x - This->sprites[i].center.x;
427 vertices[6*i+1].pos.y = This->sprites[i].pos.y - This->sprites[i].center.y;
428 vertices[6*i+1].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
429 vertices[6*i+2].pos.x = spritewidth + This->sprites[i].pos.x - This->sprites[i].center.x;
430 vertices[6*i+2].pos.y = spriteheight + This->sprites[i].pos.y - This->sprites[i].center.y;
431 vertices[6*i+2].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
432 vertices[6*i+3].pos.x = This->sprites[i].pos.x - This->sprites[i].center.x;
433 vertices[6*i+3].pos.y = spriteheight + This->sprites[i].pos.y - This->sprites[i].center.y;
434 vertices[6*i+3].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
435 vertices[6*i ].col = This->sprites[i].color;
436 vertices[6*i+1].col = This->sprites[i].color;
437 vertices[6*i+2].col = This->sprites[i].color;
438 vertices[6*i+3].col = This->sprites[i].color;
439 vertices[6*i ].tex.x = (float)This->sprites[i].rect.left / (float)This->sprites[i].texw;
440 vertices[6*i ].tex.y = (float)This->sprites[i].rect.top / (float)This->sprites[i].texh;
441 vertices[6*i+1].tex.x = (float)This->sprites[i].rect.right / (float)This->sprites[i].texw;
442 vertices[6*i+1].tex.y = (float)This->sprites[i].rect.top / (float)This->sprites[i].texh;
443 vertices[6*i+2].tex.x = (float)This->sprites[i].rect.right / (float)This->sprites[i].texw;
444 vertices[6*i+2].tex.y = (float)This->sprites[i].rect.bottom / (float)This->sprites[i].texh;
445 vertices[6*i+3].tex.x = (float)This->sprites[i].rect.left / (float)This->sprites[i].texw;
446 vertices[6*i+3].tex.y = (float)This->sprites[i].rect.bottom / (float)This->sprites[i].texh;
448 vertices[6*i+4]=vertices[6*i];
449 vertices[6*i+5]=vertices[6*i+2];
451 D3DXVec3TransformCoordArray(&vertices[6 * i].pos, sizeof(*vertices),
452 &vertices[6 * i].pos, sizeof(*vertices), &This->sprites[i].transform, 6);
453 count++;
454 i++;
457 IDirect3DDevice9_SetTexture(This->device, 0, (struct IDirect3DBaseTexture9 *)This->sprites[start].texture);
458 IDirect3DDevice9_SetVertexDeclaration(This->device, This->vdecl);
460 IDirect3DDevice9_DrawPrimitiveUP(This->device, D3DPT_TRIANGLELIST,
461 2 * count, vertices + 6 * start, sizeof(*vertices));
463 HeapFree(GetProcessHeap(), 0, vertices);
465 if(!(This->flags & D3DXSPRITE_DO_NOT_ADDREF_TEXTURE))
466 for(i=0;i<This->sprite_count;i++)
467 IDirect3DTexture9_Release(This->sprites[i].texture);
469 This->sprite_count=0;
471 /* Flush may be called more than once, so we don't reset This->ready here */
473 return D3D_OK;
476 static HRESULT WINAPI d3dx9_sprite_End(ID3DXSprite *iface)
478 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
480 TRACE("iface %p.\n", iface);
482 if (!sprite->ready)
483 return D3DERR_INVALIDCALL;
485 ID3DXSprite_Flush(iface);
487 if (sprite->stateblock && !(sprite->flags & D3DXSPRITE_DONOTSAVESTATE))
488 IDirect3DStateBlock9_Apply(sprite->stateblock); /* Restore old state */
490 sprite->ready = FALSE;
492 return D3D_OK;
495 static HRESULT WINAPI d3dx9_sprite_OnLostDevice(ID3DXSprite *iface)
497 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
499 TRACE("iface %p.\n", iface);
501 if (sprite->stateblock)
502 IDirect3DStateBlock9_Release(sprite->stateblock);
503 if (sprite->vdecl)
504 IDirect3DVertexDeclaration9_Release(sprite->vdecl);
505 sprite->vdecl = NULL;
506 sprite->stateblock = NULL;
508 /* Reset some variables */
509 ID3DXSprite_OnResetDevice(iface);
511 return D3D_OK;
514 static HRESULT WINAPI d3dx9_sprite_OnResetDevice(ID3DXSprite *iface)
516 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
517 int i;
519 TRACE("iface %p.\n", iface);
521 for (i = 0; i < sprite->sprite_count; ++i)
523 if (sprite->sprites[i].texture)
524 IDirect3DTexture9_Release(sprite->sprites[i].texture);
527 sprite->sprite_count = 0;
528 sprite->flags = 0;
529 sprite->ready = FALSE;
531 /* keep matrices */
532 /* device objects get restored on Begin */
534 return D3D_OK;
537 static const ID3DXSpriteVtbl d3dx9_sprite_vtbl =
539 d3dx9_sprite_QueryInterface,
540 d3dx9_sprite_AddRef,
541 d3dx9_sprite_Release,
542 d3dx9_sprite_GetDevice,
543 d3dx9_sprite_GetTransform,
544 d3dx9_sprite_SetTransform,
545 d3dx9_sprite_SetWorldViewRH,
546 d3dx9_sprite_SetWorldViewLH,
547 d3dx9_sprite_Begin,
548 d3dx9_sprite_Draw,
549 d3dx9_sprite_Flush,
550 d3dx9_sprite_End,
551 d3dx9_sprite_OnLostDevice,
552 d3dx9_sprite_OnResetDevice,
555 HRESULT WINAPI D3DXCreateSprite(struct IDirect3DDevice9 *device, struct ID3DXSprite **sprite)
557 struct d3dx9_sprite *object;
558 D3DCAPS9 caps;
560 TRACE("device %p, sprite %p.\n", device, sprite);
562 if(device==NULL || sprite==NULL) return D3DERR_INVALIDCALL;
564 if (!(object=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
566 *sprite = NULL;
567 return E_OUTOFMEMORY;
569 object->ID3DXSprite_iface.lpVtbl = &d3dx9_sprite_vtbl;
570 object->ref=1;
571 object->device=device;
572 IUnknown_AddRef(device);
574 object->vdecl=NULL;
575 object->stateblock=NULL;
577 D3DXMatrixIdentity(&object->transform);
578 D3DXMatrixIdentity(&object->view);
580 IDirect3DDevice9_GetDeviceCaps(device, &caps);
581 object->texfilter_caps=caps.TextureFilterCaps;
582 object->maxanisotropy=caps.MaxAnisotropy;
583 object->alphacmp_caps=caps.AlphaCmpCaps;
585 ID3DXSprite_OnResetDevice(&object->ID3DXSprite_iface);
587 object->sprites=NULL;
588 object->allocated_sprites=0;
589 *sprite=&object->ID3DXSprite_iface;
591 return D3D_OK;