* Sync up to trunk HEAD (r62286).
[reactos.git] / dll / directx / wine / d3dx9_36 / sprite.c
1 /*
2 * Copyright (C) 2008 Tony Wasserka
3 *
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.
8 *
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.
13 *
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
17 *
18 */
19
20 #include "d3dx9_36_private.h"
21
22 /* the combination of all possible D3DXSPRITE flags */
23 #define D3DXSPRITE_FLAGLIMIT 511
24
25 typedef struct _SPRITEVERTEX {
26 D3DXVECTOR3 pos;
27 DWORD col;
28 D3DXVECTOR2 tex;
29 } SPRITEVERTEX;
30
31 typedef struct _SPRITE {
32 IDirect3DTexture9 *texture;
33 UINT texw, texh;
34 RECT rect;
35 D3DXVECTOR3 center;
36 D3DXVECTOR3 pos;
37 D3DCOLOR color;
38 D3DXMATRIX transform;
39 } SPRITE;
40
41 typedef struct ID3DXSpriteImpl
42 {
43 ID3DXSprite ID3DXSprite_iface;
44 LONG ref;
45
46 IDirect3DDevice9 *device;
47 IDirect3DVertexDeclaration9 *vdecl;
48 IDirect3DStateBlock9 *stateblock;
49 D3DXMATRIX transform;
50 D3DXMATRIX view;
51 DWORD flags;
52 BOOL ready;
53
54 /* Store the relevant caps to prevent multiple GetDeviceCaps calls */
55 DWORD texfilter_caps;
56 DWORD maxanisotropy;
57 DWORD alphacmp_caps;
58
59 SPRITE *sprites;
60 int sprite_count; /* number of sprites to be drawn */
61 int allocated_sprites; /* number of (pre-)allocated sprites */
62 } ID3DXSpriteImpl;
63
64 static inline ID3DXSpriteImpl *impl_from_ID3DXSprite(ID3DXSprite *iface)
65 {
66 return CONTAINING_RECORD(iface, ID3DXSpriteImpl, ID3DXSprite_iface);
67 }
68
69 static HRESULT WINAPI ID3DXSpriteImpl_QueryInterface(ID3DXSprite *iface, REFIID riid, void **out)
70 {
71 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
72
73 if (IsEqualGUID(riid, &IID_ID3DXSprite)
74 || IsEqualGUID(riid, &IID_IUnknown))
75 {
76 IUnknown_AddRef(iface);
77 *out = iface;
78 return S_OK;
79 }
80
81 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
82
83 *out = NULL;
84 return E_NOINTERFACE;
85 }
86
87 static ULONG WINAPI ID3DXSpriteImpl_AddRef(ID3DXSprite *iface)
88 {
89 ID3DXSpriteImpl *This = impl_from_ID3DXSprite(iface);
90 ULONG ref=InterlockedIncrement(&This->ref);
91 TRACE("(%p)->(): AddRef from %d\n", This, ref-1);
92 return ref;
93 }
94
95 static ULONG WINAPI ID3DXSpriteImpl_Release(ID3DXSprite *iface)
96 {
97 ID3DXSpriteImpl *This = impl_from_ID3DXSprite(iface);
98 ULONG ref=InterlockedDecrement(&This->ref);
99
100 TRACE("(%p)->(): ReleaseRef to %d\n", This, ref);
101
102 if(ref==0) {
103 if(This->sprites) {
104 int i;
105 for(i=0;i<This->sprite_count;i++)
106 if(This->sprites[i].texture)
107 IDirect3DTexture9_Release(This->sprites[i].texture);
108
109 HeapFree(GetProcessHeap(), 0, This->sprites);
110 }
111 if(This->stateblock) IDirect3DStateBlock9_Release(This->stateblock);
112 if(This->vdecl) IDirect3DVertexDeclaration9_Release(This->vdecl);
113 if(This->device) IDirect3DDevice9_Release(This->device);
114 HeapFree(GetProcessHeap(), 0, This);
115 }
116 return ref;
117 }
118
119 static HRESULT WINAPI ID3DXSpriteImpl_GetDevice(struct ID3DXSprite *iface, struct IDirect3DDevice9 **device)
120 {
121 ID3DXSpriteImpl *This = impl_from_ID3DXSprite(iface);
122
123 TRACE("(%p)->(%p)\n", This, device);
124
125 if(device==NULL) return D3DERR_INVALIDCALL;
126 *device=This->device;
127 IDirect3DDevice9_AddRef(This->device);
128
129 return D3D_OK;
130 }
131
132 static HRESULT WINAPI ID3DXSpriteImpl_GetTransform(ID3DXSprite *iface, D3DXMATRIX *transform)
133 {
134 ID3DXSpriteImpl *This = impl_from_ID3DXSprite(iface);
135
136 TRACE("(%p)->(%p)\n", This, transform);
137
138 if(transform==NULL) return D3DERR_INVALIDCALL;
139 *transform=This->transform;
140
141 return D3D_OK;
142 }
143
144 static HRESULT WINAPI ID3DXSpriteImpl_SetTransform(ID3DXSprite *iface, CONST D3DXMATRIX *transform)
145 {
146 ID3DXSpriteImpl *This = impl_from_ID3DXSprite(iface);
147
148 TRACE("(%p)->(%p)\n", This, transform);
149
150 if(transform==NULL) return D3DERR_INVALIDCALL;
151 This->transform=*transform;
152
153 return D3D_OK;
154 }
155
156 static HRESULT WINAPI ID3DXSpriteImpl_SetWorldViewRH(ID3DXSprite *iface, CONST D3DXMATRIX *world,
157 CONST D3DXMATRIX *view)
158 {
159 ID3DXSpriteImpl *This = impl_from_ID3DXSprite(iface);
160 FIXME("(%p)->(%p, %p): stub\n", This, world, view);
161 return E_NOTIMPL;
162 }
163
164 static HRESULT WINAPI ID3DXSpriteImpl_SetWorldViewLH(ID3DXSprite *iface, CONST D3DXMATRIX *world,
165 CONST D3DXMATRIX *view)
166 {
167 ID3DXSpriteImpl *This = impl_from_ID3DXSprite(iface);
168 FIXME("(%p)->(%p, %p): stub\n", This, world, view);
169 return E_NOTIMPL;
170 }
171
172 /* Helper function */
173 static void set_states(ID3DXSpriteImpl *object)
174 {
175 D3DXMATRIX mat;
176 D3DVIEWPORT9 vp;
177
178 /* Miscellaneous stuff */
179 IDirect3DDevice9_SetVertexShader(object->device, NULL);
180 IDirect3DDevice9_SetPixelShader(object->device, NULL);
181 IDirect3DDevice9_SetNPatchMode(object->device, 0.0f);
182
183 /* Render states */
184 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHABLENDENABLE, TRUE);
185 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHAFUNC, D3DCMP_GREATER);
186 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHAREF, 0x00);
187 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHATESTENABLE, object->alphacmp_caps);
188 IDirect3DDevice9_SetRenderState(object->device, D3DRS_BLENDOP, D3DBLENDOP_ADD);
189 IDirect3DDevice9_SetRenderState(object->device, D3DRS_CLIPPING, TRUE);
190 IDirect3DDevice9_SetRenderState(object->device, D3DRS_CLIPPLANEENABLE, FALSE);
191 IDirect3DDevice9_SetRenderState(object->device, D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_BLUE |
192 D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_RED);
193 IDirect3DDevice9_SetRenderState(object->device, D3DRS_CULLMODE, D3DCULL_NONE);
194 IDirect3DDevice9_SetRenderState(object->device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
195 IDirect3DDevice9_SetRenderState(object->device, D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1);
196 IDirect3DDevice9_SetRenderState(object->device, D3DRS_ENABLEADAPTIVETESSELLATION, FALSE);
197 IDirect3DDevice9_SetRenderState(object->device, D3DRS_FILLMODE, D3DFILL_SOLID);
198 IDirect3DDevice9_SetRenderState(object->device, D3DRS_FOGENABLE, FALSE);
199 IDirect3DDevice9_SetRenderState(object->device, D3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
200 IDirect3DDevice9_SetRenderState(object->device, D3DRS_LIGHTING, FALSE);
201 IDirect3DDevice9_SetRenderState(object->device, D3DRS_RANGEFOGENABLE, FALSE);
202 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
203 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
204 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SPECULARENABLE, FALSE);
205 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
206 IDirect3DDevice9_SetRenderState(object->device, D3DRS_SRGBWRITEENABLE, FALSE);
207 IDirect3DDevice9_SetRenderState(object->device, D3DRS_STENCILENABLE, FALSE);
208 IDirect3DDevice9_SetRenderState(object->device, D3DRS_VERTEXBLEND, FALSE);
209 IDirect3DDevice9_SetRenderState(object->device, D3DRS_WRAP0, 0);
210
211 /* Texture stage states */
212 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
213 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
214 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
215 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
216 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
217 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_COLOROP, D3DTOP_MODULATE);
218 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_TEXCOORDINDEX, 0);
219 IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
220 IDirect3DDevice9_SetTextureStageState(object->device, 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
221 IDirect3DDevice9_SetTextureStageState(object->device, 1, D3DTSS_COLOROP, D3DTOP_DISABLE);
222
223 /* Sampler states */
224 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
225 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
226
227 if(object->texfilter_caps & D3DPTFILTERCAPS_MAGFANISOTROPIC)
228 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC);
229 else IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
230
231 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAXMIPLEVEL, 0);
232 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAXANISOTROPY, object->maxanisotropy);
233
234 if(object->texfilter_caps & D3DPTFILTERCAPS_MINFANISOTROPIC)
235 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC);
236 else IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
237
238 if(object->texfilter_caps & D3DPTFILTERCAPS_MIPFLINEAR)
239 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
240 else IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
241
242 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MIPMAPLODBIAS, 0);
243 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_SRGBTEXTURE, 0);
244
245 /* Matrices */
246 D3DXMatrixIdentity(&mat);
247 IDirect3DDevice9_SetTransform(object->device, D3DTS_WORLD, &mat);
248 IDirect3DDevice9_SetTransform(object->device, D3DTS_VIEW, &object->view);
249 IDirect3DDevice9_GetViewport(object->device, &vp);
250 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);
251 IDirect3DDevice9_SetTransform(object->device, D3DTS_PROJECTION, &mat);
252 }
253
254 static HRESULT WINAPI ID3DXSpriteImpl_Begin(ID3DXSprite *iface, DWORD flags)
255 {
256 ID3DXSpriteImpl *This = impl_from_ID3DXSprite(iface);
257 HRESULT hr;
258
259 TRACE("(%p)->(%#x)\n", This, flags);
260
261 if(flags>D3DXSPRITE_FLAGLIMIT || This->ready) return D3DERR_INVALIDCALL;
262
263 /* TODO: Implement flags:
264 D3DXSPRITE_BILLBOARD: makes the sprite always face the camera
265 D3DXSPRITE_DONOTMODIFY_RENDERSTATE: name says it all
266 D3DXSPRITE_OBJECTSPACE: do not change device transforms
267 D3DXSPRITE_SORT_DEPTH_BACKTOFRONT: sort by position
268 D3DXSPRITE_SORT_DEPTH_FRONTTOBACK: sort by position
269 D3DXSPRITE_SORT_TEXTURE: sort by texture (so that it doesn't change too often)
270 */
271 /* Seems like alpha blending is always enabled, regardless of D3DXSPRITE_ALPHABLEND flag */
272 if(flags & (D3DXSPRITE_BILLBOARD |
273 D3DXSPRITE_DONOTMODIFY_RENDERSTATE | D3DXSPRITE_OBJECTSPACE |
274 D3DXSPRITE_SORT_DEPTH_BACKTOFRONT))
275 FIXME("Flags unsupported: %#x\n", flags);
276 /* These flags should only matter to performances */
277 else if(flags & (D3DXSPRITE_SORT_DEPTH_FRONTTOBACK | D3DXSPRITE_SORT_TEXTURE))
278 TRACE("Flags unsupported: %#x\n", flags);
279
280 if(This->vdecl==NULL) {
281 static const D3DVERTEXELEMENT9 elements[] =
282 {
283 { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
284 { 0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
285 { 0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
286 D3DDECL_END()
287 };
288 IDirect3DDevice9_CreateVertexDeclaration(This->device, elements, &This->vdecl);
289 }
290
291 if(!(flags & D3DXSPRITE_DONOTSAVESTATE)) {
292 if(This->stateblock==NULL) {
293 /* Tell our state block what it must store */
294 hr=IDirect3DDevice9_BeginStateBlock(This->device);
295 if(hr!=D3D_OK) return hr;
296
297 set_states(This);
298
299 IDirect3DDevice9_SetVertexDeclaration(This->device, This->vdecl);
300 IDirect3DDevice9_SetStreamSource(This->device, 0, NULL, 0, sizeof(SPRITEVERTEX));
301 IDirect3DDevice9_SetIndices(This->device, NULL);
302 IDirect3DDevice9_SetTexture(This->device, 0, NULL);
303
304 IDirect3DDevice9_EndStateBlock(This->device, &This->stateblock);
305 }
306 IDirect3DStateBlock9_Capture(This->stateblock); /* Save current state */
307 }
308
309 /* Apply device state */
310 set_states(This);
311
312 This->flags=flags;
313 This->ready=TRUE;
314
315 return D3D_OK;
316 }
317
318 static HRESULT WINAPI ID3DXSpriteImpl_Draw(ID3DXSprite *iface, IDirect3DTexture9 *texture,
319 const RECT *rect, const D3DXVECTOR3 *center, const D3DXVECTOR3 *position, D3DCOLOR color)
320 {
321 ID3DXSpriteImpl *This = impl_from_ID3DXSprite(iface);
322 D3DSURFACE_DESC texdesc;
323
324 TRACE("iface %p, texture %p, rect %s, center %p, position %p, color 0x%08x.\n",
325 iface, texture, wine_dbgstr_rect(rect), center, position, color);
326
327 if(texture==NULL) return D3DERR_INVALIDCALL;
328 if(!This->ready) return D3DERR_INVALIDCALL;
329
330 if(This->allocated_sprites==0) {
331 This->sprites=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 32*sizeof(SPRITE));
332 This->allocated_sprites=32;
333 } else if(This->allocated_sprites<=This->sprite_count) {
334 This->allocated_sprites=This->allocated_sprites*3/2;
335 This->sprites=HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->sprites, This->allocated_sprites*sizeof(SPRITE));
336 }
337 This->sprites[This->sprite_count].texture=texture;
338 if(!(This->flags & D3DXSPRITE_DO_NOT_ADDREF_TEXTURE))
339 IDirect3DTexture9_AddRef(texture);
340
341 /* Reuse the texture desc if possible */
342 if(This->sprite_count) {
343 if(This->sprites[This->sprite_count-1].texture!=texture) {
344 IDirect3DTexture9_GetLevelDesc(texture, 0, &texdesc);
345 } else {
346 texdesc.Width=This->sprites[This->sprite_count-1].texw;
347 texdesc.Height=This->sprites[This->sprite_count-1].texh;
348 }
349 } else IDirect3DTexture9_GetLevelDesc(texture, 0, &texdesc);
350
351 This->sprites[This->sprite_count].texw=texdesc.Width;
352 This->sprites[This->sprite_count].texh=texdesc.Height;
353
354 if(rect==NULL) {
355 This->sprites[This->sprite_count].rect.left=0;
356 This->sprites[This->sprite_count].rect.top=0;
357 This->sprites[This->sprite_count].rect.right=texdesc.Width;
358 This->sprites[This->sprite_count].rect.bottom=texdesc.Height;
359 } else This->sprites[This->sprite_count].rect=*rect;
360
361 if(center==NULL) {
362 This->sprites[This->sprite_count].center.x=0.0f;
363 This->sprites[This->sprite_count].center.y=0.0f;
364 This->sprites[This->sprite_count].center.z=0.0f;
365 } else This->sprites[This->sprite_count].center=*center;
366
367 if(position==NULL) {
368 This->sprites[This->sprite_count].pos.x=0.0f;
369 This->sprites[This->sprite_count].pos.y=0.0f;
370 This->sprites[This->sprite_count].pos.z=0.0f;
371 } else This->sprites[This->sprite_count].pos=*position;
372
373 This->sprites[This->sprite_count].color=color;
374 This->sprites[This->sprite_count].transform=This->transform;
375 This->sprite_count++;
376
377 return D3D_OK;
378 }
379
380 static HRESULT WINAPI ID3DXSpriteImpl_Flush(ID3DXSprite *iface)
381 {
382 ID3DXSpriteImpl *This = impl_from_ID3DXSprite(iface);
383 SPRITEVERTEX *vertices;
384 int i, count=0, start;
385
386 TRACE("(%p)->()\n", This);
387
388 if(!This->ready) return D3DERR_INVALIDCALL;
389 if(!This->sprite_count) return D3D_OK;
390
391 /* TODO: use of a vertex buffer here */
392 vertices=HeapAlloc(GetProcessHeap(), 0, sizeof(SPRITEVERTEX)*6*This->sprite_count);
393
394 for(start=0;start<This->sprite_count;start+=count,count=0) {
395 i=start;
396 while(i<This->sprite_count &&
397 (count==0 || This->sprites[i].texture==This->sprites[i-1].texture)) {
398 float spritewidth=(float)This->sprites[i].rect.right-(float)This->sprites[i].rect.left;
399 float spriteheight=(float)This->sprites[i].rect.bottom-(float)This->sprites[i].rect.top;
400
401 vertices[6*i ].pos.x = This->sprites[i].pos.x - This->sprites[i].center.x;
402 vertices[6*i ].pos.y = This->sprites[i].pos.y - This->sprites[i].center.y;
403 vertices[6*i ].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
404 vertices[6*i+1].pos.x = spritewidth + This->sprites[i].pos.x - This->sprites[i].center.x;
405 vertices[6*i+1].pos.y = This->sprites[i].pos.y - This->sprites[i].center.y;
406 vertices[6*i+1].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
407 vertices[6*i+2].pos.x = spritewidth + This->sprites[i].pos.x - This->sprites[i].center.x;
408 vertices[6*i+2].pos.y = spriteheight + This->sprites[i].pos.y - This->sprites[i].center.y;
409 vertices[6*i+2].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
410 vertices[6*i+3].pos.x = This->sprites[i].pos.x - This->sprites[i].center.x;
411 vertices[6*i+3].pos.y = spriteheight + This->sprites[i].pos.y - This->sprites[i].center.y;
412 vertices[6*i+3].pos.z = This->sprites[i].pos.z - This->sprites[i].center.z;
413 vertices[6*i ].col = This->sprites[i].color;
414 vertices[6*i+1].col = This->sprites[i].color;
415 vertices[6*i+2].col = This->sprites[i].color;
416 vertices[6*i+3].col = This->sprites[i].color;
417 vertices[6*i ].tex.x = (float)This->sprites[i].rect.left / (float)This->sprites[i].texw;
418 vertices[6*i ].tex.y = (float)This->sprites[i].rect.top / (float)This->sprites[i].texh;
419 vertices[6*i+1].tex.x = (float)This->sprites[i].rect.right / (float)This->sprites[i].texw;
420 vertices[6*i+1].tex.y = (float)This->sprites[i].rect.top / (float)This->sprites[i].texh;
421 vertices[6*i+2].tex.x = (float)This->sprites[i].rect.right / (float)This->sprites[i].texw;
422 vertices[6*i+2].tex.y = (float)This->sprites[i].rect.bottom / (float)This->sprites[i].texh;
423 vertices[6*i+3].tex.x = (float)This->sprites[i].rect.left / (float)This->sprites[i].texw;
424 vertices[6*i+3].tex.y = (float)This->sprites[i].rect.bottom / (float)This->sprites[i].texh;
425
426 vertices[6*i+4]=vertices[6*i];
427 vertices[6*i+5]=vertices[6*i+2];
428
429 D3DXVec3TransformCoordArray(&vertices[6*i].pos, sizeof(SPRITEVERTEX),
430 &vertices[6*i].pos, sizeof(SPRITEVERTEX),
431 &This->sprites[i].transform, 6);
432 count++;
433 i++;
434 }
435
436 IDirect3DDevice9_SetTexture(This->device, 0, (struct IDirect3DBaseTexture9 *)This->sprites[start].texture);
437 IDirect3DDevice9_SetVertexDeclaration(This->device, This->vdecl);
438
439 IDirect3DDevice9_DrawPrimitiveUP(This->device, D3DPT_TRIANGLELIST, 2*count, vertices+6*start, sizeof(SPRITEVERTEX));
440 }
441 HeapFree(GetProcessHeap(), 0, vertices);
442
443 if(!(This->flags & D3DXSPRITE_DO_NOT_ADDREF_TEXTURE))
444 for(i=0;i<This->sprite_count;i++)
445 IDirect3DTexture9_Release(This->sprites[i].texture);
446
447 This->sprite_count=0;
448
449 /* Flush may be called more than once, so we don't reset This->ready here */
450
451 return D3D_OK;
452 }
453
454 static HRESULT WINAPI ID3DXSpriteImpl_End(ID3DXSprite *iface)
455 {
456 ID3DXSpriteImpl *This = impl_from_ID3DXSprite(iface);
457
458 TRACE("(%p)->()\n", This);
459
460 if(!This->ready) return D3DERR_INVALIDCALL;
461
462 ID3DXSprite_Flush(iface);
463
464 if(!(This->flags & D3DXSPRITE_DONOTSAVESTATE))
465 if(This->stateblock) IDirect3DStateBlock9_Apply(This->stateblock); /* Restore old state */
466
467 This->ready=FALSE;
468
469 return D3D_OK;
470 }
471
472 static HRESULT WINAPI ID3DXSpriteImpl_OnLostDevice(ID3DXSprite *iface)
473 {
474 ID3DXSpriteImpl *This = impl_from_ID3DXSprite(iface);
475
476 TRACE("(%p)->()\n", This);
477
478 if(This->stateblock) IDirect3DStateBlock9_Release(This->stateblock);
479 if(This->vdecl) IDirect3DVertexDeclaration9_Release(This->vdecl);
480 This->vdecl=NULL;
481 This->stateblock=NULL;
482
483 /* Reset some variables */
484 ID3DXSprite_OnResetDevice(iface);
485
486 return D3D_OK;
487 }
488
489 static HRESULT WINAPI ID3DXSpriteImpl_OnResetDevice(ID3DXSprite *iface)
490 {
491 ID3DXSpriteImpl *This = impl_from_ID3DXSprite(iface);
492 int i;
493
494 TRACE("(%p)->()\n", This);
495
496 for(i=0;i<This->sprite_count;i++)
497 if(This->sprites[i].texture)
498 IDirect3DTexture9_Release(This->sprites[i].texture);
499
500 This->sprite_count=0;
501
502 This->flags=0;
503 This->ready=FALSE;
504
505 /* keep matrices */
506 /* device objects get restored on Begin */
507
508 return D3D_OK;
509 }
510
511 static const ID3DXSpriteVtbl D3DXSprite_Vtbl =
512 {
513 /*** IUnknown methods ***/
514 ID3DXSpriteImpl_QueryInterface,
515 ID3DXSpriteImpl_AddRef,
516 ID3DXSpriteImpl_Release,
517 /*** ID3DXSprite methods ***/
518 ID3DXSpriteImpl_GetDevice,
519 ID3DXSpriteImpl_GetTransform,
520 ID3DXSpriteImpl_SetTransform,
521 ID3DXSpriteImpl_SetWorldViewRH,
522 ID3DXSpriteImpl_SetWorldViewLH,
523 ID3DXSpriteImpl_Begin,
524 ID3DXSpriteImpl_Draw,
525 ID3DXSpriteImpl_Flush,
526 ID3DXSpriteImpl_End,
527 ID3DXSpriteImpl_OnLostDevice,
528 ID3DXSpriteImpl_OnResetDevice
529 };
530
531 HRESULT WINAPI D3DXCreateSprite(struct IDirect3DDevice9 *device, struct ID3DXSprite **sprite)
532 {
533 ID3DXSpriteImpl *object;
534 D3DCAPS9 caps;
535
536 TRACE("(%p, %p)\n", device, sprite);
537
538 if(device==NULL || sprite==NULL) return D3DERR_INVALIDCALL;
539
540 object=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ID3DXSpriteImpl));
541 if(object==NULL) {
542 *sprite=NULL;
543 return E_OUTOFMEMORY;
544 }
545 object->ID3DXSprite_iface.lpVtbl = &D3DXSprite_Vtbl;
546 object->ref=1;
547 object->device=device;
548 IUnknown_AddRef(device);
549
550 object->vdecl=NULL;
551 object->stateblock=NULL;
552
553 D3DXMatrixIdentity(&object->transform);
554 D3DXMatrixIdentity(&object->view);
555
556 IDirect3DDevice9_GetDeviceCaps(device, &caps);
557 object->texfilter_caps=caps.TextureFilterCaps;
558 object->maxanisotropy=caps.MaxAnisotropy;
559 object->alphacmp_caps=caps.AlphaCmpCaps;
560
561 ID3DXSprite_OnResetDevice(&object->ID3DXSprite_iface);
562
563 object->sprites=NULL;
564 object->allocated_sprites=0;
565 *sprite=&object->ID3DXSprite_iface;
566
567 return D3D_OK;
568 }