PDA

View Full Version : SwapBuffers(DC); Command is not working on Win2000



09-30-2003, 01:28 AM
I want to use 'Delphi Panel' for OpenGL scene.
For this purpose I am using following code:

procedure TForm1.DecribeScene(hnd:HWND);
begin
DC := GetDC(hnd);
SetDCPixelFormat;
hrc := wglCreateContext(DC);
wglMakeCurrent(DC, hrc);
end;

I am sending Panel1.handle as parameter to this procedure.
Source code of the SetDCPixelFormat procedure is at the underside.

When I want to draw my shapes:

DrawShapes; //I am using OpenGL commands( described in Opengl unit ) in this procedure.
SwapBuffers(DC);

This code is properly working on Win98 but it is not working on Win2000.
On Win2000 'SwapBuffers' function is return false value every time.
What must I do for solving this problem?

Best regards,

Mustafa

procedure TForm1.SetDCPixelFormat;
var
hHeap: THandle;
nColors, i: Integer;
lpPalette: PLogPalette;
byRedMask, byGreenMask, byBlueMask: Byte;
nPixelFormat: Integer;
pfd: TPixelFormatDescriptor;

begin
FillChar(pfd, SizeOf(pfd), 0);

with pfd do begin
nSize := sizeof(pfd); // Size of this structure
nVersion := 1; // Version number
dwFlags := PFD_DRAW_TO_WINDOW or
PFD_SUPPORT_OPENGL or
PFD_DOUBLEBUFFER; // Flags
iPixelType:= PFD_TYPE_RGBA; // RGBA pixel values
cColorBits:= 24; // 24-bit color
cDepthBits:= 32; // 32-bit depth buffer
iLayerType:= PFD_MAIN_PLANE; // Layer type
end;

nPixelFormat := ChoosePixelFormat(DC, @pfd);
SetPixelFormat(DC, nPixelFormat, @pfd);

DescribePixelFormat(DC, nPixelFormat, sizeof(TPixelFormatDescriptor), pfd);

if ((pfd.dwFlags and PFD_NEED_PALETTE) <> 0) then begin
nColors := 1 shl pfd.cColorBits;
hHeap := GetProcessHeap;
lpPalette := HeapAlloc(hHeap, 0, sizeof(TLogPalette) + (nColors * sizeof(TPaletteEntry)));

lpPalette^.palVersion := $300;
lpPalette^.palNumEntries := nColors;

byRedMask := (1 shl pfd.cRedBits) - 1;
byGreenMask := (1 shl pfd.cGreenBits) - 1;
byBlueMask := (1 shl pfd.cBlueBits) - 1;

for i := 0 to nColors - 1 do begin
lpPalette^.palPalEntry[i].peRed := (((i shr pfd.cRedShift) and byRedMask) * 255) DIV byRedMask;
lpPalette^.palPalEntry[i].peGreen := (((i shr pfd.cGreenShift) and byGreenMask) * 255) DIV byGreenMask;
lpPalette^.palPalEntry[i].peBlue := (((i shr pfd.cBlueShift) and byBlueMask) * 255) DIV byBlueMask;
lpPalette^.palPalEntry[i].peFlags := 0;
end;

Palette := CreatePalette(lpPalette^);
HeapFree(hHeap, 0, lpPalette);

if (Palette <> 0) then begin
SelectPalette(DC, Palette, False);
RealizePalette(DC);
end;
end;
end;