Re: Help needed with C stuff again

new topic     » goto parent     » topic index » view thread      » older message » newer message

Mark Brown wrote:

> WORD color16=palette[pixels[y*width+x]];

Here's an example: imagine that you wanted to store all the pixels on the
screen in a 1 dimensional array. For simplicity, imagine that the screen was
5x5:

   00 01 02 03 04
   05 06 07 08 09
   10 11 12 13 14
   15 16 17 18 19
   20 21 22 23 24

Since most of the computing world starts counting from zero, I've also done
so in the example.
The first pixel on the second line is #05, the second pixel on the third
line is #11, and so on. The general formula for finding the index of a given
pixel is:

   ( y * screenWidth ) + x

That obviously corresponds to the formula:

   y*width+x

So what's actually *stored* in that pixel's location? Color information, of
course. The actual color isn't stored there, it's just an index to the
palette. For example, here's a box drawn on our example screen. The box's
edges are color #1, and it's filled with color #2, and everything else is
color 0:

   1 1 1 1 0
   1 2 2 1 0
   1 2 2 1 0
   1 1 1 1 0
   0 0 0 0 0

Internally, this looks like this:

   pixel[00] = 1 -- (0,0)
   pixel[01] = 1 -- (1,0)
   pixel[02] = 1 -- (2,0)
   pixel[03] = 1 -- (3,0)
   pixel[04] = 0 -- (4,0)
   pixel[05] = 1 -- (0,1)
   pixel[06] = 2 -- (1,1)
   pixel[07] = 2 -- (2,1)
   pixel[08] = 1 -- (3,1)
   pixel[09] = 0 -- (4,1)
   pixel[10] = 1 -- (0,2)
   pixel[11] = 2 -- (1,2)
   pixel[12] = 2 -- (2,2)
   pixel[13] = 1 -- (3,2)
   pixel[14] = 0 -- (4,2)
   pixel[15] = 1 -- (0,3)
   pixel[16] = 1 -- (1,3)
   pixel[17] = 1 -- (2,3)
   pixel[18] = 1 -- (3,3)
   pixel[19] = 0 -- (4,3)
   pixel[20] = 0 -- (0,4)
   pixel[21] = 0 -- (1,4)
   pixel[22] = 0 -- (2,4)
   pixel[23] = 0 -- (3,4)
   pixel[24] = 0 -- (4,4)

So you can get the index of the color for a pixel by writing:

   pixels[y*width+x]

That's fine and good, but what color is color #1? To find that out, you need
to look in the palette:

   palette[1] -> color #1, in color16 format

What's color16 format? I don't know, but a color component is typically made
up of some mixture of red, green and blue. If you have 16 bits to use over
three color elements, that's five bits per color element. So you can have
2^5 (or 2^4, it's late and I can't remember) shades of each color component
to mix together.

To recap, to figure out what color is being displayed at a given pixel
location:

   y*width+x

gets the offset to a particular pixel;

   pixels[y*width+x]

retrieves the index into the palette of the color that's set for that pixel;

   palette[pixels[y*width+x]]

retrieves the color16 value from the palette. As I mentioned, I don't have
enough information available to decode the color16 value.

I hope this helps!

-- David Cuny

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu