Hi,
I want to run an animation(in essence). I want to draw multiple images (possibly .bmps, but I'd like other formats as well.) on to the screen each frame.
I tried to do this with some C++ code, but it isn't working as it should (As I'm expecting it to I guess.). Can anyone please help me?
Here's the class definition. Just for the variables.
ctor.
The drawing code.
I fiddled around with the code for sometime but couldn't get it done.
I have couple of questions on this. I'd be grateful if anyone can help me.
1. Why doesn't my code show the .bmp on the screen?(Of course) All I see is a white window.
2. As you may have noticed, I used 3 DCs. One is the actual DC of the window.
I have a DC, bufferDC which I use for drawing behind. The other, memDC is just something I used for loading the bmp onto. I wouldn't have needed this if I only wanted to paint one image. But, as I want to draw more than one image, I thought I needed this to load the bmp into, and then to paste it onto the bufferDC. Is this the way to do this? Or is there a better way?
3. I saw somewhere that HBITMAPs use OS resources. That sounds bad. So, if I want lots of images in my app, would that create a problem? If so, is there any way around it? Without of course, using OpenGL or some lib.
Thanks!
I want to run an animation(in essence). I want to draw multiple images (possibly .bmps, but I'd like other formats as well.) on to the screen each frame.
I tried to do this with some C++ code, but it isn't working as it should (As I'm expecting it to I guess.). Can anyone please help me?
Here's the class definition. Just for the variables.
Code:
class CWorld
{
private:
//Window DC
HDC windowDC;
//This is the backbuffer. We incrementally draw onto this and paste to the actual window DC once. This reduces flicker.
HDC bufferDC;
//This is a utility DC that will be used to load bitmaps into and pasting them to the back buffer.
HDC memDC;
//The bitmap for the buffer.
HBITMAP hBufferBitMap;
public:
BOOL DrawFrame();
CWorld(HDC windowDC);
~CWorld(void);
};
Code:
CWorld::CWorld(HDC windowDC)
{
this->windowDC = windowDC;
this->bufferDC = NULL;
this->bufferDC = ::CreateCompatibleDC(windowDC);
this->memDC = NULL;
this->memDC = ::CreateCompatibleDC(this->bufferDC);
HBITMAP hbmBuffer = ::CreateCompatibleBitmap( this->bufferDC,
::GetDeviceCaps(this->bufferDC, HORZRES),
::GetDeviceCaps(this->bufferDC, VERTRES));
HBITMAP h = (HBITMAP)::SelectObject(this->bufferDC, hbmBuffer);
//Check for errors.
}
The drawing code.
Code:
BOOL CWorld::DrawFrame()
{
static HBITMAP testBmp = (HBITMAP)::LoadImageA(NULL, "C:\\dota.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
HBITMAP memOld = (HBITMAP)::SelectObject(this->memDC, testBmp);
//A white background.
::PatBlt( this->bufferDC, 0, 0,
GetDeviceCaps(this->bufferDC, HORZRES), GetDeviceCaps(this->bufferDC, VERTRES),
WHITENESS);
//BitBlt the .bmp onto the back buffer.
::BitBlt( this->bufferDC, 0, 0,
GetDeviceCaps(this->bufferDC, HORZRES), GetDeviceCaps(this->bufferDC, VERTRES),
this->memDC, 0, 0,
SRCCOPY);
//BitBlt the back buffer to the window DC.
::BitBlt( this->windowDC, 0, 0,
GetDeviceCaps(this->windowDC, HORZRES), GetDeviceCaps(this->windowDC, VERTRES),
this->bufferDC, 0, 0,
SRCCOPY);
::SelectObject(this->memDC, memOld);
return TRUE;
}
I have couple of questions on this. I'd be grateful if anyone can help me.
1. Why doesn't my code show the .bmp on the screen?(Of course) All I see is a white window.
2. As you may have noticed, I used 3 DCs. One is the actual DC of the window.
I have a DC, bufferDC which I use for drawing behind. The other, memDC is just something I used for loading the bmp onto. I wouldn't have needed this if I only wanted to paint one image. But, as I want to draw more than one image, I thought I needed this to load the bmp into, and then to paste it onto the bufferDC. Is this the way to do this? Or is there a better way?
3. I saw somewhere that HBITMAPs use OS resources. That sounds bad. So, if I want lots of images in my app, would that create a problem? If so, is there any way around it? Without of course, using OpenGL or some lib.
Thanks!
Comment