2023-07-15

How to know if the mouse is on a image in SDL2?

What I need :

I want that when the mouse is on my image, something happen. You know like in HTML, when your mouse is on a link, HTML know it and what happen is that the cursor change to pointer. But unfortunatly, I already know that it is not to easy like in HTML and it is why I am asking you this question.

What I am expecting:

I am expecting that when the user of my game put is mouse on my button, the function that draw the button, will check if the mouse is on the button and if it is clicked like this:

void draw(SDL_Rect infoImage, SDL_Renderer* renderer) {
    bool isHover = 0, isClicked = 0;
    
    //This if is for the hover, or in other word, to see if the mouse is on my image
    if(/*I need this condition*/){
       isHover = 1;
       
       //To see if the image is clicked
       int x, y;
       if(SDL_MOUSEBUTTONDOWN && SDL_BUTTON(SDL_GetMouseState(&x, &y)) == SDL_BUTTON_LEFT){
           isClicked = 1;
       }
    }
    
    //To draw the image
    SDL_RenderCopy(renderer, this->texture, NULL, &infoImage);
    return isHover, isClicked;
}


No comments:

Post a Comment