Displaying .bmps on screen + BitBlt problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DumRat
    New Member
    • Mar 2007
    • 93

    Displaying .bmps on screen + BitBlt problem

    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.
    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);
    };
    ctor.
    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 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!
  • IanWright
    New Member
    • Jan 2008
    • 179

    #2
    Hi DumRat,

    I'm actually working on similar types of stuff at the moment for the first time. Having never really done any windows handle based drawing I'm trying to understand it and have now decided to try and replicate the functionality with some simple cross-platform graphics libraries.

    Now unfortunately I'm not good enough to tell you where your program is wrong, but can I suggest that you:

    a) Check the bufferDC bits to see if you've managed to get any data into it.
    b) Try setting the colour of a pixel, or drawing a primative (e.g. line, point) to your display handle to check that its working...

    In answer to question 3, as that is almost exactly what I'm working on, one way of improving performance is through tiling the images. Once tiled you can then store them in memory, or to files and through caching can actually obtain pretty decent results.

    Comment

    • Studlyami
      Recognized Expert Contributor
      • Sep 2007
      • 464

      #3
      Heres a couple suggestions (please note i haven't done WIN32 in a while). I don't know why you need to draw multiple bitmaps at one time. The animation should only display 1 image a frame (unless your trying transparent bitmaps, but thats another topic). Then at a certain rate (what fps you want the animation to run) you will replace that image with the next one in the animation.

      First i would load the bitmaps into memory (unless your talking about 100's of bitmaps) something like: HBITMAP AnimationBitmap s[10]; Then i would load the bitmaps into the array. You will need a timer to run on the system and replace the bitmap on the screen with the next one in the AnimationBitmap array.

      Also try what Ian said. Try drawing simple shapes to the MemDC, then try drawing just 1 bitmap. If those work, then work on getting the animation working.

      Is there a reason why you are doing this in WIN32 or can you use other libraries?

      Comment

      Working...