Archived

This topic is now archived and is closed to further replies.

flukus

Need help with loading palettes

Recommended Posts

Basicly I have this code, most of which is from TOWGPG (Sick of andre lamothe yet?): for (int colour=1; colour < 255 ; colour++) { palette[colour].peRed = rand()%256; palette[colour].peGreen = rand()%256; palette[colour].peBlue = rand()%256; //set flag palette[colour].peFlags = PC_NOCOLLAPSE; }//end for //set first palette colour to blck palette[0].peRed = 0; palette[0].peGreen = 0; palette[0].peBlue = 0; palette[0].peFlags = PC_NOCOLLAPSE; palette[255].peRed = 255; palette[255].peGreen = 255; palette[255].peBlue = 255; palette[255].peFlags = PC_NOCOLLAPSE; if (FAILED(lpdd->CreatePalette(DDPCAPS_8BIT | DDPCAPS_INITIALIZE | DDPCAPS_ALLOW256, palette, &lpddpal, NULL))) MessageBox(main_window_handle, "error", "error", NULL); The problem is that the palette never gets created and I don''t have the slightest idea why!

Share this post


Link to post
Share on other sites
You''re forgetting one statement. This is how you should load the palette correctly:


  
if (FAILED(lpdd4->CreatePalette(DDPCAPS_8BIT | DDPCAPS_ALLOW256 | DDPCAPS_INITIALIZE, Palette, lpddPal, NULL)))
return (FAILED_PAL_CREATE);

if (FAILED(lpddsPrimary->SetPalette(lpddPal)))
return (FAILED_PAL_ATTACH);


You forgot the SetPalette routine. Gope it helps!

Sand Hawk

----------------
-Earth is 98% full. Please delete anybody you can.


My Site

Share this post


Link to post
Share on other sites
Check all your return codes properly, don''t just use FAILED.


HRESULT hr;

hr = (your directdraw call)
if (FAILED (hr))
{
// now report the error code so you can use the directx error lookup utility to find the cause.
}



Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites
Looking at you code, why aren''t you using lpdd4->CreateSurface?? I thought you needed to use the lpdd4 to create the palette and not the lpdd(DirectX 1 interface). I wrote already an DirectDraw wrapper class and this is my init function and my loadpalette/savepalette functions:


  
//////////////////////////////////////////////////////////////////////////////////////////

// I N I T

//

// Initialize DirectDraw6, including the creation of 2 surfaces

//////////////////////////////////////////////////////////////////////////////////////////

int DirectDraw::Init(HWND hWndApp, int ScreenWidth, int ScreenHeigth, int UsedBpp)
{
hWnd = hWndApp;
Width = ScreenWidth;
Heigth = ScreenHeigth;
Bpp = UsedBpp;

// CDebug Log("C:\\DDrawInit.log");

DDSURFACEDESC2 ddsd; // DDraw 6 surface description

LPDIRECTDRAW lpdd = NULL; // Temp DirectDraw1 interface, needed to get DDraw6 interface


hWnd = hWndApp;

// Request a DirectDraw1 interface. When failed the function returns a 1 to tell it failed

// on creating the DDraw1 surface.

if (FAILED(DirectDrawCreate(NULL, &lpdd, NULL)))
return (FAILED_INIT_DX1);

// Query an interface for DirectDraw6. IID_IDirectDraw4 is DDraw6, M$ fucked up with the

// numbers

if (FAILED(lpdd->QueryInterface(IID_IDirectDraw4, (LPVOID *)&lpdd4)))
return (FAILED_INIT_DX6);

// Release the DirectDraw1 interface, it''s no longer needed.

lpdd->Release();
lpdd = NULL;

// Set cooperation level with Windows. Needed because DX is gonna take over alot of

// things from Windows and is going to be full screen.

if (FAILED(lpdd4->SetCooperativeLevel(hWnd,
DDSCL_FULLSCREEN |
DDSCL_ALLOWMODEX |
DDSCL_EXCLUSIVE |
DDSCL_ALLOWREBOOT)))
return (FAILED_INIT_COOP);

// Set the displaymode

if (FAILED(lpdd4->SetDisplayMode(ScreenWidth, ScreenHeigth, UsedBpp, 0, 0)))
return (FAILED_INIT_SETDISPLAY);

// Fill up some stuff needed to create a surface

DDRAW_INIT_STRUCT(ddsd); // Replaces above 2 lines.

ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
ddsd.dwBackBufferCount = 1;

// Create primary surface, using the ddsd struct from above

if (FAILED(lpdd4->CreateSurface(&ddsd, &lpddsPrimary, NULL)))
return (FAILED_INIT_PRIMARY_BUFFER);

// Create the back(secondary) buffer.

ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;

if (FAILED(lpddsPrimary->GetAttachedSurface(&ddsd.ddsCaps, &lpddsBack)))
return (FAILED_INIT_SECONDARY_BUFFER);

// return success

return (SUCCESS);
}

//////////////////////////////////////////////////////////////////////////////////////////

// L O A D P A L E T T E

//

// Load the palette and attach it to the surfaces.

//////////////////////////////////////////////////////////////////////////////////////////

int DirectDraw::LoadPalette(char File[1024])
{
ifstream Input(File, ios::binary);

// If the file didn''t succeed to open

if (!Input)
{
MessageBox(NULL, "Failed to open", "Failed", MB_OK);
return (8);
}

// Check for failure to read the data from file

if (!(Input.read((char *) &Palette, sizeof(Palette))))
{
MessageBox(NULL, "Failed to open", "Failed", MB_OK);
return (9);
}

// Close the file

Input.close();

// Attach the palette for the primary surface, but first check if there isn''t already one

// attached

if (lpddPal == NULL)
{
// No palette attached, so attach it now

if (FAILED(lpdd4->CreatePalette(DDPCAPS_8BIT | DDPCAPS_ALLOW256 | DDPCAPS_INITIALIZE,
Palette, &lpddPal, NULL)))
return (FAILED_PAL_CREATE);

// Attach the palette to the surface

if (FAILED(lpddsPrimary->SetPalette(lpddPal)))
return (FAILED_PAL_ATTACH);
}
else
{
// There is already an palette attached, so we have to update it

if (FAILED(lpddPal->SetEntries(0, 0, MAX_PAL_COLORS, Palette)))
return (FAILED_PAL_UPDATE);
}

return (SUCCESS);
}

//////////////////////////////////////////////////////////////////////////////////////////

// U P D A T E P A L E T T E

//

// Update the current palette with a new one and automaticly update the palette stored

// inside the struct

//////////////////////////////////////////////////////////////////////////////////////////

int DirectDraw::UpdatePalette(PALETTEENTRY NewPalette[MAX_PAL_COLORS])
{
// Update the palette inside the DDrawStruct, then update the palettes used by DirectDraw

for (int Index = 0; Index < 256; Index++)
Palette[Index] = NewPalette[Index];

// Attach the palette for the primary surface, but first check if there isn''t already one

// attached

if (lpddPal == NULL)
{
// No palette attached, so attach it now

if (FAILED(lpdd4->CreatePalette(DDPCAPS_8BIT | DDPCAPS_ALLOW256 | DDPCAPS_INITIALIZE,
Palette, &lpddPal, NULL)))
return (FAILED_PAL_CREATE);

// Attach the palette to the surface

if (FAILED(lpddsPrimary->SetPalette(lpddPal)))
return (FAILED_PAL_ATTACH);
}
else
{
// There is already an palette attached, so we have to update it

if (FAILED(lpddPal->SetEntries(0, 0, MAX_PAL_COLORS, Palette)))
return (FAILED_PAL_UPDATE);
}

return (SUCCESS);
}

Hope this helps. You need to change the code a little(Remove the msgboxes. These are still debug versions).

Sand HAwk


----------------
-Earth is 98% full. Please delete anybody you can.


My Site

Share this post


Link to post
Share on other sites
"Looking at you code, why aren''t you using lpdd4->CreateSurface?? I thought you needed to use the lpdd4 to create the palette and not the lpdd(DirectX 1 interface). I wrote already an DirectDraw wrapper class and this is my init function and my loadpalette/savepalette functions:"

lpdd is the directdraw7 interface.

"Check all your return codes properly, don''t just use FAILED."

Where can I find all the possible return codes?

Share this post


Link to post
Share on other sites
You just have your program print out return codes so you can use the DirectX Error Lookup utility which is in the SDK.

All DirectX functions return an HRESULT - which is the return code. Also the docs tell you which return codes to expect from which functions.

It''s all in the SDK... all in the SDK...


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites
"All DirectX functions return an HRESULT - which is the return code. Also the docs tell you which return codes to expect from which functions."

I searched for everything I could think of in the direct x docs and couldn''t find anything releveant.

"You just have your program print out return codes so you can use the DirectX Error Lookup utility which is in the SDK."

How do I turn HRESULT into something readable?

Share this post


Link to post
Share on other sites
Oh boy.

An HRESULT can be treated as a 32 bit integer. Feed it into printf or cout. Every reference page for every DirectX function and method gives a list of every HRESULT you can expect that function or method to return. An example:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dx8_c/directx_cpp/Graphics/Reference/CPP/D3D/Interfaces/IDirect3D8/CreateDevice.asp

Look at the Return Values section.

The DirectX error lookup utility is in the DirectX SDK. You do have the SDK right? Remember that the SDK is more than docs.

Oh boy.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites
"An HRESULT can be treated as a 32 bit integer. Feed it into printf or cout"

How do I do that from a win32/DX app? As far a I know Message boxes are the only output I can give at the moment.

"http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dx8_c/directx_cpp/Graphics/Reference/CPP/D3D/Interfaces/IDirect3D8/ CreateDevice.asp"

Everytime I try navigating MSDN (including when I follow that link) I end up at a page that just has:

Welcome to the MSDN Library
Welcome to the MSDN Library
Component Development
Data Access
Development (General)
Enterprise Development
Graphics and Multimedia
etc.

Share this post


Link to post
Share on other sites
The link works for me, but it looks like you''ve got an accidental space in your version. I''ll copy and paste a little bit...


Return Values
If the method succeeds, the return value is D3D_OK.

If the method fails, the return value can be one of the following values.

D3DERR_INVALIDCALL
D3DERR_NOTAVAILABLE
D3DERR_OUTOFVIDEOMEMORY


In a Windows app you can use sprintf (or one of the C++ string classes) to make up a string that tells you the number, then use MessageBox. You can also use OutputDebugString to print to Visual C++''s Debug window. Or you can write a few logging functions so you can output to a text file.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites
Thanks alot!!! It turns out that the problem was "DDERR_NOCOOPERATIVELEVELSET " I don''t know what the problem was yet but at least now I know how to find out.

And the problem that I was having with MSDN is that it doesn''t work in Opera (surprise surprise).

Once again, thanks heaps!

Share this post


Link to post
Share on other sites