Categories

Meta

Gory Detail

Here then, in all its gory detail, is the the file I’ve been working on this evening. Most of my time was wasted in trying to work out how the vector class worked and getting to the bottom of arcane compiler errors. Stuff like having to include std:: to qualify the vectors I was using, and getting the template for my really bare bones simple Colour class right. Sheesh, if I was doing this game in assembler or C, I would have finished it by now, but I know I’ll reap the benefits of proper C++ if I stick with it and let it do some of the heavy lifting for me.

And yes, I know the tabs have been stripped, just be grateful there are colours here.

/*what does this class do?

given a character pointer, it constructs a 2D array of RGBA from bitmap data of varying bit depths

what are the parameters?

width, height of source

bpp of source

palette to use when mapping to RGBA

stride per byte

stride per line

endianness

width of pixel group (in bits)

What’s the real world use of this class?

This class is used to decode old school bit streams of graphics data and

turn them into displayable blocks for capture and re-use in modern environments.

// We have a palette and all the vars are set up, time to extract to our internal buffer

switch (bitsPerPixel)

{

case1:// Monochromatic spectrum

return extract1BitPerPixel(rawDataStream);

case2:// Commodore 64 or Amstrad

return extract2BitsPerPixel(rawDataStream);

default:

returnFALSE;

}

}

bool StreamToImage::extract1BitPerPixel(BYTE *rawDataStream)

{

BYTE *pb;

BYTE bit;

for (int y = 0; y < height; y++)

{

BYTE mask = 0x80;

BYTE shift = 7;

for (int x = 0; x < width; x++)

{

byte = pb[y * strideLine + (x >> 3)];

bit = (byte & mask) >> shift;

// Now we have a value for the bit, use the palette index 1 to set it in our pixel buffer

pixels[x + y * width] = palette[1];

mask >>= 1 ? mask : 0x80;

–shift ? shift : 7;

}

}

returnTRUE;

}

bool StreamToImage::extract2BitsPerPixel(BYTE *rawDataStream)

{

returnFALSE;

}

I accept that constructs like the bool “goodToGo” have no place in polite society. I felt a little dirty doing that. I also think the ternary operators resetting the mask and shift values are a cop-out. I ain’t fooling anyone with that pitiful attempt to hide two branches in the middle of a byte loop, but the engines can take it captain. At least, they can nowadays.

Ugly, isn’t it? Go on, give me tips. I can and will improve this, but you can see what I’m trying to do here.