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

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

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

#define TRANSPARENCY_OFF	0 //We don't want a transparent bitmap
#define TRANSPARENCY_ON		1 //Use transparent bitmaps
void UserText(void);

void main(void)
{
   UserText();
   
   Graphix myGfx; //Create the main object
   Bitmap myBackGround; //This object will hold the background picture
   Bitmap myBmp; //Will hold a part of the background bitmap
   Palette myPal; //We want to use just one palette
   
   if(myGfx.SetVideoMode(VGA320x400x256) < 0)
   {
      cout<<"Can't initiate video mode"<<endl;
      return;
   }

   myGfx.ClearVideoMemory(0); //Fill videomemory (all pages) with color black.
   
   if(myGfx.LoadPCX("reaper.pcx", myBackGround, myPal, TRANSPARENCY_OFF, 0) == -1) //Get picture
   {
      myGfx.SetVideoMode(TEXT);
      cout << "Can't find the file 'reaper.pcx'" << endl;
      return;
   }
   
   //Initiate the new keyboard handler. If you don't think this is
   //neccessary, then try to use getch() instead and you will see!
   Keyboard myKbd;
   volatile int* kbdPtr = myKbd.InstallKeyboard(); //Set pointer to key-pressed/released vector
   
   myGfx.SetDrawPage(0);
   myGfx.SetVisiblePage(0);
   myGfx.SetPalette(myPal); //Set active palette
   myGfx.DrawBitmap(0, 0, myBackGround); //Draw background bitmap
   
   int x = 100; //Start position in x-axis for the moving bitmap
   int y = 100;
   
   while(1)
   {
      if(*(kbdPtr + PRESS_LEFT)) //Have we pressed the left arrow key?
	 if(x > 0) //Don't cross the screen's border
	    x -= 2;
      if(*(kbdPtr + PRESS_RIGHT))
	 if(x < 288) //Don't cross the screen's border
	    x += 2;
      if(*(kbdPtr + PRESS_UP))
	 if(y > 0) //Don't cross the screen's border
	    y -= 2;
      if(*(kbdPtr + PRESS_DOWN))
	 if(y < 348) //Don't cross the screen's border
	    y += 2;
      if(*(kbdPtr + PRESS_ESC)) //Leave the main loop?
	 break; //Quit
      
      //Get a 32x52 sized bitmap from the background image and store it
      //in the bitmap object 'myBmp'.
      myGfx.GetRAMBitmap(x, y, 32, 52, myBackGround, myBmp);
      myGfx.FlipBitmapHorizontal(myBmp); //Reverse image in x-axis.
      myGfx.DrawBitmap(x, y, myBmp); //Draw the reversed bitmap
      myGfx.FlipBitmapHorizontal(myBmp); //Restore image in x-axis.
      myGfx.WaitVerticalRetrace(); //Avoid flicker on screen.
      myGfx.DrawBitmap(x, y, myBmp); //Restore the screen's background again
   }
   //Important!!! Don't forget this, otherwise you will be stucked!
   myKbd.UnInstallKeyboard(); //Restore the old(zzzz...) handler
   
   myGfx.SetVideoMode(TEXT); //Go back to text mode.
} //That's all folks!

void UserText(void)
{
   cout << "Ok, are we getting somewhere?\nNow it's time to test the new keyboard handler and to see how we\ncan extract parts from the background image by using a very\nsimple method included in this graphic library.\nThis time we will use a screen resolution of 320x400 pixels.\n" << endl;
   cout << "Use the following keys to move the extracted bitmap on the screen:" << endl;
   cout << "Left arrow key to move left" << endl;
   cout << "Right arrow key to move right" << endl;
   cout << "Up arrow key to move up" << endl;
   cout << "Down arrow key to move down" << endl;
   cout << "Escape key to quit the program" << endl;
   cout << "\nPress a key to continue" << endl;
   
   getch();
}
