检测SDL2中SDL_Surface的鼠标点击事件

huangapple go评论73阅读模式
英文:

Detecting mouse clicks for SDL_Surfaces in SDL2

问题

I wanna make a program where if you select a surface it gets put in the selectedPiece variable, but I'm using SDL_PointInRect. And it gives this error "the argument of type "SDL_Surface" is incompatible with parameter of type "const SDL_Rect"" Do you know alternatives to PointInRect that are compatible with Surfaces?

SDL_Surface* selectedPiece;
std::list<SDL_Surface*> pieces;

bool leftClick;

if (SDL_MOUSEBUTTONDOWN)
{
    if (!leftClick && windowEvent.button.button == SDL_BUTTON_LEFT)
    {
        leftClick = true;

        for (auto surface : pieces 
        {
            if (SDL_PointInRect(&mousePos, surface)) //<<<--- the sdl point in rect isnt compatible with the surface
            {
                selectedPiece = surface;
            }
        }
    }
}
英文:

I wanna make a program where if you select a surface it gets put in the selectedPiece variable, but I'm using SDL_PointInRect. And it gives this error "the argument of type "SDL_Surface" is incompatible with parameter of type "const SDL_Rect"" Do you know alternatives to PointInRect that are compatible with Surfaces?

SDL_Surface* selectedPiece;
std::list&lt;SDL_Surface*&gt; pieces;

bool leftClick;

if (SDL_MOUSEBUTTONDOWN)
{
    if (!leftClick &amp;&amp; windowEvent.button.button == SDL_BUTTON_LEFT)
    {
        leftClick = true;

        for (auto surface : pieces 
        {
            if (SDL_PointInRect(&amp;mousePos, surface)) //&lt;&lt;&lt;--- the sdl point in rect isnt compatible with the surface
            {
                selectedPiece = surface;
            }
        }
    }
}

答案1

得分: 0

由于您执行了SDL_BlitSurface(imageSurface, NULL, windowSurface, NULL);,这意味着目标矩形的左上角位于(0,0)。

正确的检查应该是:

SDL_Rect surfacerect{0, 0, surface->w, surface->h};

if (SDL_PointInRect(&mousePos, &surfacerect))
英文:

Since you SDL_BlitSurface(imageSurface, NULL, windowSurface, NULL); it means that the destination rectangle's top left corner is at (0,0).

The proper check would then be:

SDL_Rect surfacerect{0, 0, surface-&gt;w, surface-&gt;h};

if (SDL_PointInRect(&amp;mousePos, &amp;surfacerect))

</details>



huangapple
  • 本文由 发表于 2023年5月24日 19:19:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322980.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定