[client,sdl] add coordinat conversion functions

* pixel to screen mapper
* screen to pixel mapper
* local scaling add/remove for smart-sizing
This commit is contained in:
akallabeth
2026-01-25 12:14:36 +01:00
parent 02a9d61182
commit 2e7639d903
2 changed files with 56 additions and 0 deletions

View File

@@ -927,6 +927,17 @@ bool SdlContext::handleEvent(const SDL_DisplayEvent* ev)
return true;
}
bool SdlContext::eventToPixelCoordinates(SDL_WindowID id, SDL_Event& ev)
{
auto w = getWindowForId(id);
if (!w)
return false;
auto renderer = SDL_GetRenderer(w->window());
if (!renderer)
return false;
return SDL_ConvertEventToRenderCoordinates(renderer, &ev);
}
SDL_FPoint SdlContext::applyLocalScaling(const SDL_FPoint& val) const
{
if (!freerdp_settings_get_bool(context()->settings, FreeRDP_SmartSizing))
@@ -946,6 +957,44 @@ void SdlContext::removeLocalScaling(float& x, float& y)
y /= _localScale.y;
}
SDL_FPoint SdlContext::screenToPixel(SDL_WindowID id, const SDL_FPoint& pos)
{
auto w = getWindowForId(id);
if (!w)
return {};
auto renderer = SDL_GetRenderer(w->window());
if (!renderer)
return {};
SDL_FPoint rpos{};
if (!SDL_RenderCoordinatesFromWindow(renderer, pos.x, pos.y, &rpos.x, &rpos.y))
return {};
removeLocalScaling(rpos.x, rpos.y);
return rpos;
}
SDL_FPoint SdlContext::pixelToScreen(SDL_WindowID id, const SDL_FPoint& pos)
{
auto w = getWindowForId(id);
if (!w)
return {};
auto renderer = SDL_GetRenderer(w->window());
if (!renderer)
return {};
SDL_FPoint rpos{};
if (!SDL_RenderCoordinatesToWindow(renderer, pos.x, pos.y, &rpos.x, &rpos.y))
return {};
return applyLocalScaling(rpos);
}
SDL_FRect SdlContext::pixelToScreen(SDL_WindowID id, const SDL_FRect& pos)
{
const auto fpos = pixelToScreen(id, SDL_FPoint{ pos.x, pos.y });
const auto size = pixelToScreen(id, SDL_FPoint{ pos.w, pos.h });
return { fpos.x, fpos.y, size.x, size.y };
}
bool SdlContext::drawToWindows(const std::vector<SDL_Rect>& rects)
{
for (auto& window : _windows)

View File

@@ -120,6 +120,11 @@ class SdlContext
[[nodiscard]] wLog* getWLog();
[[nodiscard]] SDL_FPoint screenToPixel(SDL_WindowID id, const SDL_FPoint& pos);
[[nodiscard]] SDL_FPoint pixelToScreen(SDL_WindowID id, const SDL_FPoint& pos);
[[nodiscard]] SDL_FRect pixelToScreen(SDL_WindowID id, const SDL_FRect& pos);
[[nodiscard]] bool handleEvent(const SDL_WindowEvent* ev);
[[nodiscard]] bool handleEvent(const SDL_DisplayEvent* ev);
@@ -134,6 +139,8 @@ class SdlContext
[[nodiscard]] static BOOL endPaint(rdpContext* context);
[[nodiscard]] static DWORD WINAPI rdpThreadRun(SdlContext* sdl);
[[nodiscard]] bool eventToPixelCoordinates(SDL_WindowID id, SDL_Event& ev);
[[nodiscard]] SDL_FPoint applyLocalScaling(const SDL_FPoint& val) const;
void removeLocalScaling(float& x, float& y);