Skip to main content
GameDev.net gamedev.net
🔒 Locked

DDERR_INVALIDPIXELFORMAT

Started by BrainlessDesigns Oct 18, 2005 at 9:17 AM 3 replies 5.2k views
Original Post
BrainlessDesigns
BrainlessDesigns
hi there, i got the problem to set up a 256-color palette with the directx 9.0c sdk. i tried the following code - it's my initialization stuff - important is the place where SetPalette() is called...

/*method:      init()
  parameters:  hWnd     -> main handle from window
               width    -> width of the primary surface
			   height   -> height of the primary surface
			   bpp      -> bits per pixel of the primary surface (color depth)
			   windowed -> flag for NO fullscreen mode
  returns:     0  -> BD_DirectDraw object successfully initialized
               >0 -> an error occured
  description: initializes the BD_DirectDraw object
*/
u8 BD_DirectDraw::init(const HWND hWnd,
					   const u16 width,
					   const u16 height,
					   const u32 bpp,
					   const u8 windowed)
{
	// copy parameters
	this->hWnd    = hWnd;
	displayWidth  = width;
	displayHeight = height;
	displayBPP    = bpp;
	isWindowed    = windowed;

	// initialize windows stuff
	RECT windowRect = {0,0,displayWidth,displayHeight};
	hMenu           = NULL;
	windowstyle     = GetWindowLong(hWnd,GWL_style);
	windowExstyle   = GetWindowLong(hWnd,GWL_EXstyle);
	AdjustWindowRectEx(&windowRect,
		               windowstyle,
					   GetMenu(hWnd) != NULL,
					   windowExstyle);
	MoveWindow(hWnd,
		       CW_USEDEFAULT,
			   CW_USEDEFAULT,
			   displayWidth,
			   displayHeight,
			   FALSE);
    GetWindowRect(hWnd,&windowBounds);
    GetClientRect(hWnd,&windowClient);
	
	// build DirectDraw interface
	hr = DirectDrawCreate(NULL,
		                  &pDD,  
						  NULL);
	if(checkForErrors("DirectDrawCreate"))
		return 1;

	// now look for the latest DirectDraw interface
	hr = pDD->QueryInterface(IID_IDirectDraw7,(LPVOID*)&pDD7);
	if(checkForErrors("QueryInterface"))
		return 2;

	// release old DirectDraw object - no more need here
	pDD->Release();
	pDD = NULL;

	// set the cooperative level
	if(isWindowed)
	{
		// set DDSCL_NORMAL to use windowed mode
		hr = pDD7->SetCooperativeLevel(hWnd,DDSCL_NORMAL);
		if(checkForErrors("SetCooperativeLevel windowed"))
			return 3;
	}
	else
	{
		// set the cooperative level for fullscreen mode
		hr = pDD7->SetCooperativeLevel(hWnd,
			                           DDSCL_ALLOWMODEX | 
									   DDSCL_FULLSCREEN | 
									   DDSCL_EXCLUSIVE | 
									   DDSCL_ALLOWREBOOT);
		if(checkForErrors("SetCooperativeLevel"))
			return 3;

		// set the display mode
		hr = pDD7->SetDisplayMode(displayWidth,
			                      displayHeight,
								  displayBPP,
								  0,
								  0);
		if(checkForErrors("SetDisplayMode"))
			return 4;
	}

	
	// clear DIRECTDRAWSURFACEDESC2 structure
	memset(&ddsd,0,sizeof(ddsd));
	ddsd.dwSize = sizeof(ddsd);

	if(isWindowed)
	{
		ddsd.dwFlags = DDSD_CAPS;
		ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
		ddsd.ddsCaps.dwCaps2 = 0;
		ddsd.dwBackBufferCount = 0;
	}
	else
	{
		ddsd.dwFlags = DDSD_CAPS | 
			           DDSD_BACKBUFFERCOUNT;
		ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | 
			                  DDSCAPS_FLIP | 
							  DDSCAPS_COMPLEX;
		ddsd.ddsCaps.dwCaps2 = 0;
		ddsd.dwBackBufferCount = 1;
	}
	
	// create the primary surface
	hr = pDD7->CreateSurface(&ddsd,
				             &pDDSPrimary,
							 NULL);
	if(checkForErrors("Create primary surface"))
		return 5;

	// get the pixel format of the primary surface
	DDPIXELFORMAT ddpf; // used to get pixel format

	// initialize structure
    memset(&ddpf,0,sizeof(ddpf));
	ddpf.dwSize = sizeof(ddpf);

	// query the format from primary surface
	pDDSPrimary->GetPixelFormat(&ddpf);
	
	// get the pixel format
	pixelFormat = ddpf.dwRGBBitCount;

	// only need a backbuffer for fullscreen modes
	if(windowed)
	{
		// must be windowed, so create a double buffer that will be blitted
		// rather than flipped as in full screen mode
		pDDSBack = createSurface(width,
			                     height,
								 DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
								 0);
	}
	else
	{
		// query for the backbuffer i.e the secondary surface
		ddscaps.dwCaps = DDSCAPS_BACKBUFFER;

		hr = pDDSPrimary->GetAttachedSurface(&ddscaps,&pDDSBack);
		if(checkForErrors("Create back buffer"))
			return 6;
   }

	// create a palette only if 8bit mode
	if(bpp == 8)
	{
		// create and attach palette
		// clear all entries, defensive programming
		memset(palette,0,MAX_COLORS_PALETTE * sizeof(PALETTEENTRY));

		// load a pre-made "good" palette off disk
		//loadPalette(DEFAULT_PALETTE_FILE, palette);
		for(u16 i=0; i<256; i++)
		{
			palette.peRed   = 1;
            palette.peGreen = 1;
			palette.peBlue  = 1;
			palette.peFlags = 0;			
		}

		// load and attach the palette, test for windowed mode
		if(isWindowed)
		{
			// in windowed mode, so the first 10 and last 10 entries have
			// to be slightly modified as does the call to createpalette
			// reset the peFlags bit to PC_EXPLICIT for the "windows" colors
			/*for (u16 i=0; i < 10; i++)
				palette.peFlags = palette[i + 246].peFlags = PC_EXPLICIT;*/

			// now create the palette object, but disable access to all 256 entries
			hr = pDD7->CreatePalette(DDPCAPS_8BIT | DDPCAPS_INITIALIZE,
				                     palette,
									 &pDDPal,
									 NULL);
		}
		else
		{
			// in fullscreen mode, so simple create the palette with the default palette
			// and fill in all 256 entries
			hr = pDD7->CreatePalette(DDPCAPS_8BIT | 
				                     DDPCAPS_INITIALIZE | 
									 DDPCAPS_ALLOW256,
                                     palette,
									 &pDDPal,
									 NULL);
		}
		
		if(checkForErrors("CreatePalette failed"))
			return 7;

		// now attach the palette to the primary surface
        hr = pDDSPrimary->SetPalette(pDDPal);
		if(checkForErrors("SetPalette failed"))
			return 8;
	}

	// clear out both primary and secondary surfaces
	if(isWindowed)
	{
		// only clear backbuffer
		clearSurface(BACK_BUFFER);
	}
	else
	{
		// fullscreen, simply clear everything
		clearSurface(PRIMARY_SURFACE);
		clearSurface(BACK_BUFFER);
	}

	// setup backbuffer clipper always
	if((pClipperBack = attachClipper(pDDSBack,1,&windowClient)) == NULL)
		return 9;

	// set up windowed mode clipper
	if(isWindowed)
	{
		// set windowed clipper
		hr = pDD7->CreateClipper(0,&pClipperPrimary,NULL);
		if(checkForErrors("CreateClipper failed"))
			return 10;

		hr = pClipperPrimary->SetHWnd(0,hWnd);
		if(checkForErrors("SetHWnd failed"))
			return 11;

		hr = pDDSPrimary->SetClipper(pClipperPrimary);
		if(checkForErrors("SetClipper failed"))
			return 12;
	}
	
	return 0;
}

