Hello,
I'm using winapi to create a window for my opengl rendering. My simple opengl scene shows up fine, but when dragging the title bar, my window does not move. Also, moving the mouse cursor near the window borders does not change the cursor to a resizing cursor and thus the window can not be resized. On top of that, clicking the minimize/maximize/close buttons of the window does not do anything.
My window does react to wm_input events though.
My game loop looks like this:
Application* m_app = new Application();
// while app is running
while(m_app->isRunning())
{
// run
m_app->iterateProcessingLoop();
// sleep for a few msecs
m_app->sleep();
}
Within iterateProcessingLoop(), I call my message pump:
m_window->pumpSystemEvents();
which is just your run-of-the-mill message pump:
void WindowsWindow::pumpSystemEvents()
{
//std::cout << "[WindowsWindow::pumpSystemEvents] called." << std::endl;
//message handling
while(PeekMessage(&m_msg,nullptr,0,0,PM_REMOVE))
{
TranslateMessage(&m_msg);
DispatchMessage(&m_msg);
}
}
I create my window like this:
WindowsWindow::WindowsWindow()
{
m_hWnd = nullptr; // Holds Our Window Handle
m_hInstance = nullptr;
WNDCLASS wc; // Windows Class Structure
DWORD dwExStyle; // Window Extended Style
DWORD dwStyle; // Window Style
RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
WindowRect.left=(long)0; // Set Left Value To 0
WindowRect.right=(long)getWindowWidth(); // Set Right Value To Requested Width
WindowRect.top=(long)0; // Set Top Value To 0
WindowRect.bottom=(long)getWindowHeight(); // Set Bottom Value To Requested Height
m_hInstance = GetModuleHandle(nullptr); // Grab An Instance For Our Window
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Move, And Own DC For Window
wc.lpfnWndProc = (WNDPROC) &WindowsWindow::WndProc; // WndProc Handles Messages
wc.cbClsExtra = 0; // No Extra Class Data
wc.cbWndExtra = 0; // No Extra Window Data
wc.hInstance = m_hInstance; // Set The Instance
wc.hIcon = LoadIcon(nullptr, IDI_WINLOGO); // Load The Default Icon
wc.hCursor = LoadCursor(nullptr, IDC_ARROW); // Load The Arrow Pointer
wc.hbrBackground = nullptr; // No Background Required For GL
wc.lpszMenuName = nullptr; // We Don't Want A Menu
wc.lpszClassName = "Cow2019WindowsWindow"; // Set The Class Name
if (!RegisterClass(&wc)) // Attempt To Register The Window Class
{
MessageBox(nullptr,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return;
//return FALSE; // Exit And Return FALSE
}
dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style
AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size
if (!(m_hWnd = CreateWindowEx( dwExStyle, // Extended Style For The Window
"Cow2019WindowsWindow\0", // Class Name
"Cow2019\0", // Window Title
WS_CLIPSIBLINGS | // Required Window Style
WS_CLIPCHILDREN | // Required Window Style
dwStyle, // Selected Window Style
0, 0, // Window Position
WindowRect.right-WindowRect.left, // Calculate Adjusted Window Width
WindowRect.bottom-WindowRect.top, // Calculate Adjusted Window Height
nullptr, // No Parent Window
nullptr, // No Menu
m_hInstance, // Instance
this))) // Pass self to WM_CREATE
{
MessageBox(nullptr,"Window Creation Error.\0","ERROR\0",MB_OK|MB_ICONEXCLAMATION);
return;
}
}
Then I create the opengl context like this:
WindowsOpenGLContext::WindowsOpenGLContext(void* p_hWnd)
{
m_hRC = nullptr;
m_hDC = nullptr;
m_hWnd = (HWND)p_hWnd;
// device context
m_hDC = GetDC(m_hWnd); // Get the device context for our window
PIXELFORMATDESCRIPTOR pfd; // Create a new PIXELFORMATDESCRIPTOR (PFD)
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)); // Clear our PFD
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); // Set the size of the PFD to the size of the class
pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW; // Enable double buffering, opengl support and drawing to a window
pfd.iPixelType = PFD_TYPE_RGBA; // Set our application to use RGBA pixels
pfd.cColorBits = 32; // Give us 32 bits of color information (the higher, the more colors)
pfd.cDepthBits = 32; // Give us 32 bits of depth information (the higher, the more depth levels)
pfd.iLayerType = PFD_MAIN_PLANE; // Set the layer of the PFD
//set pixel format
int nPixelFormat = ChoosePixelFormat(m_hDC, &pfd); // Check if our PFD is valid and get a pixel format back
if(nPixelFormat == 0) // If it fails
{
MessageBox(nullptr,"Unable to choose pixel format.\0","ERROR\0",MB_OK|MB_ICONEXCLAMATION);
return;
}
bool bResult = SetPixelFormat(m_hDC, nPixelFormat, &pfd); // Try and set the pixel format based on our PFD
if(!bResult) // If it fails
{
MessageBox(nullptr,"Unable to set pixel format.\0","ERROR\0",MB_OK|MB_ICONEXCLAMATION);
return;
}
//temporary openGL 2.1 context
HGLRC tempOpenGLContext = wglCreateContext(m_hDC); // Create an OpenGL 2.1 context for our device context
wglMakeCurrent(m_hDC, tempOpenGLContext); // Make the OpenGL 2.1 context current and active
//fetch context creation procedure
PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = NULL;
wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC) wglGetProcAddress("wglCreateContextAttribsARB");
if(wglCreateContextAttribsARB == nullptr)
{
MessageBox(nullptr,"Initialization Failed - could not fetch wglCreateContextAttribsARB.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return;
}
//attributes of 3.x/4.x context
int attributes[] = {
WGL_CONTEXT_MAJOR_VERSION_ARB, m_openGLMajorVersion, // Set the MAJOR version of OpenGL
WGL_CONTEXT_MINOR_VERSION_ARB, m_openGLMinorVersion, // Set the MINOR version of OpenGL
WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB | WGL_CONTEXT_DEBUG_BIT_ARB, // Set our OpenGL context to be forward compatible & debug
0
};
//actually create the context
m_hRC = wglCreateContextAttribsARB(m_hDC, NULL, attributes); // Create and OpenGL 3.x context based on the given attributes
wglMakeCurrent(NULL, NULL); // Remove the temporary context from being active
wglDeleteContext(tempOpenGLContext); // Delete the temporary OpenGL 2.1 context
wglMakeCurrent(m_hDC, m_hRC); // Make our OpenGL 4.2 context current
}
Then I show the window:
void WindowsWindow::showWindow()
{
//show window
ShowWindow(m_hWnd,SW_SHOW); // Show The Window
SetForegroundWindow(m_hWnd); // Slightly Higher Priority
SetFocus(m_hWnd); // set keyboard focus
UpdateWindow(m_hWnd); // update
}