// Example 'paltest'
// Tommy Grandefors
// 2D32 Graphic library
// Beta v1.0
// February 1997

#include <graphix.hh>
#include <videodef.hh>

#include <iostream.h>
#include <conio.h>

#define NO_TRANSPARENCY	0 //We don't want a transparent bitmap

void UserText(void);

void main(void)
{
   UserText();
   
   Graphix myGfx; //Instantiate the main object
   Bitmap myBmp; //This object will hold the PCX picture
   Palette myPal; //Will hold the picture's palette
   
   if(myGfx.SetVideoMode(SVGA800x600x256) < 0)
   {
      cout<<"Can't initiate video mode"<<endl;
      return;
   }   

   myGfx.ClearVideoMemory(0); //Fill videomemory (all pages) with color black.
   
   if(myGfx.LoadPCX("newton.pcx", myBmp, myPal, NO_TRANSPARENCY, 0) == -1) //Get picture
   {
      myGfx.SetVideoMode(TEXT);
      cout << "Can't open the file 'newton.pcx'" << endl;
      return;
   }
   
   myGfx.SetDrawPage(0); //Set first memory page to be the drawing page
   myGfx.SetVisiblePage(0); //Set the drawing page to be the visible page.
   myGfx.DrawBitmap(0, 0, myBmp); //Draw bitmap on screen (upper left corner)
   myGfx.SetPalette(myPal); //Set active palette again every time you change mode.
   getch();

   unsigned char z, red, green, blue = 0; //Some declarations
   unsigned char key = getch();

   while(key != 'q')
   {
      switch(key)
      {
	 //Increase RED palette
	 case 'a': for( z = 0; z < 255; z++)
	 {
	    red = myPal.GetRed(z);
	    myPal.SetRed(z,++red);
	 }
	 myGfx.SetPalette(myPal);
	 break;
	 //Deccrease RED palette
	 case 'z': for( z = 0; z < 255; z++)
	 {
	    red = myPal.GetRed(z);
	    myPal.SetRed(z, --red);
	 }
	 myGfx.SetPalette(myPal);
	 break;
	 //Increase GREEN palette
	 case 's': for( z = 0; z < 255; z++)
	 {
	    green = myPal.GetGreen(z);
	    myPal.SetGreen(z, ++green);
	 }
	 myGfx.SetPalette(myPal);
	 break;
	 //Deccrease GREEN palette
	 case 'x': for( z = 0; z < 255; z++)
	 {
	    green = myPal.GetGreen(z);
	    myPal.SetGreen(z, --green);
	 }
	 myGfx.SetPalette(myPal);
	 break;
	 //Increase BLUE palette
	 case 'd': for( z = 0; z < 255; z++)
	 {
	    blue = myPal.GetBlue(z);
	    myPal.SetBlue(z, ++blue);
	 }
	 myGfx.SetPalette(myPal);
	 break;
	 //Deccrease BLUE palette
	 case 'c': for(z = 0; z < 255; z++)
	 {
	    blue = myPal.GetBlue(z);
	    myPal.SetBlue(z, --blue);
	 }
	 myGfx.SetPalette(myPal);
	 break;
	 //Flip bitmap in horizontal axis
	 case 'f': myGfx.FlipBitmapHorizontal(myBmp);
	 myGfx.DrawBitmap(0, 0, myBmp); //Draw it again!
	 break;
	 //Flip bitmap in vertical axis
	 case 'v': myGfx.FlipBitmapVertical(myBmp);
	 myGfx.DrawBitmap(0, 0, myBmp); //Draw it again!
	 break;
	 //Toggle negative palette
	 case 'g': myPal.ConvertToNegativePalette();
	 myGfx.SetPalette(myPal);
	 break;
      }
      key = getch();
   }
   
   myGfx.SetVideoMode(TEXT); //Go back to text mode.
} //That's all folks!

void UserText(void)
{
   cout<<"This example will show you the different palette manipulation methods."<<endl;
   cout<<"You will also be able to flip a bitmap in different directions."<<endl;
   cout << "This example will use a screen resolution of 800x600 pixels." << endl;
   cout<<"The followings keys are available:"<<endl;
   cout<<"'a' will increase the RED palette index"<<endl;
   cout<<"'z' will decrease the RED palette index"<<endl;
   cout<<"'s' will increase the GREEN palette index"<<endl;
   cout<<"'x' will decrease the GRENN palette index"<<endl;
   cout<<"'d' will increase the BLUE palette index"<<endl;
   cout<<"'c' will decrease the BLUE palette index"<<endl;
   cout<<"'f' will flip the bitmap in the horizontal axis"<<endl;
   cout<<"'v' will flip the bitmap in the vertical axis"<<endl;
   cout<<"'g' will toggle the palette into its negative colors"<<endl;
   cout<<"'q' will quit the program"<<endl;

   getch();
}
