Hi!
How do I refresh my window after I've bitblt:ed to it to show any changes I've made (i.e. moved a sprite)?
Thanks in advance
//Tobbe McCain
Printable View
Hi!
How do I refresh my window after I've bitblt:ed to it to show any changes I've made (i.e. moved a sprite)?
Thanks in advance
//Tobbe McCain
use a double buffer, do changes on the off screen dc, and then blit them altogether on the window
I do that allready. And I know that the void that does the drawing is called, the problem is that nothing is drawed to the window. I call the void in WM_PAINT and it works just fine, but when I try to call it in a while loop it just doesn't draw to the window...
You don't use the same code to react to WM_PAINT and do any in-between drawing. For in-between drawing BeginPaint won't work.
You should use GetDC instead.
In WM_PAINT I have BeginPaint and then I have a function that does the drawing and then EndPaint. In my loop I just call the function that does the drawing. And I use GetDC in that function.
Hmmm...
Still sounds like a clipping area problem.
Does the drawing function simply blit the back buffer to the screen or does it do anything else?
In WM_CREATE I have
And then I callCode:g_hdc = GetDC(hwnd);
GetClientRect(hwnd, &g_rcClient);
in my function that looks like:Code:DrawBall(g_hdc, &g_rcClient);
I know it's not the fastest way to do it but it will have to do untill I get it working. I just copied the code from the tutorial over at www.winprog.netCode:void DrawBall(HDC hdc, RECT* prc)
{
HDC hdcBuffer = CreateCompatibleDC(hdc);
HBITMAP hbmBuffer = CreateCompatibleBitmap(hdc, prc->right, prc->bottom);
HBITMAP hbmOldBuffer = (HBITMAP)SelectObject(hdcBuffer, hbmBuffer);
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, g_hbmMask);
FillRect(hdcBuffer, prc, (HBRUSH)GetStockObject(WHITE_BRUSH));
BitBlt(hdcBuffer, g_ballInfo.x, g_ballInfo.y, g_ballInfo.width, g_ballInfo.height, hdcMem, 0, 0, SRCAND);
SelectObject(hdcMem, g_hbmBall);
BitBlt(hdcBuffer, g_ballInfo.x, g_ballInfo.y, g_ballInfo.width, g_ballInfo.height, hdcMem, 0, 0, SRCPAINT);
BitBlt(hdc, 0, 0, prc->right, prc->bottom, hdcBuffer, 0, 0, SRCCOPY);
//Clean up
SelectObject(hdcMem, hbmOld);
DeleteDC(hdc);
SelectObject(hdcBuffer, hbmOldBuffer);
DeleteDC(hdcBuffer);
DeleteObject(hbmBuffer);
}
Hmm...
Looks ok. A few things:
You should not take for granted that the client rect will range from (0,0) to (x,y), so to get the width and height you should calculate
int width = prc->right - prc->left;
int height = prc->bottom - prc->top;
And are you sure you specified CS_OWNDC for the class style when registering the window class? If you want to store the HDC in a global variable you have to do that, else the handle will become invalid.
You might also want to try doing a single blit instead of the mask blit, just to test if that causes the error (unlikely if it, as you said, works for WM_PAINT).
Now it works, sort of...
i added CS_OWNDC and in "//Clean up" I changed DeleteDC(hdc) to DeleteDC(hdcMem)... And this works. But now I've tried to move the code out of the function and just put it straight in to my code and I thought that it would be a mather of copy and paste with some small changes but as it turned out it wasn't... Now when I BitBlt my buffer to the screen my sprite that used to be multi-color is now all black, it looks as if I only BitBlt the mask... and I think that is what happens because if I comment out the line that SRCANDs the mask I just get a blank screen. What can cause this? If I SRCCOPY my sprite all I get is a black square...