• Create Account

## DirectX7 SetPalette always failed

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

### #1Maple Wan  Members

229
Like
0Likes
Like

Posted 04 September 2013 - 10:16 AM

Dear all:

I'm learning DirectX, I want to Bind a palette to the PrimarySurface, but the process always failed. I give my code below:

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define SCREEN_BPP 32
#define MAX_COLORS_PALETTE 256

#define DDRAW_INIT_STRUCT(ddstruct) { memset(&ddstruct, 0, sizeof(ddstruct)); ddstruct.dwSize = sizeof(ddstruct); }

LPDIRECTDRAW7 lpdd = NULL;
LPDIRECTDRAWSURFACE7 lpddPrimarySurface = NULL;
LPDIRECTDRAWPALETTE lpddPalette = NULL;
PALETTEENTRY palette[256];

// Omit the unneccessary content

int GameInit()
{
if (FAILED(DirectDrawCreateEx(NULL, (void**)&lpdd, IID_IDirectDraw7, NULL)))
return 0;
if (FAILED(lpdd->SetCooperativeLevel(g_GameHwnd, DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)))
return 0;
if (FAILED(lpdd->SetDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, 0, 0)))
return 0;

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

if (FAILED(lpdd->CreateSurface(&ddsd, &lpddPrimarySurface, NULL)))
return 0;

ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
if (FAILED(lpddPrimarySurface->GetAttachedSurface(&ddsd.ddsCaps, &lpddBackSurface)))
return 0;

memset(palette, 0, MAX_COLORS_PALETTE * sizeof(PALETTEENTRY));

for (int index = 0; index < MAX_COLORS_PALETTE; index++)
{
if (index < 64)
palette[index].peRed = index * 4;
else if (index >= 64 && index < 128)
palette[index].peGreen = (index - 64) * 4;
else if (index >= 128 && index < 192)
palette[index].peBlue = (index - 128) * 4;
else if (index >= 192 && index < 256)
palette[index].peRed = palette[index].peGreen = palette[index].peBlue = (index - 192) * 4;

palette[index].peFlags = PC_NOCOLLAPSE;
}

if (FAILED(lpdd->CreatePalette(DDPCAPS_8BIT | DDPCAPS_ALLOW256 | DDPCAPS_INITIALIZE, palette, &lpddPalette, NULL)))
return 0;

// I always failed to set palette to primary surface here....
if (FAILED(lpddPrimarySurface->SetPalette(lpddPalette)))
{
MessageBox(NULL, "Failed", NULL, MB_OK);
return 0;
}

DDRAW_INIT_STRUCT(ddsd);

if (FAILED(lpddBackSurface->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL)))
return 0;

UINT *videoBuffer = (UINT*)ddsd.lpSurface;

for (int y = 0; y < SCREEN_HEIGHT; y++)
{
memset((void*)videoBuffer, y % 256, SCREEN_WIDTH * sizeof(UINT));
videoBuffer += ddsd.lPitch >> 2;
}

if (FAILED(lpddBackSurface->Unlock(NULL)))
return 0;

return 1;
}


I don't know why I always failed to SetPalette to the primary surface. I set the DisplayMode to 640*480*32, my palette is only 256-color, is this the reason? But I consult the MSDN, CreatePalette can only create 2Bit, 4Bit, 8Bit palettes. Can a 32-bit display mode be compatible with 8Bit palette? Where is the problem?

I feel sorry about that my English is not good. I will be grateful if someone could give me some advice. Thanks.

### #2Juliean  GDNet+

6170
Like
1Likes
Like

Posted 04 September 2013 - 11:11 AM

I'm learning DirectX

Any specific reason why you are starting with the Version from 1999, which is outdated since too many years to count? Besides being virtually pointless (unless you have a very specific reason yourself), it limits the number of people being able to help you...

### #3Nik02  Members

4072
Like
0Likes
Like

Posted 04 September 2013 - 12:17 PM

Palettes cannot be used with more than 8-bit surfaces. If you set your display mode to 32-bit, then your primary buffer (and back buffer) are 32-bit.

