| 1 | /*
|
|---|
| 2 | * DirectDraw Palette class
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 5 | *
|
|---|
| 6 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 7 | *
|
|---|
| 8 | */
|
|---|
| 9 |
|
|---|
| 10 | /*@Const************************************************************************
|
|---|
| 11 | * Defined Constants *
|
|---|
| 12 | *******************************************************************************/
|
|---|
| 13 | #define INCL_GUID
|
|---|
| 14 | #define WIN32SDK_NOPOSTWRAPPER
|
|---|
| 15 |
|
|---|
| 16 | /*@Header***********************************************************************
|
|---|
| 17 | * Header Files *
|
|---|
| 18 | *******************************************************************************/
|
|---|
| 19 | #include <os2win.h>
|
|---|
| 20 | #include <dive.h>
|
|---|
| 21 |
|
|---|
| 22 | #include <stdio.h>
|
|---|
| 23 | #include <stdlib.h>
|
|---|
| 24 | #include <string.h>
|
|---|
| 25 | #include <memory.h>
|
|---|
| 26 |
|
|---|
| 27 | #include "no.h"
|
|---|
| 28 | #include <w_windows.h>
|
|---|
| 29 | #include <ddraw.h>
|
|---|
| 30 | #include <d3d.h>
|
|---|
| 31 | #include <Win32SDKPostWrapper.h>
|
|---|
| 32 |
|
|---|
| 33 | #include "os2ddraw.h"
|
|---|
| 34 | #include "os2palette.h"
|
|---|
| 35 | #include "misc.h"
|
|---|
| 36 | #include "os2palset.h"
|
|---|
| 37 |
|
|---|
| 38 | extern DIVE_CAPS dcaps;
|
|---|
| 39 |
|
|---|
| 40 | //******************************************************************************
|
|---|
| 41 | //******************************************************************************
|
|---|
| 42 | OS2IDirectDrawPalette::OS2IDirectDrawPalette(OS2IDirectDraw *lpDirectDraw,
|
|---|
| 43 | int palsize,
|
|---|
| 44 | W32_LPPALETTEENTRY lpColorTable) :
|
|---|
| 45 | Referenced(0), os2pal(NULL),
|
|---|
| 46 | lastError(DD_OK), lpDraw(NULL)
|
|---|
| 47 | {
|
|---|
| 48 | lpVtbl = &Vtbl;
|
|---|
| 49 | Vtbl.AddRef = PalAddRef;
|
|---|
| 50 | Vtbl.Release = PalRelease;
|
|---|
| 51 | Vtbl.QueryInterface = PalQueryInterface;
|
|---|
| 52 | Vtbl.GetCaps = PalGetCaps;
|
|---|
| 53 | Vtbl.GetEntries = PalGetEntries;
|
|---|
| 54 | Vtbl.Initialize = PalInitialize;
|
|---|
| 55 | Vtbl.SetEntries = PalSetEntries;
|
|---|
| 56 |
|
|---|
| 57 | lpDraw = lpDirectDraw;
|
|---|
| 58 | lpDraw->Vtbl.AddRef((IDirectDraw2*)lpDraw);
|
|---|
| 59 | hDive = lpDirectDraw->GetDiveInstance();
|
|---|
| 60 | nrColors = palsize;
|
|---|
| 61 |
|
|---|
| 62 | dprintf(("OS2IDirectDrawPalette::OS2IDirectDrawPalette nr colors %d", palsize));
|
|---|
| 63 | os2pal = (W32_LPPALETTEENTRY)malloc(palsize*sizeof(PALETTEENTRY));
|
|---|
| 64 | memcpy((char *)os2pal, (char *)lpColorTable, palsize*sizeof(PALETTEENTRY));
|
|---|
| 65 | }
|
|---|
| 66 | //******************************************************************************
|
|---|
| 67 | //******************************************************************************
|
|---|
| 68 | OS2IDirectDrawPalette::~OS2IDirectDrawPalette()
|
|---|
| 69 | {
|
|---|
| 70 | if(os2pal) free(os2pal);
|
|---|
| 71 | lpDraw->Vtbl.Release((IDirectDraw2*)lpDraw);
|
|---|
| 72 | RestorePhysPalette();
|
|---|
| 73 | }
|
|---|
| 74 | //******************************************************************************
|
|---|
| 75 | //******************************************************************************
|
|---|
| 76 | HRESULT __stdcall PalQueryInterface(THIS, REFIID riid, LPVOID FAR * ppvObj)
|
|---|
| 77 | {
|
|---|
| 78 | dprintf(("OS2IDirectDrawPalette::PalQueryInterface\n"));
|
|---|
| 79 | *ppvObj = NULL;
|
|---|
| 80 |
|
|---|
| 81 | if(!IsEqualGUID(riid, IID_IDirectDrawPalette))
|
|---|
| 82 | //&& !IsEqualGUID(riid, IID_IUnknown))
|
|---|
| 83 | return E_NOINTERFACE;
|
|---|
| 84 |
|
|---|
| 85 | *ppvObj = This;
|
|---|
| 86 |
|
|---|
| 87 | PalAddRef(This);
|
|---|
| 88 | return(DD_OK);
|
|---|
| 89 | }
|
|---|
| 90 | //******************************************************************************
|
|---|
| 91 | //******************************************************************************
|
|---|
| 92 | ULONG __stdcall PalAddRef(THIS)
|
|---|
| 93 | {
|
|---|
| 94 | OS2IDirectDrawPalette *me = (OS2IDirectDrawPalette *)This;
|
|---|
| 95 |
|
|---|
| 96 | dprintf(("OS2IDirectDrawPalette::PalAddRef %d\n", me->Referenced+1));
|
|---|
| 97 | return ++me->Referenced;
|
|---|
| 98 | }
|
|---|
| 99 | //******************************************************************************
|
|---|
| 100 | //******************************************************************************
|
|---|
| 101 | ULONG __stdcall PalRelease(THIS)
|
|---|
| 102 | {
|
|---|
| 103 | OS2IDirectDrawPalette *me = (OS2IDirectDrawPalette *)This;
|
|---|
| 104 |
|
|---|
| 105 | dprintf(("OS2IDirectDrawPalette::PalRelease %d\n", me->Referenced-1));
|
|---|
| 106 | if(me->Referenced) {
|
|---|
| 107 | me->Referenced--;
|
|---|
| 108 | if(me->Referenced == 0) {
|
|---|
| 109 | delete me;
|
|---|
| 110 | return(0);
|
|---|
| 111 | }
|
|---|
| 112 | else return me->Referenced;
|
|---|
| 113 | }
|
|---|
| 114 | else return(0);
|
|---|
| 115 | }
|
|---|
| 116 | //******************************************************************************
|
|---|
| 117 | //******************************************************************************
|
|---|
| 118 | HRESULT __stdcall PalGetCaps(THIS_ LPDWORD)
|
|---|
| 119 | {
|
|---|
| 120 | dprintf(("OS2IDirectDrawPalette::GetCaps\n"));
|
|---|
| 121 | return(DD_OK);
|
|---|
| 122 | }
|
|---|
| 123 | //******************************************************************************
|
|---|
| 124 | //******************************************************************************
|
|---|
| 125 | HRESULT __stdcall PalGetEntries(THIS_ DWORD dwFlags, DWORD dwBase, DWORD dwNumEntries,
|
|---|
| 126 | W32_LPPALETTEENTRY lpEntries)
|
|---|
| 127 | {
|
|---|
| 128 | OS2IDirectDrawPalette *me = (OS2IDirectDrawPalette *)This;
|
|---|
| 129 |
|
|---|
| 130 | dprintf(("OS2IDirectDrawPalette::PalGetEntries %d %d\n", dwBase, dwNumEntries));
|
|---|
| 131 |
|
|---|
| 132 | if(dwBase + dwNumEntries > 256)
|
|---|
| 133 | return(DDERR_UNSUPPORTED);
|
|---|
| 134 |
|
|---|
| 135 | memcpy((char *)lpEntries, (char *)(me->os2pal + dwBase), dwNumEntries*sizeof(PALETTEENTRY));
|
|---|
| 136 | return(DD_OK);
|
|---|
| 137 | }
|
|---|
| 138 | //******************************************************************************
|
|---|
| 139 | //******************************************************************************
|
|---|
| 140 | HRESULT __stdcall PalInitialize(THIS_ LPDIRECTDRAW, DWORD, W32_LPPALETTEENTRY)
|
|---|
| 141 | {
|
|---|
| 142 | dprintf(("OS2IDirectDrawPalette::PalInitialize\n"));
|
|---|
| 143 | return(DD_OK);
|
|---|
| 144 | }
|
|---|
| 145 | //******************************************************************************
|
|---|
| 146 | #pragma pack(1) /* pack on wordboundary */
|
|---|
| 147 |
|
|---|
| 148 | typedef struct _RGB2 /* rgb2 */
|
|---|
| 149 | {
|
|---|
| 150 | BYTE bBlue; /* Blue component of the color definition */
|
|---|
| 151 | BYTE bGreen; /* Green component of the color definition*/
|
|---|
| 152 | BYTE bRed; /* Red component of the color definition */
|
|---|
| 153 | BYTE fcOptions; /* Reserved, must be zero */
|
|---|
| 154 | } RGB2;
|
|---|
| 155 | typedef RGB2 *PRGB2;
|
|---|
| 156 | #pragma pack()
|
|---|
| 157 | //******************************************************************************
|
|---|
| 158 | HRESULT __stdcall PalSetEntries(THIS_ DWORD dwFlags, DWORD dwStartingEntry,
|
|---|
| 159 | DWORD dwCount, W32_LPPALETTEENTRY lpEntries)
|
|---|
| 160 | {
|
|---|
| 161 | OS2IDirectDrawPalette *me = (OS2IDirectDrawPalette *)This;
|
|---|
| 162 | int i, rc;
|
|---|
| 163 | RGB2 os2rgb[256];
|
|---|
| 164 |
|
|---|
| 165 | dprintf(("OS2IDirectDrawPalette::PalSetEntries %X %X %d %d\n", me, dwFlags, dwStartingEntry, dwCount));
|
|---|
| 166 | memcpy(&me->os2pal[dwStartingEntry], lpEntries, sizeof(PALETTEENTRY)*dwCount);
|
|---|
| 167 |
|
|---|
| 168 | for(i=0;i<256;i++) {
|
|---|
| 169 | os2rgb[i].bBlue = me->os2pal[i].peBlue;
|
|---|
| 170 | os2rgb[i].bGreen = me->os2pal[i].peGreen;
|
|---|
| 171 | os2rgb[i].bRed = me->os2pal[i].peRed;
|
|---|
| 172 | os2rgb[i].fcOptions = 0;
|
|---|
| 173 | // dprintf(("pal (%3d,%3d,%3d) %d\n", me->os2pal[i].peBlue, me->os2pal[i].peGreen, me->os2pal[i].peRed, me->os2pal[i].peFlags));
|
|---|
| 174 | }
|
|---|
| 175 | #if 0
|
|---|
| 176 | rc = DiveSetSourcePalette(me->hDive, dwStartingEntry, dwCount, (PBYTE)os2rgb);
|
|---|
| 177 | if(rc != DIVE_SUCCESS) {
|
|---|
| 178 | dprintf(("DiveSetSourcePalette returned %d\n", rc));
|
|---|
| 179 | return(DDERR_INVALIDPARAMS);
|
|---|
| 180 | }
|
|---|
| 181 | if(dcaps.ulDepth == 8) {
|
|---|
| 182 | rc = DiveSetDestinationPalette(me->hDive, dwStartingEntry, dwCount, (PBYTE)os2rgb);
|
|---|
| 183 | if(rc != DIVE_SUCCESS) {
|
|---|
| 184 | dprintf(("DiveSetDestinationPalette returned %d\n", rc));
|
|---|
| 185 | return(DDERR_INVALIDPARAMS);
|
|---|
| 186 | }
|
|---|
| 187 | }
|
|---|
| 188 | #else
|
|---|
| 189 | if(dcaps.ulDepth == 8) {
|
|---|
| 190 | OS2SetPhysPalette((HWND)me->lpDraw->GetWindowHandle(), me->os2pal);
|
|---|
| 191 | rc = DiveSetDestinationPalette(me->hDive, 0, 0, 0);
|
|---|
| 192 | if(rc != DIVE_SUCCESS) {
|
|---|
| 193 | dprintf(("DiveSetDestinationPalette returned %d\n", rc));
|
|---|
| 194 | return(DDERR_INVALIDPARAMS);
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| 197 | else {
|
|---|
| 198 | rc = DiveSetSourcePalette(me->hDive, 0, me->nrColors, (PBYTE)os2rgb);
|
|---|
| 199 | if(rc != DIVE_SUCCESS) {
|
|---|
| 200 | dprintf(("DiveSetSourcePalette returned %d\n", rc));
|
|---|
| 201 | return(DDERR_INVALIDPARAMS);
|
|---|
| 202 | }
|
|---|
| 203 | }
|
|---|
| 204 | #endif
|
|---|
| 205 | return(DD_OK);
|
|---|
| 206 | }
|
|---|
| 207 | //******************************************************************************
|
|---|
| 208 | //******************************************************************************
|
|---|
| 209 | void OS2IDirectDrawPalette::SetPhysPalette()
|
|---|
| 210 | {
|
|---|
| 211 | int i, rc;
|
|---|
| 212 | RGB2 os2rgb[256];
|
|---|
| 213 |
|
|---|
| 214 | for(i=0;i<nrColors;i++) {
|
|---|
| 215 | os2rgb[i].bBlue = os2pal[i].peBlue;
|
|---|
| 216 | os2rgb[i].bGreen = os2pal[i].peGreen;
|
|---|
| 217 | os2rgb[i].bRed = os2pal[i].peRed;
|
|---|
| 218 | os2rgb[i].fcOptions = 0;
|
|---|
| 219 | dprintf(("pal (%3d,%3d,%3d) %d\n", os2pal[i].peBlue, os2pal[i].peGreen, os2pal[i].peRed, os2pal[i].peFlags));
|
|---|
| 220 | }
|
|---|
| 221 | #if 0
|
|---|
| 222 | rc = DiveSetSourcePalette(hDive, 0, nrColors, (PBYTE)os2rgb);
|
|---|
| 223 | if(rc != DIVE_SUCCESS) {
|
|---|
| 224 | dprintf(("DiveSetSourcePalette returned %d\n", rc));
|
|---|
| 225 | return;
|
|---|
| 226 | }
|
|---|
| 227 | if(dcaps.ulDepth == 8) {
|
|---|
| 228 | rc = DiveSetDestinationPalette(hDive, 0, nrColors, (PBYTE)os2rgb);
|
|---|
| 229 | if(rc != DIVE_SUCCESS) {
|
|---|
| 230 | dprintf(("DiveSetDestinationPalette returned %d\n", rc));
|
|---|
| 231 | return;
|
|---|
| 232 | }
|
|---|
| 233 | }
|
|---|
| 234 | #else
|
|---|
| 235 | if(dcaps.ulDepth == 8) {
|
|---|
| 236 | OS2SetPhysPalette((HWND)lpDraw->GetWindowHandle(), os2pal);
|
|---|
| 237 | rc = DiveSetDestinationPalette(hDive, 0, 0, 0);
|
|---|
| 238 | if(rc != DIVE_SUCCESS) {
|
|---|
| 239 | dprintf(("DiveSetDestinationPalette returned %d\n", rc));
|
|---|
| 240 | return;
|
|---|
| 241 | }
|
|---|
| 242 | }
|
|---|
| 243 | else {
|
|---|
| 244 | rc = DiveSetSourcePalette(hDive, 0, nrColors, (PBYTE)os2rgb);
|
|---|
| 245 | if(rc != DIVE_SUCCESS) {
|
|---|
| 246 | dprintf(("DiveSetSourcePalette returned %d\n", rc));
|
|---|
| 247 | return;
|
|---|
| 248 | }
|
|---|
| 249 | }
|
|---|
| 250 | #endif
|
|---|
| 251 | }
|
|---|
| 252 | //******************************************************************************
|
|---|
| 253 | //******************************************************************************
|
|---|
| 254 | void OS2IDirectDrawPalette::RestorePhysPalette()
|
|---|
| 255 | {
|
|---|
| 256 | if(dcaps.ulDepth == 8) {
|
|---|
| 257 | OS2ResetPhysPalette();
|
|---|
| 258 | }
|
|---|
| 259 | }
|
|---|
| 260 | //******************************************************************************
|
|---|
| 261 | //******************************************************************************
|
|---|
| 262 |
|
|---|