2D rendererSDL 3.2

SDL_RenderCoordinatesFromWindow

Converts a window position (e.g. the mouse) into the renderer's drawing coordinates.

Signature

bool SDL_RenderCoordinatesFromWindow(SDL_Renderer *renderer, float window_x, float window_y, float *x, float *y);

How it works

With a logical presentation or a render scale, a mouse at window point (800, 450) may be at game point (320, 180). This function undoes the scaling and the black bars, so you can compare the mouse with your sprites directly. SDL_ConvertEventToRenderCoordinates converts a whole event in place.

Parameters

rendererSDL_Renderer *

The renderer.

window_xfloat

x in window coordinates.

window_yfloat

y in window coordinates.

xfloat *

Receives x in render coordinates.

yfloat *

Receives y in render coordinates.

Return value

true on success, false on failure.

Example

example.cpp
1float lx, ly;
2SDL_RenderCoordinatesFromWindow(renderer, e.motion.x, e.motion.y, &lx, &ly);
3hovered = pointInRect(lx, ly, button);
Official SDL wiki