1
$\begingroup$

I have a work thread function which updates the value of a float called scale:

    DWORD WINAPI WorkThreadFn(LPVOID lpParam)
{
    MyWin* pWin = (MyWin*)lpParam;
    while (pWin->running)
    {
        //pWin->scale = pWin->scale - 0.001;
        pWin->scale = 0.5;
        SendMessage(pWin->hwnd, WM_PAINT, NULL, NULL);
        Sleep(1);
    }
    return 0;
}

If I change the value of scale in this function, the WM_PAINT draws my triangle to the correct scale. But if I uncomment the section which increments the scale, then remove the line setting the scale, the scale of the triangle remains the same, despite the fact I can see from the window procedure that the value of scale is decreasing.

Code for Window procedure:

    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    MyWin* pThis = NULL;
    if (uMsg == WM_CREATE)
    {
        CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;
        pThis = (MyWin*)pCreate->lpCreateParams;
        SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);
    }
    else
    {
        pThis = (MyWin*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
    }
..
switch(uMsg) {
case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        RECT region = ps.rcPaint;
        glClearColor(0.62, 0.3, 0.5, 1.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        pThis->glUniform1f(pThis->scaleLoc, pThis->scale);
        
        glBindVertexArray(vaNames[0]);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        glBindVertexArray(0);
        SwapBuffers(hdc);

And also here is the vertex shader code

in vec3 in_Position;
in vec3 in_Color;
out vec3 ex_Color;

uniform float scale;

void main(void)
{
    gl_Position = vec4(in_Position * scale, 1.0);
    ex_Color = in_Color;
}

I'm sure I'm doing something stupid here, can anybody help?

Edit (forgot to add the main function):

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
    MyWin win(hInstance, nCmdShow);

    
    DWORD ThreadID;
    HANDLE hThread;
    hThread = CreateThread(NULL, 0, WorkThreadFn, &win, 0, &ThreadID);

    win.MsgLoop();

    if (hThread != NULL)
        CloseHandle(hThread);

    return 0;
}
$\endgroup$
1
  • $\begingroup$ Ok so, I discovered that if I resize the window the expected change in the triangle size takes place. Which I guess, means that windows does not like the way I am sending WM_PAINT messages to the window procedure. Still working on it. $\endgroup$
    – lewis
    Jan 17, 2023 at 2:43

1 Answer 1

1
$\begingroup$

I can send WM_PAINT messages with SendMessage or PostMessage, but the window wont be redrawn. To redraw the window I have to call InvalidateRect or InvalidateRgn. You can also use RedrawWindow.

In the end that wasn't the problem though. I was calling glViewport on WM_SIZE, which for some season unknown to me means that WM_PAINT wasn't being processed properly, but the window was drawing correctly during resizing.

I also changed my message loop, to clear the message queue before redrawing the window, which also seems to speed things up a lot.

Edit: Ok, I know why glViewport in WM_SIZE was causing issues now. I wasn't returning 0 after processing that message, and MSDN docs mentions this. After making this change, it all works as expected.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.