include: Move struct WSABUF and WSAMSG to ws2def.h.
[wine.git] / dlls / d3dx9_36 / sprite.c
blob580289f673bdfc2e97355615d2ec0833f93975d8
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 "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 struct sprite_vertex
30 D3DXVECTOR3 pos;
31 DWORD col;
32 D3DXVECTOR2 tex;
35 typedef struct _SPRITE {
36 IDirect3DTexture9 *texture;
37 UINT texw, texh;
38 RECT rect;
39 D3DXVECTOR3 center;
40 D3DXVECTOR3 pos;
41 D3DCOLOR color;
42 D3DXMATRIX transform;
43 } SPRITE;
45 struct d3dx9_sprite
47 ID3DXSprite ID3DXSprite_iface;
48 LONG ref;
50 IDirect3DDevice9 *device;
51 IDirect3DVertexDeclaration9 *vdecl;
52 IDirect3DStateBlock9 *stateblock;
53 D3DXMATRIX transform;
54 D3DXMATRIX view;
55 DWORD flags;
56 BOOL ready;
58 /* Store the relevant caps to prevent multiple GetDeviceCaps calls */
59 DWORD texfilter_caps;
60 DWORD maxanisotropy;
61 DWORD alphacmp_caps;
63 SPRITE *sprites;
64 int sprite_count; /* number of sprites to be drawn */
65 int allocated_sprites; /* number of (pre-)allocated sprites */
68 static inline struct d3dx9_sprite *impl_from_ID3DXSprite(ID3DXSprite *iface)
70 return CONTAINING_RECORD(iface, struct d3dx9_sprite, ID3DXSprite_iface);
73 static HRESULT WINAPI d3dx9_sprite_QueryInterface(ID3DXSprite *iface, REFIID riid, void **out)
75 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
77 if (IsEqualGUID(riid, &IID_ID3DXSprite)
78 || IsEqualGUID(riid, &IID_IUnknown))
80 IUnknown_AddRef(iface);
81 *out = iface;
82 return S_OK;
85 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
87 *out = NULL;
88 return E_NOINTERFACE;
91 static ULONG WINAPI d3dx9_sprite_AddRef(ID3DXSprite *iface)
93 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
94 ULONG refcount = InterlockedIncrement(&sprite->ref);
96 TRACE("%p increasing refcount to %u.\n", sprite, refcount);
98 return refcount;
101 static ULONG WINAPI d3dx9_sprite_Release(ID3DXSprite *iface)
103 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
104 ULONG refcount = InterlockedDecrement(&sprite->ref);
106 TRACE("%p decreasing refcount to %u.\n", sprite, refcount);
108 if (!refcount)
110 if (sprite->sprites)
112 int i;
114 for (i = 0; i < sprite->sprite_count; ++i)
116 if (sprite->sprites[i].texture)
117 IDirect3DTexture9_Release(sprite->sprites[i].texture);
120 HeapFree(GetProcessHeap(), 0, sprite->sprites);
123 if (sprite->stateblock)
124 IDirect3DStateBlock9_Release(sprite->stateblock);
125 if (sprite->vdecl)
126 IDirect3DVertexDeclaration9_Release(sprite->vdecl);
127 if (sprite->device)
128 IDirect3DDevice9_Release(sprite->device);
129 HeapFree(GetProcessHeap(), 0, sprite);
132 return refcount;
135 static HRESULT WINAPI d3dx9_sprite_GetDevice(struct ID3DXSprite *iface, struct IDirect3DDevice9 **device)
137 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
139 TRACE("iface %p, device %p.\n", iface, device);
141 if (!device)
142 return D3DERR_INVALIDCALL;
143 *device = sprite->device;
144 IDirect3DDevice9_AddRef(sprite->device);
146 return D3D_OK;
149 static HRESULT WINAPI d3dx9_sprite_GetTransform(ID3DXSprite *iface, D3DXMATRIX *transform)
151 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
153 TRACE("iface %p, transform %p.\n", iface, transform);
155 if (!transform)
156 return D3DERR_INVALIDCALL;
157 *transform = sprite->transform;
159 return D3D_OK;
162 static HRESULT WINAPI d3dx9_sprite_SetTransform(ID3DXSprite *iface, const D3DXMATRIX *transform)
164 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
166 TRACE("iface %p, transform %p.\n", iface, transform);
168 if (!transform)
169 return D3DERR_INVALIDCALL;
170 sprite->transform = *transform;
172 return D3D_OK;
175 static HRESULT WINAPI d3dx9_sprite_SetWorldViewRH(ID3DXSprite *iface,
176 const D3DXMATRIX *world, const D3DXMATRIX *view)
178 FIXME("iface %p, world %p, view %p stub!\n", iface, world, view);
180 return E_NOTIMPL;
183 static HRESULT WINAPI d3dx9_sprite_SetWorldViewLH(ID3DXSprite *iface,
184 const D3DXMATRIX *world, const D3DXMATRIX *view)
186 FIXME("iface %p, world %p, view %p stub!\n", iface, world, view);
188 return E_NOTIMPL;
191 /* Helper function */
192 static void set_states(struct d3dx9_sprite *object)
194 D3DXMATRIX mat;
195 D3DVIEWPORT9 vp;
197 /* Miscellaneous stuff */
198 IDirect3DDevice9_SetVertexShader(object->device, NULL);
199 IDirect3DDevice9_SetPixelShader(object->device, NULL);
200 IDirect3DDevice9_SetNPatchMode(object->device, 0.0f);
202 /* Render states */
203 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHABLENDENABLE, TRUE);
204 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHAFUNC, D3DCMP_GREATER);
205 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHAREF, 0x00);
206 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHATESTENABLE, object->alphacmp_caps);
207 IDirect3DDevice9_SetRenderState(object->device, D3DRS_BLENDOP, D3DBLENDOP_ADD);
208 IDirect3DDevice9_SetRenderState(object->device, D3DRS_CLIPPING, TRUE);
209 IDirect3DDevice9_SetRenderState(object->device, D3DRS_CLIPPLANEENABLE, FALSE);
210 IDirect3DDevice9_SetRenderState(object->device, D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_BLUE |
211 D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_RED);
212 IDirect3DDevice9_SetRenderState(object->device, D3DRS_CULLMODE, D3DCULL_NONE);
213 IDirect3DDevice9_SetRenderState(object->device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
214 IDirect3DDevice9_SetRenderState(object->device, D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1);
215 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ENABLEADAPTIVETESSELLATION, FALSE);
216 IDirect3DDevice9_SetRenderState(object->device, D3DRS_FILLMODE, D3DFILL_SOLID);
217 IDirect3DDevice9_SetRenderState(object->device, D3DRS_FOGENABLE, FALSE);
218 IDirect3DDevice9_SetRenderState(object->device, D3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
219 IDirect3DDevice9_SetRenderState(object->device, D3DRS_LIGHTING, FALSE);
220 IDirect3DDevice9_SetRenderState(object->device, D3DRS_RANGEFOGENABLE, FALSE);
221 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
222 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
223 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SPECULARENABLE, FALSE);
224 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
225 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SRGBWRITEENABLE, FALSE);
226 IDirect3DDevice9_SetRenderState(object->device, D3DRS_STENCILENABLE, FALSE);
227 IDirect3DDevice9_SetRenderState(object->device, D3DRS_VERTEXBLEND, FALSE);
228 IDirect3DDevice9_SetRenderState(object->device, D3DRS_WRAP0, 0);
230 /* Texture stage states */
231 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
232 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
233 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
234 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
235 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
236 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_COLOROP, D3DTOP_MODULATE);
237 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_TEXCOORDINDEX, 0);
238 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
239 IDirect3DDevice9_SetTextureStageState(object->device, 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
240 IDirect3DDevice9_SetTextureStageState(object->device, 1, D3DTSS_COLOROP, D3DTOP_DISABLE);
242 /* Sampler states */
243 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
244 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
246 if(object->texfilter_caps & D3DPTFILTERCAPS_MAGFANISOTROPIC)
247 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC);
248 else IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
250 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAXMIPLEVEL, 0);
251 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAXANISOTROPY, object->maxanisotropy);
253 if(object->texfilter_caps & D3DPTFILTERCAPS_MINFANISOTROPIC)
254 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC);
255 else IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
257 if(object->texfilter_caps & D3DPTFILTERCAPS_MIPFLINEAR)
258 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
259 else IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
261 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MIPMAPLODBIAS, 0);
262 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_SRGBTEXTURE, 0);
264 /* Matrices */
265 D3DXMatrixIdentity(&mat);
266 IDirect3DDevice9_SetTransform(object->device, D3DTS_WORLD, &mat);
267 IDirect3DDevice9_SetTransform(object->device, D3DTS_VIEW, &object->view);
268 IDirect3DDevice9_GetViewport(object->device, &vp);
269 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);
270 IDirect3DDevice9_SetTransform(object->device, D3DTS_PROJECTION, &mat);
273 static HRESULT WINAPI d3dx9_sprite_Begin(ID3DXSprite *iface, DWORD flags)
275 struct d3dx9_sprite *This = impl_from_ID3DXSprite(iface);
276 HRESULT hr;
278 TRACE("iface %p, flags %#x.\n", iface, flags);
280 if(flags>D3DXSPRITE_FLAGLIMIT || This->ready) return D3DERR_INVALIDCALL;
282 /* TODO: Implement flags:
283 D3DXSPRITE_BILLBOARD: makes the sprite always face the camera
284 D3DXSPRITE_DONOTMODIFY_RENDERSTATE: name says it all
285 D3DXSPRITE_OBJECTSPACE: do not change device transforms
286 D3DXSPRITE_SORT_DEPTH_BACKTOFRONT: sort by position
287 D3DXSPRITE_SORT_DEPTH_FRONTTOBACK: sort by position
288 D3DXSPRITE_SORT_TEXTURE: sort by texture (so that it doesn't change too often)
290 /* Seems like alpha blending is always enabled, regardless of D3DXSPRITE_ALPHABLEND flag */
291 if(flags & (D3DXSPRITE_BILLBOARD |
292 D3DXSPRITE_DONOTMODIFY_RENDERSTATE | D3DXSPRITE_OBJECTSPACE |
293 D3DXSPRITE_SORT_DEPTH_BACKTOFRONT))
294 FIXME("Flags unsupported: %#x\n", flags);
295 /* These flags should only matter to performances */
296 else if(flags & (D3DXSPRITE_SORT_DEPTH_FRONTTOBACK | D3DXSPRITE_SORT_TEXTURE))
297 TRACE("Flags unsupported: %#x\n", flags);
299 if(This->vdecl==NULL) {
300 static const D3DVERTEXELEMENT9 elements[] =
302 { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
303 { 0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
304 { 0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
305 D3DDECL_END()
307 IDirect3DDevice9_CreateVertexDeclaration(This->device, elements, &This->vdecl);
310 if(!(flags & D3DXSPRITE_DONOTSAVESTATE)) {
311 if(This->stateblock==NULL) {
312 /* Tell our state block what it must store */
313 hr=IDirect3DDevice9_BeginStateBlock(This->device);
314 if(hr!=D3D_OK) return hr;
316 set_states(This);
318 IDirect3DDevice9_SetVertexDeclaration(This->device, This->vdecl);
319 IDirect3DDevice9_SetStreamSource(This->device, 0, NULL, 0, sizeof(struct sprite_vertex));
320 IDirect3DDevice9_SetIndices(This->device, NULL);
321 IDirect3DDevice9_SetTexture(This->device, 0, NULL);
323 IDirect3DDevice9_EndStateBlock(This->device, &This->stateblock);
325 IDirect3DStateBlock9_Capture(This->stateblock); /* Save current state */
328 /* Apply device state */
329 set_states(This);
331 This->flags=flags;
332 This->ready=TRUE;
334 return D3D_OK;
337 static HRESULT WINAPI d3dx9_sprite_Draw(ID3DXSprite *iface, IDirect3DTexture9 *texture,
338 const RECT *rect, const D3DXVECTOR3 *center, const D3DXVECTOR3 *position, D3DCOLOR color)
340 struct d3dx9_sprite *This = impl_from_ID3DXSprite(iface);
341 D3DSURFACE_DESC texdesc;
343 TRACE("iface %p, texture %p, rect %s, center %p, position %p, color 0x%08x.\n",
344 iface, texture, wine_dbgstr_rect(rect), center, position, color);
346 if(texture==NULL) return D3DERR_INVALIDCALL;
347 if(!This->ready) return D3DERR_INVALIDCALL;
349 if(This->allocated_sprites==0) {
350 This->sprites=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 32*sizeof(SPRITE));
351 This->allocated_sprites=32;
352 } else if(This->allocated_sprites<=This->sprite_count) {
353 This->allocated_sprites=This->allocated_sprites*3/2;
354 This->sprites=HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->sprites, This->allocated_sprites*sizeof(SPRITE));
356 This->sprites[This->sprite_count].texture=texture;
357 if(!(This->flags & D3DXSPRITE_DO_NOT_ADDREF_TEXTURE))
358 IDirect3DTexture9_AddRef(texture);
360 /* Reuse the texture desc if possible */
361 if(This->sprite_count) {
362 if(This->sprites[This->sprite_count-1].texture!=texture) {
363 IDirect3DTexture9_GetLevelDesc(texture, 0, &texdesc);
364 } else {
365 texdesc.Width=This->sprites[This->sprite_count-1].texw;
366 texdesc.Height=This->sprites[This->sprite_count-1].texh;
368 } else IDirect3DTexture9_GetLevelDesc(texture, 0, &texdesc);
370 This->sprites[This->sprite_count].texw=texdesc.Width;
371 This->sprites[This->sprite_count].texh=texdesc.Height;
373 if(rect==NULL) {
374 This->sprites[This->sprite_count].rect.left=0;
375 This->sprites[This->sprite_count].rect.top=0;
376 This->sprites[This->sprite_count].rect.right=texdesc.Width;
377 This->sprites[This->sprite_count].rect.bottom=texdesc.Height;
378 } else This->sprites[This->sprite_count].rect=*rect;
380 if(center==NULL) {
381 This->sprites[This->sprite_count].center.x=0.0f;
382 This->sprites[This->sprite_count].center.y=0.0f;
383 This->sprites[This->sprite_count].center.z=0.0f;
384 } else This->sprites[This->sprite_count].center=*center;
386 if(position==NULL) {
387 This->sprites[This->sprite_count].pos.x=0.0f;
388 This->sprites[This->sprite_count].pos.y=0.0f;
389 This->sprites[This->sprite_count].pos.z=0.0f;
390 } else This->sprites[This->sprite_count].pos=*position;
392 This->sprites[This->sprite_count].color=color;
393 This->sprites[This->sprite_count].transform=This->transform;
394 This->sprite_count++;
396 return D3D_OK;
399 static HRESULT WINAPI d3dx9_sprite_Flush(ID3DXSprite *iface)
401 struct d3dx9_sprite *This = impl_from_ID3DXSprite(iface);
402 struct sprite_vertex *vertices;
403 int i, count=0, start;
405 TRACE("iface %p.\n", iface);
407 if(!This->ready) return D3DERR_INVALIDCALL;
408 if(!This->sprite_count) return D3D_OK;
410 /* TODO: use of a vertex buffer here */
411 vertices = HeapAlloc(GetProcessHeap(), 0, sizeof(*vertices) * 6 * This->sprite_count);
413 for(start=0;start<This->sprite_count;start+=count,count=0) {
414 i=start;
415 while(i<This->sprite_count &&
416 (count==0 || This->sprites[i].texture==This->sprites[i-1].texture)) {
417 float spritewidth=(float)This->sprites[i].rect.right-(float)This->sprites[i].rect.left;
418 float spriteheight=(float)This->sprites[i].rect.bottom-(float)This->sprites[i].rect.top;
420 vertices[6*i ].pos.x = This->sprites[i].pos.x - This->sprites[i].center.x;
421 vertices[6*i ].pos.y = This->sprites[i].pos.y - This->sprites[i].center.y;
422 vertices[6*i ].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
423 vertices[6*i+1].pos.x = spritewidth + This->sprites[i].pos.x - This->sprites[i].center.x;
424 vertices[6*i+1].pos.y = This->sprites[i].pos.y - This->sprites[i].center.y;
425 vertices[6*i+1].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
426 vertices[6*i+2].pos.x = spritewidth + This->sprites[i].pos.x - This->sprites[i].center.x;
427 vertices[6*i+2].pos.y = spriteheight + This->sprites[i].pos.y - This->sprites[i].center.y;
428 vertices[6*i+2].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
429 vertices[6*i+3].pos.x = This->sprites[i].pos.x - This->sprites[i].center.x;
430 vertices[6*i+3].pos.y = spriteheight + This->sprites[i].pos.y - This->sprites[i].center.y;
431 vertices[6*i+3].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
432 vertices[6*i ].col = This->sprites[i].color;
433 vertices[6*i+1].col = This->sprites[i].color;
434 vertices[6*i+2].col = This->sprites[i].color;
435 vertices[6*i+3].col = This->sprites[i].color;
436 vertices[6*i ].tex.x = (float)This->sprites[i].rect.left / (float)This->sprites[i].texw;
437 vertices[6*i ].tex.y = (float)This->sprites[i].rect.top / (float)This->sprites[i].texh;
438 vertices[6*i+1].tex.x = (float)This->sprites[i].rect.right / (float)This->sprites[i].texw;
439 vertices[6*i+1].tex.y = (float)This->sprites[i].rect.top / (float)This->sprites[i].texh;
440 vertices[6*i+2].tex.x = (float)This->sprites[i].rect.right / (float)This->sprites[i].texw;
441 vertices[6*i+2].tex.y = (float)This->sprites[i].rect.bottom / (float)This->sprites[i].texh;
442 vertices[6*i+3].tex.x = (float)This->sprites[i].rect.left / (float)This->sprites[i].texw;
443 vertices[6*i+3].tex.y = (float)This->sprites[i].rect.bottom / (float)This->sprites[i].texh;
445 vertices[6*i+4]=vertices[6*i];
446 vertices[6*i+5]=vertices[6*i+2];
448 D3DXVec3TransformCoordArray(&vertices[6 * i].pos, sizeof(*vertices),
449 &vertices[6 * i].pos, sizeof(*vertices), &This->sprites[i].transform, 6);
450 count++;
451 i++;
454 IDirect3DDevice9_SetTexture(This->device, 0, (struct IDirect3DBaseTexture9 *)This->sprites[start].texture);
455 IDirect3DDevice9_SetVertexDeclaration(This->device, This->vdecl);
457 IDirect3DDevice9_DrawPrimitiveUP(This->device, D3DPT_TRIANGLELIST,
458 2 * count, vertices + 6 * start, sizeof(*vertices));
460 HeapFree(GetProcessHeap(), 0, vertices);
462 if(!(This->flags & D3DXSPRITE_DO_NOT_ADDREF_TEXTURE))
463 for(i=0;i<This->sprite_count;i++)
464 IDirect3DTexture9_Release(This->sprites[i].texture);
466 This->sprite_count=0;
468 /* Flush may be called more than once, so we don't reset This->ready here */
470 return D3D_OK;
473 static HRESULT WINAPI d3dx9_sprite_End(ID3DXSprite *iface)
475 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
477 TRACE("iface %p.\n", iface);
479 if (!sprite->ready)
480 return D3DERR_INVALIDCALL;
482 ID3DXSprite_Flush(iface);
484 if (sprite->stateblock && !(sprite->flags & D3DXSPRITE_DONOTSAVESTATE))
485 IDirect3DStateBlock9_Apply(sprite->stateblock); /* Restore old state */
487 sprite->ready = FALSE;
489 return D3D_OK;
492 static HRESULT WINAPI d3dx9_sprite_OnLostDevice(ID3DXSprite *iface)
494 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
496 TRACE("iface %p.\n", iface);
498 if (sprite->stateblock)
499 IDirect3DStateBlock9_Release(sprite->stateblock);
500 if (sprite->vdecl)
501 IDirect3DVertexDeclaration9_Release(sprite->vdecl);
502 sprite->vdecl = NULL;
503 sprite->stateblock = NULL;
505 /* Reset some variables */
506 ID3DXSprite_OnResetDevice(iface);
508 return D3D_OK;
511 static HRESULT WINAPI d3dx9_sprite_OnResetDevice(ID3DXSprite *iface)
513 struct d3dx9_sprite *sprite = impl_from_ID3DXSprite(iface);
514 int i;
516 TRACE("iface %p.\n", iface);
518 for (i = 0; i < sprite->sprite_count; ++i)
520 if (sprite->sprites[i].texture)
521 IDirect3DTexture9_Release(sprite->sprites[i].texture);
524 sprite->sprite_count = 0;
525 sprite->flags = 0;
526 sprite->ready = FALSE;
528 /* keep matrices */
529 /* device objects get restored on Begin */
531 return D3D_OK;
534 static const ID3DXSpriteVtbl d3dx9_sprite_vtbl =
536 d3dx9_sprite_QueryInterface,
537 d3dx9_sprite_AddRef,
538 d3dx9_sprite_Release,
539 d3dx9_sprite_GetDevice,
540 d3dx9_sprite_GetTransform,
541 d3dx9_sprite_SetTransform,
542 d3dx9_sprite_SetWorldViewRH,
543 d3dx9_sprite_SetWorldViewLH,
544 d3dx9_sprite_Begin,
545 d3dx9_sprite_Draw,
546 d3dx9_sprite_Flush,
547 d3dx9_sprite_End,
548 d3dx9_sprite_OnLostDevice,
549 d3dx9_sprite_OnResetDevice,
552 HRESULT WINAPI D3DXCreateSprite(struct IDirect3DDevice9 *device, struct ID3DXSprite **sprite)
554 struct d3dx9_sprite *object;
555 D3DCAPS9 caps;
557 TRACE("device %p, sprite %p.\n", device, sprite);
559 if(device==NULL || sprite==NULL) return D3DERR_INVALIDCALL;
561 if (!(object=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
563 *sprite = NULL;
564 return E_OUTOFMEMORY;
566 object->ID3DXSprite_iface.lpVtbl = &d3dx9_sprite_vtbl;
567 object->ref=1;
568 object->device=device;
569 IUnknown_AddRef(device);
571 object->vdecl=NULL;
572 object->stateblock=NULL;
574 D3DXMatrixIdentity(&object->transform);
575 D3DXMatrixIdentity(&object->view);
577 IDirect3DDevice9_GetDeviceCaps(device, &caps);
578 object->texfilter_caps=caps.TextureFilterCaps;
579 object->maxanisotropy=caps.MaxAnisotropy;
580 object->alphacmp_caps=caps.AlphaCmpCaps;
582 ID3DXSprite_OnResetDevice(&object->ID3DXSprite_iface);
584 object->sprites=NULL;
585 object->allocated_sprites=0;
586 *sprite=&object->ID3DXSprite_iface;
588 return D3D_OK;