How Do I Shorten These Several Statements Into One Statement?
JaanDoh
Posts: 129
in Propeller 1
Hello again everyone,
Well moving into learning Spin,
I'm trying to put together a program to write colour values directly to the video buffer,
Colors are made up of 6 bits, 2bits per colour (rgb)
They are stored in a byte array for now.
screen width is 200
screen height is 112
The data for 4 pixels is stored in 3 byte blocks
Bytes RRGGBBRR | GGBBRRGG | BBRRGGBB
Pixels RRGGBB | RRGGBB | RRGGBB | RRGGBB
My main query this time is how do I shorten these several statements into one formula?
1. Get Pixel-Id
2. Get Pixel-Bits-Offset
3. Get Internal-Pixel-Id Inside 3-Byte-Block
4. Use Modulus with above to get byte address for pixel in video buffer
The furthest I could get was:
I could not figure out how to incorporate the modulus part into my formula nor how to BitShift?
I'm sure the getbit setbit statements could be shortened too,
but I'm struggling to figure out how
It probably has a few bugs/errors, and maybe someone can help me fix them along the way too?.
Thank You
JD
Well moving into learning Spin,
I'm trying to put together a program to write colour values directly to the video buffer,
Colors are made up of 6 bits, 2bits per colour (rgb)
They are stored in a byte array for now.
screen width is 200
screen height is 112
The data for 4 pixels is stored in 3 byte blocks
Bytes RRGGBBRR | GGBBRRGG | BBRRGGBB
Pixels RRGGBB | RRGGBB | RRGGBB | RRGGBB
My main query this time is how do I shorten these several statements into one formula?
1. Get Pixel-Id
PixelId:= (pPixelY * vwScrnWidth) + pPixelX 'X = (Py * ScrnWidth) + Px
2. Get Pixel-Bits-Offset
PxlOffsetBits:= (PixelId * COLORBITS) 'OffsetBits = PixelId (from above) * 6
3. Get Internal-Pixel-Id Inside 3-Byte-Block
Modulus:= PixelId // 4
4. Use Modulus with above to get byte address for pixel in video buffer
Case Modulus 0: CanvasByteAddr:= PxlOffsetBits / 8 DataByte:= Byte[CanvasByteAddr] If GetBit(BITINBYTE, pPixelColor, 5) == 1 SetBit(BITINBYTE, DataByte, 7) If GetBit(BITINBYTE, pPixelColor, 4) == 1 SetBit(BITINBYTE, DataByte, 6) If GetBit(BITINBYTE, pPixelColor, 3) == 1 SetBit(BITINBYTE, DataByte, 5) If GetBit(BITINBYTE, pPixelColor, 2) == 1 SetBit(BITINBYTE, DataByte, 4) If GetBit(BITINBYTE, pPixelColor, 1) == 1 SetBit(BITINBYTE, DataByte, 3) If GetBit(BITINBYTE, pPixelColor, 0) == 1 SetBit(BITINBYTE, DataByte, 2) Byte[CanvasByteAddr]:= DataByte 1: CanvasByteAddr:= (PxlOffsetBits-6) / 8 2: CanvasByteAddr:= (PxlOffsetBits-4) / 8 3: CanvasByteAddr:= (PxlOffsetBits-2) / 8
The furthest I could get was:
DirectAddr:= ((pPixelY * vwScrnWidth) + pPixelX) * COLORBITS
I could not figure out how to incorporate the modulus part into my formula nor how to BitShift?
I'm sure the getbit setbit statements could be shortened too,
but I'm struggling to figure out how
It probably has a few bugs/errors, and maybe someone can help me fix them along the way too?.
Thank You
JD
Comments
Can you tell us what the code is supposed to accomplish?
It may be easier for some of us to figure out an algorithm based on a description of what you are trying to do rather than trying to shorten your code.
What video driver are you using?
What you programmed so painstakingly was just a left-shift by 2.
-Phil
Hello Duane,
I'm trying to set/unset (program) a PIXEL, as opposed to using characters like in the default Parallax VGA driver.
So instead of using characters to draw a border or frame, I want to be able to use pixel level addressing instead of characters on an X,Y grid
I had planned on using 6 bits to represent the color of a pixel.
The full source of my dabblings can be found below.
It may give some of you a laugh hahaha
Thank you
Hello Phil,
Thank You for your input,
So If I understand correctly, my direct pixel address formula should be....
Is that right?
I'm looking forward to trying this stuff out when I get the voltage regulator arrives in the post via ebay,
to down the voltage from 5V to 3.3v for the propeller chip.
Until then its simply just dabbling and exercises in trying to get used to the SPIN language unfortunately.
Thank You
JD