Saturday, May 23, 2015

Gash - Masking Understood

A while ago, I had a conversation on IRC with a programmer in the Keen community who has some experience working with the CGAGRAPH format used in the CGA distros of some iD games, which I would like to support in Gash.

While starting out on the rewrite of FMod/FleexCore2's GalaxyGraphics class, I quickly ran into a problem: non-EGA formats use a sane human's intuitive expectation of bit layout for each pixel. Rather than the EGA business of having all the red bits, then all the green bits, etc., the others have all the bits necessary to represent one pixel right next to each other. (Makes sense.) The problem is that such a scheme seems to require a non-factor-of-two number of bits per pixel in masked graphics, e.g. 3 for CGA (color, intensity, mask). That would be a problem because handling constantly-shifting non-byte aligned bit sequences is a huge pain and, as far as I know, not done in the actual game.

This community member and I, after some poking around, discovered that non-EGA layouts have a fairly obvious but also clever fix for the byte alignment problem. Rather than CGA doing this:

cimcimcimcimcimcimcim...

(Color, intensity, mask; respectively. The underline shows byte boundaries.) It does this:

cimmcimmcimmcimmcimmcimm

The mask bit is duplicated, rounding out the pixel representation to 4bpp. VGA does something similar, guzzling 16 bits per pixel! (Eight for the actual color, and eight instances of the mask bit.) Also, I learned that VGA games tend to keep only the UI/menu bitmaps in VGAGRAPH; the game pictures and sprites are in another file.

Armed with this knowledge, and lots of summer free time, I'll be able to create the most versatile xGAGRAPH manager the Keen community has seen.

No comments:

Post a Comment