1 /*
2  * COPYRIGHT:            See COPYING in the top level directory
3  * PROJECT:              ReactOS DirectX
4  * FILE:                 dll/directx/ddraw/Palette/createpalette.c
5  * PURPOSE:              IDirectDrawPalette Creation
6  * PROGRAMMER:           J�r�me Gardou
7  *
8  */
9 #include "rosdraw.h"
10 
11 DWORD ConvertPCapsFlags(DWORD dwFlags)
12 {
13     DWORD ret = 0;
14     if(dwFlags & DDPCAPS_4BIT)
15         ret|=DDRAWIPAL_16;
16     if(dwFlags & DDPCAPS_8BIT)
17         ret|=DDRAWIPAL_256;
18     if(dwFlags & DDPCAPS_8BITENTRIES)
19         ret|=DDRAWIPAL_STORED_8INDEX;
20     if(dwFlags & DDPCAPS_ALLOW256)
21         ret|=DDRAWIPAL_ALLOW256;
22     if(dwFlags & DDPCAPS_ALPHA)
23         ret|=DDRAWIPAL_ALPHA;
24     if(dwFlags & DDPCAPS_1BIT)
25         ret|=DDRAWIPAL_2;
26     if(dwFlags & DDPCAPS_2BIT)
27         ret|=DDRAWIPAL_4;
28 
29     return ret;
30 }
31 
32 HRESULT
33 Internal_CreatePalette( LPDDRAWI_DIRECTDRAW_INT pDDraw, DWORD dwFlags,
34                   LPPALETTEENTRY palent, LPDIRECTDRAWPALETTE* ppPalette, LPUNKNOWN pUnkOuter)
35 {
36     DDHAL_CREATEPALETTEDATA mDdCreatePalette = { 0 };
37 
38     LPDDRAWI_DDRAWPALETTE_INT ThisPalInt = NULL;
39     LPDDRAWI_DDRAWPALETTE_LCL ThisPalLcl = NULL;
40     LPDDRAWI_DDRAWPALETTE_GBL ThisPalGbl = NULL;
41 
42     HRESULT ret;
43 
44     if(pUnkOuter)
45     {
46         return CLASS_E_NOAGGREGATION;
47     }
48 
49     if(!(pDDraw->lpLcl->dwLocalFlags & DDRAWILCL_SETCOOPCALLED))
50     {
51         return DDERR_NOCOOPERATIVELEVELSET;
52     }
53 
54 
55     if (pDDraw->lpLcl->dwProcessId != GetCurrentProcessId() )
56     {
57         return DDERR_INVALIDOBJECT;
58     }
59 
60     /* Allocate the palette interface and needed members */
61     DxHeapMemAlloc(ThisPalInt,  sizeof( DDRAWI_DDRAWPALETTE_INT ) );
62     if( ThisPalInt == NULL )
63     {
64         ret = DDERR_OUTOFMEMORY;
65         goto cleanup;
66     }
67 
68     DxHeapMemAlloc(ThisPalLcl,  sizeof( DDRAWI_DDRAWPALETTE_LCL ) );
69     if( ThisPalLcl == NULL )
70     {
71         ret = DDERR_OUTOFMEMORY;
72         goto cleanup;
73     }
74 
75     DxHeapMemAlloc(ThisPalGbl,  sizeof( DDRAWI_DDRAWPALETTE_GBL ) );
76     if( ThisPalGbl == NULL )
77     {
78         ret = DDERR_OUTOFMEMORY;
79         goto cleanup;
80     }
81 
82     /*Some initial setup*/
83 
84     ThisPalInt->lpLcl = ThisPalLcl;
85     ThisPalLcl->lpGbl = ThisPalGbl;
86 
87     ThisPalLcl->lpDD_lcl = ThisPalGbl->lpDD_lcl = pDDraw->lpLcl;
88     ThisPalGbl->dwFlags = ConvertPCapsFlags(dwFlags);
89 
90     ThisPalInt->lpVtbl = (PVOID)&DirectDrawPalette_Vtable;
91     ThisPalGbl->dwProcessId = GetCurrentProcessId();
92 
93     mDdCreatePalette.lpDD = pDDraw->lpLcl->lpGbl;
94     mDdCreatePalette.lpDDPalette = ThisPalGbl;
95     if(pDDraw->lpLcl->lpGbl->lpDDCBtmp->HALDD.dwFlags & DDHAL_CB32_CREATEPALETTE) {
96         mDdCreatePalette.CreatePalette = pDDraw->lpLcl->lpGbl->lpDDCBtmp->HALDD.CreatePalette;
97         DX_STUB_str("Using HAL CreatePalette\n");
98     }
99     else {
100         mDdCreatePalette.CreatePalette = pDDraw->lpLcl->lpGbl->lpDDCBtmp->HELDD.CreatePalette;
101         DX_STUB_str("Using HEL CreatePalette\n");
102     }
103     mDdCreatePalette.ddRVal = DDERR_GENERIC;
104     mDdCreatePalette.lpColorTable = palent;
105 
106     if (mDdCreatePalette.CreatePalette(&mDdCreatePalette) == DDHAL_DRIVER_NOTHANDLED)
107     {
108         DX_STUB_str("mDdCreateSurface failed with DDHAL_DRIVER_NOTHANDLED.");
109         ret = DDERR_NOTINITIALIZED;
110         goto cleanup;
111     }
112 
113     if (mDdCreatePalette.ddRVal != DD_OK)
114     {
115         DX_STUB_str("mDdCreateSurface failed.");
116         ret = mDdCreatePalette.ddRVal;
117         goto cleanup;
118     }
119 
120     *ppPalette = (LPDIRECTDRAWPALETTE)ThisPalInt;
121     ThisPalInt->lpLink = pDDraw->lpLcl->lpGbl->palList;
122     pDDraw->lpLcl->lpGbl->palList = ThisPalInt;
123     ThisPalInt->lpLcl->dwReserved1 = (ULONG_PTR)pDDraw;
124     IDirectDrawPalette_AddRef(*ppPalette);
125 
126     return DD_OK;
127 
128 cleanup:
129     if(ThisPalInt) DxHeapMemFree(ThisPalInt);
130     if(ThisPalLcl) DxHeapMemFree(ThisPalLcl);
131     if(ThisPalGbl) DxHeapMemFree(ThisPalGbl);
132 
133     return ret;
134 }
135