PDA

View Full Version : Please, tell me my error?



buain
08-30-2006, 08:27 AM
Hello! I am beginner in OpenGL. I use Delphi+OpenGL. In my small programm one moment don't work.
In event KeyDown procedure InvalidateRect is. But all opengl programm don't work after this procedure. All scene don't change. Help me please.
This is programm:


program test_game;

uses
Windows,
Messages,
OpenGL;

var
y: Single;
wid, heig: cardinal;
modul_id: HWND;
hlr: HGLRC;
dc: HDC;
my_paint: TPaintStruct;
WindowClass: TWndClass;
Handle: HWND;
msg: TMsg;
default_screen: TDevMode;
old_wid, old_heig, old_bits: integer;

procedure DoExit;
begin
Halt;
end;

procedure SetDCPixelFormat(hd: HDC);
var
pfd: TPixelFormatDescriptor;
nPixelFormat: integer;
begin
FillChar(pfd, sizeof(pfd), 0);
pfd.dwFlags:=PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL;
nPixelFormat:=ChoosePixelFormat(hd, @pfd);
SetPixelFormat(hd, nPixelFormat, @pfd);
end;

procedure start;
begin
glViewPort(0, 0, wid, heig);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
glFrustum(-1, 1, -1, 1, 3, 10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
glTranslatef(0.0, 0.0, -5.0);
glRotatef(30.0, 1.0, 0.0, 0.0);
glRotatef(70.0, 0.0, 1.0, 0.0);
end;

function WindowProc(Hwn,msg,wpr,lpr: longint): longint; stdcall;
var
modes: TDevMode;
hdcscreen: HDC;
begin
result:=defwindowproc(hwn,msg,wpr,lpr);
if msg=wm_Create then
begin
y:=0;
hdcscreen:=GetDC(0);
old_wid:=GetDeviceCaps(hdcscreen, HORZRES);
old_heig:=GetDeviceCaps(hdcscreen, VERTRES);
old_bits:=GetDeviceCaps(hdcscreen, BITSPIXEL);
ReleaseDC(0, hdcscreen);
DeleteDC(hdcscreen);

modes.dmBitsPerPel:=32;
modes.dmPelsWidth:=800;
modes.dmPelsHeight:=600;
modes.dmDisplayFrequency:=0;
modes.dmFields:=DM_BITSPERPEL or DM_PELSWIDTH or
DM_PELSHEIGHT or DM_DISPLAYFLAGS;
wid:=modes.dmPelsWidth;
heig:=modes.dmPelsHeight;
ChangeDisplaySettings(Modes, CDS_FULLSCREEN);

dc:=GetDC(Hwn);
SetDCPixelFormat(dc);
hlr:=wglCreateContext(dc);
wglMakeCurrent(dc, hlr);
glClearColor(0.0,0.0,0.0,1.0);
glEnable(GL_DEPTH_TEST);
//glEnable(GL_LIGHTING);
//glEnable(GL_LIGHT0);
//glEnable(GL_COLOR_MATERIAL);
ReleaseDC(Hwn, dc);
end;
if msg=wm_destroy then
begin
default_screen.dmPelsWidth:=old_wid;
default_screen.dmPelsHeight:=old_heig;
default_screen.dmFields:=DM_BITSPERPEL or DM_PELSWIDTH or
DM_PELSHEIGHT or DM_DISPLAYFLAGS;
default_screen.dmBitsPerPel:=old_bits;
default_screen.dmDisplayFrequency:=0;
ChangeDisplaySettings(default_screen, CDS_FULLSCREEN);

wglDeleteContext(hlr);
DeleteDC(dc);
DoExit;
end;
if msg=wm_Paint then
begin
dc:=BeginPaint(Hwn, my_paint);
start;
glRotatef(y,0.0,0.0,1.0);

glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glPointSize(10);
glEnable(GL_POINT_SMOOTH);
glPointSize(10);
glBegin(GL_POINTS);
glVertex3f(0,0,0);
glEnd;

glColor3f(0.4,0.4,0.0);
glBegin(GL_QUADS);
glNormal3f(0,-1,0);
glVertex3f(-1,-1,1);
glVertex3f(-1,-1,-1);
glVertex3f(1,-1,-1);
glVertex3f(1,-1,1);
glEnd;
glColor3f(0.0,0.4,0.4);
glBegin(GL_QUADS);
glNormal3f(0,0,-1);
glVertex3f(-1,-1,-1);
glVertex3f(1,-1,-1);
glVertex3f(1,1,-1);
glVertex3f(-1,1,-1);
glEnd;
glBegin(GL_QUADS);
glNormal3f(1,0,0);
glVertex3f(1,-1,-1);
glVertex3f(1,1,-1);
glVertex3f(1,1,1);
glVertex3f(1,-1,1);
glEnd;
SwapBuffers(dc);
EndPaint(Hwn, my_paint);
wglMakeCurrent(dc, 0);
ReleaseDC(Hwn, dc);
end;
if msg=wm_KeyDown then
begin
if wpr=VK_ESCAPE then
DoExit;
y:=y+30;
InvalidateRect(hwn, nil, false);
end;
end;

begin
modul_id:=GetModuleHandle(nil);
WindowClass.style:=CS_HRedraw or CS_VRedraw;
WindowClass.lpfnWndProc:=@windowproc;
WindowClass.hInstance:=modul_id;
WindowClass.lpszClassName:='main';
WindowClass.hCursor:=LoadCursor(0, IDC_ARROW);

RegisterClass(WindowClass);
Handle:=CreateWindowEx(WS_EX_TOPMOST, 'main', '', ws_visible or ws_Popup or WS_MAXIMIZE, cw_UseDefault, cw_UseDefault, cw_UseDefault, cw_UseDefault,0,0,modul_id,nil);
ShowWindow(Handle, SW_Show);
UpdateWindow(Handle);

while (GetMessage(msg,0,0,0)) do
begin
translatemessage(msg);
dispatchmessage(msg);
end;
end.

Komat
08-30-2006, 01:04 PM
Actually your program will not draw anything after first wm_Paint message is processed because of the wglMakeCurrent(dc, 0) line. The InvalidateRect simply shows this problem by clearing the window.

buain
08-30-2006, 08:43 PM
thank you! I understand my error!