This file contains source code for adding HP 525 support to GAPI games. It should work fine on any 256 color palette based pocket pc as far as I know, but it has only been tested on the following machines:
HP Jornada 525
HP Jornada 545 set to 256 color mode
If you test on any other machines please let me know the results.
Overview:
The code uses GDI functions to set up the palette, but this does not affect game speed as it only needs doing at:
1) Start up after grabbing the screen with GAPI
2) If the game is hidden then reactivated in the WM_SETFOCUS message
3) If you want to change the palette
so it doesnt need to be done each update. You can set the palette to anything you like, but to save trouble you might find it easier to just set up a RGB 332 palette and convert from the standard RGB 565 you would normally be using with shift and mask operations. The example code below uses a RGB 332 palette but it is obvious how to set up your own custom colors if you like.
General code changes:
You can detect that you are running on a 256 color machine with the GXGetDisplayProperties function. If you find:
g_gxdp.cBPP == 8 && (g_gxdp.ffFormat & kfPalette) != 0
then you should use the code for palette based machines.
YOU MUST TAKE INTO ACCOUNT that each pixel in the GAPI buffer is an unsigned char instead of an unsigned short and alter your screen update code accordingly.
Setting up the palette.
This should be done in the 3 situations mentioned above, and the code is:
struct {
LOGPALETTE lp;
PALETTEENTRY ape[255];
} pal;
LOGPALETTE *pLP = (LOGPALETTE*) &pal;
pLP->palVersion = 0x300;
pLP->palNumEntries = 256;
for (int i=0; i<256; i++)
{
pLP->palPalEntry[i].peRed = ((i&0xE0)>>5)<<5;
pLP->palPalEntry[i].peGreen = ((i&0x1C)>>2)<<5;
pLP->palPalEntry[i].peBlue = (i&0x03)<<6;
pLP->palPalEntry[i].peFlags = 0;
}
HPALETTE mypal = CreatePalette(pLP);
if (mypal)
{
HDC screenDC = GetDC(NULL);
if (screenDC)
{
SelectPalette(screenDC,mypal,FALSE);
RealizePalette(screenDC);
ReleaseDC(NULL,screenDC);
}
DeleteObject(mypal);
}
This creates an RGB 332 palette, you can do your own colors by altering the red/green/blue settings for each color in the for (int i=0; i<256; i++) loop.
Writing to the screen.
Exactly the same as normal, but remember each pixel is one byte long! If you write the number 1 to a pixel you will get the color in palette entry 1. If you set up the palette as above you will have the equivalent of a RGB 332 palette, that is writing 0x03 will give you pure blue. The code to convert an RGB 565 color to RGB 332 is:
unsigned short rgb565col = whatever;
int rval = (rgb565col&0xE000)>>8;
int gval = (rgb565col&0x0700)>>6;
int bval = (rgb565col&0x0018)>>3;
unsigned char rgb332col = rval|gval|bval;