if you have any idea, why i get after that the error DDERR_INVALIDPIXELFORMAT, i would be very pleased... thx 4 ur answers [Edited by - Coder on October 18, 2005 11:45:16 AM]
__BR4iNL355 D35iGN5__
Supernat02
Supernat02
Do you mean that it crashes during the call to CreatePalette? According to the latest SDK online, that function can't return DDERR_INVALIDPIXELFORMAT. Run your debugger to find out what function is returning the error (by stepping through each line) and then it will be more obvious what the problem is.
Chris ByersMicrosoft DirectX MVP - 2005
BrainlessDesigns
BrainlessDesigns
hi supernat02!

// now attach the palette to the primary surface
hr = pDDSPrimary->SetPalette(pDDPal);
if(checkForErrors("SetPalette failed"))
return 8;

no it returns here DDERR_INVALIDPIXELFORMAT - creating the palette returns no error...

any ideas?

thx

__BR4iNL355 D35iGN5__
Supernat02
Supernat02
I don't see where you set the pixel format of the primary surface you're creating.
Chris ByersMicrosoft DirectX MVP - 2005
BrainlessDesigns
BrainlessDesigns
the problem is solved - thx 4 ur answer.
it was that i've forgotten to call the ZeroMemory() function - i realized that this is an often made bug in DirectX programming - i am new to DirectX though...
ALWAYS CLEAR THE DDSURFACEDESC2 STRUCTURE OR ANY OTHER ;-)
__BR4iNL355 D35iGN5__

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.