msvcrt: Add scheduler_resource_allocation_error class implementation.
[wine.git] / dlls / d3dx9_36 / sprite.c
blob17ad16a1ad57c4e08be25fd1f00fd066c499e3b8
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
20 #include "config.h"
21 #include "wine/port.h"
23 #include "d3dx9_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
27 /* the combination of all possible D3DXSPRITE flags */
28 #define D3DXSPRITE_FLAGLIMIT 511
30 struct sprite_vertex
32 D3DXVECTOR3 pos;
33 DWORD col;
34 D3DXVECTOR2 tex;
37 struct sprite
39 IDirect3DTexture9 *texture;
40 UINT texw, texh;
41 RECT rect;
42 D3DXVECTOR3 center;
43 D3DXVECTOR3 pos;
44 D3DCOLOR color;
45 D3DXMATRIX transform;
48 struct d3dx9_sprite
50 ID3DXSprite ID3DXSprite_iface;
51 LONG ref;
53 IDirect3DDevice9 *device;
54 IDirect3DVertexDeclaration9 *vdecl;
55 IDirect3DStateBlock9 *stateblock;
56 D3DXMATRIX transform;
57 D3DXMATRIX view;
58 DWORD flags;
59 BOOL ready;
61 /* Store the relevant caps to prevent multiple GetDeviceCaps calls */
62 DWORD texfilter_caps;
63 DWORD maxanisotropy;
64 DWORD alphacmp_caps;
66 struct sprite *sprites;
67 int sprite_count; /* number of sprites to be drawn */
68 int allocated_sprites; /* number of (pre-)allocated sprites */
71 static inline struct d3dx9_sprite *impl_from_ID3DXSprite(ID3DXSprite *iface)
73 return CONTAINING_RECORD(iface, struct d3dx9_sprite, ID3DXSprite_iface);
76 static HRESULT WINAPI d3dx9_sprite_QueryInterface(ID3DXSprite *iface, REFIID riid, void **out)
78 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
80 if (IsEqualGUID(riid, &IID_ID3DXSprite)
81 || IsEqualGUID(riid, &IID_IUnknown))
83 IUnknown_AddRef(iface);
84 *out = iface;
85 return S_OK;
88 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
90 *out = NULL;
91 return E_NOINTERFACE;
94 static ULONG WINAPI d3dx9_sprite_AddRef(ID3DXSprite *iface)
96 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
97 ULONG refcount = InterlockedIncrement(&sprite->ref);
99 TRACE("%p increasing refcount to %u.\n", sprite, refcount);
101 return refcount;
104 static ULONG WINAPI d3dx9_sprite_Release(ID3DXSprite *iface)
106 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
107 ULONG refcount = InterlockedDecrement(&sprite->ref);
109 TRACE("%p decreasing refcount to %u.\n", sprite, refcount);
111 if (!refcount)
113 if (sprite->sprites)
115 int i;
117 for (i = 0; i < sprite->sprite_count; ++i)
119 if (sprite->sprites[i].texture)
120 IDirect3DTexture9_Release(sprite->sprites[i].texture);
123 HeapFree(GetProcessHeap(), 0, sprite->sprites);
126 if (sprite->stateblock)
127 IDirect3DStateBlock9_Release(sprite->stateblock);
128 if (sprite->vdecl)
129 IDirect3DVertexDeclaration9_Release(sprite->vdecl);
130 if (sprite->device)
131 IDirect3DDevice9_Release(sprite->device);
132 HeapFree(GetProcessHeap(), 0, sprite);
135 return refcount;
138 static HRESULT WINAPI d3dx9_sprite_GetDevice(struct ID3DXSprite *iface, struct IDirect3DDevice9 **device)
140 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
142 TRACE("iface %p, device %p.\n", iface, device);
144 if (!device)
145 return D3DERR_INVALIDCALL;
146 *device = sprite->device;
147 IDirect3DDevice9_AddRef(sprite->device);
149 return D3D_OK;
152 static HRESULT WINAPI d3dx9_sprite_GetTransform(ID3DXSprite *iface, D3DXMATRIX *transform)
154 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
156 TRACE("iface %p, transform %p.\n", iface, transform);
158 if (!transform)
159 return D3DERR_INVALIDCALL;
160 *transform = sprite->transform;
162 return D3D_OK;
165 static HRESULT WINAPI d3dx9_sprite_SetTransform(ID3DXSprite *iface, const D3DXMATRIX *transform)
167 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
169 TRACE("iface %p, transform %p.\n", iface, transform);
171 if (!transform)
172 return D3DERR_INVALIDCALL;
173 sprite->transform = *transform;
175 return D3D_OK;
178 static HRESULT WINAPI d3dx9_sprite_SetWorldViewRH(ID3DXSprite *iface,
179 const D3DXMATRIX *world, const D3DXMATRIX *view)
181 FIXME("iface %p, world %p, view %p stub!\n", iface, world, view);
183 return E_NOTIMPL;
186 static HRESULT WINAPI d3dx9_sprite_SetWorldViewLH(ID3DXSprite *iface,
187 const D3DXMATRIX *world, const D3DXMATRIX *view)
189 FIXME("iface %p, world %p, view %p stub!\n", iface, world, view);
191 return E_NOTIMPL;
194 /* Helper function */
195 static void set_states(struct d3dx9_sprite *object)
197 D3DXMATRIX mat;
198 D3DVIEWPORT9 vp;
200 /* Miscellaneous stuff */
201 IDirect3DDevice9_SetVertexShader(object->device, NULL);
202 IDirect3DDevice9_SetPixelShader(object->device, NULL);
203 IDirect3DDevice9_SetNPatchMode(object->device, 0.0f);
205 /* Render states */
206 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHABLENDENABLE, TRUE);
207 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHAFUNC, D3DCMP_GREATER);
208 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHAREF, 0x00);
209 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHATESTENABLE, object->alphacmp_caps);
210 IDirect3DDevice9_SetRenderState(object->device, D3DRS_BLENDOP, D3DBLENDOP_ADD);
211 IDirect3DDevice9_SetRenderState(object->device, D3DRS_CLIPPING, TRUE);
212 IDirect3DDevice9_SetRenderState(object->device, D3DRS_CLIPPLANEENABLE, FALSE);
213 IDirect3DDevice9_SetRenderState(object->device, D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_BLUE |
214 D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_RED);
215 IDirect3DDevice9_SetRenderState(object->device, D3DRS_CULLMODE, D3DCULL_NONE);
216 IDirect3DDevice9_SetRenderState(object->device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
217 IDirect3DDevice9_SetRenderState(object->device, D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1);
218 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ENABLEADAPTIVETESSELLATION, FALSE);
219 IDirect3DDevice9_SetRenderState(object->device, D3DRS_FILLMODE, D3DFILL_SOLID);
220 IDirect3DDevice9_SetRenderState(object->device, D3DRS_FOGENABLE, FALSE);
221 IDirect3DDevice9_SetRenderState(object->device, D3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
222 IDirect3DDevice9_SetRenderState(object->device, D3DRS_LIGHTING, FALSE);
223 IDirect3DDevice9_SetRenderState(object->device, D3DRS_RANGEFOGENABLE, FALSE);
224 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
225 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
226 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SPECULARENABLE, FALSE);
227 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
228 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SRGBWRITEENABLE, FALSE);
229 IDirect3DDevice9_SetRenderState(object->device, D3DRS_STENCILENABLE, FALSE);
230 IDirect3DDevice9_SetRenderState(object->device, D3DRS_VERTEXBLEND, FALSE);
231 IDirect3DDevice9_SetRenderState(object->device, D3DRS_WRAP0, 0);
233 /* Texture stage states */
234 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
235 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
236 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
237 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
238 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
239 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_COLOROP, D3DTOP_MODULATE);
240 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_TEXCOORDINDEX, 0);
241 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
242 IDirect3DDevice9_SetTextureStageState(object->device, 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
243 IDirect3DDevice9_SetTextureStageState(object->device, 1, D3DTSS_COLOROP, D3DTOP_DISABLE);
245 /* Sampler states */
246 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
247 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
249 if(object->texfilter_caps & D3DPTFILTERCAPS_MAGFANISOTROPIC)
250 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC);
251 else IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
253 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAXMIPLEVEL, 0);
254 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAXANISOTROPY, object->maxanisotropy);
256 if(object->texfilter_caps & D3DPTFILTERCAPS_MINFANISOTROPIC)
257 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC);
258 else IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
260 if(object->texfilter_caps & D3DPTFILTERCAPS_MIPFLINEAR)
261 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
262 else IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
264 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MIPMAPLODBIAS, 0);
265 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_SRGBTEXTURE, 0);
267 /* Matrices */
268 D3DXMatrixIdentity(&mat);
269 IDirect3DDevice9_SetTransform(object->device, D3DTS_WORLD, &mat);
270 IDirect3DDevice9_SetTransform(object->device, D3DTS_VIEW, &object->view);
271 IDirect3DDevice9_GetViewport(object->device, &vp);
272 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);
273 IDirect3DDevice9_SetTransform(object->device, D3DTS_PROJECTION, &mat);
276 static HRESULT WINAPI d3dx9_sprite_Begin(ID3DXSprite *iface, DWORD flags)
278 struct d3dx9_sprite *This = impl_from_ID3DXSprite(iface);
279 HRESULT hr;
281 TRACE("iface %p, flags %#x.\n", iface, flags);
283 if(flags>D3DXSPRITE_FLAGLIMIT || This->ready) return D3DERR_INVALIDCALL;
285 /* TODO: Implement flags:
286 D3DXSPRITE_BILLBOARD: makes the sprite always face the camera
287 D3DXSPRITE_DONOTMODIFY_RENDERSTATE: name says it all
288 D3DXSPRITE_OBJECTSPACE: do not change device transforms
289 D3DXSPRITE_SORT_DEPTH_BACKTOFRONT: sort by position
290 D3DXSPRITE_SORT_DEPTH_FRONTTOBACK: sort by position
291 D3DXSPRITE_SORT_TEXTURE: sort by texture (so that it doesn't change too often)
293 /* Seems like alpha blending is always enabled, regardless of D3DXSPRITE_ALPHABLEND flag */
294 if(flags & (D3DXSPRITE_BILLBOARD |
295 D3DXSPRITE_DONOTMODIFY_RENDERSTATE | D3DXSPRITE_OBJECTSPACE |
296 D3DXSPRITE_SORT_DEPTH_BACKTOFRONT))
297 FIXME("Flags unsupported: %#x\n", flags);
298 /* These flags should only matter to performance */
299 else if(flags & (D3DXSPRITE_SORT_DEPTH_FRONTTOBACK | D3DXSPRITE_SORT_TEXTURE))
300 TRACE("Flags unsupported: %#x\n", flags);
302 if(This->vdecl==NULL) {
303 static const D3DVERTEXELEMENT9 elements[] =
305 { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
306 { 0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
307 { 0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
308 D3DDECL_END()
310 IDirect3DDevice9_CreateVertexDeclaration(This->device, elements, &This->vdecl);
313 if(!(flags & D3DXSPRITE_DONOTSAVESTATE)) {
314 if(This->stateblock==NULL) {
315 /* Tell our state block what it must store */
316 hr=IDirect3DDevice9_BeginStateBlock(This->device);
317 if(hr!=D3D_OK) return hr;
319 set_states(This);
321 IDirect3DDevice9_SetVertexDeclaration(This->device, This->vdecl);
322 IDirect3DDevice9_SetStreamSource(This->device, 0, NULL, 0, sizeof(struct sprite_vertex));
323 IDirect3DDevice9_SetIndices(This->device, NULL);
324 IDirect3DDevice9_SetTexture(This->device, 0, NULL);
326 IDirect3DDevice9_EndStateBlock(This->device, &This->stateblock);
328 IDirect3DStateBlock9_Capture(This->stateblock); /* Save current state */
331 /* Apply device state */
332 set_states(This);
334 This->flags=flags;
335 This->ready=TRUE;
337 return D3D_OK;
340 static HRESULT WINAPI d3dx9_sprite_Draw(ID3DXSprite *iface, IDirect3DTexture9 *texture,
341 const RECT *rect, const D3DXVECTOR3 *center, const D3DXVECTOR3 *position, D3DCOLOR color)
343 struct d3dx9_sprite *This = impl_from_ID3DXSprite(iface);
344 D3DSURFACE_DESC texdesc;
346 TRACE("iface %p, texture %p, rect %s, center %p, position %p, color 0x%08x.\n",
347 iface, texture, wine_dbgstr_rect(rect), center, position, color);
349 if(texture==NULL) return D3DERR_INVALIDCALL;
350 if(!This->ready) return D3DERR_INVALIDCALL;
352 if (!This->allocated_sprites)
354 This->sprites = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 32 * sizeof(*This->sprites));
355 This->allocated_sprites = 32;
357 else if (This->allocated_sprites <= This->sprite_count)
359 This->allocated_sprites += This->allocated_sprites / 2;
360 This->sprites = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
361 This->sprites, This->allocated_sprites * sizeof(*This->sprites));
363 This->sprites[This->sprite_count].texture=texture;
364 if(!(This->flags & D3DXSPRITE_DO_NOT_ADDREF_TEXTURE))
365 IDirect3DTexture9_AddRef(texture);
367 /* Reuse the texture desc if possible */
368 if(This->sprite_count) {
369 if(This->sprites[This->sprite_count-1].texture!=texture) {
370 IDirect3DTexture9_GetLevelDesc(texture, 0, &texdesc);
371 } else {
372 texdesc.Width=This->sprites[This->sprite_count-1].texw;
373 texdesc.Height=This->sprites[This->sprite_count-1].texh;
375 } else IDirect3DTexture9_GetLevelDesc(texture, 0, &texdesc);
377 This->sprites[This->sprite_count].texw=texdesc.Width;
378 This->sprites[This->sprite_count].texh=texdesc.Height;
380 if (rect)
381 This->sprites[This->sprite_count].rect = *rect;
382 else
383 SetRect(&This->sprites[This->sprite_count].rect, 0, 0, texdesc.Width, texdesc.Height);
385 if(center==NULL) {
386 This->sprites[This->sprite_count].center.x=0.0f;
387 This->sprites[This->sprite_count].center.y=0.0f;
388 This->sprites[This->sprite_count].center.z=0.0f;
389 } else This->sprites[This->sprite_count].center=*center;
391 if(position==NULL) {
392 This->sprites[This->sprite_count].pos.x=0.0f;
393 This->sprites[This->sprite_count].pos.y=0.0f;
394 This->sprites[This->sprite_count].pos.z=0.0f;
395 } else This->sprites[This->sprite_count].pos=*position;
397 This->sprites[This->sprite_count].color=color;
398 This->sprites[This->sprite_count].transform=This->transform;
399 This->sprite_count++;
401 return D3D_OK;
404 static HRESULT WINAPI d3dx9_sprite_Flush(ID3DXSprite *iface)
406 struct d3dx9_sprite *This = impl_from_ID3DXSprite(iface);
407 struct sprite_vertex *vertices;
408 int i, count=0, start;
410 TRACE("iface %p.\n", iface);
412 if(!This->ready) return D3DERR_INVALIDCALL;
413 if(!This->sprite_count) return D3D_OK;
415 /* TODO: use of a vertex buffer here */
416 vertices = HeapAlloc(GetProcessHeap(), 0, sizeof(*vertices) * 6 * This->sprite_count);
418 for(start=0;start<This->sprite_count;start+=count,count=0) {
419 i=start;
420 while(i<This->sprite_count &&
421 (count==0 || This->sprites[i].texture==This->sprites[i-1].texture)) {
422 float spritewidth=(float)This->sprites[i].rect.right-(float)This->sprites[i].rect.left;
423 float spriteheight=(float)This->sprites[i].rect.bottom-(float)This->sprites[i].rect.top;
425 vertices[6*i ].pos.x = This->sprites[i].pos.x - This->sprites[i].center.x;
426 vertices[6*i ].pos.y = This->sprites[i].pos.y - This->sprites[i].center.y;
427 vertices[6*i ].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
428 vertices[6*i+1].pos.x = spritewidth + This->sprites[i].pos.x - This->sprites[i].center.x;
429 vertices[6*i+1].pos.y = This->sprites[i].pos.y - This->sprites[i].center.y;
430 vertices[6*i+1].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
431 vertices[6*i+2].pos.x = spritewidth + This->sprites[i].pos.x - This->sprites[i].center.x;
432 vertices[6*i+2].pos.y = spriteheight + This->sprites[i].pos.y - This->sprites[i].center.y;
433 vertices[6*i+2].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
434 vertices[6*i+3].pos.x = This->sprites[i].pos.x - This->sprites[i].center.x;
435 vertices[6*i+3].pos.y = spriteheight + This->sprites[i].pos.y - This->sprites[i].center.y;
436 vertices[6*i+3].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
437 vertices[6*i ].col = This->sprites[i].color;
438 vertices[6*i+1].col = This->sprites[i].color;
439 vertices[6*i+2].col = This->sprites[i].color;
440 vertices[6*i+3].col = This->sprites[i].color;
441 vertices[6*i ].tex.x = (float)This->sprites[i].rect.left / (float)This->sprites[i].texw;
442 vertices[6*i ].tex.y = (float)This->sprites[i].rect.top / (float)This->sprites[i].texh;
443 vertices[6*i+1].tex.x = (float)This->sprites[i].rect.right / (float)This->sprites[i].texw;
444 vertices[6*i+1].tex.y = (float)This->sprites[i].rect.top / (float)This->sprites[i].texh;
445 vertices[6*i+2].tex.x = (float)This->sprites[i].rect.right / (float)This->sprites[i].texw;
446 vertices[6*i+2].tex.y = (float)This->sprites[i].rect.bottom / (float)This->sprites[i].texh;
447 vertices[6*i+3].tex.x = (float)This->sprites[i].rect.left / (float)This->sprites[i].texw;
448 vertices[6*i+3].tex.y = (float)This->sprites[i].rect.bottom / (float)This->sprites[i].texh;
450 vertices[6*i+4]=vertices[6*i];
451 vertices[6*i+5]=vertices[6*i+2];
453 D3DXVec3TransformCoordArray(&vertices[6 * i].pos, sizeof(*vertices),
454 &vertices[6 * i].pos, sizeof(*vertices), &This->sprites[i].transform, 6);
455 count++;
456 i++;
459 IDirect3DDevice9_SetTexture(This->device, 0, (struct IDirect3DBaseTexture9 *)This->sprites[start].texture);
460 IDirect3DDevice9_SetVertexDeclaration(This->device, This->vdecl);
462 IDirect3DDevice9_DrawPrimitiveUP(This->device, D3DPT_TRIANGLELIST,
463 2 * count, vertices + 6 * start, sizeof(*vertices));
465 HeapFree(GetProcessHeap(), 0, vertices);
467 if(!(This->flags & D3DXSPRITE_DO_NOT_ADDREF_TEXTURE))
468 for(i=0;i<This->sprite_count;i++)
469 IDirect3DTexture9_Release(This->sprites[i].texture);
471 This->sprite_count=0;
473 /* Flush may be called more than once, so we don't reset This->ready here */
475 return D3D_OK;
478 static HRESULT WINAPI d3dx9_sprite_End(ID3DXSprite *iface)
480 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
482 TRACE("iface %p.\n", iface);
484 if (!sprite->ready)
485 return D3DERR_INVALIDCALL;
487 ID3DXSprite_Flush(iface);
489 if (sprite->stateblock && !(sprite->flags & D3DXSPRITE_DONOTSAVESTATE))
490 IDirect3DStateBlock9_Apply(sprite->stateblock); /* Restore old state */
492 sprite->ready = FALSE;
494 return D3D_OK;
497 static HRESULT WINAPI d3dx9_sprite_OnLostDevice(ID3DXSprite *iface)
499 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
501 TRACE("iface %p.\n", iface);
503 if (sprite->stateblock)
504 IDirect3DStateBlock9_Release(sprite->stateblock);
505 if (sprite->vdecl)
506 IDirect3DVertexDeclaration9_Release(sprite->vdecl);
507 sprite->vdecl = NULL;
508 sprite->stateblock = NULL;
510 /* Reset some variables */
511 ID3DXSprite_OnResetDevice(iface);
513 return D3D_OK;
516 static HRESULT WINAPI d3dx9_sprite_OnResetDevice(ID3DXSprite *iface)
518 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
519 int i;
521 TRACE("iface %p.\n", iface);
523 for (i = 0; i < sprite->sprite_count; ++i)
525 if (sprite->sprites[i].texture)
526 IDirect3DTexture9_Release(sprite->sprites[i].texture);
529 sprite->sprite_count = 0;
530 sprite->flags = 0;
531 sprite->ready = FALSE;
533 /* keep matrices */
534 /* device objects get restored on Begin */
536 return D3D_OK;
539 static const ID3DXSpriteVtbl d3dx9_sprite_vtbl =
541 d3dx9_sprite_QueryInterface,
542 d3dx9_sprite_AddRef,
543 d3dx9_sprite_Release,
544 d3dx9_sprite_GetDevice,
545 d3dx9_sprite_GetTransform,
546 d3dx9_sprite_SetTransform,
547 d3dx9_sprite_SetWorldViewRH,
548 d3dx9_sprite_SetWorldViewLH,
549 d3dx9_sprite_Begin,
550 d3dx9_sprite_Draw,
551 d3dx9_sprite_Flush,
552 d3dx9_sprite_End,
553 d3dx9_sprite_OnLostDevice,
554 d3dx9_sprite_OnResetDevice,
557 HRESULT WINAPI D3DXCreateSprite(struct IDirect3DDevice9 *device, struct ID3DXSprite **sprite)
559 struct d3dx9_sprite *object;
560 D3DCAPS9 caps;
562 TRACE("device %p, sprite %p.\n", device, sprite);
564 if(device==NULL || sprite==NULL) return D3DERR_INVALIDCALL;
566 if (!(object=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
568 *sprite = NULL;
569 return E_OUTOFMEMORY;
571 object->ID3DXSprite_iface.lpVtbl = &d3dx9_sprite_vtbl;
572 object->ref=1;
573 object->device=device;
574 IUnknown_AddRef(device);
576 object->vdecl=NULL;
577 object->stateblock=NULL;
579 D3DXMatrixIdentity(&object->transform);
580 D3DXMatrixIdentity(&object->view);
582 IDirect3DDevice9_GetDeviceCaps(device, &caps);
583 object->texfilter_caps=caps.TextureFilterCaps;
584 object->maxanisotropy=caps.MaxAnisotropy;
585 object->alphacmp_caps=caps.AlphaCmpCaps;
587 ID3DXSprite_OnResetDevice(&object->ID3DXSprite_iface);
589 object->sprites=NULL;
590 object->allocated_sprites=0;
591 *sprite=&object->ID3DXSprite_iface;
593 return D3D_OK;