4

I'm sort of new to rendering graphics with GDI...

I made a paint program, and it works fine, it's just that it causes a lot of annoying screen flickering. I will admit my paint code is not really optimized (lack of time), but it shouldn't be super inefficient either, so I'm puzzled.

What I'm basically doing is creating a compatible DC on init, then create a compatible bitmap. Then I select it into the compatible DC, and paint to the compatible DC. Then I use BitBlit() to copy it to the window hDC...

Could anyone tell me the possible causes for this screen tearing? EDIT: btw, screen flickering only occurs during the drawing of a path (before the path gets drawn to the hMemDC, it gets drawn to the hDC of the window)

Code samples: (EDIT: If you need to see any more code that you think is relevant, comment and I'll edit)


Path::DrawTo(HDC)

bool Path::DrawTo(HDC hDC)
{
    if(hDC == NULL || m_PointVector.size() <= 0) {
        return false;
    }

    switch (m_Tool) {
    case Tool_Pen:
        {
            Point2D p = m_PointVector.at(0);

            if(m_PointVector.size() > 1) {
                HPEN oldPen = (HPEN)SelectObject(hDC,m_hPen);

                MoveToEx(hDC, p.x, p.y, nullptr);

                for(UINT i = 1; i < m_PointVector.size(); ++i) {
                    p = m_PointVector.at(i);
                    LineTo(hDC,p.x,p.y);
                }

                SelectObject(hDC,oldPen);
                break;
            } //else

            SetPixel(hDC,p.x-1,p.y,m_Col);
            SetPixel(hDC,p.x,p.y,m_Col);
            SetPixel(hDC,p.x+1,p.y,m_Col);
            SetPixel(hDC,p.x,p.y-1,m_Col);
            SetPixel(hDC,p.x,p.y+1,m_Col);
            break;
        }
    case Tool_Line:
        {
            if(m_PointVector.size() > 1) {
                Point2D p = m_PointVector.at(0);
                HPEN oldPen = (HPEN)SelectObject(hDC,m_hPen);

                MoveToEx(hDC, p.x, p.y, nullptr);

                for(UINT i = 1; i < m_PointVector.size(); ++i) {
                    p = m_PointVector.at(i);
                    LineTo(hDC,p.x,p.y);
                }

                SelectObject(hDC,oldPen);
            }
            break;
        }
    case Tool_Ellipse:
        {
            if(m_PointVector.size() > 1) {
                HPEN oldPen = (HPEN)SelectObject(hDC,m_hPen);
                SelectObject(hDC,m_hBrush);

                Point2D p1 = m_PointVector.at(0);
                Point2D p2 = m_PointVector.at(1);

                if(p1.x > p2.x) {
                    int iTemp = p1.x;
                    p1.x = p2.x;
                    p2.x = iTemp;
                }
                if(p1.y > p2.y) {
                    int iTemp = p1.y;
                    p1.y = p2.y;
                    p2.y = iTemp;
                }

                Ellipse(hDC,p1.x,p1.y,p2.x,p2.y);

                SelectObject(hDC,oldPen);
            }
            break;
        }
    case Tool_Rectangle:
        {
            if(m_PointVector.size() > 1) {
                HPEN oldPen = (HPEN)SelectObject(hDC,m_hPen);
                SelectObject(hDC,m_hBrush);

                Point2D p1 = m_PointVector.at(0);
                Point2D p2 = m_PointVector.at(1);

                if(p1.x > p2.x) {
                    int iTemp = p1.x;
                    p1.x = p2.x;
                    p2.x = iTemp;
                }
                if(p1.y > p2.y) {
                    int iTemp = p1.y;
                    p1.y = p2.y;
                    p2.y = iTemp;
                }

                Rectangle(hDC,p1.x,p1.y,p2.x,p2.y);

                SelectObject(hDC,oldPen);
            }
            break;
        }
    case Tool_LineTrack:
        {
            HPEN oldPen = (HPEN)SelectObject(hDC,m_hPen);
            SelectObject(hDC,m_hBrush);

            int vSize = (int)m_PointVector.size();
            Point2D p = m_PointVector.at(0);

            if (vSize <= 1) {
                Ellipse(hDC,p.x-10,p.y-10,p.x+10,p.y+10);
            }
            else {
                //draw LineTrack
                Point2D pTemp = m_PointVector.at(1);
                MoveToEx(hDC,p.x,p.y,nullptr);

                for (int i = 1; i < vSize; ++i) {
                    p = m_PointVector.at(i);
                    pTemp = m_PointVector.at(i-1);
                    LineTo(hDC,p.x,p.y);
                    Ellipse(hDC,pTemp.x-10,pTemp.y-10,pTemp.x+10,pTemp.y+10);
                }

                Ellipse(hDC,p.x-10,p.y-10,p.x+10,p.y+10);
            }

            SelectObject(hDC,oldPen);
            break;
        }
    }

    return true;
}

WndProc(HWND, UINT, WPARAM, LPARAM)

LRESULT MyApp::WndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    if(iMsg == WM_CREATE)
    {
        CREATESTRUCT *pCS = (CREATESTRUCT*)lParam;
        SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG)pCS->lpCreateParams);

    }
    else
    {
        //retrieve the stored "this" pointer
        MyApp* pApp = (MyApp*)GetWindowLongPtr(hWnd, GWLP_USERDATA);

        switch (iMsg)
        {
            case WM_PAINT:
                {
                pApp->Paint();
                return 0;
                }

            case WM_COMMAND:
            {
                int wmId    = LOWORD(wParam);
                int wmEvent = HIWORD(wParam);

                // Parse the menu selections:
                switch (wmId)
                {
                case IDM_NEW:
                    {
                        ////
                        return 0;
                    }
                    return 0;
                case IDM_LOAD:
                    {
                        //////
                        return 0;
                    }
                case IDM_SAVE:
                    {
                    //////
                    return 0;
                    }
                case IDM_SAVEAS:
                    {
                        //////
                        return 0;
                    }
                case IDM_COLOURMAIN:
                    {
                        COLORREF col;
                        if(MyWin32Funcs::OnColorPick(col)) {
                            pApp->m_pPath->SetColor1(col);
                        }
                    return 0;
                    }
                case IDM_COLOURSECONDARY:
                    {
                    COLORREF col;
                        if(MyWin32Funcs::OnColorPick(col)) {
                            pApp->m_pPath->SetColor2(col);
                        }
                    return 0;
                    }
                case IDM_PEN:
                    {
                        pApp->m_pPath->SetTool(Tool_Pen);
                        return 0;
                    }
                case IDM_LINE:
                    {
                        pApp->m_pPath->SetTool(Tool_Line);
                        return 0;
                    }
                case IDM_ELLIPSE:
                    {
                        pApp->m_pPath->SetTool(Tool_Ellipse);
                        return 0;
                    }
                case IDM_RECTANGLE:
                    {
                        pApp->m_pPath->SetTool(Tool_Rectangle);
                        return 0;
                    }
                case IDM_LINETRACK:
                    {
                        pApp->m_pPath->SetTool(Tool_LineTrack);
                        return 0;
                    }
                default:
                    {
                    //////
                    return 0;
                    }
                }
            }

            case WM_LBUTTONUP:
                {
                    //////
                    Point2D p;
                    p.x = LOWORD(lParam); 
                    p.y = HIWORD(lParam);

                    switch(pApp->m_pPath->GetTool()) {
                        case Tool_Pen:
                            {
                                pApp->m_bPaintToBitmap = true;
                                InvalidateRect(pApp->m_hWnd,NULL,true);
                                break;
                            }
                        case Tool_Ellipse:
                            {
                                pApp->m_bPaintToBitmap = true;
                                InvalidateRect(pApp->m_hWnd,NULL,true);
                                break;
                            }
                        case Tool_Rectangle:
                            {
                                pApp->m_bPaintToBitmap = true;
                                InvalidateRect(pApp->m_hWnd,NULL,true);
                                break;
                            }
                        case Tool_Line:
                            {
                                pApp->m_pPath->AddPoint(p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
                                InvalidateRect(pApp->m_hWnd,NULL,false);
                                break;
                            }
                        case Tool_LineTrack:
                            {
                                pApp->m_pPath->AddPoint(p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
                                InvalidateRect(pApp->m_hWnd,NULL,false);
                                break;
                            }
                    }

                    return 0;
                }

            case WM_RBUTTONUP:
                {
                    //////
                    int x = LOWORD(lParam);
                    int y = HIWORD(lParam);

                    switch(pApp->m_pPath->GetTool()) {
                        case Tool_Line:
                            {
                                pApp->m_bPaintToBitmap = true;
                                InvalidateRect(pApp->m_hWnd,NULL,true);
                                break;
                            }
                        case Tool_LineTrack:
                            {
                                pApp->m_bPaintToBitmap = true;
                                InvalidateRect(pApp->m_hWnd,NULL,true);
                                break;
                            }
                    }

                    return 0;
                }
            case WM_LBUTTONDOWN:
                {
                    Point2D p;
                    p.x = LOWORD(lParam);
                    p.y = HIWORD(lParam);
                    switch(pApp->m_pPath->GetTool()) {
                    case Tool_Pen:
                        pApp->m_pPath->AddPoint(p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
                        InvalidateRect(pApp->m_hWnd,NULL,false);
                        break;
                    }
                }
            case WM_MOUSEMOVE:
                {
                    Point2D p;
                    p.x = LOWORD(lParam);
                    p.y = HIWORD(lParam);
                    if (wParam & MK_LBUTTON) {
                        switch(pApp->m_pPath->GetTool()) {
                        case Tool_Pen:
                            {
                            pApp->m_pPath->AddPoint(p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
                            InvalidateRect(pApp->m_hWnd,NULL,false);
                            break;
                            }
                        case Tool_Ellipse:
                            {
                            if( pApp->m_pPath->GetLen() >= 1) {
                                pApp->m_pPath->SetPointAt(1,p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
                            }
                            else {
                                pApp->m_pPath->AddPoint(p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
                            }
                            InvalidateRect(pApp->m_hWnd,NULL,false);
                            break;
                            }
                        case Tool_Rectangle:
                            {
                            if( pApp->m_pPath->GetLen() >= 1) {
                                pApp->m_pPath->SetPointAt(1,p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
                            }
                            else {
                                pApp->m_pPath->AddPoint(p,pApp->m_pBitmapPainter->GetBmpSize(),pApp->m_pBitmapPainter->GetBmpOffset());
                            }
                            InvalidateRect(pApp->m_hWnd,NULL,false);
                            break;
                            }
                        }
                    }

                    return 0;
                }

            case WM_CLOSE:
                {
                    //////
                                return 0;
                            }
                        }
                    PostQuitMessage(0);
                    return 0;
                }
        }
    }
    return DefWindowProc (hWnd, iMsg, wParam, lParam) ;
}

MyApp::Paint()

void MyApp::Paint()
{
    BeginPaint(m_hWnd,&m_PaintStruct);
    if (m_bPaintToBitmap) {
        Point2D p;
        p.x = BMPXOFFSET;
        p.y = BMPYOFFSET;
        m_pPath->Offset(p);
        m_pPath->DrawTo(m_pBitmapPainter->GetMemDC());
        m_pPath->ClrPath();
        m_pBitmapPainter->Paint(); //this is where BitBlt() occurs
        m_bPaintToBitmap = false;
        if(m_pBitmapPainter->IsAdjusted() == false) {
            m_pBitmapPainter->SetbAdjusted(true);
        }
    }
    else {
        m_pBitmapPainter->Paint(); //this is where BitBlt() occurs
        m_pPath->DrawTo(m_hDC);
    }
    EndPaint(m_hWnd,&m_PaintStruct);
}

Any help would much be appreciated.

Tae-Sung Shin
  • 20,215
  • 33
  • 138
  • 240
xcrypt
  • 3,276
  • 5
  • 39
  • 63
  • You don't get tearing in a paint program, it is an artifact caused by moving objects. I'd have to guess at flicker. – Hans Passant Oct 03 '11 at 11:50
  • 2
    The basic rule is simple, do nothing in `WM_ERASEBKGND` only copy the memory DC to screen with a quick `BitBlt` in `WM_PAINT`. Important tip: the `WM_PAINT` messages are only sent when other messages have been handled. You can speed up refreshing of the screen by doing `InvalidateRect(hWnd, NULL, FALSE)` and `PostMessage(hWnd, WM_PAINT, 0, 0)` to the window in for example your response to `WM_MOUSEMOVE`. Another tip: call `SetCapture` and `ReleaseCapture` if don't already, that way you receive `WM_MOUSEMOVE` faster (on slow CPU's). – demorge Oct 03 '11 at 17:48
  • And you really shouldn't use `SetPixel` that's so slow. Create a memory DC using `CreateDIBSection` and modify the pixels (`RGBQUAD`s) directly. – demorge Oct 03 '11 at 17:54
  • 1
    http://stackoverflow.com/questions/197948/reduce-flicker-with-gdi-and-c/199526#comment13601985_199526 seems related – rogerdpack May 09 '12 at 14:34

4 Answers4

9

I think what you're seeing is flicker, not tearing. To minimize flicker, your WM_PAINT should write to the window DC exactly once. Typically, this one operation is a BitBlt:

HDC hdc = BeginPaint(m_hwnd, &m_PaintStruct);
... paint to bitmap ...
BitBlt(hdc, ...); // blt from bitmap to screen
EndPaint(m_hwnd, &m_PaintStruct);

If you paint to the window DC in multiple steps, then you open the window for flicker.

Your description of the problem doesn't match the code. In your description, you say that you're blitting from the compatible DC to the window hDC. But in your code, your BitBlt is followed by a m_pPath->DrawTo(m_hDC). If a refresh occurs during the DrawTo, then the screen will refresh with a partially-drawn view, resulting in flicker.

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
  • I asked my teacher. the description of my code is correct, but it is not complete. I am drawing to the window DC after the bitblt(), which is the problem. I asked my teacher today, basically he told me I have to use double buffering to avoid flicker. – xcrypt Oct 03 '11 at 20:28
  • It seems like GDI can decide to refresh the screen after any drawing call (`BitBlt`, etc), and that it doesn't wait for `EndPaint`, is that correct? Just out of curiosity, does this happen on a timer or something? I'm guessing that `BitBlt` writes to a pixel buffer somewhere, and Windows occasionally copies that buffer to video memory after a drawing call if enough time has elapsed since the last refresh? – jrh Jun 12 '18 at 15:43
  • 1
    Classic GDI gives you a DC whose backing store is the video card itself. If you write a pixel to that DC, it goes straight to the video card and shows up on the screen. (DWM changes this model, but you should generally work with the classic model.) – Raymond Chen Jun 12 '18 at 18:12
4

If you are drawing the entire client area, override WM_ERASEBKGND, and simply return TRUE. That will reduce the flicker.

As others have pointed out; use the HDC given by WM_PAINT, as it may contain clipping regions, and other stuff that may optimize the screen update.

EDIT If you are not drawing the entire client area, you can perform background painting in the areas you know where your WM_PAINT handler won't paint.

Jörgen Sigvardsson
  • 4,839
  • 3
  • 28
  • 51
0

BeginPaint gives you HDC you are supposed to paint to. You are ignoring it.

Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • doesn't seem to fix my problem... I tried it, still same screen tearing. m_hDC is the HDC of the window I'm painting to. BeginPaint() Should receive the same handle to DC (I think) – xcrypt Oct 03 '11 at 10:36
  • 1
    @xcrypt Why should BeginPaint do that? That's not what the documentation says. – David Heffernan Oct 03 '11 at 11:07
  • 1
    You should paint into the DC provided by BeginPaint, because that DC is clipped to the invalid region, and it's the one descripbed by the PAINTSTRUCT. – Raymond Chen Oct 03 '11 at 14:18
0

What OS are you running on ? If its Vista or Windows 7, are you running in some sort of "compatibility mode" disabling desktop compositing ?

Supposedly one of the advantages of the Desktop Window Manager (DWM) introduced with Vista is (source):

In Windows XP, applications update their windows directly when the OS requests them to. These requests could be executed asynchronously with respect to the refresh rate of the monitor or to any updates that may be currently running. The effect of these requests is that the user sees windows tearing and re-drawing incorrectly or slowly. The DWM style of window presentation eliminates the tearing artifacts, providing a high quality desktop experience. The benefit to the end user is that the system appears to be more responsive and the experience is cleaner.

ie with the DWM, compositing to the desktop will be synchronized with drawing to avoid ever seeing partially drawn "torn" things.

If that's not it, are you sure you're not confusing "tearing" with background erase flicker or something ?

timday
  • 24,582
  • 12
  • 83
  • 135