LuckyHorseCoin
LuckyHorseCoin

Reputation: 61

openGL render works in WS_OVERLAPPEDWINDOW, but not in WS_POPUP

I'm trying to create a very simple openGL window in windows. Problem is, openGL render works when window is created with WS_OVERLAPPEDWINDOW, but when I change that to WS_POPUP for fullscreen, it only displays a fullscreen white/blank window.

here is main:

int main(int argc, char **argv)
{
    hInstance = GetModuleHandle(NULL);
    if (hInstance == NULL)
    {
        return 1;
    }

    int retCode = createWindow(hInstance);
    if (retCode != 0)
    {
        printf("Window Creation Failed!\n");
        std::cin.ignore();
        return -1;
    }

    retCode = createOpenGLContext(hWindow);
    if (retCode != 0)
    {
        printf("GLRenderContext creation failed!\n");
        std::cin.ignore();
        return -1;
    }

    printf("Initialization Complete!\n");

    MSG message;
    while (true)
    {
        if (hWindow == NULL)
        {
            printf("hWindow is null, skipping!\n");
            continue;
        }

        PeekMessage(&message, NULL, 0, 0, PM_REMOVE);
        TranslateMessage(&message);
        DispatchMessage(&message);
    }

    return 0;
}

here is createWindow:

int createWindow(HINSTANCE hInstance)
{
////////////////CREATE THE CLASS
WNDCLASS windowClass = {};
windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
windowClass.lpfnWndProc = windowProc;
windowClass.hCursor = LoadCursor(NULL, IDC_CROSS);
windowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
windowClass.hInstance = hInstance;
windowClass.lpszClassName = WINDOW_CLASSNAME;

////////////////REGISTER THE CLASS
RegisterClass(&windowClass);

////////////////CREATE THE WINDOW
hWindow = CreateWindowA(WINDOW_CLASSNAME, WINDOW_NAME, WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 
    0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
    nullptr, nullptr, hInstance, nullptr);

ShowWindow(hWindow, SW_SHOW);
UpdateWindow(hWindow);

if (hWindow != NULL)
{
    printf("hWindow is Created!\n");
    return 0;
}
else
{
    printf("hWindow creation failed!\n");
    return -1;
}
}

and finally, createOpenGLContext:

int createOpenGLContext(HWND hWindow)
{
hDeviceContext = GetDC(hWindow);
if (hDeviceContext == NULL)
{
    printf("Device context is null!\n");
    return -1;
}

///////////////////SET PIXEL FORMAT
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.iLayerType = PFD_MAIN_PLANE;

int chosenFormat = ChoosePixelFormat(hDeviceContext, &pfd);
if (SetPixelFormat(hDeviceContext, chosenFormat, &pfd) == TRUE)
{
    printf("PixelFormat set!\n");
}
else
{
    printf("PixelFormat couldn't be set!\n");
    return -1;
}

hGLRenderContext = wglCreateContext(hDeviceContext);
if (hGLRenderContext == NULL)
{
    char errorString[256];
    FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 0, errorString, 255, NULL);

    printf("RenderContext is null!\n");
    std::cout << errorString << "\n";
    return -1;
}

wglMakeCurrent(hDeviceContext, hGLRenderContext);

return 0;
}

Anyone have any idea why just changing WS_OVERLAPPEDWINDOW to WS_POPUP breaks it?

EDIT: Here is windowProc as requested:

LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
int x;
int y;

switch (uMsg)
{
case WM_MOUSEMOVE:
    x = LOWORD(lParam);
    y = HIWORD(lParam);

    //printf("Mouse Movement: %d, %d\n", x, y);
    break;

case WM_COMMAND:
{
    int wmId = LOWORD(wParam);
    // Parse the menu selections:
    switch (wmId)
    {
    case 105:
        DestroyWindow(hWnd);
        break;
    default:
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
}
break;
case WM_PAINT:
{
    printf("WM_PAINT message!\n");
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(hWnd, &ps);
    // TODO: Add any drawing code that uses hdc here...
    EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
    PostQuitMessage(0);
    break;
default:
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}

Upvotes: 1

Views: 555

Answers (1)

Daniel Sęk
Daniel Sęk

Reputation: 2769

You didn't show your WindowProc, but if your window shows different behavior depending on WS_POPUP vs WS_OVERLAPPENDWINDOW, first thing to check is if your code depends on WM_SIZE for view reconfiguration (glViewport, gluPerspective etc). If this is the case, call this code also just after initializing a window.

Upvotes: 1

Related Questions