ddraw: Use unsafe_impl_from_IDirectDrawPalette for application provided interfaces.
[wine.git] / dlls / ddraw / palette.c
blobff784119d9e6000a48bd3938a4c834e29ab6371c
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 = (IDirectDrawPaletteImpl *)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 = (IDirectDrawPaletteImpl *)iface;
94 ULONG ref = InterlockedDecrement(&This->ref);
96 TRACE("%p decreasing refcount to %u.\n", This, ref);
98 if (ref == 0)
100 EnterCriticalSection(&ddraw_cs);
101 wined3d_palette_decref(This->wineD3DPalette);
102 if(This->ifaceToRelease)
104 IUnknown_Release(This->ifaceToRelease);
106 LeaveCriticalSection(&ddraw_cs);
107 HeapFree(GetProcessHeap(), 0, This);
110 return ref;
113 /*****************************************************************************
114 * IDirectDrawPalette::Initialize
116 * Initializes the palette. As we start initialized, return
117 * DDERR_ALREADYINITIALIZED
119 * Params:
120 * DD: DirectDraw interface this palette is assigned to
121 * Flags: Some flags, as usual
122 * ColorTable: The startup color table
124 * Returns:
125 * DDERR_ALREADYINITIALIZED
127 *****************************************************************************/
128 static HRESULT WINAPI
129 IDirectDrawPaletteImpl_Initialize(IDirectDrawPalette *iface,
130 IDirectDraw *DD,
131 DWORD Flags,
132 PALETTEENTRY *ColorTable)
134 TRACE("iface %p, ddraw %p, flags %#x, entries %p.\n",
135 iface, DD, Flags, ColorTable);
137 return DDERR_ALREADYINITIALIZED;
140 /*****************************************************************************
141 * IDirectDrawPalette::GetCaps
143 * Returns the palette description
145 * Params:
146 * Caps: Address to store the caps at
148 * Returns:
149 * D3D_OK on success
150 * DDERR_INVALIDPARAMS if Caps is NULL
151 * For more details, see IWineD3DPalette::GetCaps
153 *****************************************************************************/
154 static HRESULT WINAPI
155 IDirectDrawPaletteImpl_GetCaps(IDirectDrawPalette *iface,
156 DWORD *Caps)
158 IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
160 TRACE("iface %p, caps %p.\n", iface, Caps);
162 EnterCriticalSection(&ddraw_cs);
163 *Caps = wined3d_palette_get_flags(This->wineD3DPalette);
164 LeaveCriticalSection(&ddraw_cs);
166 return D3D_OK;
169 /*****************************************************************************
170 * IDirectDrawPalette::SetEntries
172 * Sets the palette entries from a PALETTEENTRY structure. WineD3D takes
173 * care for updating the surface.
175 * Params:
176 * Flags: Flags, as usual
177 * Start: First palette entry to set
178 * Count: Number of entries to set
179 * PalEnt: Source entries
181 * Returns:
182 * D3D_OK on success
183 * DDERR_INVALIDPARAMS if PalEnt is NULL
184 * For details, see IWineD3DDevice::SetEntries
186 *****************************************************************************/
187 static HRESULT WINAPI
188 IDirectDrawPaletteImpl_SetEntries(IDirectDrawPalette *iface,
189 DWORD Flags,
190 DWORD Start,
191 DWORD Count,
192 PALETTEENTRY *PalEnt)
194 IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
195 HRESULT hr;
197 TRACE("iface %p, flags %#x, start %u, count %u, entries %p.\n",
198 iface, Flags, Start, Count, PalEnt);
200 if(!PalEnt)
201 return DDERR_INVALIDPARAMS;
203 EnterCriticalSection(&ddraw_cs);
204 hr = wined3d_palette_set_entries(This->wineD3DPalette, Flags, Start, Count, PalEnt);
205 LeaveCriticalSection(&ddraw_cs);
206 return hr;
209 /*****************************************************************************
210 * IDirectDrawPalette::GetEntries
212 * Returns the entries stored in this interface.
214 * Params:
215 * Flags: Flags :)
216 * Start: First entry to return
217 * Count: The number of entries to return
218 * PalEnt: PALETTEENTRY structure to write the entries to
220 * Returns:
221 * D3D_OK on success
222 * DDERR_INVALIDPARAMS if PalEnt is NULL
223 * For details, see IWineD3DDevice::SetEntries
225 *****************************************************************************/
226 static HRESULT WINAPI
227 IDirectDrawPaletteImpl_GetEntries(IDirectDrawPalette *iface,
228 DWORD Flags,
229 DWORD Start,
230 DWORD Count,
231 PALETTEENTRY *PalEnt)
233 IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
234 HRESULT hr;
236 TRACE("iface %p, flags %#x, start %u, count %u, entries %p.\n",
237 iface, Flags, Start, Count, PalEnt);
239 if(!PalEnt)
240 return DDERR_INVALIDPARAMS;
242 EnterCriticalSection(&ddraw_cs);
243 hr = wined3d_palette_get_entries(This->wineD3DPalette, Flags, Start, Count, PalEnt);
244 LeaveCriticalSection(&ddraw_cs);
245 return hr;
248 static const struct IDirectDrawPaletteVtbl ddraw_palette_vtbl =
250 /*** IUnknown ***/
251 IDirectDrawPaletteImpl_QueryInterface,
252 IDirectDrawPaletteImpl_AddRef,
253 IDirectDrawPaletteImpl_Release,
254 /*** IDirectDrawPalette ***/
255 IDirectDrawPaletteImpl_GetCaps,
256 IDirectDrawPaletteImpl_GetEntries,
257 IDirectDrawPaletteImpl_Initialize,
258 IDirectDrawPaletteImpl_SetEntries
261 IDirectDrawPaletteImpl *unsafe_impl_from_IDirectDrawPalette(IDirectDrawPalette *iface)
263 if (!iface) return NULL;
264 assert(iface->lpVtbl == &ddraw_palette_vtbl);
265 return CONTAINING_RECORD(iface, IDirectDrawPaletteImpl, lpVtbl);
268 HRESULT ddraw_palette_init(IDirectDrawPaletteImpl *palette,
269 IDirectDrawImpl *ddraw, DWORD flags, PALETTEENTRY *entries)
271 HRESULT hr;
273 palette->lpVtbl = &ddraw_palette_vtbl;
274 palette->ref = 1;
276 hr = wined3d_palette_create(ddraw->wined3d_device, flags,
277 entries, palette, &palette->wineD3DPalette);
278 if (FAILED(hr))
280 WARN("Failed to create wined3d palette, hr %#x.\n", hr);
281 return hr;
284 palette->ifaceToRelease = (IUnknown *)&ddraw->IDirectDraw7_iface;
285 IUnknown_AddRef(palette->ifaceToRelease);
287 return DD_OK;