opengl: Add support for rendering on bitmaps.
[wine/wine64.git] / dlls / ddraw / palette_main.c
blobf78b5e237d306198cc33173958fa21fa117bf70d
1 /* DirectDraw - IDirectPalette base interface
3 * Copyright 1997-2000 Marcus Meissner
4 * Copyright 2000-2001 TransGaming Technologies Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "winerror.h"
23 #include "wine/debug.h"
25 #include <assert.h>
26 #include <string.h>
28 #include "ddraw_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
32 #define SIZE_BITS (DDPCAPS_1BIT | DDPCAPS_2BIT | DDPCAPS_4BIT | DDPCAPS_8BIT)
34 /* For unsigned x. 0 is not a power of 2. */
35 #define IS_POW_2(x) (((x) & ((x) - 1)) == 0)
37 static const IDirectDrawPaletteVtbl DDRAW_Main_Palette_VTable;
39 /******************************************************************************
40 * IDirectDrawPalette
42 HRESULT Main_DirectDrawPalette_Construct(IDirectDrawPaletteImpl* This,
43 IDirectDrawImpl* pDD, DWORD dwFlags)
45 if (!IS_POW_2(dwFlags & SIZE_BITS)) return DDERR_INVALIDPARAMS;
47 if (dwFlags & DDPCAPS_8BITENTRIES)
48 WARN("creating palette with 8 bit entries\n");
50 This->palNumEntries = Main_DirectDrawPalette_Size(dwFlags);
51 This->ref = 1;
53 This->local.lpGbl = &This->global;
54 This->local.lpDD_lcl = &pDD->local;
55 This->global.lpDD_lcl = &pDD->local;
56 This->global.dwProcessId = GetCurrentProcessId();
57 This->global.dwFlags = dwFlags;
59 This->final_release = Main_DirectDrawPalette_final_release;
60 ICOM_INIT_INTERFACE(This, IDirectDrawPalette, DDRAW_Main_Palette_VTable);
62 /* we could defer hpal creation until we need it,
63 * but does anyone have a case where it would be useful? */
64 This->hpal = CreatePalette((const LOGPALETTE*)&(This->palVersion));
66 Main_DirectDraw_AddPalette(pDD, This);
68 return DD_OK;
71 HRESULT
72 Main_DirectDrawPalette_Create(IDirectDrawImpl* pDD, DWORD dwFlags,
73 LPDIRECTDRAWPALETTE* ppPalette,
74 LPUNKNOWN pUnkOuter)
76 IDirectDrawPaletteImpl* This;
77 HRESULT hr;
79 if (pUnkOuter != NULL)
80 return CLASS_E_NOAGGREGATION; /* unchecked */
82 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This));
83 if (This == NULL) return E_OUTOFMEMORY;
85 hr = Main_DirectDrawPalette_Construct(This, pDD, dwFlags);
86 if (FAILED(hr))
87 HeapFree(GetProcessHeap(), 0, This);
88 else
89 *ppPalette = ICOM_INTERFACE(This, IDirectDrawPalette);
91 return hr;
94 DWORD Main_DirectDrawPalette_Size(DWORD dwFlags)
96 switch (dwFlags & SIZE_BITS)
98 case DDPCAPS_1BIT: return 2;
99 case DDPCAPS_2BIT: return 4;
100 case DDPCAPS_4BIT: return 16;
101 case DDPCAPS_8BIT: return 256;
102 default: assert(0); return 256;
106 HRESULT WINAPI
107 Main_DirectDrawPalette_GetEntries(LPDIRECTDRAWPALETTE iface, DWORD dwFlags,
108 DWORD dwStart, DWORD dwCount,
109 LPPALETTEENTRY palent)
111 IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
113 TRACE("(%p)->GetEntries(%08lx,%ld,%ld,%p)\n",This,dwFlags,dwStart,dwCount,
114 palent);
116 if (dwFlags != 0) return DDERR_INVALIDPARAMS; /* unchecked */
117 if (dwStart + dwCount > Main_DirectDrawPalette_Size(This->global.dwFlags))
118 return DDERR_INVALIDPARAMS;
120 if (This->global.dwFlags & DDPCAPS_8BITENTRIES)
122 unsigned int i;
123 LPBYTE entry = (LPBYTE)palent;
125 for (i=dwStart; i < dwCount+dwStart; i++)
126 *entry++ = This->palents[i].peRed;
128 else
129 memcpy(palent, This->palents+dwStart, dwCount * sizeof(PALETTEENTRY));
131 return DD_OK;
134 HRESULT WINAPI
135 Main_DirectDrawPalette_SetEntries(LPDIRECTDRAWPALETTE iface, DWORD dwFlags,
136 DWORD dwStart, DWORD dwCount,
137 LPPALETTEENTRY palent)
139 IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
141 TRACE("(%p)->SetEntries(%08lx,%ld,%ld,%p)\n",This,dwFlags,dwStart,dwCount,
142 palent);
144 if (This->global.dwFlags & DDPCAPS_8BITENTRIES)
146 unsigned int i;
147 const BYTE* entry = (const BYTE*)palent;
149 for (i=dwStart; i < dwCount+dwStart; i++)
150 This->palents[i].peRed = *entry++;
152 else {
153 memcpy(This->palents+dwStart, palent, dwCount * sizeof(PALETTEENTRY));
155 if (This->hpal)
156 SetPaletteEntries(This->hpal, dwStart, dwCount, This->palents+dwStart);
158 if (This->global.dwFlags & DDPCAPS_PRIMARYSURFACE) {
159 /* update physical palette */
160 LPDIRECTDRAWSURFACE7 psurf = NULL;
161 IDirectDraw7_GetGDISurface(ICOM_INTERFACE(This->ddraw_owner,IDirectDraw7), &psurf);
162 if (psurf) {
163 IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl,
164 IDirectDrawSurface7, psurf);
165 surf->update_palette(surf, This, dwStart, dwCount, palent);
166 IDirectDrawSurface7_Release(psurf);
168 else ERR("can't find GDI surface!!\n");
172 #if 0
173 /* Now, if we are in 'depth conversion mode', update the screen palette */
174 /* FIXME: we need to update the image or we won't get palette fading. */
175 if (This->ddraw->d->palette_convert != NULL)
176 This->ddraw->d->palette_convert(palent,This->screen_palents,start,count);
177 #endif
179 return DD_OK;
182 void Main_DirectDrawPalette_final_release(IDirectDrawPaletteImpl* This)
184 Main_DirectDraw_RemovePalette(This->ddraw_owner, This);
186 if (This->hpal) DeleteObject(This->hpal);
189 static void Main_DirectDrawPalette_Destroy(IDirectDrawPaletteImpl* This)
191 This->final_release(This);
193 if (This->private != This+1)
194 HeapFree(GetProcessHeap(), 0, This->private);
196 HeapFree(GetProcessHeap(),0,This);
199 void Main_DirectDrawPalette_ForceDestroy(IDirectDrawPaletteImpl* This)
201 WARN("deleting palette %p with refcnt %lu\n", This, This->ref);
202 Main_DirectDrawPalette_Destroy(This);
205 ULONG WINAPI
206 Main_DirectDrawPalette_Release(LPDIRECTDRAWPALETTE iface)
208 IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
209 ULONG ref = InterlockedDecrement(&This->ref);
211 TRACE("(%p)->() decrementing from %lu.\n", This, ref + 1);
213 if (!ref)
215 Main_DirectDrawPalette_Destroy(This);
216 return 0;
219 return ref;
222 ULONG WINAPI Main_DirectDrawPalette_AddRef(LPDIRECTDRAWPALETTE iface) {
223 IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
224 ULONG ref = InterlockedIncrement(&This->ref);
226 TRACE("(%p)->() incrementing from %lu.\n", This, ref - 1);
228 return ref;
231 HRESULT WINAPI
232 Main_DirectDrawPalette_Initialize(LPDIRECTDRAWPALETTE iface,
233 LPDIRECTDRAW ddraw, DWORD dwFlags,
234 LPPALETTEENTRY palent)
236 IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
237 TRACE("(%p)->(%p,%ld,%p)\n", This, ddraw, dwFlags, palent);
238 return DDERR_ALREADYINITIALIZED;
241 HRESULT WINAPI
242 Main_DirectDrawPalette_GetCaps(LPDIRECTDRAWPALETTE iface, LPDWORD lpdwCaps)
244 IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
245 TRACE("(%p)->(%p)\n",This,lpdwCaps);
247 *lpdwCaps = This->global.dwFlags;
249 return DD_OK;
252 HRESULT WINAPI
253 Main_DirectDrawPalette_QueryInterface(LPDIRECTDRAWPALETTE iface,
254 REFIID refiid, LPVOID *obj)
256 IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
257 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
259 if (IsEqualGUID(refiid, &IID_IUnknown)
260 || IsEqualGUID(refiid, &IID_IDirectDrawPalette))
262 *obj = iface;
263 IDirectDrawPalette_AddRef(iface);
264 return S_OK;
266 else
268 return E_NOINTERFACE;
272 static const IDirectDrawPaletteVtbl DDRAW_Main_Palette_VTable =
274 Main_DirectDrawPalette_QueryInterface,
275 Main_DirectDrawPalette_AddRef,
276 Main_DirectDrawPalette_Release,
277 Main_DirectDrawPalette_GetCaps,
278 Main_DirectDrawPalette_GetEntries,
279 Main_DirectDrawPalette_Initialize,
280 Main_DirectDrawPalette_SetEntries