d3dx9: Implement ID3DXSprite_Draw.
[wine/testsucceed.git] / dlls / d3dx9_36 / sprite.c
blob61de2d60d9210d3516a1ac3f0ac89c90f9c2c592
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 static HRESULT WINAPI ID3DXSpriteImpl_QueryInterface(LPD3DXSPRITE iface, REFIID riid, LPVOID *object)
27 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
29 TRACE("(%p): QueryInterface from %s\n", This, debugstr_guid(riid));
30 if(IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ID3DXSprite)) {
31 IUnknown_AddRef(iface);
32 *object=This;
33 return S_OK;
35 WARN("(%p)->(%s, %p): not found\n", iface, debugstr_guid(riid), *object);
36 return E_NOINTERFACE;
39 static ULONG WINAPI ID3DXSpriteImpl_AddRef(LPD3DXSPRITE iface)
41 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
42 ULONG ref=InterlockedIncrement(&This->ref);
43 TRACE("(%p): AddRef from %d\n", This, ref-1);
44 return ref;
47 static ULONG WINAPI ID3DXSpriteImpl_Release(LPD3DXSPRITE iface)
49 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
50 ULONG ref=InterlockedDecrement(&This->ref);
51 TRACE("(%p): ReleaseRef to %d\n", This, ref);
53 if(ref==0) {
54 if(This->sprites) {
55 int i;
56 for(i=0;i<This->sprite_count;i++)
57 if(This->sprites[i].texture)
58 IDirect3DTexture9_Release(This->sprites[i].texture);
60 HeapFree(GetProcessHeap(), 0, This->sprites);
62 if(This->stateblock) IDirect3DStateBlock9_Release(This->stateblock);
63 if(This->vdecl) IDirect3DVertexDeclaration9_Release(This->vdecl);
64 if(This->device) IDirect3DDevice9_Release(This->device);
65 HeapFree(GetProcessHeap(), 0, This);
67 return ref;
70 static HRESULT WINAPI ID3DXSpriteImpl_GetDevice(LPD3DXSPRITE iface, LPDIRECT3DDEVICE9 *device)
72 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
73 FIXME("(%p): stub\n", This);
74 return E_NOTIMPL;
77 static HRESULT WINAPI ID3DXSpriteImpl_GetTransform(LPD3DXSPRITE iface, D3DXMATRIX *transform)
79 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
80 FIXME("(%p): stub\n", This);
81 return E_NOTIMPL;
84 static HRESULT WINAPI ID3DXSpriteImpl_SetTransform(LPD3DXSPRITE iface, CONST D3DXMATRIX *transform)
86 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
87 FIXME("(%p): stub\n", This);
88 return E_NOTIMPL;
91 static HRESULT WINAPI ID3DXSpriteImpl_SetWorldViewRH(LPD3DXSPRITE iface, CONST D3DXMATRIX *world, CONST D3DXMATRIX *view)
93 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
94 FIXME("(%p): stub\n", This);
95 return E_NOTIMPL;
98 static HRESULT WINAPI ID3DXSpriteImpl_SetWorldViewLH(LPD3DXSPRITE iface, CONST D3DXMATRIX *world, CONST D3DXMATRIX *view)
100 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
101 FIXME("(%p): stub\n", This);
102 return E_NOTIMPL;
105 static HRESULT WINAPI ID3DXSpriteImpl_Begin(LPD3DXSPRITE iface, DWORD flags)
107 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
108 FIXME("(%p): stub\n", This);
109 return E_NOTIMPL;
112 static HRESULT WINAPI ID3DXSpriteImpl_Draw(LPD3DXSPRITE iface, LPDIRECT3DTEXTURE9 texture, CONST RECT *rect, CONST D3DXVECTOR3 *center,
113 CONST D3DXVECTOR3 *position, D3DCOLOR color)
115 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
116 D3DSURFACE_DESC texdesc;
117 TRACE("(%p): relay\n", This);
119 if(texture==NULL) return D3DERR_INVALIDCALL;
121 if(This->allocated_sprites==0) {
122 This->sprites=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 32*sizeof(SPRITE));
123 This->allocated_sprites=32;
124 } else if(This->allocated_sprites<=This->sprite_count) {
125 This->allocated_sprites=This->allocated_sprites*3/2;
126 This->sprites=HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->sprites, This->allocated_sprites*sizeof(SPRITE));
128 This->sprites[This->sprite_count].texture=texture;
129 IUnknown_AddRef(texture);
131 /* Reuse the texture desc if possible */
132 if(This->sprite_count) {
133 if(This->sprites[This->sprite_count-1].texture!=texture) {
134 IDirect3DTexture9_GetLevelDesc(texture, 0, &texdesc);
135 } else {
136 texdesc.Width=This->sprites[This->sprite_count-1].texw;
137 texdesc.Height=This->sprites[This->sprite_count-1].texh;
139 } else IDirect3DTexture9_GetLevelDesc(texture, 0, &texdesc);
141 This->sprites[This->sprite_count].texw=texdesc.Width;
142 This->sprites[This->sprite_count].texh=texdesc.Height;
144 if(rect==NULL) {
145 This->sprites[This->sprite_count].rect.left=0;
146 This->sprites[This->sprite_count].rect.top=0;
147 This->sprites[This->sprite_count].rect.right=texdesc.Width;
148 This->sprites[This->sprite_count].rect.bottom=texdesc.Height;
149 } else This->sprites[This->sprite_count].rect=*rect;
151 if(center==NULL) {
152 This->sprites[This->sprite_count].center.x=0.0f;
153 This->sprites[This->sprite_count].center.y=0.0f;
154 This->sprites[This->sprite_count].center.z=0.0f;
155 } else This->sprites[This->sprite_count].center=*center;
157 if(position==NULL) {
158 This->sprites[This->sprite_count].pos.x=0.0f;
159 This->sprites[This->sprite_count].pos.y=0.0f;
160 This->sprites[This->sprite_count].pos.z=0.0f;
161 } else This->sprites[This->sprite_count].pos=*position;
163 This->sprites[This->sprite_count].color=color;
164 This->sprite_count++;
166 return D3D_OK;
169 static HRESULT WINAPI ID3DXSpriteImpl_Flush(LPD3DXSPRITE iface)
171 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
172 int i;
173 FIXME("(%p): stub\n", This);
175 for(i=0;i<This->sprite_count;i++)
176 if(This->sprites[i].texture)
177 IDirect3DTexture9_Release(This->sprites[i].texture);
179 This->sprite_count=0;
181 return E_NOTIMPL;
184 static HRESULT WINAPI ID3DXSpriteImpl_End(LPD3DXSPRITE iface)
186 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
187 FIXME("(%p): stub\n", This);
189 ID3DXSprite_Flush(iface);
191 return E_NOTIMPL;
194 static HRESULT WINAPI ID3DXSpriteImpl_OnLostDevice(LPD3DXSPRITE iface)
196 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
197 FIXME("(%p): stub\n", This);
198 return E_NOTIMPL;
201 static HRESULT WINAPI ID3DXSpriteImpl_OnResetDevice(LPD3DXSPRITE iface)
203 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
204 FIXME("(%p): stub\n", This);
205 return E_NOTIMPL;
208 static const ID3DXSpriteVtbl D3DXSprite_Vtbl =
210 /*** IUnknown methods ***/
211 ID3DXSpriteImpl_QueryInterface,
212 ID3DXSpriteImpl_AddRef,
213 ID3DXSpriteImpl_Release,
214 /*** ID3DXSprite methods ***/
215 ID3DXSpriteImpl_GetDevice,
216 ID3DXSpriteImpl_GetTransform,
217 ID3DXSpriteImpl_SetTransform,
218 ID3DXSpriteImpl_SetWorldViewRH,
219 ID3DXSpriteImpl_SetWorldViewLH,
220 ID3DXSpriteImpl_Begin,
221 ID3DXSpriteImpl_Draw,
222 ID3DXSpriteImpl_Flush,
223 ID3DXSpriteImpl_End,
224 ID3DXSpriteImpl_OnLostDevice,
225 ID3DXSpriteImpl_OnResetDevice
228 HRESULT WINAPI D3DXCreateSprite(LPDIRECT3DDEVICE9 device, LPD3DXSPRITE *sprite)
230 ID3DXSpriteImpl *object;
231 D3DCAPS9 caps;
232 static const D3DVERTEXELEMENT9 elements[] =
234 { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
235 { 0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
236 { 0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
237 D3DDECL_END()
240 TRACE("(void): relay\n");
242 if(device==NULL || sprite==NULL) return D3DERR_INVALIDCALL;
244 object=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ID3DXSpriteImpl));
245 if(object==NULL) {
246 *sprite=NULL;
247 return E_OUTOFMEMORY;
249 object->lpVtbl=&D3DXSprite_Vtbl;
250 object->ref=1;
251 object->device=device;
252 IUnknown_AddRef(device);
254 IDirect3DDevice9_CreateVertexDeclaration(object->device, elements, &object->vdecl);
255 object->stateblock=NULL;
257 D3DXMatrixIdentity(&object->transform);
258 D3DXMatrixIdentity(&object->view);
260 object->flags=0;
262 IDirect3DDevice9_GetDeviceCaps(device, &caps);
263 object->texfilter_caps=caps.TextureFilterCaps;
264 object->maxanisotropy=caps.MaxAnisotropy;
265 object->alphacmp_caps=caps.AlphaCmpCaps;
267 object->sprites=NULL;
268 object->sprite_count=0;
269 object->allocated_sprites=0;
271 *sprite=(ID3DXSprite*)object;
273 return D3D_OK;