///////////////////////
// 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 FUZZY 8bit
        ptc.Init(320,200,FUZZY8);
        if (!ptc.ok())
        {
            ptc.Close();
            cout << "could not initialize ptc\n";
            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())
    {
        ptc.Close();
        cout << "could not create 256 color surface\n";
        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/4,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();
    while (!ptc.kbhit());
    return 0;
}
