Jump to content
  • Advertisement
Sign in to follow this  

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

This topic is 3171 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

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);

	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);

		SelectObject(hdcMem, g_hbmRedPaddle);
		BitBlt(hdcBuffer, g_RedPaddle.x, g_RedPaddle.y, g_RedPaddle.w, g_RedPaddle.h, hdcMem, 0, 0, SRCPAINT);

		SelectObject(hdcMem, g_hbmBluePaddle);
		BitBlt(hdcBuffer, g_BluePaddle.x, g_BluePaddle.y, g_BluePaddle.w, g_BluePaddle.h, hdcMem, 0, 0, SRCPAINT);
		//if (g_bRedWins == true)
			SelectObject(hdcMem, g_hbmRedPlayerWins);
			BitBlt(hdcBuffer, g_Ball.x, g_Ball.y, 430, 143, hdcMem, 0, 0, SRCPAINT);
		//	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);

	SelectObject(hdcBuffer, hbmOldBuffer);

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

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!