Original Post
Hey guys, I'm working on my opengl window template. For some reason my exiting the program is really slow, it's usually instantly. I think it's because it has to delete the device contect and stuff, but I've seen it exit faster on other opengl programs. Can anyone let me know what's wrong? It also might be the way in which my message pump is made, but I'm not sure, have a look: Ignore the fullscreen prompt function, I dont think it has anything to do with it. Thanks, it's just that I've never seen any other pump so I can't compare. Thanks again. [Edited by - blankdev on September 5, 2005 3:40:53 PM]
#define WIN32_LEAN_AND_MEAN // trim the excess fat from Windows
#include <windows.h> // standard Windows app include
#include <gl/gl.h> // standard OpenGL include
#include <gl/glu.h> // OpenGL utilities
#include <gl/glaux.h> // OpenGL auxiliary functions
#include "glfuncs.h"
HDC g_HDC; // global device context
bool fullscreen = true; // start off in windowed mode
int width, height; // window width and height
// end of global variable declarations and preprocessor directives
void cycle(HDC ghdc, MSG gmsg)
{
// clear screen and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); // reset modelview matrix
// Do Rendering here
// render functions, then...
SwapBuffers(ghdc); // ...bring back buffer to foreground
TranslateMessage(&gmsg); // translate and dispatch to event queue
DispatchMessage(&gmsg);
}
// the Windows Procedure event handler
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HGLRC hRC; // rendering context
static HDC hDC; // device context
switch(message)
{
case WM_CREATE: // window is being created
hDC = GetDC(hwnd); // get current window's device context
g_HDC = hDC;
SetupPixelFormat(hDC); // call your pixel format setup function
// create rendering context and make it current
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
return 0;
break;
case WM_CLOSE: // windows is closing
// deselect rendering context and delete it
wglMakeCurrent(hDC, NULL);
wglDeleteContext(hRC);
// send WM_QUIT to message queue
PostQuitMessage(0);
return 0;
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
case WM_SIZE:
UpdateWindow(hwnd);
height = HIWORD(lParam); // retrieve width and height
width = LOWORD(lParam);
if (height == 0) // don't want a divide by zero
height = 1;
// reset the viewport to new dimensions
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);// set projection matrix
glLoadIdentity(); // reset projection matrix
// calculate aspect ratio of window
gluPerspective(45.0f, (GLfloat)width/(GLfloat)height,1.0f,1000.0f);
glMatrixMode(GL_MODELVIEW); // set modelview matrix
glLoadIdentity(); // reset modelview matrix
return 0;
break;
case WM_CHAR:
switch (toupper(wParam))
{
case VK_ESCAPE:
// send WM_QUIT to message queue
PostQuitMessage(0);
return 0;
}
return 0;
break;
default:
break;
}
return (DefWindowProc(hwnd, message, wParam, lParam));
}
// the main Windows entry point
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX windowClass; // window class
HWND hwnd; // window handle
MSG msg; // message
RECT windowrect;
DWORD dwExstyle; // window extended style
DWORD dwstyle;
int bits = 32;
FullScreenPrompt(&fullscreen, "Would you like to run in fullscreen mode?", "FULLSCREEN");
width = 800;
height = 600;
// fill out the window class structure
windowClass.cbSize = sizeof(WNDCLASSEX); // size of our structure
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = WndProc; // name of your wndproc loop
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = hInstance;
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); // default icon
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW); // default arrow
windowClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); // white background
windowClass.lpszMenuName = NULL; // no menu
windowClass.lpszClassName = "ClassName"; // classname
windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO); // windows logo small icon
// register the windows class
if (!RegisterClassEx(&windowClass))
return 0;
if (fullscreen)
{
DEVMODE dmScreenSettings;
memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
dmScreenSettings.dmSize = sizeof(dmScreenSettings);
dmScreenSettings.dmPelsWidth = width; // screen width
dmScreenSettings.dmPelsHeight = height; // screen height
dmScreenSettings.dmBitsPerPel = bits; // bits per pixel
dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
{
// setting display mode failed, switch to windowed
MessageBox(NULL, "Switch to FullScreen Failed", NULL, MB_OK);
fullscreen = false;
}
}
if (fullscreen) // are we still in full-screen mode?
{
dwExstyle = WS_EX_APPWINDOW; // window extended style
dwstyle = WS_POPUP; // windows style
ShowCursor(TRUE); // show cursor
}
else if (!fullscreen)
{
dwExstyle = NULL;
// window extended style
dwstyle = WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_SYSMENU | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
}
windowrect.top = (long)0;
windowrect.left = (long)0;
windowrect.bottom = (long)height;
windowrect.right = (long)width;
// readjust your window
AdjustWindowRectEx(&windowrect, dwstyle, FALSE, dwExstyle);
// class registered, so now create our window
hwnd = CreateWindowEx(NULL, // extended style
"ClassName", // class name
"Taskbar Text", // app name
dwstyle | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, // style
0, 0, // x,y coordinate
windowrect.right - windowrect.left,// width
windowrect.bottom - windowrect.top,// height
NULL, // handle to parent
NULL, // handle to menu
hInstance, // application instance
NULL); // no extra params
// check if window creation failed (hwnd would equal NULL)
if (!hwnd)
return 0;
ShowWindow(hwnd, SW_SHOW); // display the window
UpdateWindow(hwnd); // update the window
// main message loop
while (TRUE)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
cycle(g_HDC, msg);
}
}
return msg.wParam;
}