RobeeZ
RobeeZ

Reputation: 95

Windows Creation Error - Maybe event handling

I'm trying to create an OpenGL context with WinAPI, it works great, but I think I messed up something, because when I click on the upper bar (where close button, minimize button is), it calls the ResizeWindow function somehow...Image showing this

Here is my main function:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    GLWindow *window = new GLWindow();
    Event *evt = new Event();

    window->Create("OpenGL Window", 800, 600, 32);

    while (window->IsRunning())
    {
        evt->Clear();
        while (window->DispatchEvent(evt))
        {
        }
        Render();
        window->Present();
    }

    delete evt;
    delete window;
}

Event is just a class with a map for containing event params. I removed every event handling in WndProc so it couldn't be a problem

Window creation code:

bool GLWindow::Create(char* title, int width, int height, int bits)
{
    GLuint PixelFormat;
    WNDCLASS wClass;
    DWORD dwExStyle;
    DWORD dwStyle;
    RECT WindowRect;
    WindowRect.left = (long)0;
    WindowRect.top = (long)0;
    WindowRect.right = (long)width;
    WindowRect.bottom = (long)height;

    hInstance = GetModuleHandle(NULL);
    wClass.cbClsExtra = 0;
    wClass.cbWndExtra = 0;
    wClass.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
    wClass.hCursor = LoadCursor(hInstance, IDC_ARROW);
    wClass.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
    wClass.hInstance = hInstance;
    wClass.lpfnWndProc = &GLWindow::WndProc;
    wClass.lpszClassName = "GL";
    wClass.lpszMenuName = NULL;
    wClass.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC;

    dwStyle = WS_OVERLAPPEDWINDOW;
    dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;

    if (!RegisterClass(&wClass))
    {
        MessageBox(NULL, "Error! Cannot register window class!", "ERROR", MB_OK);
        return 0;
    }

    AdjustWindowRectEx(&WindowRect, dwStyle, false, dwExStyle);

    if (!(hWnd = CreateWindowEx(
        dwExStyle,
        "GL",
        "Title",
        WS_CLIPSIBLINGS | WS_CLIPCHILDREN | dwStyle,
        0,
        0,
        WINDOW_WIDTH,
        WINDOW_HEIGHT,
        NULL,
        NULL,
        hInstance,
        NULL)))
    {
        KillWindow();
        MessageBox(NULL, "Window creation error.", "Error", MB_OK);
        return false;
    }

    static PIXELFORMATDESCRIPTOR pfd =
    {
        sizeof(PIXELFORMATDESCRIPTOR),
        1,                                                                              //Version number
        PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_TYPE_RGBA,     //Formats
        bits,                                                                           //color depth
        0, 0, 0, 0, 0, 0,                                                               //color bits ignored
        0,                                                                              //no alpha buffer
        0,                                                                              //shift bit ignored
        0,                                                                              //no accumulation buffer
        0, 0, 0, 0,                                                                     //accumulation bits ignored
        16,                                                                             //16bit Zbuffer (depth buffer)
        0,                                                                              //no stencilbuffer
        0,                                                                              //no auxiliary buffer
        PFD_MAIN_PLANE,                                                                 //main drawing layer
        0,                                                                              //reserved
        0, 0, 0                                                                         //layer masks ignored
    };

    if (!(hDC = GetDC(hWnd)))
    {
        KillWindow();
        MessageBox(NULL, "Can't create OpenGL Window", "Error", MB_OK);
        return false;
    }

    if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd)))
    {
        KillWindow();
        MessageBox(NULL, "Can't find suitable PixelFormat", "Error", MB_OK);
        return false;
    }

    if (!SetPixelFormat(hDC, PixelFormat, &pfd))
    {
        KillWindow();
        MessageBox(NULL, "Can't set pixelformat", "Error", MB_OK);
        return false;
    }

    if (!(hRC = wglCreateContext(hDC)))
    {
        KillWindow();
        MessageBox(NULL, "Can't create OpenGL context", "Error", MB_OK);
        return false;
    }

    if (!wglMakeCurrent(hDC, hRC))
    {
        KillWindow();
        MessageBox(NULL, "Can't activate OGL context", "Error", MB_OK);
        return false;
    }

    ShowWindow(hWnd, SW_SHOW);
    SetForegroundWindow(hWnd);
    SetFocus(hWnd);
    ResizeWindow(800, 600);

    if (!InitGL())
    {
        KillWindow();
        MessageBox(NULL, "Failed initialization", "Error", MB_OK);
        return false;
    }

    return true;
}

Dispatching events:

bool GLWindow::DispatchEvent(Event* evt)
{
    MSG msg;

    bool wasThereAnyEvent = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);

    if (wasThereAnyEvent)
    {

        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return wasThereAnyEvent;
}

I removed event buffer filling, so it just processes every message.

WndProc:

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch (msg)
        {
            case WM_CREATE:
                break;
            case WM_DESTROY:
            case WM_QUIT:
            case WM_CLOSE:
                isRunning = false;
                PostQuitMessage(0);
                return 0;
            case WM_ACTIVATE:
            {
                isActiveWindow = !HIWORD(wParam);
                return 0;
            }
            case WM_SYSCOMMAND:
                switch (wParam)
                {
                    case SC_SCREENSAVE:
                    case SC_MONITORPOWER:
                        return 0;
                }
            case WM_SIZE:
                ResizeWindow(LOWORD(lParam), HIWORD(lParam));
                return 0;
            }

        return DefWindowProc(hWnd, msg, wParam, lParam);
    }

Upvotes: 0

Views: 100

Answers (1)

Cyclonecode
Cyclonecode

Reputation: 30001

I think the problem is that you're not using any breaks inside you switch statement (of course they won't be needed in every case). When you receive a WM_SYSCOMMAND which not contains SC_SCREENSAVE or SC_MONITORPOWER the next case statement, in this case WM_SIZE, gets executed.

    switch (msg)
    {
        case WM_CREATE:
            break;
        case WM_DESTROY:
        case WM_QUIT:
        case WM_CLOSE:
            isRunning = false;
            PostQuitMessage(0);
            return 0;
            break;
        case WM_ACTIVATE:
        {
            isActiveWindow = !HIWORD(wParam);
            return 0;
        } break;
        case WM_SYSCOMMAND:
        {
            switch (wParam)
            {
                case SC_SCREENSAVE:
                case SC_MONITORPOWER:
                    return 0;
            }
        } break;
        case WM_SIZE:
            ResizeWindow(LOWORD(lParam), HIWORD(lParam));
            return 0;
      }

Upvotes: 1

Related Questions