msvcrt: List the this pointer as an additional argument for thiscall functions.
[wine] / dlls / ddraw / palette.c
1 /*              DirectDraw - IDirectPalette base interface
2  *
3  * Copyright 2006 Stefan Dösinger
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include "config.h"
21 #include "wine/port.h"
22
23 #include "ddraw_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
26
27 /*****************************************************************************
28  * IDirectDrawPalette::QueryInterface
29  *
30  * A usual QueryInterface implementation. Can only Query IUnknown and
31  * IDirectDrawPalette
32  *
33  * Params:
34  *  refiid: The interface id queried for
35  *  obj: Address to return the interface pointer at
36  *
37  * Returns:
38  *  S_OK on success
39  *  E_NOINTERFACE if the requested interface wasn't found
40  *****************************************************************************/
41 static HRESULT WINAPI
42 IDirectDrawPaletteImpl_QueryInterface(IDirectDrawPalette *iface,
43                                       REFIID refiid,
44                                       void **obj)
45 {
46     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(refiid), obj);
47
48     if (IsEqualGUID(refiid, &IID_IUnknown)
49         || IsEqualGUID(refiid, &IID_IDirectDrawPalette))
50     {
51         *obj = iface;
52         IDirectDrawPalette_AddRef(iface);
53         return S_OK;
54     }
55     else
56     {
57         *obj = NULL;
58         return E_NOINTERFACE;
59     }
60 }
61
62 /*****************************************************************************
63  * IDirectDrawPaletteImpl::AddRef
64  *
65  * Increases the refcount.
66  *
67  * Returns:
68  *  The new refcount
69  *
70  *****************************************************************************/
71 static ULONG WINAPI
72 IDirectDrawPaletteImpl_AddRef(IDirectDrawPalette *iface)
73 {
74     IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
75     ULONG ref = InterlockedIncrement(&This->ref);
76
77     TRACE("%p increasing refcount to %u.\n", This, ref);
78
79     return ref;
80 }
81
82 /*****************************************************************************
83  * IDirectDrawPaletteImpl::Release
84  *
85  * Reduces the refcount. If the refcount falls to 0, the object is destroyed
86  *
87  * Returns:
88  *  The new refcount
89  *
90  *****************************************************************************/
91 static ULONG WINAPI
92 IDirectDrawPaletteImpl_Release(IDirectDrawPalette *iface)
93 {
94     IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
95     ULONG ref = InterlockedDecrement(&This->ref);
96
97     TRACE("%p decreasing refcount to %u.\n", This, ref);
98
99     if (ref == 0)
100     {
101         EnterCriticalSection(&ddraw_cs);
102         IWineD3DPalette_Release(This->wineD3DPalette);
103         if(This->ifaceToRelease)
104         {
105             IUnknown_Release(This->ifaceToRelease);
106         }
107         LeaveCriticalSection(&ddraw_cs);
108         HeapFree(GetProcessHeap(), 0, This);
109     }
110
111     return ref;
112 }
113
114 /*****************************************************************************
115  * IDirectDrawPalette::Initialize
116  *
117  * Initializes the palette. As we start initialized, return
118  * DDERR_ALREADYINITIALIZED
119  *
120  * Params:
121  *  DD: DirectDraw interface this palette is assigned to
122  *  Flags: Some flags, as usual
123  *  ColorTable: The startup color table
124  *
125  * Returns:
126  *  DDERR_ALREADYINITIALIZED
127  *
128  *****************************************************************************/
129 static HRESULT WINAPI
130 IDirectDrawPaletteImpl_Initialize(IDirectDrawPalette *iface,
131                                   IDirectDraw *DD,
132                                   DWORD Flags,
133                                   PALETTEENTRY *ColorTable)
134 {
135     TRACE("iface %p, ddraw %p, flags %#x, entries %p.\n",
136             iface, DD, Flags, ColorTable);
137
138     return DDERR_ALREADYINITIALIZED;
139 }
140
141 /*****************************************************************************
142  * IDirectDrawPalette::GetCaps
143  *
144  * Returns the palette description
145  *
146  * Params:
147  *  Caps: Address to store the caps at
148  *
149  * Returns:
150  *  D3D_OK on success
151  *  DDERR_INVALIDPARAMS if Caps is NULL
152  *  For more details, see IWineD3DPalette::GetCaps
153  *
154  *****************************************************************************/
155 static HRESULT WINAPI
156 IDirectDrawPaletteImpl_GetCaps(IDirectDrawPalette *iface,
157                                DWORD *Caps)
158 {
159     IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
160     HRESULT hr;
161
162     TRACE("iface %p, caps %p.\n", iface, Caps);
163
164     EnterCriticalSection(&ddraw_cs);
165     hr = IWineD3DPalette_GetCaps(This->wineD3DPalette, Caps);
166     LeaveCriticalSection(&ddraw_cs);
167     return hr;
168 }
169
170 /*****************************************************************************
171  * IDirectDrawPalette::SetEntries
172  *
173  * Sets the palette entries from a PALETTEENTRY structure. WineD3D takes
174  * care for updating the surface.
175  *
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
181  *
182  * Returns:
183  *  D3D_OK on success
184  *  DDERR_INVALIDPARAMS if PalEnt is NULL
185  *  For details, see IWineD3DDevice::SetEntries
186  *
187  *****************************************************************************/
188 static HRESULT WINAPI
189 IDirectDrawPaletteImpl_SetEntries(IDirectDrawPalette *iface,
190                                   DWORD Flags,
191                                   DWORD Start,
192                                   DWORD Count,
193                                   PALETTEENTRY *PalEnt)
194 {
195     IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
196     HRESULT hr;
197
198     TRACE("iface %p, flags %#x, start %u, count %u, entries %p.\n",
199             iface, Flags, Start, Count, PalEnt);
200
201     if(!PalEnt)
202         return DDERR_INVALIDPARAMS;
203
204     EnterCriticalSection(&ddraw_cs);
205     hr = IWineD3DPalette_SetEntries(This->wineD3DPalette, Flags, Start, Count, PalEnt);
206     LeaveCriticalSection(&ddraw_cs);
207     return hr;
208 }
209
210 /*****************************************************************************
211  * IDirectDrawPalette::GetEntries
212  *
213  * Returns the entries stored in this interface.
214  *
215  * Params:
216  *  Flags: Flags :)
217  *  Start: First entry to return
218  *  Count: The number of entries to return
219  *  PalEnt: PALETTEENTRY structure to write the entries to
220  *
221  * Returns:
222  *  D3D_OK on success
223  *  DDERR_INVALIDPARAMS if PalEnt is NULL
224  *  For details, see IWineD3DDevice::SetEntries
225  *
226  *****************************************************************************/
227 static HRESULT WINAPI
228 IDirectDrawPaletteImpl_GetEntries(IDirectDrawPalette *iface,
229                                   DWORD Flags,
230                                   DWORD Start,
231                                   DWORD Count,
232                                   PALETTEENTRY *PalEnt)
233 {
234     IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
235     HRESULT hr;
236
237     TRACE("iface %p, flags %#x, start %u, count %u, entries %p.\n",
238             iface, Flags, Start, Count, PalEnt);
239
240     if(!PalEnt)
241         return DDERR_INVALIDPARAMS;
242
243     EnterCriticalSection(&ddraw_cs);
244     hr = IWineD3DPalette_GetEntries(This->wineD3DPalette, Flags, Start, Count, PalEnt);
245     LeaveCriticalSection(&ddraw_cs);
246     return hr;
247 }
248
249 static const struct IDirectDrawPaletteVtbl ddraw_palette_vtbl =
250 {
251     /*** IUnknown ***/
252     IDirectDrawPaletteImpl_QueryInterface,
253     IDirectDrawPaletteImpl_AddRef,
254     IDirectDrawPaletteImpl_Release,
255     /*** IDirectDrawPalette ***/
256     IDirectDrawPaletteImpl_GetCaps,
257     IDirectDrawPaletteImpl_GetEntries,
258     IDirectDrawPaletteImpl_Initialize,
259     IDirectDrawPaletteImpl_SetEntries
260 };
261
262 HRESULT ddraw_palette_init(IDirectDrawPaletteImpl *palette,
263         IDirectDrawImpl *ddraw, DWORD flags, PALETTEENTRY *entries)
264 {
265     HRESULT hr;
266
267     palette->lpVtbl = &ddraw_palette_vtbl;
268     palette->ref = 1;
269
270     hr = IWineD3DDevice_CreatePalette(ddraw->wineD3DDevice, flags,
271             entries, &palette->wineD3DPalette, (IUnknown *)palette);
272     if (FAILED(hr))
273     {
274         WARN("Failed to create wined3d palette, hr %#x.\n", hr);
275         return hr;
276     }
277
278     palette->ifaceToRelease = (IUnknown *)ddraw;
279     IUnknown_AddRef(palette->ifaceToRelease);
280
281     return DD_OK;
282 }