void UpdatePalette() { if(IsPaletted() && PrimarySurface) { IDirectDrawPalette *pal = 0; ((IDirectDrawSurface *) PrimarySurface)->GetPalette(&pal); if(pal) { PALETTEENTRY entries[256]; pal->GetEntries(0,0,256,entries); SetPalette(entries,256); pal->Release(); } else printLog("video: couldn't get palette!\n"); } }
//+------------------------------------------------------------------------ // // Member: COffScreenContext::GetDDSurface // // Synopsis: Create a suitable DD surface or get one from the cache. // //------------------------------------------------------------------------- BOOL COffScreenContext::GetDDSurface(HPALETTE hpal) { HRESULT hr; LOCK_SECTION(g_csOscCache); if(!g_OscCache._fInUse) { ClearDDBCache(); // don't allow both DD & DDB in the cache if(g_OscCache._pDDSurface != NULL) { Assert(g_OscCache._fUseDD); if(_widthActual>g_OscCache._size.cx || _heightActual>g_OscCache._size.cy || _cBitsPixel!=g_OscCache._cBitsPixel || (_fUse3D&&!g_OscCache._fUse3D)) { ++g_OscCache._cMisses; ClearSurfaceCache(); } else { ++g_OscCache._cHits; g_OscCache._fInUse = TRUE; _pDDSurface = g_OscCache._pDDSurface; _pDDSurface->AddRef(); _hdcMem = g_OscCache._hdcMem; _widthActual = g_OscCache._size.cx; _heightActual = g_OscCache._size.cy; } } if(g_OscCache._pDDSurface == NULL) { // use max area allowed for the cache, so we get max reuse potential // favor width over height for those wide text runs // also adjust the max size in a growing fashion based on former allocations g_OscCache._size.cx = max(g_OscCache._size.cx, max(min(g_OscCache._areaTgt/_heightActual, g_OscCache._sizeTgt.cx), _widthActual)); g_OscCache._size.cy = max(g_OscCache._size.cy , max(g_OscCache._areaTgt/g_OscCache._size.cx, _heightActual)); if(!CreateDDSurface(g_OscCache._size.cx, g_OscCache._size.cy, hpal)) { return FALSE; } Trace0("surface cache created\n"); g_OscCache._fInUse = TRUE; g_OscCache._fUseDD = TRUE; g_OscCache._fUse3D = _fUse3D; g_OscCache._pDDSurface = _pDDSurface; g_OscCache._pDDSurface->AddRef(); // addref the global surface g_OscCache._cBitsPixel = _cBitsPixel; g_OscCache._hdcMem = _hdcMem; g_OscCache._hpal = hpal; _widthActual = g_OscCache._size.cx; _heightActual = g_OscCache._size.cy; } } else { Trace0("surface cache in use\n"); } if(_pDDSurface == NULL) { if(!CreateDDSurface(_widthActual, _heightActual, hpal)) { return FALSE; } } // reset the color table when using cache if(_cBitsPixel==8 && _pDDSurface==g_OscCache._pDDSurface && (hpal!=g_OscCache._hpal || hpal!=g_hpalHalftone)) { IDirectDrawPalette* pDDPal; PALETTEENTRY pal256[256]; long cEntries; cEntries = GetPaletteEntries(hpal, 0, 256, pal256); // get the DD palette and set entries hr = _pDDSurface->GetPalette(&pDDPal); if(SUCCEEDED(hr)) { hr = pDDPal->SetEntries(0, 0, cEntries, pal256); pDDPal->Release(); if(SUCCEEDED(hr)) { g_OscCache._hpal = hpal; } } } return TRUE; }
//+------------------------------------------------------------------------ // // Member: COffScreenContext::CreateDDSurface // // Synopsis: Create a DD surface with the specified dimensions and palette. // //------------------------------------------------------------------------- BOOL COffScreenContext::CreateDDSurface(long width, long height, HPALETTE hpal) { HRESULT hr = InitSurface(); if(FAILED(hr)) { return FALSE; } DDPIXELFORMAT* pPF = PixelFormat(_hdcWnd, _cBitsPixel); if(!pPF) { return FALSE; } DDSURFACEDESC ddsd; ddsd.dwSize = sizeof(ddsd); ddsd.ddpfPixelFormat = *pPF; ddsd.dwFlags = DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT; ddsd.ddsCaps.dwCaps = DDSCAPS_DATAEXCHANGE|DDSCAPS_OWNDC; if(_fUse3D) { ddsd.ddsCaps.dwCaps |= DDSCAPS_3DDEVICE; } ddsd.dwWidth = width; ddsd.dwHeight = height; hr = g_pDirectDraw->CreateSurface(&ddsd, &_pDDSurface, NULL); if(FAILED(hr)) { return FALSE; } // set color table if(_cBitsPixel <= 8) { IDirectDrawPalette* pDDPal; PALETTEENTRY* pPal; PALETTEENTRY pal256[256]; long cEntries; DWORD pcaps; if(_cBitsPixel == 8) { cEntries = GetPaletteEntries(hpal, 0, 256, pal256); pPal = pal256; pcaps = DDPCAPS_8BIT; } else if(_cBitsPixel == 4) { cEntries = 16; pPal = g_pal16; pcaps = DDPCAPS_4BIT; } else if(_cBitsPixel == 1) { cEntries = 2; pPal = g_pal2; pcaps = DDPCAPS_1BIT; } else { Assert(0 && "invalid cBitsPerPixel"); return FALSE; } // create and initialize a new DD palette hr = g_pDirectDraw->CreatePalette(pcaps|DDPCAPS_INITIALIZE, pPal, &pDDPal, NULL); if(SUCCEEDED(hr)) { // attach the DD palette to the DD surface hr = _pDDSurface->SetPalette(pDDPal); pDDPal->Release(); } if(FAILED(hr)) { return FALSE; } } hr = _pDDSurface->GetDC(&_hdcMem); return SUCCEEDED(hr); }