[Kde-games-devel] QCanvas performance - alpha channels

Thomas Friedrichsmeier thomas.friedrichsmeier at ruhr-uni-bochum.de
Sun Feb 15 00:21:44 CET 2004


Hi!

Maybe this isn't news to most of you, however, I think this might be helpful 
to many:

I've tried for hours to improve performance in my QCanvas-based arcade type 
game (http://taxipilot.sf.net). Today I finally found out what had been 
biting performance all along.
I'm storing all the sprite-pixmaps as full-color PNGs. What I did not realize 
up to now, is that Qt was reading all those PNGs with a full 8bit 
alpha-channel. However, for most of those, I really only need a 1bit mask, 
and somehow I had always assumed, they were treated that way.
If you want to strip a QCanvasPixmapArray of the alpha-channel, you can use 
something like this:

	for (unsigned int i = 0; i < array->count (); ++i) {
		// following if-statement turned out to be true for _all_ my graphics
		if (array->image (i)->hasAlphaChannel ()) {
			// need to make a copy first
			QBitmap maskcopy = QBitmap (*(array->image (i)->mask ()));
			array->image (i)->setMask (maskcopy);
		}
	}

I haven't done any exact measurements, but in my game, doing this with most 
sprites yields a performance boost by at least factor 2.

Quite an obvious thing, once you've found it, but I didn't realize this for 
literally years. Hope this mail will be saving others the same trouble.

Thomas



More information about the kde-games-devel mailing list