473,386 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Displaying .bmps on screen + BitBlt problem

93
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.
Expand|Select|Wrap|Line Numbers
  1. class CWorld
  2. {
  3. private:
  4.  
  5.     //Window DC
  6.     HDC windowDC;
  7.     //This is the backbuffer. We incrementally draw onto this and paste to the actual window DC once. This reduces flicker.
  8.     HDC bufferDC;
  9.     //This is a utility DC that will be used to load bitmaps into and pasting them to the back buffer.
  10.     HDC memDC;
  11.  
  12.     //The bitmap for the buffer.
  13.     HBITMAP hBufferBitMap;
  14.  
  15. public:
  16.     BOOL DrawFrame();
  17.  
  18.     CWorld(HDC windowDC);
  19.     ~CWorld(void);
  20. };
  21.  
ctor.
Expand|Select|Wrap|Line Numbers
  1. CWorld::CWorld(HDC windowDC)
  2. {
  3.     this->windowDC = windowDC;
  4.  
  5.     this->bufferDC = NULL;
  6.     this->bufferDC = ::CreateCompatibleDC(windowDC);
  7.  
  8.     this->memDC = NULL;
  9.     this->memDC = ::CreateCompatibleDC(this->bufferDC);
  10.  
  11.     HBITMAP hbmBuffer = ::CreateCompatibleBitmap(    this->bufferDC,
  12.                                                     ::GetDeviceCaps(this->bufferDC, HORZRES),
  13.                                                     ::GetDeviceCaps(this->bufferDC, VERTRES));
  14.     HBITMAP h = (HBITMAP)::SelectObject(this->bufferDC, hbmBuffer);
  15.  
  16.     //Check for errors.
  17. }
  18.  

The drawing code.
Expand|Select|Wrap|Line Numbers
  1. BOOL CWorld::DrawFrame()
  2. {
  3.     static HBITMAP testBmp = (HBITMAP)::LoadImageA(NULL, "C:\\dota.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  4.  
  5.     HBITMAP memOld = (HBITMAP)::SelectObject(this->memDC, testBmp);
  6.  
  7.     //A white background.
  8.     ::PatBlt(    this->bufferDC, 0, 0, 
  9.                 GetDeviceCaps(this->bufferDC, HORZRES), GetDeviceCaps(this->bufferDC, VERTRES),
  10.                 WHITENESS);
  11.  
  12.     //BitBlt the .bmp onto the back buffer.
  13.     ::BitBlt(    this->bufferDC, 0, 0,
  14.                 GetDeviceCaps(this->bufferDC, HORZRES), GetDeviceCaps(this->bufferDC, VERTRES),
  15.                 this->memDC, 0, 0,
  16.                 SRCCOPY);
  17.  
  18.     //BitBlt the back buffer to the window DC.
  19.     ::BitBlt(    this->windowDC, 0, 0,
  20.                 GetDeviceCaps(this->windowDC, HORZRES), GetDeviceCaps(this->windowDC, VERTRES),
  21.                 this->bufferDC, 0, 0,
  22.                 SRCCOPY);
  23.  
  24.  
  25.     ::SelectObject(this->memDC, memOld);
  26.  
  27.     return TRUE;
  28. }
  29.  
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!
Apr 4 '08 #1
2 2838
IanWright
179 100+
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.
Apr 4 '08 #2
Studlyami
464 Expert 256MB
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 AnimationBitmaps[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?
Apr 4 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Me Mine | last post by:
i, I have trying to code a small console app that will allow a user to select a window and then create a screen capture of the window. I haven't been able to figure out how to do the screen...
8
by: gregory_may | last post by:
Is there a way to grab a "Screen Shot" that includes "Tool Tips"? I saw this code someplace, cant remember where. But it doesnt grab "Tool Tips". Is there a better way to do this in .net?...
1
by: Johan | last post by:
Hello, I'm trying to get a part of the regular screen onto my own dialog box.... Does anyone know how to do this?? (I guess i have to BitBlt something to something (HDC handle; handle=...
1
by: xc | last post by:
Greetings. I encountered a wield problem when grabing screen images. Sometimes in some computers I can capture the screen, but other times not so. In some computer I cannot capture the screen...
2
by: Wayne | last post by:
In a Delphi application I would use GetDC passing the handle of the window I wanted to capture, then with the handle and a caption I would be able to convert the image to a bitmap. I would like to...
9
by: Rajat Tandon | last post by:
Hello there, I am relatively new to the newsgroups and C#. I have never been disappointed with the groups and always got the prompt replies to my queries.This is yet another strange issue, I am...
4
by: fong01 | last post by:
Hi, Below source code I have try on Win 2k, XP & 2003 is work, but I try on win 98 & ME the screen is capture black screen. Dim dInt_dwRop As Integer = &HCC0020 Dim dImg As New...
2
by: Eddie Dunn | last post by:
I have one here that I cannot find anything on in my searching. I am implementing a screen capture functionality into my Visual Basic ..NET application. The code I am using calls the bitblt...
4
by: Steven | last post by:
Hi, I want to create an image that looks exactly the same as the things I see in my application. I have an application in which you can create some controls, drag them anywhere you like. Now I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.