Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Pogwizd on Sun 05/04/2020 15:24:19

Title: Is there a way to detect the transparent background of an image.
Post by: Pogwizd on Sun 05/04/2020 15:24:19
Hi guys,

Is there a way to detect if a mouse click happened on the transparent background of an image (of an object)? I'm sorry if this question's been asked before. I did go through many old threats but couldn't find the answer I was looking for.
Title: Re: Is there a way to detect the transparent background of an image.
Post by: Cassiebsg on Sun 05/04/2020 17:21:46
Quick answer is yes. You have two choices, as far as I remember, if you want this for all your images, then go to the game settings and there's an option called something like "pixel perfect" turn it to false, and I belive that'll do it (I'm not 100% sure though).

If if you want this for only a few images and the others react normal to alpha, then edit your spirte, and give the alpha 99% transparency. Player won't notice it, but the engine will.  ;)
Title: Re: Is there a way to detect the transparent background of an image.
Post by: Pogwizd on Sun 05/04/2020 21:39:59
Cassiebsg, thank you for a quick reply :) Your solution suggested me another approach to my problem. However, I think what I had in mind was a way to check whether or not the given pixel of the image is a part of the transparent background.
Title: Re: Is there a way to detect the transparent background of an image.
Post by: ManicMatt on Sun 05/04/2020 23:34:25
i take it your image has a solid green colour for its background. You know you can just have no background solid colour, right? But leave transparency as is when importing sprites. Are you wanting to make sure that green isn't being used as part of the object? I'm confused as to what you are trying to achieve.
Title: Re: Is there a way to detect the transparent background of an image.
Post by: Privateer Puddin' on Mon 06/04/2020 01:20:15
This is taken from doing it for a GUI, but the same concept should apply - draw a Dynamic Sprite version of the object image without transparency, check the color of where the mouse is and that should tell you whether it's transparent or not. (Snarky did this for me in another thread somewhere)

Code (ags) Select
bool MouseOverPixelPerfect(this Button*, int mouseOverGraphic, String label)
{
  // Calculate mouse coordinates relative to button sprite canvas
  int x = mouse.x - this.X - this.OwningGUI.X;
  int y = mouse.y - this.Y - this.OwningGUI.Y;

  // Get the color on button sprite canvas at mouse cursor pixel
  DynamicSprite* buttonMask = DynamicSprite.CreateFromExistingSprite(this.NormalGraphic, false);
  DrawingSurface* maskSurface = buttonMask.GetDrawingSurface();
  int pixelColor = maskSurface.GetPixel(x,y);
  maskSurface.Release();
  buttonMask.Delete();

  if(pixelColor == COLOR_TRANSPARENT) // if this doesn't work then try your harcoded number, 63519
  {
    // Transparent, so don't highlight
    this.MouseOverGraphic = this.NormalGraphic;
    return false;
  }
  // Otherwise, highlight
  this.MouseOverGraphic = mouseOverGraphic;
  return true;
}
Title: Re: Is there a way to detect the transparent background of an image.
Post by: Pogwizd on Mon 06/04/2020 16:18:26
Privateer Puddin', thank you so much for your help. This is exactly what I needed! I've slightly adjusted the code you posted and it works like a charm.

ManicMatt, I've made a jigsaw puzzle where you drag pieces with the mouse. However, because most of the pieces don't have perfect rectangular shapes, I wanted to prevent dragging them when the player clicks on their transparent background.
Title: Re: Is there a way to detect the transparent background of an image.
Post by: ManicMatt on Mon 06/04/2020 20:02:28
Oh, well if the code works for you, great! I don't really understand why all of that is necessary though if one uses a transparent background instead of a purple or green etc background, but hey ho it works pixel perfect for me. (Not for a GUI)
Title: Re: Is there a way to detect the transparent background of an image.
Post by: Cassiebsg on Tue 07/04/2020 06:59:53
Yes, I thought actually you wanted to increase the clickable area of small objects/characters in rooms and was wondering how to detect the alpha of an object for that purpose.  (laugh)
Title: Re: Is there a way to detect the transparent background of an image.
Post by: Pogwizd on Tue 07/04/2020 18:41:02
Quote
I thought actually you wanted to increase the clickable area of small objects/characters

No, I haven't got this issue (yet!), but when I do I will use your suggestion ;)