# 256color

Overview

This example demonstrates how to work in a virtual 8 bit display mode. It enters a virtual 8bit mode, sets up surface and display palettes, draws color gradient across an 8bit surface and updates this surface to the display.

Description

Initialization
The program starts by initializing PTC. It first tries to initialize from the command line, and if that fails falls back to a virtual 8 bit mode. Then the program gets the display resolution from the PTC object and creates a fullscreen INDEX8 surface.

Palette setup
Next a smooth purple gradient palette is created and used to set the surface palette, and if needed the display palette as well. Setting the surface palette is required because when INDEX8 surfaces are first created, they have no palette attached. Until you set the surface palette, all bitblts and updates from an INDEX8 surface will appear solid black. Note that the display palette is set carefully because the output format could be anything. Setting the display palette in truecolor modes is avoided for obvious reasons (there isn't one), however the reason for not setting the display palette when in greyscale is not so obvious. The reason is that if the palette is set while in a GREY8 mode then the greyscale lookup table is modified and the output will not longer be grey. Even though this can be quite useful to change the greyscale output to another color, it is not desirable in this case because we want plain greyscale output.

Drawing the color gradient
The color gradient is drawn by locking the surface to access the surface memory, then stepping through each pixel setting the color to the current x position (looping back to zero after 255), then when finished drawing, unlocking the surface memory. The surface is then updated to the display and the program waits for the user to press a key.

Source code

`///////////////////////`
`// 256 color example //`
`///////////////////////`
`#include "ptc.h"`

`int main(int argc,char *argv[])`
`{`
`// initialize ptc from command line`
`PTC ptc(argc,argv);`
`if (!ptc.ok())`
`{`
`// default to virtual 8bit`
`ptc.Init(320,200,VIRTUAL8);`
`if (!ptc.ok())`
`{`
`// failure`
`ptc.Error("could not initialize ptc");`
`return 1;`
`}`
`}`
``
`// get display resolution`
`int xres=ptc.GetXResolution();`
`int yres=ptc.GetYResolution();`

`// create fullscreen 256 color surface`
`Surface surface(ptc,xres,yres,INDEX8);`
`if (!surface.ok())`
`{`
`// failure`
`ptc.Error("could not create 256 color surface");`
`return 1;`
`}`

`// create palette`
`Palette palette;`
`uint* data=(uint*)palette.Lock();`
`if (!data) return 1;`
`for (int i=0; i<256; i++) data[i]=RGB32(i/2,i/4,i);`
`palette.Unlock();`

`// set surface palette`
`surface.SetPalette(palette);`

`// set display palette if required`
`FORMAT format=ptc.GetFormat();`
`if (format.type==INDEXED && format.model!=GREYSCALE) ptc.SetPalette(palette);`

`// draw a color gradient across the surface`
`uchar *buffer=(uchar*)surface.Lock();`
`if (!buffer) return 1;`
`int pitch=surface.GetPitch();`
`for (int y=0; y<yres; y++)`
`{`
`for (int x=0; x<xres; x++)`
`{`
`*(buffer+y*pitch+x)=(uchar)x;`
`}`
`}`
`surface.Unlock();`

`// update to display`
`surface.Update();`
`ptc.getch();`
`return 0;`
`}`