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

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

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

#define TRANSPARENCY_ON	   1

void UserText(void);

Graphix myGfx; //Create the main object of this library
Mouse myMouse;

void main(void)
{
   UserText();
   
   if( !myMouse.IsMouseAvailable() )
   {
      cout << "There is no mouse available!\nAborting!" << endl;
      return;
   }

   myGfx.SetVideoMode(VGA320x200x256);
   
   Bitmap Sprite;
   Bitmap EnemySprite;
   Palette SpritePal;
   
   //Load the ship and store it in the bitmap object.
   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);
   
   short int playerX = 150;
   short int playerY = 150;
   short int enemyX = 100;
   short int enemyY = 100;
   short int playerWidth = Sprite.GetWidth();
   short int playerHeight = Sprite.GetHeight();

   Sprite.SetCollisionSensitivity(80); //60% sensitivity in collisions.
   short int leftMouseButton;
   short int dummyButton;

   while(1)
   {
      //Get the current mouse pointer position on screen.
      myMouse.GetMousePositionAndButtons(playerX, playerY, leftMouseButton, dummyButton, dummyButton);

      if( leftMouseButton ) //Did we press the left button on the mouse.
	 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 mouse functions supported by this library.\nThe program will try to detect a mouse and if there is one present you will\nbe able to move a sprite with your mouse (press the left mouse button to stop\nthe test program). If there is no mouse present the program will abort." << endl;
   cout << "Press a key to continue!" << endl;

   getch();
}
