user1788175
user1788175

Reputation: 1518

Double Buffering and Viewport Origin?

I wanted to put the origin in the centre so I did:

SetViewportOrgEx(hdc,width/2,height/2,NULL);

(as seen in the code below)

Now, after implementing double buffering it doesn't seem to work properly. Any ideas why and what should be best practice here? Many Thanks

Code Below:

case WM_PAINT:
        {
            hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code here...
            RECT rect;
            GetClientRect(hWnd, &rect);
            int width=rect.right;
            int height=rect.bottom;

                    //Changing Origin position
            SetViewportOrgEx(hdc,width/2,height/2,NULL);


            HDC backbuffDC = CreateCompatibleDC(hdc);

            HBITMAP backbuffer = CreateCompatibleBitmap( hdc, width, height);

            int savedDC = SaveDC(backbuffDC);
            SelectObject( backbuffDC, backbuffer );
            HBRUSH hBrush = CreateSolidBrush(RGB(255,255,255));
            FillRect(backbuffDC,&rect,hBrush);
            DeleteObject(hBrush);

                    //Drawing on backbuffDC


            BitBlt(hdc,0,0,width,height,backbuffDC,0,0,SRCCOPY);
            RestoreDC(backbuffDC,savedDC);
            DeleteObject(backbuffer);
            DeleteDC(backbuffDC);
            EndPaint(hWnd, &ps);
        }

Upvotes: 1

Views: 394

Answers (1)

Qaz
Qaz

Reputation: 61900

Since you changed the viewport origin, you must ensure that the coordinates are updated accordingly. In the case of BitBlt, you are now passing (width/2, height/2) as the upper left corner, width as the width, and height as the height. This results in a blit to an area that is half off of your device context, which should end up causing the portion that is within the destination device context to be displayed as such, and the rest clipped.

To fix it, change the coordinates:

BitBlt(hdc,-width/2,-height/2,width,height,backbuffDC,0,0,SRCCOPY);

Secondly, I'm not sure about this one myself, but make sure valid coordinates are being passed to FillRect. What you're passing retrieved coordinates independent to the device context and its viewport, so those might have to be transformed as well.

Upvotes: 1

Related Questions