Public Group

# Me and my Double Buffer again

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

## Recommended Posts

hy , what im trying to do is to load a masked sprite to my backbuffer ,the problem is it isn't happening. I want to state that i can BitBlt the hSpriteDC to hWindowDC with the desired output , same with the hBBDC to hWindowDC, again right , but when i try to load an image onto the hSpriteDC,blit it with hBBDC and, then blit The hBBDC with hwindowDC it goes blank.

-images are 35 pixels square
-all Dc's are valid and operational (compatible to each other)
-if adidional parts of code are required pls say...

BitBlt(hBBDC, 0, 0, 35, 35, hSpriteDC, 0, 0, SRCAND);

SelectObject(hSpriteDC, hBmp);
BitBlt(hBBDC, 0, 0, 35, 35, hSpriteDC, 0, 0, SRCPAINT);

Thanks

##### Share on other sites
You should probably include the code surrounding that which causes the problem. The above given is a bare minimum and having more to go on would ease the process.

##### Share on other sites
Quote:
 Original post by boogyman19946You should probably include the code surrounding that which causes the problem. The above given is a bare minimum and having more to go on would ease the process.

gues i found what was the problem but another one came up... im doomed, the thing is that i forgot to associate any bitmap with the backbuffer so i didnt have what to blit to, now the bitwise operations dont work, and instead of maked bitmap, its only the mask: here is the Class code (ps i combined what was supposed to be the backbuffer class and the sprites class into 1 with a purpose). And another thing , i got 3 dcs at the same time without releasing any, ppl say it's a bad thing to do, does any1 know why,resources or repaint issues?

#include "BackBuffer.h"

HDC hWndDC;
BackBuffer::BackBuffer(HWND hWindow,HINSTANCE hAppInst)
{
hBBWindow=hWindow;
hBBAppInst=hAppInst;
RECT rBBRect;
GetWindowRect(hWindow, &rBBRect);
this->iClientWidth=rBBRect.right;
this->iClientHeight=rBBRect.bottom;
this->hWndDC=GetDC(hWindow);
this->hBBDC = CreateCompatibleDC(hWndDC);
this->hSpriteDC=CreateCompatibleDC(hBBDC);
HBITMAP bmp=CreateCompatibleBitmap(hBBDC,600,600);
SelectObject(hBBDC,bmp);
}

BackBuffer::~BackBuffer()
{
DeleteDC(hBBDC);
}

inline HDC BackBuffer::DC()
{
return hBBDC;
}

void BackBuffer::Present()
{
BitBlt(hWndDC,0,0,35,35,hBBDC,0,0,SRCCOPY);

}

{
SelectObject(hBBDC, GetStockObject(WHITE_BRUSH));
Rectangle(hBBDC,0,0,35,35);
BitBlt(hBBDC, 0, 0, 35, 35, hSpriteDC, 0, 0, SRCAND);

SelectObject(hSpriteDC, hBmp);
BitBlt(hBBDC, 0, 0, 35, 35, hSpriteDC, 0, 0, SRCPAINT);
}

##### Share on other sites
Use source tags; they are there for a reason. No one is going to look at your code if it's hard to read. The Back Buffer implementation is still a mess. I see that there is still that global definition of hWndDC that does absolutely nothing.

Here's my implementation of the back buffer.

// BackBuffer.h#ifndef BACKBUFFER_H#define BACKBUFFER_H#include <windows.h>#include <string>struct BackBuffer {	///////////////	// FUNCTIONS //	///////////////	BackBuffer(HWND hWnd, int width, int height);	~BackBuffer();		void ClearBuffer();		bool DrawBitmap(HBITMAP arghbmp, int xPos, int yPos, int width, int height, DWORD RastCode);	bool DrawText(std::string argStr, int xPos, int yPos); 		void PresentBuffer();private:	///////////////	// VARIABLES //	///////////////	HWND mhWnd;	HDC mhDC;	HDC mhMemDC;		HBITMAP mhbmpSurface;	HBITMAP mhbmpOld;		int mWidth, mHeight;};#endif // BACKBUFFER_H

