|
|
|
Win32 Drawing and Bitmaps How-ToWindows Programming Tutorial for Graphics and Bitmap HandlingA Win32 graphics programming article detailing how to handle bitmaps and device contexts when using Windows for drawing and other graphical applications.
One issue that programmers face in writing graphics routines for the Win32 GDI API, is that it is not performance-oriented. While the rest of the Win32 GUI is more or less real-time, the graphics capabilities are anything but. Luckily there is built-in support for off-screen drawing using bitmaps. Even if it is not possible in Win32 GDI programming to load a bitmap from a file, the standard GDI API does let the programmer create bitmaps for use in their own applications. The CreateCompatibleBitmap FunctionThe way that the bitmap handling works is to base it on an existing device context. The CreateCompatibleBitmap function takes a handle to a device context, and then returns a bitmap of the specified dimensions that is compatible with it. It could be a handle to a screen, printer, plotter or braille device - the programmer does not need to worry about where the end result is going to end up. The definition of the CreateCompatibleBitmap is as follows: CreateCompatibleBitmap ( HDC, int, int ); The two integers are the width and height of the bitmap, and the HDC is a handle to an existing device context. Getting an Appropriate Device ContextIt is possible to use the device context that is currently selected. However, this would mean that, as the bitmap was built up, the user would be able to see the result, and it would be as slow, if not slower, than not using a bitmap at all. While using the current device context is handy for taking a snapshot (the bitmap will capture the existing image), for offscreen drawing, it is necessary to create a separate device context: HDC hOffscreenDC = CreateCompatibleDC(hdc); // hdc is current DC Then, the bitmap can be created: HBITMAP CreateCompatibleBitmap(hOffscreenDC, 640, 480); // assuming small area To get the right dimensions in this step, the programmer should call GetClientRect to ascertain the available screen area, or calculate an appropriate amount : device contexts o not actually contain dimension information. The BitBlt FunctionWith a bitmap, source, and target device contexts in hand, the programmer just needs to update the image, and then copy it from one device context (the offscreen DC) to the other (the visible DC). This is done with the BitBlt function: BitBlt( HDC, int, int, int int, HDC, int, int, DWORD ); The above might look complicated, but essentially, the first HDC and associated integers is just the source image context and dimensions, and the second HDC is, predictably, the target context. The two integers associated with it is the X and Y position of the top corner of the bitmap : the size will be retained. The final DWORD is the raster operation to use, which tells the Windows GDI API how to transfer the bitmap from one context to the other. Common values are:
Having dealt with the individual components, these can then be put in a simple series of steps. Offscreen Drawing TutorialThe following steps can be used to perform offscreen drawing:
As the reader will note from this Win32 graphics programming tutorial, it is very easy to handle bitmaps in Windows. While the speed will probably not be up to a full video game, it is ample for most non-performance critical applications. The technique is the same as for other graphical environments (like DirectX or OpenGL), but the implementation is different across SDKs.
The copyright of the article Win32 Drawing and Bitmaps How-To in Windows Programming is owned by Guy Lecky-Thompson. Permission to republish Win32 Drawing and Bitmaps How-To in print or online must be granted by the author in writing.
|
|
|
|
|
|
|
|