d3dx9: Allow setting texture to NULL.
[wine.git] / dlls / ddraw / palette.c
blobcff59a6340b3eb0b1e9dff4c436fd107d81c9f62
1 /*
2 * Copyright 2006 Stefan Dösinger
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
19 #include "config.h"
20 #include "wine/port.h"
22 #include "ddraw_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
26 /*****************************************************************************
27 * IDirectDrawPalette::QueryInterface
29 * A usual QueryInterface implementation. Can only Query IUnknown and
30 * IDirectDrawPalette
32 * Params:
33 * refiid: The interface id queried for
34 * obj: Address to return the interface pointer at
36 * Returns:
37 * S_OK on success
38 * E_NOINTERFACE if the requested interface wasn't found
39 *****************************************************************************/
40 static HRESULT WINAPI
41 IDirectDrawPaletteImpl_QueryInterface(IDirectDrawPalette *iface,
42 REFIID refiid,
43 void **obj)
45 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(refiid), obj);
47 if (IsEqualGUID(refiid, &IID_IUnknown)
48 || IsEqualGUID(refiid, &IID_IDirectDrawPalette))
50 *obj = iface;
51 IDirectDrawPalette_AddRef(iface);
52 return S_OK;
54 else
56 *obj = NULL;
57 return E_NOINTERFACE;
61 /*****************************************************************************
62 * IDirectDrawPaletteImpl::AddRef
64 * Increases the refcount.
66 * Returns:
67 * The new refcount
69 *****************************************************************************/
70 static ULONG WINAPI
71 IDirectDrawPaletteImpl_AddRef(IDirectDrawPalette *iface)
73 IDirectDrawPaletteImpl *This = impl_from_IDirectDrawPalette(iface);
74 ULONG ref = InterlockedIncrement(&This->ref);
76 TRACE("%p increasing refcount to %u.\n", This, ref);
78 return ref;
81 /*****************************************************************************
82 * IDirectDrawPaletteImpl::Release
84 * Reduces the refcount. If the refcount falls to 0, the object is destroyed
86 * Returns:
87 * The new refcount
89 *****************************************************************************/
90 static ULONG WINAPI
91 IDirectDrawPaletteImpl_Release(IDirectDrawPalette *iface)
93 IDirectDrawPaletteImpl *This = impl_from_IDirectDrawPalette(iface);
94 ULONG ref = InterlockedDecrement(&This->ref);
96 TRACE("%p decreasing refcount to %u.\n", This, ref);
98 if (ref == 0)
100 wined3d_mutex_lock();
101 wined3d_palette_decref(This->wineD3DPalette);
102 if(This->ifaceToRelease)
104 IUnknown_Release(This->ifaceToRelease);
106 wined3d_mutex_unlock();
108 HeapFree(GetProcessHeap(), 0, This);
111 return ref;
114 /*****************************************************************************
115 * IDirectDrawPalette::Initialize
117 * Initializes the palette. As we start initialized, return
118 * DDERR_ALREADYINITIALIZED
120 * Params:
121 * DD: DirectDraw interface this palette is assigned to
122 * Flags: Some flags, as usual
123 * ColorTable: The startup color table
125 * Returns:
126 * DDERR_ALREADYINITIALIZED
128 *****************************************************************************/
129 static HRESULT WINAPI
130 IDirectDrawPaletteImpl_Initialize(IDirectDrawPalette *iface,
131 IDirectDraw *DD,
132 DWORD Flags,
133 PALETTEENTRY *ColorTable)
135 TRACE("iface %p, ddraw %p, flags %#x, entries %p.\n",
136 iface, DD, Flags, ColorTable);
138 return DDERR_ALREADYINITIALIZED;
141 /*****************************************************************************
142 * IDirectDrawPalette::GetCaps
144 * Returns the palette description
146 * Params:
147 * Caps: Address to store the caps at
149 * Returns:
150 * D3D_OK on success
151 * DDERR_INVALIDPARAMS if Caps is NULL
152 * For more details, see IWineD3DPalette::GetCaps
154 *****************************************************************************/
155 static HRESULT WINAPI
156 IDirectDrawPaletteImpl_GetCaps(IDirectDrawPalette *iface,
157 DWORD *Caps)
159 IDirectDrawPaletteImpl *This = impl_from_IDirectDrawPalette(iface);
161 TRACE("iface %p, caps %p.\n", iface, Caps);
163 wined3d_mutex_lock();
164 *Caps = wined3d_palette_get_flags(This->wineD3DPalette);
165 wined3d_mutex_unlock();
167 return D3D_OK;
170 /*****************************************************************************
171 * IDirectDrawPalette::SetEntries
173 * Sets the palette entries from a PALETTEENTRY structure. WineD3D takes
174 * care for updating the surface.
176 * Params:
177 * Flags: Flags, as usual
178 * Start: First palette entry to set
179 * Count: Number of entries to set
180 * PalEnt: Source entries
182 * Returns:
183 * D3D_OK on success
184 * DDERR_INVALIDPARAMS if PalEnt is NULL
185 * For details, see IWineD3DDevice::SetEntries
187 *****************************************************************************/
188 static HRESULT WINAPI
189 IDirectDrawPaletteImpl_SetEntries(IDirectDrawPalette *iface,
190 DWORD Flags,
191 DWORD Start,
192 DWORD Count,
193 PALETTEENTRY *PalEnt)
195 IDirectDrawPaletteImpl *This = impl_from_IDirectDrawPalette(iface);
196 HRESULT hr;
198 TRACE("iface %p, flags %#x, start %u, count %u, entries %p.\n",
199 iface, Flags, Start, Count, PalEnt);
201 if(!PalEnt)
202 return DDERR_INVALIDPARAMS;
204 wined3d_mutex_lock();
205 hr = wined3d_palette_set_entries(This->wineD3DPalette, Flags, Start, Count, PalEnt);
206 wined3d_mutex_unlock();
208 return hr;
211 /*****************************************************************************
212 * IDirectDrawPalette::GetEntries
214 * Returns the entries stored in this interface.
216 * Params:
217 * Flags: Flags :)
218 * Start: First entry to return
219 * Count: The number of entries to return
220 * PalEnt: PALETTEENTRY structure to write the entries to
222 * Returns:
223 * D3D_OK on success
224 * DDERR_INVALIDPARAMS if PalEnt is NULL
225 * For details, see IWineD3DDevice::SetEntries
227 *****************************************************************************/
228 static HRESULT WINAPI
229 IDirectDrawPaletteImpl_GetEntries(IDirectDrawPalette *iface,
230 DWORD Flags,
231 DWORD Start,
232 DWORD Count,
233 PALETTEENTRY *PalEnt)
235 IDirectDrawPaletteImpl *This = impl_from_IDirectDrawPalette(iface);
236 HRESULT hr;
238 TRACE("iface %p, flags %#x, start %u, count %u, entries %p.\n",
239 iface, Flags, Start, Count, PalEnt);
241 if(!PalEnt)
242 return DDERR_INVALIDPARAMS;
244 wined3d_mutex_lock();
245 hr = wined3d_palette_get_entries(This->wineD3DPalette, Flags, Start, Count, PalEnt);
246 wined3d_mutex_unlock();
248 return hr;
251 static const struct IDirectDrawPaletteVtbl ddraw_palette_vtbl =
253 /*** IUnknown ***/
254 IDirectDrawPaletteImpl_QueryInterface,
255 IDirectDrawPaletteImpl_AddRef,
256 IDirectDrawPaletteImpl_Release,
257 /*** IDirectDrawPalette ***/
258 IDirectDrawPaletteImpl_GetCaps,
259 IDirectDrawPaletteImpl_GetEntries,
260 IDirectDrawPaletteImpl_Initialize,
261 IDirectDrawPaletteImpl_SetEntries
264 IDirectDrawPaletteImpl *unsafe_impl_from_IDirectDrawPalette(IDirectDrawPalette *iface)
266 if (!iface) return NULL;
267 assert(iface->lpVtbl == &ddraw_palette_vtbl);
268 return CONTAINING_RECORD(iface, IDirectDrawPaletteImpl, IDirectDrawPalette_iface);
271 HRESULT ddraw_palette_init(IDirectDrawPaletteImpl *palette,
272 IDirectDrawImpl *ddraw, DWORD flags, PALETTEENTRY *entries)
274 HRESULT hr;
276 palette->IDirectDrawPalette_iface.lpVtbl = &ddraw_palette_vtbl;
277 palette->ref = 1;
279 hr = wined3d_palette_create(ddraw->wined3d_device, flags,
280 entries, palette, &palette->wineD3DPalette);
281 if (FAILED(hr))
283 WARN("Failed to create wined3d palette, hr %#x.\n", hr);
284 return hr;
287 palette->ifaceToRelease = (IUnknown *)&ddraw->IDirectDraw7_iface;
288 IUnknown_AddRef(palette->ifaceToRelease);
290 return DD_OK;