# rand16

Overview

This example demonstrates how to work in a virtual 16 bit display mode. It draws random pixels to a 16 bit RGB565 surface and updates this surface to the display until the user presses a key.

Description

This example is exactly the same as the rand32 example, except that it works in a virtual 16 bit mode and renders to RGB565 internally. It uses the RGB16 function to pack the red, green and blue bytes into a 16 bit RGB565 pixel.

Source code

`//////////////////////////////////`
`// random 16bit RGB565 putpixel //`
`//////////////////////////////////`
`#include "ptc.h"`

`int main(int argc,char *argv[])`
`{`
`// initialize ptc from command line (eg: "rand32 640 480 ARGB8888")`
`PTC ptc(argc,argv);`
`if (!ptc.ok())`
`{`
`// fallback to virtual 16bit`
`if (!ptc.Init(320,200,VIRTUAL16))`
`{`
`// failure`
`ptc.Error("could not initialize ptc");`
`return 1;`
`}`
`}`

`// get display resolution`
`int xres=ptc.GetXResolution();`
`int yres=ptc.GetYResolution();`

`// create fullscreen surface`
`Surface surface(ptc,xres,yres,RGB565);`

`// main loop`
`while (!ptc.kbhit())`
`{`
`// lock surface`
`char *buffer=(char*)surface.Lock();`
`if (!buffer) return 1;`

`// plot 100 random pixels`
`int pitch=surface.GetPitch();`
`for (int i=0; i<100; i++)`
`{`
`int x=random(xres);`
`int y=random(yres);`
`ushort *pixel=(ushort*)(buffer+pitch*y+x*4);`
`*pixel=RGB16(random(255),random(255),random(255));`
`}`

`// unlock surface`
`surface.Unlock();`

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