| . |
| Palm Programming |
|
|
| Subroutines and Snippets |
In the previous subroutine we had directly accessed the screen to peek at the value of a single pixel. Now we will move on to it's complementary routine - directly accessing the screen to change the value of a single pixel. In this example we will be erasing a single pixel, but the method may be used to change the pixel to any value desired.
Please refer to our peek routine before reading this page. On this page we will assume you have read and understood peeking, and that you know about the global variables we are using (BPP and OSpre35).
subroutines that simplify poking to the screen
The color coding used:
light blue = variables, constants, and subroutines names chosen by the programmer
light green = C defined words, Palm API calls, and all else NOT chosen by the programmer
//a subroutine to write to the screen memory and erase the desired pixel
void ErasePixel(int x,int y)
{
unsigned char *
buffer; // pointer to your offscreen bufferunsigned char *
p;unsigned char
output=0;
// The first order of business is to get a pointer to the start of screen memory ( buffer )
MainDrawWinH = WinGetDrawWindow();
if (OSpre35)
{
buffer = (unsigned char*) MainDrawWinH->displayAddrV20;
} else {
buffer = (unsigned char*) BmpGetBits(WinGetBitmap(MainDrawWinH));
}
// Next we determine which byte to read (depending on the current bit depth and the desired pixel)
if (BPP==8)
{
// 256 color mode
p = buffer + y * 160 + (x);
} else {
if (BPP==4) {
// 16 shades of grey
p = buffer + y * 80 + (x >> 1);
} else {
// 4 shades of grey
p = buffer + y * 40 + (x >> 2);
}
}
output = (*p);
// Finally we use bitmasking to erase the desired pixel from the byte we have read before writing it back to the screen.
if (BPP==8)
{
// output cleared (equal to whole byte)
output=0;
} else {
if (BPP==4)
{
if (x%2==0)
output=((output & 0x0f));
else
output=(output & 0xf0);
} else {
if (x%4==0)
output=((output & 0x3f));
if (x%4==1)
output=((output & 0xcf));
if (x%4==2)
output=((output & 0xf3));
if (x%4==3)
output=(output & 0xfc);
}
}
*p = output;
}
Once we know which OS version we have, and what bitdepth we are using, we are ready to use our new ErasePixel() routine (or the Palm API call WinErasePixel() if we are running on OS3.5 or later)
if (OSpre35)
ErasePixel(x,y);
else
WinErasePixel(x,y);
| Poking to the screen - direct screen access |