Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render: convert tmotion vectors to render basis #11611

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 25 additions & 31 deletions src/render/SDL_render.c
Original file line number Diff line number Diff line change
Expand Up @@ -2788,6 +2788,29 @@ static void SDL_RenderLogicalPresentation(SDL_Renderer *renderer)
}
}

static bool SDL_RenderVectorFromWindow(SDL_Renderer *renderer, float window_dx, float window_dy, float *restrict dx, float *restrict dy)
{
// Convert from window coordinates to pixels within the window
window_dx *= renderer->dpi_scale.x;
window_dy *= renderer->dpi_scale.y;

// Convert from pixels within the window to pixels within the view
if (renderer->logical_presentation_mode != SDL_LOGICAL_PRESENTATION_DISABLED) {
const SDL_FRect *src = &renderer->logical_src_rect;
const SDL_FRect *dst = &renderer->logical_dst_rect;
window_dx = (window_dx * src->w) / dst->w;
window_dy = (window_dy * src->h) / dst->h;
}

const SDL_RenderViewState *view = &renderer->main_view;
window_dx /= view->scale.x;
window_dy /= view->scale.y;

*dx = window_dx;
*dy = window_dy;
return true;
}

bool SDL_RenderCoordinatesFromWindow(SDL_Renderer *renderer, float window_x, float window_y, float *x, float *y)
{
float render_x, render_y;
Expand Down Expand Up @@ -2856,37 +2879,7 @@ bool SDL_ConvertEventToRenderCoordinates(SDL_Renderer *renderer, SDL_Event *even
SDL_Window *window = SDL_GetWindowFromID(event->motion.windowID);
if (window == renderer->window) {
SDL_RenderCoordinatesFromWindow(renderer, event->motion.x, event->motion.y, &event->motion.x, &event->motion.y);

if (event->motion.xrel != 0.0f) {
// Convert from window coordinates to pixels within the window
float scale = renderer->dpi_scale.x;

// Convert from pixels within the window to pixels within the view
if (renderer->logical_presentation_mode != SDL_LOGICAL_PRESENTATION_DISABLED) {
const SDL_FRect *src = &renderer->logical_src_rect;
const SDL_FRect *dst = &renderer->logical_dst_rect;
scale = (scale * src->w) / dst->w;
}

// Convert from pixels within the view to render coordinates
scale = (scale / renderer->main_view.scale.x);
event->motion.xrel *= scale;
}
if (event->motion.yrel != 0.0f) {
// Convert from window coordinates to pixels within the window
float scale = renderer->dpi_scale.y;

// Convert from pixels within the window to pixels within the view
if (renderer->logical_presentation_mode != SDL_LOGICAL_PRESENTATION_DISABLED) {
const SDL_FRect *src = &renderer->logical_src_rect;
const SDL_FRect *dst = &renderer->logical_dst_rect;
scale = (scale * src->h) / dst->h;
}

// Convert from pixels within the view to render coordinates
scale = (scale / renderer->main_view.scale.y);
event->motion.yrel *= scale;
}
SDL_RenderVectorFromWindow(renderer, event->motion.xrel, event->motion.yrel, &event->motion.xrel, &event->motion.yrel);
}
} else if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN ||
event->type == SDL_EVENT_MOUSE_BUTTON_UP) {
Expand All @@ -2912,6 +2905,7 @@ bool SDL_ConvertEventToRenderCoordinates(SDL_Renderer *renderer, SDL_Event *even
return false;
}
SDL_RenderCoordinatesFromWindow(renderer, event->tfinger.x * w, event->tfinger.y * h, &event->tfinger.x, &event->tfinger.y);
SDL_RenderVectorFromWindow(renderer, event->tfinger.dx * w, event->tfinger.dy * h, &event->tfinger.dx, &event->tfinger.dy);
}
} else if (event->type == SDL_EVENT_PEN_MOTION) {
SDL_Window *window = SDL_GetWindowFromID(event->pmotion.windowID);
Expand Down
Loading