# rand32

Overview

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

Description

Initialization
The program starts by initializing PTC. Firstly it tried a command line initialize so that the user can pass mode parameters on the command line like this: "rand32 xres yres format". For example "rand32 320 200 RGB565" would run the rand32 program in a 320x200xRGB565 display mode. The program then checks if the command line setup failed with "ptc.ok", and if it has failed falls back to a 320x200 virtual 32 bit mode. This means that the user can just type "rand32" and get a default virtual 32 bit display mode. After PTC initialization the program gets the display resolution from the PTC object and creates a fullscreen ARGB8888 surface.

Main loop
The program loops around until the user presses a key. Each loop around it locks the surface to access the memory, then draws 100 random pixels at a time to this surface before unlocking it and updating to the display. The RGB32 function is used to pack the three red, green, blue bytes into a 32 bit ARGB8888 pixel.

Clean up
The PTC object is cleaned up automatically when the main procedure exits, there is no need to explicitly shut down PTC. At times during the program uses the function "PTC::Error" to shutdown PTC and report an error message.

Source code

``

`////////////////////////////////////`
`// random 32bit ARGB8888 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 32bit`
`if (!ptc.Init(320,200))`
`{
// 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,ARGB8888);`

`// 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);`
`uint *pixel=(uint*)(buffer+pitch*y+x*4);`
`*pixel=RGB32(random(255),random(255),random(255));`
`}`

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

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