Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

johnc82

Closing window problem

This topic is 5512 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi... My code:

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>

HWND g_hwnd = NULL;
static char clsn[] = "ogl_win32_test";

float angle = 0.0f;
HDC g_hdc = NULL;

void SetupPixelFormat(HDC);
LRESULT CALLBACK MsgProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
		   LPSTR lpstrCmdLine, int iCmdShow)
{
	WNDCLASSEX wcx;

	wcx.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wcx.cbSize = sizeof(WNDCLASSEX);
	wcx.lpfnWndProc = MsgProc;
	wcx.lpszClassName = clsn;
	wcx.hInstance = hInstance;

	wcx.cbClsExtra = 0;
	wcx.cbWndExtra = 0;
	wcx.lpszMenuName = NULL;

	wcx.hbrBackground = NULL;
	wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wcx.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
	
	if(!RegisterClassEx(&wcx))
	{
		return -1;
	}

	g_hwnd = CreateWindowEx(NULL,
				clsn,
				"OpenGL Window Test",
				WS_OVERLAPPEDWINDOW,
				0,
				0,
				CW_USEDEFAULT,
				CW_USEDEFAULT,
				NULL,
				NULL,
				hInstance,
				NULL);

	if(!g_hwnd)
	{
		return -2;
	}
	
	ShowWindow(g_hwnd, iCmdShow);
	UpdateWindow(g_hwnd);

	MSG msg;
	
	bool done = false;
	
	while(!done)
	{
        PeekMessage(&msg, g_hwnd, 0, 0, PM_REMOVE);

		if(msg.message == WM_QUIT)
		{
			done = true;
		}
		else
		{
			
               glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
                        glLoadIdentity();

			angle = angle + 0.1f;

			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(0.0f, 0.0f, 0.0f);
				glVertex3f(1.0f, 0.0f, 0.0f);
				glVertex3f(1.0f, 1.0f, 0.0f);
			glEnd();

			SwapBuffers(g_hdc);

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}	
	//problem unable to unregister the class

	if(!UnregisterClass(clsn, hInstance))
	{
		return -3;
	}

	DestroyWindow(g_hwnd);

	return (int)msg.wParam;
}

void SetupPixelFormat(HDC hdc)
{
	int iPixelFormat;

	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 };

		iPixelFormat = ChoosePixelFormat(hdc, &pfd);

		SetPixelFormat(hdc, iPixelFormat, &pfd);

}

LRESULT CALLBACK MsgProc(HWND hwnd, UINT imsg, WPARAM wparam, 
			 LPARAM lparam)
{
	static HGLRC hrc;
	static HDC hdc;

	//char string[] = "Yahoo, OpenGL Window";

	int width, height;


	switch(imsg)
	{
		case WM_CREATE:
			{
				hdc = GetDC(hwnd);
				g_hdc = hdc;

				SetupPixelFormat(hdc);

				hrc = wglCreateContext(hdc);
				wglMakeCurrent(hdc, hrc);

				return 0;
				//break;

			}
		case WM_PAINT:
			{
				
				return 0;
				//break;

			}
		case WM_CLOSE:
			{
				wglMakeCurrent(hdc, NULL);
				wglDeleteContext(hrc);

				PostQuitMessage(0);
				return 0;
				break;
			}
		case WM_DESTROY:
			{
				wglMakeCurrent(hdc, NULL);
				wglDeleteContext(hrc);

				PostQuitMessage(0);
				return 0;
				break;
			}
		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, 1000.0f);

				glMatrixMode(GL_MODELVIEW);
				glLoadIdentity();

				return 0;
				//break;

			}
		default:
			{
			
               return DefWindowProc(hwnd , imsg, wparam, lparam);
				//break;

			}
	}

	return DefWindowProc(hwnd , imsg, wparam, lparam);
}
My problem: 1) Unable to unregister the class.... window class(WNDCLASSEX) 2) Sometime when i close the programe hang... the programe just hang there(forzen) Thanks :-)

Share this post


Link to post
Share on other sites
Advertisement
well this mean:


DestroyWindow(g_hwnd);

if(!UnregisterClass(clsn, hInstance))
{
return -3;
}



the DestroyWindow() fucntion come first before the UnregisterClass() function.....

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
in the PeekMessage call, change g_hwnd to a NULL. WM_QUIT is a non-window specific msg so your app will never see it in the PeekMessage call if you specify an HWND.

in an exe, you don''t need to call UnregisterClass. the window classes that an exe registers are automatically destroyed when the app terminates.

don''t call DestroyWindow outside of the message processing function. the window should have already been destroyed by the time the WM_QUIT msg has been received.

put a DestroyWindow call in the processing of the WM_CLOSE msg in place of the PostQuitMessage call. this will generate the WM_DESTROY msg and the processing of the WM_DESTROY msg is where the PostQuitMessage call is needed. in fact, you don''t even need the DestroyWindow call if you would just let the DefWindowProc do its thing instead of returning.

change all those "return 0" statements in the msg processing function to breaks. you need to let the DefWindowProc function do its thing. there is rarely any reason not to call DefWindowProc for a received msg. not calling DefWindowProc doesn''t really buy you any performance gain worthwhile and there are many things which get handled automatically if you would just let the msg be processed by DefWindowProc. its best to try and minimize the overall msg flow instead. there''s internal variable processing that goes on within DefWindowProc that needs to happen in order for the system to handle the normal windows functionality.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!