Public Group

# Need help understanding draw code and why its not drawing a bmp

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

## Recommended Posts

I've been following the forgers win32 api tutorials for a while now and I've been making pong using the windows api and C++. I tried to understand what exactly was happening in this code but I'm still not completely sure what things do and why. What I got from the tutorial was hdcBuffer is a handle to the device context. In this case it would be the main buffer, or canvas, for the final masterpiece to go on. The hdcMem is basically the draft drawing where we have the art go before it's ready to be shipped to hdcBuffer. What I'm not sure of is why hbmBuffer, hbmOldBuffer, and hbmOld need to be there. Also, I modified the code to draw the paddles and pong ball. When I tried to add a RedPlayerWins bmp or BluePlayerWins bmp the image is being displayed on the screen wrong. I'm not sure why that is but then again I don't entirely understand what is going on here. I would also like to know if I drew these correctly. I've used SDL before and I know for SDL there are surfaces and each surface can only hold one bmp so am I correctly drawing these objects to memory? Should I be freeing something after each BitBlt or what?
void Draw(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 = NULL;

FillRect(hdcBuffer, prc, (HBRUSH)GetStockObject(BLACK_BRUSH));
if (g_bGameDone != true)
{
BitBlt(hdcBuffer, g_Ball.x, g_Ball.y, g_Ball.w, g_Ball.h, hdcMem, 0, 0, SRCAND);

SelectObject(hdcMem, g_hbmBall);
BitBlt(hdcBuffer, g_Ball.x, g_Ball.y, g_Ball.w, g_Ball.h, hdcMem, 0, 0, SRCPAINT);

//}
//else
//{
//if (g_bRedWins == true)
//{
SelectObject(hdcMem, g_hbmRedPlayerWins);
BitBlt(hdcBuffer, g_Ball.x, g_Ball.y, 430, 143, hdcMem, 0, 0, SRCPAINT);
//}
//else
//{
//	SelectObject(hdcMem, g_hbmBluePlayerWins);
//	BitBlt(hdcBuffer, prc->right / 2, prc->bottom / 2, 430, 143, hdcMem, 0, 0, SRCPAINT);
//}
}
BitBlt(hdc, 0, 0, prc->right, prc->bottom, hdcBuffer, 0, 0, SRCCOPY);

SelectObject(hdcMem, hbmOld);
DeleteDC(hdcMem);

SelectObject(hdcBuffer, hbmOldBuffer);
DeleteDC(hdcBuffer);
DeleteObject(hbmBuffer);
}


The RedPlayerWins has the coordinates of the g_Ball because I couldn't see anything of the RedPlayerWins bmp until another bmp was in front of it. I noticed it was taking part of the last bmp that was BitBlt. In the code I have now, it would take the BluePaddle bmp and mix it with the ball.bmp I'm not sure why this is happening. If you could relate the drawing code to SDL's surfaces and freeing them and whatnot (if the two are similar) I would appreciate it a lot. Thanks. What I'm using: C++, windows api, visual studio 2008 prof. ed. Tutorial link that I'm using: http://www.winprog.org/tutorial/bitmaps.html

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 32
• ### Forum Statistics

• Total Topics
632630
• Total Posts
3007526
• ### Who's Online (See full list)

There are no registered users currently online

×