opengl game programming book example.

Started by
3 comments, last by gsamour 12 years, 6 months ago
hi,
i recently bought a book called opengl game programming. i typed in the example started at page 49. i changed the opengl header files to glut.h because i lost the accompany cd. it compile and run just fine but i can not close it. i clicked the x box in the upper right corner but the window just wont close. what is wrong with it? i had to restart my computer to get rid of the program. one thing is that after i clicked the x box to close it, the triangle stop rotating.

my system is window 7, 64 bits

my program is as follow:

thanks

#define WIN32_LEAN_AND_MEAN

#include <Windows.h>
#include "GL/glut.h"

float angle = 0.0f;
HDC g_HDC;

void SetUpPixelFormat(HDC hDC) {
int nPixelFormat;
static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
0,0,0,0,0,0,
0,0,0,0,0,0,0,
16,
0,0,
PFD_MAIN_PLANE,
0,0,0,0};
nPixelFormat = ChoosePixelFormat(hDC, &pfd);
SetPixelFormat(hDC, nPixelFormat, &pfd);
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
static HGLRC hRC;
static HDC hDC;
char string[] = "Hello , world";
int width, height;

switch (message) {
case WM_CREATE:
hDC = GetDC(hwnd);
g_HDC = hDC;
SetUpPixelFormat(hDC);
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
return 0;

case WM_CLOSE:
wglMakeCurrent(hDC, NULL);
wglDeleteContext(hRC);
return 0;

case WM_SIZE:
height = HIWORD(lParam);
width = LOWORD(lParam);

if (height == 0)
height = 1;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0f, (GLfloat) width/ (GLfloat) height, 1.0f, 10000.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

return 0;
default: break;}
return (DefWindowProc(hwnd, message, wParam, lParam));
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
// MessageBox(NULL, "\tHello, world", "MY first windows application", NULL);

WNDCLASSEX windowClass;
HWND hwnd;
MSG msg;
bool done;

windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = WndProc;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = hInstance;
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.hbrBackground= NULL;
windowClass.lpszMenuName = NULL;
windowClass.lpszClassName= "MyClass";
windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);

if (!RegisterClassEx(&windowClass))
return 0;
hwnd = CreateWindowEx(NULL, "MYClass", "The Opengl Window application",
WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_SYSMENU | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
100, 100,
400, 400,
NULL, NULL,
hInstance,
NULL);
if (!hwnd)
return 0;

ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
done = false;

while (!done) {
PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE);
if (msg.message == WM_QUIT)
done = true;
else {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

angle += 1.0f;
if (angle >= 360.0f)
angle = 0.0f;
glTranslatef(0.0f, 0.0f, -5.0f);
glRotatef(angle, 0.0f, 0.0f, 1.0f);

glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLES);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glEnd();

SwapBuffers(g_HDC);

TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
Advertisement
I'm pretty sure when you handle WM_CLOSE you should also call DestroyWindow(hwnd)... then you should handle WM_DESTROY and call PostQuitMessage(0) there.

Here's my minimal WndProc:


LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
hi,
thanks for your reply.
i will try it tomorrow.
bye
hi gsamour:

i followed you recommendations and i can now close the window after running it. but the program seemed not to terminate. after i closed it, a few minutes later, my Norton 360 warned me about the program used a lot of cpu. why is that. here is the code that i changed according to your suggestions.


LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
static HGLRC hRC;
static HDC hDC;
char string[] = "Hello , world";
int width, height;

switch (message) {
case WM_CREATE:
hDC = GetDC(hwnd);
g_HDC = hDC;
SetUpPixelFormat(hDC);
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
return 0;

case WM_CLOSE:
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hRC);
DestroyWindow(hwnd);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;

case WM_SIZE:
height = HIWORD(lParam);
width = LOWORD(lParam);

if (height == 0)
height = 1;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0f, (GLfloat) width/ (GLfloat) height, 1.0f, 10000.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

return 0;
default: break;}
return (DefWindowProc(hwnd, message, wParam, lParam));
}
Can you post your WinMain() ?

This topic is closed to new replies.

Advertisement