Hey all.
Finally had a bit of time to work towards some basic GDI drawing (busy year), but after designing a class to load and store an external bitmap resource, it''s brought up a question. As the topic says, just how far would you take error handling? Here''s sort of what I''m talking about:
BitmapObj.h
// Definition of class, CBitmapObj
#ifndef BITMAPOBJ_H
#define BITMAPOBJ_H
#pragma once
#include <windows.h>
#include <string>
class CBitmapObj
{
public:
CBitmapObj(); // Constructor.
~CBitmapObj(); // Destructor.
// Utility functions.
BOOL LoadBMP(std::string path); // Load BMP from external file.
BOOL PrepareDC(); // Prepare the memory DC.
BOOL SelectBMP(); // Select the BMP into the DC.
BOOL SaveDimensions(); // Save the BMP attributes.
// Getter/setter functions.
HDC GetMemDC();
int GetWidth();
int GetHeight();
private:
HDC m_hMemDC; // Memory DC.
HBITMAP m_hBitmap; // Handle to the bitmap resource.
int m_width; // Bitmap''s width.
int m_height; // Bitmap''s height.
};
#endif
BitmapObj.cpp
// Implementation of class, CBitmapObj.
#include "bitmapobj.h"
CBitmapObj::CBitmapObj()
{
m_hMemDC = NULL;
m_hBitmap = NULL;
m_width = m_height = 0;
}
CBitmapObj::~CBitmapObj()
{
m_hBitmap = NULL;
if (m_hMemDC)
DeleteDC(m_hMemDC);
}
BOOL CBitmapObj::LoadBMP(std::string path)
{
m_hBitmap = reinterpret_cast< HBITMAP > (LoadImage(
NULL, path.c_str(), IMAGE_BITMAP,
0, 0, LR_LOADFROMFILE
));
return (m_hBitmap == NULL) ? FALSE : TRUE;
}
BOOL CBitmapObj::PrepareDC()
{
m_hMemDC = CreateCompatibleDC(NULL);
return (m_hMemDC == NULL) ? FALSE : TRUE;
}
BOOL CBitmapObj::SelectBMP()
{
// No BMP handle saved, so simply return.
if (m_hBitmap == NULL)
return FALSE;
return (SelectObject(m_hMemDC, m_hBitmap) == NULL) ? FALSE : TRUE;
}
BOOL CBitmapObj::SaveDimensions()
{
// No BMP handle saved, so simply return.
if (m_hBitmap == NULL)
return FALSE;
BITMAP bmpInfo; // Structure to hold BMP attributes.
if (GetObject(m_hBitmap, sizeof(BITMAP), &bmpInfo) == NULL)
return FALSE;
m_width = bmpInfo.bmWidth;
m_height = bmpInfo.bmHeight;
return TRUE;
}
HDC CBitmapObj::GetMemDC() { return m_hMemDC; }
int CBitmapObj::GetWidth() { return m_width; }
int CBitmapObj::GetHeight() { return m_height; }
Apologies for the slight lack of commenting, only just finished that. Anyway, here''s an example of it in use:
Extract from WinMain()
CBitmapObj bmp;
HDC hdc = GetDC(gameWin.m_hwnd);
if (bmp.LoadBMP("Images/red.bmp") == NULL)
MessageBox(NULL, "LoadBMP", "LoadBMP", MB_OK);
if (bmp.PrepareDC() == NULL)
MessageBox(NULL, "PrepareDC", "PrepareDC", MB_OK);
if (bmp.SelectBMP() == NULL)
MessageBox(NULL, "SelectBMP", "SelectBMP", MB_OK);
if (bmp.SaveDimensions() == NULL)
MessageBox(NULL, "SaveDimensions", "SaveDimensions", MB_OK);
if (BitBlt(
hdc, 0, 0, bmp.GetWidth(), bmp.GetHeight(),
bmp.GetMemDC(), 0, 0, SRCCOPY
) == NULL)
MessageBox(NULL, "BitBlt", "BitBlt", MB_OK);
ReleaseDC(gameWin.m_hwnd, hdc);
As you can see, I''ve added several calls to MessageBox(), just to illustrate my point. After seeing that little lot, do you think that would be overly cautious (imagining that calls to MessageBox() wouldn''t be the way any errors would be handled - something like a log file instead)?
Thanks for any replies.
-hellz