Don't use old_spacing-1 if old_spacing already has the minimum value.
[wine] / dlls / ddraw / dpalette / main.c
1 /*              DirectDraw - IDirectPalette base interface
2  *
3  * Copyright 1997-2000 Marcus Meissner
4  * Copyright 2000-2001 TransGaming Technologies Inc.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "winerror.h"
23 #include "wine/debug.h"
24
25 #include <assert.h>
26 #include <string.h>
27
28 #include "ddraw_private.h"
29 #include "dpalette/main.h"
30 #include "ddraw/main.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
33
34 #define SIZE_BITS (DDPCAPS_1BIT | DDPCAPS_2BIT | DDPCAPS_4BIT | DDPCAPS_8BIT)
35
36 /* For unsigned x. 0 is not a power of 2. */
37 #define IS_POW_2(x) (((x) & ((x) - 1)) == 0)
38
39 static ICOM_VTABLE(IDirectDrawPalette) DDRAW_Main_Palette_VTable;
40
41 /******************************************************************************
42  *                      IDirectDrawPalette
43  */
44 HRESULT Main_DirectDrawPalette_Construct(IDirectDrawPaletteImpl* This,
45                                          IDirectDrawImpl* pDD, DWORD dwFlags)
46 {
47     if (!IS_POW_2(dwFlags & SIZE_BITS)) return DDERR_INVALIDPARAMS;
48
49     if (dwFlags & DDPCAPS_8BITENTRIES)
50         WARN("creating palette with 8 bit entries\n");
51
52     This->palNumEntries = Main_DirectDrawPalette_Size(dwFlags);
53     This->ref = 1;
54
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;
60
61     This->final_release = Main_DirectDrawPalette_final_release;
62     ICOM_INIT_INTERFACE(This, IDirectDrawPalette, DDRAW_Main_Palette_VTable);
63
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));
67
68     Main_DirectDraw_AddPalette(pDD, This);
69
70     return DD_OK;
71 }
72
73 HRESULT
74 Main_DirectDrawPalette_Create(IDirectDrawImpl* pDD, DWORD dwFlags,
75                               LPDIRECTDRAWPALETTE* ppPalette,
76                               LPUNKNOWN pUnkOuter)
77 {
78     IDirectDrawPaletteImpl* This;
79     HRESULT hr;
80
81     if (pUnkOuter != NULL)
82         return CLASS_E_NOAGGREGATION; /* unchecked */
83
84     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This));
85     if (This == NULL) return E_OUTOFMEMORY;
86
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);
92
93     return hr;
94 }
95
96 DWORD Main_DirectDrawPalette_Size(DWORD dwFlags)
97 {
98     switch (dwFlags & SIZE_BITS)
99     {
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;
105     }
106 }
107
108 HRESULT WINAPI
109 Main_DirectDrawPalette_GetEntries(LPDIRECTDRAWPALETTE iface, DWORD dwFlags,
110                                   DWORD dwStart, DWORD dwCount,
111                                   LPPALETTEENTRY palent)
112 {
113     ICOM_THIS(IDirectDrawPaletteImpl,iface);
114
115     TRACE("(%p)->GetEntries(%08lx,%ld,%ld,%p)\n",This,dwFlags,dwStart,dwCount,
116           palent);
117
118     if (dwFlags != 0) return DDERR_INVALIDPARAMS; /* unchecked */
119     if (dwStart + dwCount > Main_DirectDrawPalette_Size(This->global.dwFlags))
120         return DDERR_INVALIDPARAMS;
121
122     if (This->global.dwFlags & DDPCAPS_8BITENTRIES)
123     {
124         int i;
125         LPBYTE entry = (LPBYTE)palent;
126
127         for (i=dwStart; i < dwCount+dwStart; i++)
128             *entry++ = This->palents[i].peRed;
129     }
130     else
131         memcpy(palent, This->palents+dwStart, dwCount * sizeof(PALETTEENTRY));
132
133     return DD_OK;
134 }
135
136 HRESULT WINAPI
137 Main_DirectDrawPalette_SetEntries(LPDIRECTDRAWPALETTE iface, DWORD dwFlags,
138                                   DWORD dwStart, DWORD dwCount,
139                                   LPPALETTEENTRY palent)
140 {
141     ICOM_THIS(IDirectDrawPaletteImpl,iface);
142
143     TRACE("(%p)->SetEntries(%08lx,%ld,%ld,%p)\n",This,dwFlags,dwStart,dwCount,
144           palent);
145
146     if (This->global.dwFlags & DDPCAPS_8BITENTRIES)
147     {
148         int i;
149         const BYTE* entry = (const BYTE*)palent;
150
151         for (i=dwStart; i < dwCount+dwStart; i++)
152             This->palents[i].peRed = *entry++;
153     }
154     else {
155         memcpy(This->palents+dwStart, palent, dwCount * sizeof(PALETTEENTRY));
156
157         if (This->hpal)
158             SetPaletteEntries(This->hpal, dwStart, dwCount, This->palents+dwStart);
159
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);
169             }
170             else ERR("can't find GDI surface!!\n");
171         }
172     }
173
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
180
181     return DD_OK;
182 }
183
184 void Main_DirectDrawPalette_final_release(IDirectDrawPaletteImpl* This)
185 {
186     Main_DirectDraw_RemovePalette(This->ddraw_owner, This);
187
188     if (This->hpal) DeleteObject(This->hpal);
189 }
190
191 static void Main_DirectDrawPalette_Destroy(IDirectDrawPaletteImpl* This)
192 {
193     This->final_release(This);
194
195     if (This->private != This+1)
196         HeapFree(GetProcessHeap(), 0, This->private);
197
198     HeapFree(GetProcessHeap(),0,This);
199 }
200
201 void Main_DirectDrawPalette_ForceDestroy(IDirectDrawPaletteImpl* This)
202 {
203     WARN("deleting palette %p with refcnt %lu\n", This, This->ref);
204     Main_DirectDrawPalette_Destroy(This);
205 }
206
207 ULONG WINAPI
208 Main_DirectDrawPalette_Release(LPDIRECTDRAWPALETTE iface)
209 {
210     ICOM_THIS(IDirectDrawPaletteImpl,iface);
211     TRACE("(%p)->() decrementing from %lu.\n", This, This->ref );
212
213     if (!--This->ref)
214     {
215         Main_DirectDrawPalette_Destroy(This);
216         return 0;
217     }
218
219     return This->ref;
220 }
221
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;
226 }
227
228 HRESULT WINAPI
229 Main_DirectDrawPalette_Initialize(LPDIRECTDRAWPALETTE iface,
230                                   LPDIRECTDRAW ddraw, DWORD dwFlags,
231                                   LPPALETTEENTRY palent)
232 {
233     ICOM_THIS(IDirectDrawPaletteImpl,iface);
234     TRACE("(%p)->(%p,%ld,%p)\n", This, ddraw, dwFlags, palent);
235     return DDERR_ALREADYINITIALIZED;
236 }
237
238 HRESULT WINAPI
239 Main_DirectDrawPalette_GetCaps(LPDIRECTDRAWPALETTE iface, LPDWORD lpdwCaps)
240 {
241    ICOM_THIS(IDirectDrawPaletteImpl,iface);
242    TRACE("(%p)->(%p)\n",This,lpdwCaps);
243
244    *lpdwCaps = This->global.dwFlags;
245
246    return DD_OK;
247 }
248
249 HRESULT WINAPI
250 Main_DirectDrawPalette_QueryInterface(LPDIRECTDRAWPALETTE iface,
251                                       REFIID refiid, LPVOID *obj)
252 {
253     ICOM_THIS(IDirectDrawPaletteImpl,iface);
254     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
255
256     if (IsEqualGUID(refiid, &IID_IUnknown)
257         || IsEqualGUID(refiid, &IID_IDirectDrawPalette))
258     {
259         *obj = iface;
260         IDirectDrawPalette_AddRef(iface);
261         return S_OK;
262     }
263     else
264     {
265         return E_NOINTERFACE;
266     }
267 }
268
269 static ICOM_VTABLE(IDirectDrawPalette) DDRAW_Main_Palette_VTable =
270 {
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
279 };