The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
[SDL2] Texture with alpha and mouse cursor over it
Rang


Joined: 05 Jun 2015
Posts: 13
I load image from png file to surface. It have some transparent parts.
Then i create a texture from surface and drawing it with render.

How can i check if mouse cursor is over transparent pixels of this image?

With old version of SDL (1.2) it was simple. We had surface and function SDL_GetPixel(), checking alpha value with threshold and all is works.

But with new version we have texture, and surface needed to be cleaned, because wasted a lot of memory.

How can i determine the mouse cursor is over the visible part of the image?
Is there simple solution?
Naith


Joined: 03 Jul 2014
Posts: 158
It is possible to retrieve the pixels of a SDL_Texture, but it's a costly process and you shouldn't do it. What you should do instead is checking the pixel color of the surface that is created before the texture is created. This means that you'll have to save the SDL_Surface in some way and destroy it when the SDL_Texture is destroyed. One way you can achieve this is by having both the SDL_Surface and the SDL_Texture inside a struct and destroy everything when the program ends.
Example code below:

Code:

struct STexture
{
   SDL_Surface*  pSurface = NULL;
   SDL_Texture*  pTexture = NULL;
   SDL_Rect      Quad;
};

STexture* pMyTexture = NULL;


Then, when the program is executed, check if the mouse pointer is inside the texture quad (see struct above) and if it is, retrieve the pixel color from the surface and then do whatever you want with the pixel information you retrieve.
Example code for retrieving the pixel color of a SDL_Surface:

Code:

SDL_Color GetPixelColor(SDL_Surface* pSurface, const int X, const int Y)
{
   int      Bpp      = pSurface->format->BytesPerPixel;
   Uint8*   pPixel   = (Uint8*)pSurface->pixels + Y * pSurface->pitch + X * Bpp;

   Uint32 PixelColor = *(Uint32*)pPixel;

   SDL_Color Color = {0, 0, 0, 0};

   SDL_GetRGBA(PixelColor, pSurface->format, &Color.r, &Color.g, &Color.b, &Color.a);

   return Color;
}



And example code on how to use it:

Code:

   // Where on the texture the mouse pointer is positioned, relative to the texture quad's upper left corner
   SVector2D Position = {MousePosition.x - pMyTexture->Quad.x, MousePosition.y - pMyTexture->Quad.y};

   const SDL_Color PixelColor = GetPixelColor(pMyTexture->pSurface, (int)Position.x, (int)Position.y);
      
   printf("Pixel alpha: %i\n", PixelColor.a);



Hope it helps Smile
Rang


Joined: 05 Jun 2015
Posts: 13
In this moment i use same method as you describe, but memory we use x2 (RAM + GPU). Sad

I thought use SDL_Rect (or other structure) for saving borders\points of borders for visible part, but this is more complicated code.
I already have images, and need dynamicly creating this zones.
[SDL2] Texture with alpha and mouse cursor over it
Jonny D


Joined: 12 Sep 2009
Posts: 932
If the use is rare, like on user input, then the performance penalty of reading the pixels directly from the texture might be acceptable.


It also depends on what you want to use this info for.  In some cases, you can do everything you need to do in a shader.



Jonny D



On Thu, Aug 25, 2016 at 12:43 PM, Rang wrote:
Quote:
In this moment i use same method as you describe, but memory we use x2 (RAM + GPU).

I thought use SDL_Rect (or other structure) for saving borders\points of borders for visible part, but this is more complicated code.
I already have images, and need dynamicly creating this zones.


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Re: [SDL2] Texture with alpha and mouse cursor over it
Rang


Joined: 05 Jun 2015
Posts: 13
I need highlight the object and handle click events. Images like buttons. But they not have rectangle forms.

Jonny D wrote:
If the use is rare, like on user input, then the performance penalty of reading the pixels directly from the texture might be acceptable.


It also depends on what you want to use this info for.  In some cases, you can do everything you need to do in a shader.



Jonny D



On Thu, Aug 25, 2016 at 12:43 PM, Rang wrote:
Quote:
In this moment i use same method as you describe, but memory we use x2 (RAM + GPU).

I thought use SDL_Rect (or other structure) for saving borders\points of borders for visible part, but this is more complicated code.
I already have images, and need dynamicly creating this zones.


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org