Jump to content
  • Advertisement
Sign in to follow this  
GosuDrew

Copying Memory Device Contexts

This topic is 4817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I would like to be able to copy the back buffer memory device context into another one efficiently. So, I was wondering if it could be done with memcpy() or prolly new() or malloc(). I could do the following: HDC temp = CreateCompatibleDC (hdcBackBuffer); HBITMAP back = CreateCompatibleBitmap (hdcBackBuffer, 486, 486); HBITMAP old_temp = (HBITMAP) SelectObject (temp, back); BitBlt (temp, 0, 0, 486, 486, hdcBackBuffer, 0, 0, SRCCOPY); But, would it be more efficient to use memcpy() or a new() or malloc() or is the above fine? I don't know the size of hdcBackBuffer to use any of these though.

Share this post


Link to post
Share on other sites
Advertisement
BitBlt with SRCCOPY is probably fast enough. It is also the most flexible, since memcpy will not work if there is a difference in resolution or bitdepth. Finally, it is also more efficient to code, which is likely to be more important than shaving a half millisecond off the execution time.

Share this post


Link to post
Share on other sites
Thanks, I'll just stick with that then.

But, I'm trying to fix the animation in my chess game. The BitBlting can not keep up with the mouse cursor. Initially, as a piece was being moved across the screen, I BitBlted the board, all the stationary pieces, and the moving piece. You could definitely see that the BitBlting was not keeping up with the mouse cursor. So, I BitBlted the board and the stationary pieces to another device context. As a piece was being moved across the screen, I BitBlted this and the moving piece. This did make the BitBlting keep up better with the mouse cursor, but I can still slightly see that it is not keeping up. I looked at another chess program(BlitzIn 2) and it seems perfect for it.

Any ideas or suggestions on how to make BitBlting keep up with the mouse cursor?

Share this post


Link to post
Share on other sites
Instead of redrawing the entire board remember the last place that your piece was and make use of the rcPaint RECT in the PAINTSTRUCT obtained from BeginPaint(). I assume that you are calling InvalidateRect() or RedrawWindow() when the piece is moved to trigger the redraw; when you use these make sure that you use the RECT that piece occupied previously and the RECT that the piece is being moved to to specify the RECT of the window that will actually need to be redrawn. This update RECT will be the union of the previous RECT and the new RECT which you can calculate using UnionRect().

In your WM_PAINT rcPaint will be the update rect. Use BitBlt() to draw the board and the piece only in that RECT.

Also, if your board covers the entire client area of the window either set the background brush of the window to NULL when you set up your WNDCLASS or handle WM_ERASEBKGND by simply returning 1 to prevent the background from automaticlly being erased and redrawn.

Share this post


Link to post
Share on other sites
Thanks, I didn't think to just redraw what only needed to be redrawn. However, I don't use InvalidateRect() or ReDrawWindow() to signal redrawing the screen. Instead, as the left button is down and the mouse moves, Animation() is called.
Would making use of the rcPaintRect instead be more efficient as opposed to what I'm currently doing?


case WM_PAINT:
{
BeginPaint (hwnd, &ps);
BitBlt (hdcFrontBuffer, 0, 0, 486, 486, hdcBackBuffer, 0, 0,
SRCCOPY);
EndPaint (hwnd, &ps);
return 0;
}
//xPos and yPos are the mouse coordinates
VOID Animation (HDC &hdcPieceMaskBuffer, HDC &hdcPieceBuffer,
int &xPos, int &yPos)
{
BitBlt(hdcBackBuffer, xPos - 26, yPos - 26, 52,
52, hdcPieceMaskBuffer, 0, 0, SRCAND);

BitBlt(hdcBackBuffer, xPos - 26, yPos - 26, 52,
52, hdcPieceBuffer, 0, 0, SRCPAINT);

BitBlt(hdcFrontBuffer, 0, 0, 486, 486, hdcBackBuffer, 0, 0,
SRCCOPY);
//restore back buffer to orginal state
BitBlt (hdcBackBuffer, xPos - 26, yPos - 26, 52,
52, hdcBackBufferCopy, xPos - 26, yPos - 26,
SRCCOPY);
}

Share this post


Link to post
Share on other sites
You can modify Animation() to get the same results. If you remember the previous x and y mouse coordinates then you can construct the current rectangle that will be updated (the rectangle of the piece at the current coordinates) and the previous rectangle (the rectangle of the piece at the previous mouse coordinates). You can use UnionRect() for those and then only blit that small rectangle from the back buffer to the front buffer.

It's the same method as you would use as if you were in WM_PAINT.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!