Image class

This class contains DirectX code, but the interface to it is general enough that it can be used with other graphics libraries. It is very important that the constructor and destructor are properly written to avoid memory leaks or worse.

Member Data

Member Functions

Image::Image()
{
	pTexture = NULL;
}

The texture pointer is set to null to show that no texture has been loaded.

Image::Image(string path)
{
	pTexture = LoadTexture(path.c_str());
	pTexture->GetLevelDesc(0, &desc);
	SetRect(&sourceRect, 0, 0, desc.Width, desc.Height);
}

LoadTexture was written by George Geczy and was in an article that I downloaded from www.gamasutra.com. (NOTE: Only registered members are allowed to view articles, but joining is free.)

Note that textures are padded out so that their dimensions are powers of two. In some cases there are also maximum sizes of textures that might be an issue. For the most part these won't be big issues for us, but you should be aware of them. (As a case in point, I based my ship pictures on squares that are 35 pixels wide. 30 would have been a better choice, because with squares that are 35 pixels wide almost half of the space is wasted. In main memory, that might not be a big deal, but since textures are loaded into the memory on the graphics card, it could very well be significant.)

Image::~Image()
{
	pTexture->Release();
}
void draw(int x, int y, float radians = 0, float scale = 1.0,
		POINT *prcenter = NULL);

It should be easy to see how to implement this function using BltSpriteEx. Make sure you check the pointer to texture before drawing, because if it is null you don't want to try to draw anything. Also be sure to that the source and destination rectangles are set correctly. Note that the last parameter of BltSpriteEx has a default value. Don't include a value for that parameter because you want it to use the default value. (To see the default values, look for the header of BltSpriteEx in engine.h.)

The radians parameter specifies an angle to rotate the image. The prcenter parameter tells the center for rotation (or pivot point). If the prcenter parm is null (which is the default value), BltSpriteEx will calculate the center of the image and use that for the pivot point. Keep in mind, though, that the center calculation might not be what you expect because the dimensions are increased to be powers of two.