using palette in Direct3D (Direct9)

cossack5

Newcomer
Joined
Jun 24, 2012
I have some large project written in directdraw. But I want to port it to Direct9.
Everythying is OK, except the colors on the screen. I don't know how to use palette in D3D (SetPaletteEntries and SetCurrentTexturePalette function).
Here is my code of creating d3d and device
Code:
   if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        return E_FAIL;
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof( d3dpp ) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.Flags=D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
	d3dpp.BackBufferCount=1;
	d3dpp.hDeviceWindow=hWnd;
      if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_HARDWARE_VERTEXPROCESSING,&d3dpp,&g_pd3dDevice ) ) )
      {
        return E_FAIL;
      }
HRESULT hr=g_pd3dDevice->CreateOffscreenPlainSurface(width,height,D3DFMT_L8,D3DPOOL_DEFAULT,&g_p2DSurface,NULL);
As you see, I create surface using D3DFMT_L8 format (my game uses 8-bit format and palette in DirectDraw). The rest of 8 bit formats in D3D doesn't work - if i choose D3DFMT_P8 i get the black screen with nothing more, A8 - the same as D3DFMT_P8.
Then I get pointer to the screen using this function:
Code:
bool LockScreen() // lock screen running Direct3D 9
{
	if (!g_p2DSurface) return false;
	D3DLOCKED_RECT rectLock;
	long dderr=g_p2DSurface->LockRect(&rectLock,NULL,D3DLOCK_NOSYSLOCK|D3DLOCK_DONOTWAIT);
	if (dderr!=D3D_OK) return false;
	RSCRSizeX=rectLock.Pitch;
	RealScreenPtr=rectLock.pBits;
	return true;
};
I'm trying to use palette like this:
Code:
PALETTEENRY GPal[256];
// here we load palette from file
		 if (g_pd3dDevice->SetPaletteEntries(16384,&GPal[0])!=D3D_OK) return false;
	 else{
	if (g_pd3dDevice->SetCurrentTexturePalette(16384)!=D3D_OK)
                       return false;
			   };
But that code does nothing. Where have I made a mistake?

When i'm going to expose all the images to the screen i call this function:
Code:
void EndRender()
{
	if (g_p2DSurface) 
	{
     IDirect3DSurface9* pBackbuffer;
	g_pd3dDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&pBackbuffer);
	g_pd3dDevice->StretchRect(g_p2DSurface,NULL,pBackbuffer,NULL,D3DTEXF_POINT);
      g_pd3dDevice->EndScene(); // works without it
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
	};
};
So the problem is that I get the grey images (though in DirectDraw with palette loaded and applied to the surface they are fine). What am I doing wrong?
Any help will be appreciated.
 
Last edited:
Top Bottom