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

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

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

#define TRANSPARENCY_ON	   1

void UserText(void);

Graphix myGfx; //Create the main object of this library.
Joystick myStick(1); //Use joystick no 1.

void main(void)
{
   UserText();

   if(!myStick.IsJoystickAvailable())
   {
      cout << "Can't detect a joystick!" << endl;
      return;
   }
   else
   {
      cout << "A joystick was detected!" << endl;
      cout << "Now we must calibrate the joystick.\n" << endl;
   }
   
   cout << "Move joystick to left and then press fire." << endl;
   while( !myStick.IsButtonPressed(1) )
   { myStick.CalibrateLeft(); }
   while( myStick.IsButtonPressed(1) ) {}
   
   cout << "Move joystick to Right and then press fire." << endl;
   while( !myStick.IsButtonPressed(1) )
   { myStick.CalibrateRight(); }
   while( myStick.IsButtonPressed(1) ) {}
   
   cout << "Move joystick up and then press fire"<<endl;
   while( !myStick.IsButtonPressed(1) )
   { myStick.CalibrateUp(); }
   while( myStick.IsButtonPressed(1) ) {}
   
   cout << "Move joystick Down and then press fire" << endl;
   while( !myStick.IsButtonPressed(1) )
   { myStick.CalibrateDown(); }
   while( myStick.IsButtonPressed(1) ) {}
   
   cout<<"Move joystick to neutral and then press fire"<<endl;
   while( !myStick.IsButtonPressed(1) )
   { myStick.CalibrateNeutral(); }
   while( myStick.IsButtonPressed(1) ) {}
   
   myStick.SetSensitivity(90); //90% out of 100%
   
   myGfx.SetVideoMode(VGA320x200x256);
   
   Bitmap Sprite;
   Bitmap EnemySprite;
   Palette SpritePal;
   
   myGfx.LoadPCX("ship.pcx", Sprite, SpritePal, TRANSPARENCY_ON, 192);

   EnemySprite = Sprite; //Let the enemy use the same bitmap.
   
   myGfx.SetPalette(SpritePal);
   
   myGfx.SetVisiblePage(0);
   myGfx.SetDrawPage(0);
   
   int playerX = 150;
   int playerY = 150;
   int enemyX = 100;
   int enemyY = 100;
   int playerWidth = Sprite.GetWidth();
   int playerHeight = Sprite.GetHeight();

   Sprite.SetCollisionSensitivity(80); //60% sensitivity in collisions.

   while(1)
   {
      if( myStick.MoveLeft() ) //Did we move the joystick to the left?
	 playerX -= 2;
      else
	 if( myStick.MoveRight() ) //Did we move the joystick to the right?
	    playerX += 2;

      if( myStick.MoveUp() ) //Did we move the joystick upwards?
	 playerY -= 2;
      else
	 if( myStick.MoveDown() ) //Did we move the joystick downwards?
	    playerY += 2;

      if( myStick.IsButtonPressed(1) ) //Did we press button no 1?
	 break; //Quit

      if( playerX <= 0 ) //We don't want to move the sprite outside the border
	 playerX = 0;
      else
	 if( (playerX + playerWidth) >= 320 )
	    playerX = 320 - playerWidth;
      if( playerY <= 0 )
	 playerY = 0;
      else
	 if( (playerY + playerHeight) >= 200 )
	    playerY = 200 - playerHeight;

      //Check if the player collided with the enemy
      if( Sprite.DetectBitmapCollision(playerX, playerY, EnemySprite, enemyX, enemyY) )
	 myGfx.Print(0, 0, "Collision detected!");
      else
	 myGfx.FilledBox(0, 0, 160, 8, 0); //Hide the text!
      
      myGfx.DrawBitmap(playerX, playerY, Sprite);
      myGfx.DrawBitmap(enemyX, enemyY, EnemySprite);
      myGfx.WaitVerticalRetrace();
      myGfx.FilledBox(playerX, playerY, playerX + playerWidth, playerY + playerHeight, 0); //Erase the sprite
   }
   
   myGfx.SetVideoMode(TEXT);
}

void UserText(void)
{
   cout << "This example will let you test the joystick functions available in this\nlibrary. The program will try to detect a joystick and if there is one present\nyou will be promted to calibrate the josytick and then move a sprite with the\njoystick (press the first joystick button to stop the test program). If there\nis no joystick present the program will abort." << endl;
   cout << "Press a key to continue!" << endl;

   getch();
}
