Thanks to Lucho, text support in samples now starts to
[wine/dcerpc.git] / dlls / ddraw / dpalette / main.c
blobc83b90a4abf886b2d20a049f9c816f292ffbd9a4
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"
29 #include "dpalette/main.h"
30 #include "ddraw/main.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
34 #define SIZE_BITS (DDPCAPS_1BIT | DDPCAPS_2BIT | DDPCAPS_4BIT | DDPCAPS_8BIT)
36 /* For unsigned x. 0 is not a power of 2. */
37 #define IS_POW_2(x) (((x) & ((x) - 1)) == 0)
39 static ICOM_VTABLE(IDirectDrawPalette) DDRAW_Main_Palette_VTable;
41 /******************************************************************************
42 * IDirectDrawPalette
44 HRESULT Main_DirectDrawPalette_Construct(IDirectDrawPaletteImpl* This,
45 IDirectDrawImpl* pDD, DWORD dwFlags)
47 if (!IS_POW_2(dwFlags & SIZE_BITS)) return DDERR_INVALIDPARAMS;
49 if (dwFlags & DDPCAPS_8BITENTRIES)
50 WARN("creating palette with 8 bit entries\n");
52 This->palNumEntries = Main_DirectDrawPalette_Size(dwFlags);
53 This->ref = 1;
55 This->local.lpGbl = &This->global;
56 This->local.lpDD_lcl = &pDD->local;
57 This->global.lpDD_lcl = &pDD->local;
58 This->global.dwProcessId = GetCurrentProcessId();
59 This->global.dwFlags = dwFlags;
61 This->final_release = Main_DirectDrawPalette_final_release;
62 ICOM_INIT_INTERFACE(This, IDirectDrawPalette, DDRAW_Main_Palette_VTable);
64 /* we could defer hpal creation until we need it,
65 * but does anyone have a case where it would be useful? */
66 This->hpal = CreatePalette((const LOGPALETTE*)&(This->palVersion));
68 Main_DirectDraw_AddPalette(pDD, This);
70 return DD_OK;
73 HRESULT
74 Main_DirectDrawPalette_Create(IDirectDrawImpl* pDD, DWORD dwFlags,
75 LPDIRECTDRAWPALETTE* ppPalette,
76 LPUNKNOWN pUnkOuter)
78 IDirectDrawPaletteImpl* This;
79 HRESULT hr;
81 if (pUnkOuter != NULL)
82 return CLASS_E_NOAGGREGATION; /* unchecked */
84 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This));
85 if (This == NULL) return E_OUTOFMEMORY;
87 hr = Main_DirectDrawPalette_Construct(This, pDD, dwFlags);
88 if (FAILED(hr))
89 HeapFree(GetProcessHeap(), 0, This);
90 else
91 *ppPalette = ICOM_INTERFACE(This, IDirectDrawPalette);
93 return hr;
96 DWORD Main_DirectDrawPalette_Size(DWORD dwFlags)
98 switch (dwFlags & SIZE_BITS)
100 case DDPCAPS_1BIT: return 2;
101 case DDPCAPS_2BIT: return 4;
102 case DDPCAPS_4BIT: return 16;
103 case DDPCAPS_8BIT: return 256;
104 default: assert(0); return 256;
108 HRESULT WINAPI
109 Main_DirectDrawPalette_GetEntries(LPDIRECTDRAWPALETTE iface, DWORD dwFlags,
110 DWORD dwStart, DWORD dwCount,
111 LPPALETTEENTRY palent)
113 ICOM_THIS(IDirectDrawPaletteImpl,iface);
115 TRACE("(%p)->GetEntries(%08lx,%ld,%ld,%p)\n",This,dwFlags,dwStart,dwCount,
116 palent);
118 if (dwFlags != 0) return DDERR_INVALIDPARAMS; /* unchecked */
119 if (dwStart + dwCount > Main_DirectDrawPalette_Size(This->global.dwFlags))
120 return DDERR_INVALIDPARAMS;
122 if (This->global.dwFlags & DDPCAPS_8BITENTRIES)
124 int i;
125 LPBYTE entry = (LPBYTE)palent;
127 for (i=dwStart; i < dwCount+dwStart; i++)
128 *entry++ = This->palents[i].peRed;
130 else
131 memcpy(palent, This->palents+dwStart, dwCount * sizeof(PALETTEENTRY));
133 return DD_OK;
136 HRESULT WINAPI
137 Main_DirectDrawPalette_SetEntries(LPDIRECTDRAWPALETTE iface, DWORD dwFlags,
138 DWORD dwStart, DWORD dwCount,
139 LPPALETTEENTRY palent)
141 ICOM_THIS(IDirectDrawPaletteImpl,iface);
143 TRACE("(%p)->SetEntries(%08lx,%ld,%ld,%p)\n",This,dwFlags,dwStart,dwCount,
144 palent);
146 if (This->global.dwFlags & DDPCAPS_8BITENTRIES)
148 int i;
149 const BYTE* entry = (const BYTE*)palent;
151 for (i=dwStart; i < dwCount+dwStart; i++)
152 This->palents[i].peRed = *entry++;
154 else {
155 memcpy(This->palents+dwStart, palent, dwCount * sizeof(PALETTEENTRY));
157 if (This->hpal)
158 SetPaletteEntries(This->hpal, dwStart, dwCount, This->palents+dwStart);
160 if (This->global.dwFlags & DDPCAPS_PRIMARYSURFACE) {
161 /* update physical palette */
162 LPDIRECTDRAWSURFACE7 psurf = NULL;
163 IDirectDraw7_GetGDISurface(ICOM_INTERFACE(This->ddraw_owner,IDirectDraw7), &psurf);
164 if (psurf) {
165 IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl,
166 IDirectDrawSurface7, psurf);
167 surf->update_palette(surf, This, dwStart, dwCount, palent);
168 IDirectDrawSurface7_Release(psurf);
170 else ERR("can't find GDI surface!!\n");
174 #if 0
175 /* Now, if we are in 'depth conversion mode', update the screen palette */
176 /* FIXME: we need to update the image or we won't get palette fading. */
177 if (This->ddraw->d->palette_convert != NULL)
178 This->ddraw->d->palette_convert(palent,This->screen_palents,start,count);
179 #endif
181 return DD_OK;
184 void Main_DirectDrawPalette_final_release(IDirectDrawPaletteImpl* This)
186 Main_DirectDraw_RemovePalette(This->ddraw_owner, This);
188 if (This->hpal) DeleteObject(This->hpal);
191 static void Main_DirectDrawPalette_Destroy(IDirectDrawPaletteImpl* This)
193 This->final_release(This);
195 if (This->private != This+1)
196 HeapFree(GetProcessHeap(), 0, This->private);
198 HeapFree(GetProcessHeap(),0,This);
201 void Main_DirectDrawPalette_ForceDestroy(IDirectDrawPaletteImpl* This)
203 WARN("deleting palette %p with refcnt %lu\n", This, This->ref);
204 Main_DirectDrawPalette_Destroy(This);
207 ULONG WINAPI
208 Main_DirectDrawPalette_Release(LPDIRECTDRAWPALETTE iface)
210 ICOM_THIS(IDirectDrawPaletteImpl,iface);
211 TRACE("(%p)->() decrementing from %lu.\n", This, This->ref );
213 if (!--This->ref)
215 Main_DirectDrawPalette_Destroy(This);
216 return 0;
219 return This->ref;
222 ULONG WINAPI Main_DirectDrawPalette_AddRef(LPDIRECTDRAWPALETTE iface) {
223 ICOM_THIS(IDirectDrawPaletteImpl,iface);
224 TRACE("(%p)->() incrementing from %lu.\n", This, This->ref );
225 return ++This->ref;
228 HRESULT WINAPI
229 Main_DirectDrawPalette_Initialize(LPDIRECTDRAWPALETTE iface,
230 LPDIRECTDRAW ddraw, DWORD dwFlags,
231 LPPALETTEENTRY palent)
233 ICOM_THIS(IDirectDrawPaletteImpl,iface);
234 TRACE("(%p)->(%p,%ld,%p)\n", This, ddraw, dwFlags, palent);
235 return DDERR_ALREADYINITIALIZED;
238 HRESULT WINAPI
239 Main_DirectDrawPalette_GetCaps(LPDIRECTDRAWPALETTE iface, LPDWORD lpdwCaps)
241 ICOM_THIS(IDirectDrawPaletteImpl,iface);
242 TRACE("(%p)->(%p)\n",This,lpdwCaps);
244 *lpdwCaps = This->global.dwFlags;
246 return DD_OK;
249 HRESULT WINAPI
250 Main_DirectDrawPalette_QueryInterface(LPDIRECTDRAWPALETTE iface,
251 REFIID refiid, LPVOID *obj)
253 ICOM_THIS(IDirectDrawPaletteImpl,iface);
254 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
256 if (IsEqualGUID(refiid, &IID_IUnknown)
257 || IsEqualGUID(refiid, &IID_IDirectDrawPalette))
259 *obj = iface;
260 IDirectDrawPalette_AddRef(iface);
261 return S_OK;
263 else
265 return E_NOINTERFACE;
269 static ICOM_VTABLE(IDirectDrawPalette) DDRAW_Main_Palette_VTable =
271 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
272 Main_DirectDrawPalette_QueryInterface,
273 Main_DirectDrawPalette_AddRef,
274 Main_DirectDrawPalette_Release,
275 Main_DirectDrawPalette_GetCaps,
276 Main_DirectDrawPalette_GetEntries,
277 Main_DirectDrawPalette_Initialize,
278 Main_DirectDrawPalette_SetEntries