我正在学习 DirectX,我想将调色板绑定到 PrimarySurface,但该过程总是失败。我在下面给出我的代码:
#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;
}
我不知道为什么我总是无法将 SetPalette 设置到主表面。我将DisplayMode设置为640*480*32,我的调色板只有256色,是这个原因吗?但是我查阅了MSDN,CreatePalette只能创建2Bit、4Bit、8Bit调色板。32 位显示模式可以兼容 8 位调色板吗?哪里有问题?
如果有人能给我一些建议,我将不胜感激。谢谢。