//Example 'Scroll'
//Tommy Grandefors
//2D32 Graphic library
//Version: Beta v1.0
//October 1997

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

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

#define TRANSPARENCY_OFF	0
#define UP     0
#define DOWN   1

void UserText(void);

Graphix myGfx;
Keyboard myKbd;

void main(void)
{
   UserText();

   if(myGfx.SetVideoMode(SVGA640x480x256, 32) < 0) //32 = max. viewport size
   {
      cout<<"Can't initiate video mode"<<endl;
      return;
   }

   Bitmap Sprite;
   Palette SpritePal;
   
   //The tiles has a size of 32x32 pixels.
   if( myGfx.LoadPCX("tile.pcx", Sprite, SpritePal, TRANSPARENCY_OFF, 0) == -1)
   {
      myGfx.SetVideoMode(TEXT);
      cout << "Can't find the file 'tile.pcx'" << endl;
      return;
   }
   
   myGfx.SetPalette(SpritePal);

   //Draw the first row of tiles outside the visible screen (32 lines)
   for(int i = 0; i < 20; i++)
	 myGfx.DrawBitmap(i * 32, -31, Sprite);
   
   for(i = 0; i < 20; i++)
      for(int j = 0; j < 16; j++)
	 myGfx.DrawBitmap(i * 32, j * 32, Sprite);

   volatile int* kbdPtr = myKbd.InstallKeyboard(); //Set pointer to key-pressed/released vector
   
   int scrollSpeed = 1; //Scroll 1 row each time (max. is 32 rows).

   while(1)
   {
      if( *(kbdPtr + PRESS_LEFT) ) //Left arrow key pressed?
      {
	 if( --scrollSpeed < 0) //Decrease scrolling speed by 1.
	    scrollSpeed = 0;
	 myGfx.SetScrollingSpeed(--scrollSpeed);
      }
      else
	 if( *(kbdPtr + PRESS_RIGHT) )
	 {
	    if( ++scrollSpeed > 32) //Increase scrolling speed by 1.
	       scrollSpeed = 32; 
	    myGfx.SetScrollingSpeed(scrollSpeed);
	 }
      if( *(kbdPtr + PRESS_UP) )
	 myGfx.ScrollY(UP);
      else
	 if( *(kbdPtr + PRESS_DOWN) )
	    myGfx.ScrollY(DOWN);
      if( *(kbdPtr + PRESS_ESC) )
	 break; //Quit
      
      myGfx.WaitVerticalRetrace();
   }
   
   myKbd.UnInstallKeyboard();
   myGfx.SetVideoMode(TEXT);
}

void UserText(void)
{
   cout << "This small example will show you the scrolling capabilities supported by the" << endl;
   cout << "2D32 library. You will be able to scroll the whole screen with your arrow keys:" << endl;
   cout << "Left arrow key will let you decrease the scrolling speed." << endl;
   cout << "Right arrow key will let you increase the scrolling speed." << endl;
   cout << "Up arrow key will let you scroll the screen upwards." << endl; 
   cout << "Down arrow key will let you scroll the screen downwards." << endl; 
   cout << "The 'Esc' key will let you quit the example." << endl;
   cout << "The screen resolution will be 640x480 pixels." << endl;
   cout << "\nPress a key to continue!" << endl;
   getch();
}