Niko Suni

### #4Maple Wan  Members

229
Like
0Likes
Like

Posted 04 September 2013 - 07:32 PM

I'm learning DirectX

Any specific reason why you are starting with the Version from 1999, which is outdated since too many years to count? Besides being virtually pointless (unless you have a very specific reason yourself), it limits the number of people being able to help you...

Thanks. In fact, I would like to learn dx10 instead of dx7, but I'm reading a book about how to do game programming, however the samples in the book is all about dx7. What I think is to study dx10 after complete this book reading.

### #5Maple Wan  Members

229
Like
0Likes
Like

Posted 04 September 2013 - 07:37 PM

Palettes cannot be used with more than 8-bit surfaces. If you set your display mode to 32-bit, then your primary buffer (and back buffer) are 32-bit.

Thanks very much. So I can't set palette in 32-bit surfaces? There is no palette concept in 32-bit mode, can I comprehend it in this way?

### #6Khatharr  Members

7659
Like
0Likes
Like

Posted 04 September 2013 - 08:09 PM

You may want to check out:

http://www.drunkenhyena.com/cgi-bin/dx9.pl

http://www.directxtutorial.com

http://www.rastertek.com

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

### #7Endurion  Members

5162
Like
0Likes
Like

Posted 04 September 2013 - 11:45 PM

There is no palette concept in 32-bit mode, can I comprehend it in this way?

Yes, as Nik02 said. Palettes exist only for 8-bit and lower bitty surfaces. Anything higher than that uses direct color values.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

### #8Maple Wan  Members

229
Like
0Likes
Like

Posted 04 September 2013 - 11:57 PM

There is no palette concept in 32-bit mode, can I comprehend it in this way?

Yes, as Nik02 said. Palettes exist only for 8-bit and lower bitty surfaces. Anything higher than that uses direct color values.

Thanks very much~~

### #9Juliean  GDNet+

6170
Like
1Likes
Like

Posted 05 September 2013 - 10:17 AM

Thanks. In fact, I would like to learn dx10 instead of dx7, but I'm reading a book about how to do game programming, however the samples in the book is all about dx7. What I think is to study dx10 after complete this book reading.

Word of advice, if you'd like some more, screw that book, most of the techniques are probably horribly outdated anyway. Some of the books from about 6 years ago with DirectX9 tended to teach practices not nearly practically usable, so I quess with your book, you'll spend more time learning things that are eigther:

- outdated, and not needed anymore (like the palette thing) or

- outdated, and overly complicated or "bad" (like twenty-depth inheritance chains for game objects, yieks!)

If I where you, I'd get a decent DirectX10, or at least DirectX9 book. Also, learning DirectX10, there are better alternatives, DirectX11 is almost the same as its predecessor, just with a few more features. Unless you are stuck with Vista, why not start right there? That way you'll be most up to date ;)

Edited by Juliean, 05 September 2013 - 10:17 AM.

### #10Maple Wan  Members

229
Like
0Likes
Like

Posted 06 September 2013 - 09:06 AM

Thanks. In fact, I would like to learn dx10 instead of dx7, but I'm reading a book about how to do game programming, however the samples in the book is all about dx7. What I think is to study dx10 after complete this book reading.

Word of advice, if you'd like some more, screw that book, most of the techniques are probably horribly outdated anyway. Some of the books from about 6 years ago with DirectX9 tended to teach practices not nearly practically usable, so I quess with your book, you'll spend more time learning things that are eigther:

- outdated, and not needed anymore (like the palette thing) or

- outdated, and overly complicated or "bad" (like twenty-depth inheritance chains for game objects, yieks!)

If I where you, I'd get a decent DirectX10, or at least DirectX9 book. Also, learning DirectX10, there are better alternatives, DirectX11 is almost the same as its predecessor, just with a few more features. Unless you are stuck with Vista, why not start right there? That way you'll be most up to date ;)

Thanks for your suggestion, I'll try it~