// BackBuffer.cpp#include "BackBuffer.h"BackBuffer::BackBuffer(HWND hWnd, int width, int height){	mhWnd = hWnd;		HDC tempDC = GetDC(hWnd);		mhDC = CreateCompatibleDC(tempDC);	mhMemDC = CreateCompatibleDC(tempDC);		mWidth = width;	mHeight = height;		mhbmpSurface = CreateCompatibleBitmap(tempDC, width, height);		ReleaseDC(hWnd, tempDC);		mhbmpOld = (HBITMAP)SelectObject(mhDC, mhbmpSurface);		HBRUSH hbrTempBrush = (HBRUSH)GetStockObject(BLACK_BRUSH);	HBRUSH hbrOld = (HBRUSH)SelectObject(mhDC, hbrTempBrush);		Rectangle(mhDC, 0, 0, mWidth, mHeight);		SelectObject(mhDC, hbrOld);		DeleteObject(hbrTempBrush);}BackBuffer::~BackBuffer(){	DeleteObject(mhbmpSurface);	DeleteObject(mhbmpOld);	DeleteDC(mhDC);	DeleteDC(mhMemDC);}void BackBuffer::ClearBuffer(){	HBRUSH hbrTempBrush = (HBRUSH)GetStockObject(WHITE_BRUSH);	HBRUSH hbrOld = (HBRUSH)SelectObject(mhDC, hbrTempBrush);		Rectangle(mhDC, 0, 0, mWidth, mHeight);		SelectObject(mhDC, hbrOld);		DeleteObject(hbrTempBrush);}bool BackBuffer::DrawBitmap(HBITMAP argbmp, int xPos, int yPos, int width, int height, DWORD RastCode){	mhbmpOld = (HBITMAP)SelectObject(mhMemDC, argbmp);		if (mhbmpOld == 0) {		MessageBox(0, "Error selecting bitmap", "Error!", MB_OK);		return 0;	}		BitBlt(mhDC, xPos, yPos, width, height, mhMemDC, 0, 0, RastCode);		SelectObject(mhMemDC, mhbmpOld);		return 1;}bool BackBuffer::DrawText(std::string argStr, int xPos, int yPos){	if (argStr.empty()) {		MessageBox(0, "String is empty", "Error!", MB_OK);		return 0;	}		TextOut(mhDC, xPos, yPos, argStr.c_str(), argStr.length());		return 1;}void BackBuffer::PresentBuffer(){	HDC hDC = GetDC(mhWnd);		BitBlt(hDC, 0, 0, mWidth, mHeight, mhDC, 0, 0, SRCCOPY);		ReleaseDC(mhWnd, hDC);}

It's pretty straight forward and all you need is the header file in order to be able to use it. You create a back buffer and associate it with a window. Everything you draw, you draw into the back buffer. The buffer has it's own clearing and presenting functions so you don't have to mess around with the HDCs outside of the actual backbuffer. To draw a bitmap, you draw it with DrawBitmap() and to draw text, you draw it with DrawText(). When you need it in a function, pass a pointer to it and you still get to use it's functions. This is the very bare minimum for what you need to do. Of course, I use this class outside of WM_PAINT so with this implementation it's a must that the main DC isn't in use, otherwise it won't work, but you can easily see where the changes need to be applied in order for it to be functional.

##### Share on other sites
nope there is no global redefinition , its a class member (i just didnt put this-> up front)
got careless,and im gonna look why i put it over there and thanks for the code , its an very elegant one ,no comment but il push with my using yours for guidance (its easy to copy paste someones work , but much more interesting to build something from scrap)

##### Share on other sites
Quote:
 Original post by NextEpisode1nope there is no global redefinition , its a class member (i just didnt put this-> up front) got careless,and im gonna look why i put it over there and thanks for the code , its an very elegant one ,no comment but il push with my using yours for guidance (its easy to copy paste someones work , but much more interesting to build something from scrap)

heh, yes it is :) I built this implementation based on the example of many that I've seen. I added features as I needed them, but there is still to be implemented and whatnot. I should've probably included some comments in it, but I feel like the function names pretty much summarize the functionality. Once I know it works, I don't have to mess around with it, I can just carry it with me to the next project and only extend on what I need.

When you make something, it becomes your advantage because you are the one who knows how it functions and you don't need much of a reference (but when you start extending on your code, you will eventually be in need of it).

1. 1
Rutin
65
2. 2
3. 3
4. 4
5. 5

• 18
• 10
• 30
• 20
• 9
• ### Forum Statistics

• Total Topics
633416
• Total Posts
3011771
• ### Who's Online (See full list)

There are no registered users currently online

×