// Example 'scaling'
// Tommy Grandefors
// 2D32 Graphic library
// Beta v1.0
// May 1997

#include <graphix.hh>
#include <videodef.hh>
#include <keydefs.hh> //Contains defines for the most common keyboard codes.
#include <keyboard.hh>

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

#define TRANSPARENCY_OFF   0

void UserText(void);

void main(void)
{
   Graphix myGfx; //Create the main object of this library
   Keyboard myKbd; //Use the special keyboard handler.

   UserText();

   volatile int* kbdPtr = myKbd.InstallKeyboard(); //Set pointer to key-pressed/released vector
   
   if( myGfx.SetVideoMode(SVGA640x480x256) < 0)
   {
      cout << "Can't initiate SVGA video mode 640x480x256" << endl;
      return;
   }
   
   Bitmap Sprite;
   Bitmap tmpBmp; //Used for storing the scaled result in.
   Palette SpritePal;
   
   //Load the ship and store it in the bitmap object.
   myGfx.LoadPCX("ship.pcx", Sprite, SpritePal, TRANSPARENCY_OFF, 192);

   myGfx.SetPalette(SpritePal);
   
   myGfx.SetVisiblePage(0);
   myGfx.SetDrawPage(0);
   
   short int newXSize = Sprite.GetWidth();
   short int newYSize = Sprite.GetHeight();

   while(1)
   {
      if( *(kbdPtr + PRESS_LEFT) ) //Did the user press the 'left-arrow' key?
	 newXSize -= 2;
      else
	 if( *(kbdPtr+PRESS_RIGHT) )//Did the user press the 'right-arrow' key?
	    newXSize += 2;

      if( *(kbdPtr + PRESS_UP) ) //Did the user press the 'up-arrow' key?
	 newYSize -= 2;
      else
	 if( *(kbdPtr+PRESS_DOWN) ) //Did the user press the 'down-arrow' key?
	    newYSize += 2;

      if( *(kbdPtr + PRESS_ESC) ) //Did the user press the 'Esc' key?
	 break; //Quit

      if( newXSize <= 8 ) 
	 newXSize = 8;
      else
	 if( (newXSize) >= 640 )
	    newXSize = 638;
      if( newYSize <= 8 )
	 newYSize = 8;
      else
	 if( newYSize >= 400 )
	    newYSize = 398;

      //Store the result in 'tmpBmp' since we don't want to destroy the origional 'Sprite'.
      myGfx.ScaleBitmap(Sprite, tmpBmp, newXSize, newYSize);
      myGfx.DrawBitmap(0, 0, tmpBmp); //Draw the new scaled bitmap
      myGfx.WaitVerticalRetrace();
   }
   
   myGfx.SetVideoMode(TEXT);
}

void UserText(void)
{
   cout << "This example will let you scale a bitmap in both x- and y-axis at the same time.This time we are using the screen resolution of 640x400 pixels and we will\nscale the bitmap 2 pixels each time in x- and y-axis during scaling\nUse the arrow keys to scale the bitmap to different sizes" << endl;
   cout << "Press a key to continue!" << endl;

   getch();
}
