[source lang="cpp"]/*** main.cpp*/# include <stdlib.h># include <time.h># include <windows.h># if defined(_WINDOWS_)# include <gl\gl.h># endif /* defined(_WINDOWS_) */LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);HDC hdc;PIXELFORMATDESCRIPTOR pfd;int format;HGLRC hglrc;/*** entry point*/int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){ WNDCLASS WndClass; HWND hWnd; MSG msg; WndClass.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW; WndClass.lpfnWndProc = WndProc; WndClass.cbClsExtra = 0; WndClass.cbWndExtra = 0; WndClass.hInstance = hInstance; WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); WndClass.hbrBackground = NULL; WndClass.lpszMenuName = NULL; WndClass.lpszClassName = "greyscale"; if(RegisterClass(&WndClass) == 0) { MessageBox(NULL, "Unable to register the window's class.", "", MB_OK | MB_ICONERROR); return 0; } hWnd = CreateWindow("greyscale", "greyscale", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 800, 600, NULL, NULL, hInstance, NULL); if(!hWnd) { MessageBox(NULL, "Failed to create window.", "", MB_OK | MB_ICONERROR); return 0; } hdc = GetDC(hWnd); ZeroMemory(&pfd, sizeof(PIXELFORMATDESCRIPTOR)); pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion = 1; pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 24; format = ChoosePixelFormat(hdc, &pfd); SetPixelFormat(hdc, format, &pfd); hglrc = wglCreateContext(hdc); if(!hglrc) { MessageBox(NULL, "Failed to create context for OpenGL.", "", MB_OK | MB_ICONERROR); return 0; } if(wglMakeCurrent(hdc, hglrc) == FALSE) { MessageBox(NULL, "Unable to change the current context.", "", MB_OK | MB_ICONERROR); return 0; } ReleaseDC(hWnd, hdc); ShowWindow(hWnd, nShowCmd); UpdateWindow(hWnd); glClearColor(0.0f, 1.0f, 0.0f, 0.0f); for(;;) { if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if(msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } glClear(GL_COLOR_BUFFER_BIT); glFlush(); } wglMakeCurrent(NULL, NULL); wglDeleteContext(hglrc); return msg.wParam;}/*** message handler*/LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){ switch(msg) { case WM_NULL: { return 0; } case WM_DESTROY: { PostQuitMessage(0); return 0; } case WM_CLOSE: { if(MessageBox(hWnd, "Are you sure you want to exit?", "", MB_YESNO | MB_ICONQUESTION) == IDYES) DestroyWindow(hWnd); return 0; } } return DefWindowProc(hWnd, msg, wParam, lParam);}[/source]
This has been bugging me for a while so I thought I might as well see if it's happened to anyone else or see if someone can tell me how badly I'm going wrong.
I've finally managed to get OpenGL rendering within Win32 (and not through the other third party libraries like GLUT).
My issue though is that when I actually include rendering commands in the main loop the window appears to become unresponsive. I say appears because I can drag it around occasionally and it takes ages to respond.
The issue disappears with I remove the Clear and Flush commands in the loop.
I've added a timer (not shown above) to limit the speed at when the rendering runs and the issue then goes away. The only conclusion I can come to is that the OpenGL commands take to long and cause PeekMessage to miss messages in the queue (that was all I had, I only started Win32 yesterday so I'm probably wrong).
The code compiles correctly and produces no errors in Visual Studio. What exactly is causing this behaviour?
Edited by BinaryPhysics, 08 July 2012 - 05:34 PM.







