Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: mapPixelToCoords(point,view) as static function or public member of sf::View?  (Read 120 times)

0 Members and 1 Guest are viewing this topic.

20lbpizza

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
In the current implementation of mapPixelToCoords it requires a RenderTarget to call.
I have a function handleEvent(sf::event e) which will have information about the relevant view for the object that calls it but not the RenderTarget so I can't call mapPixelToCoords even though I'd like to check for a specific position within a vertex array.

Based on the current implementation it doesn't appear that any member of RenderTarget is needed to perform the calculation:
FloatRect viewport --> Defined from view parameter
normalized --> Defined from parameter point & parameter view
Calculation calls member functions of the view.

Based on this would it make sense to allow for views to directly call this helper function without a RenderTarget object either as a member of view or static call to RenderTarget? If not the context would probably benefit my personal thought process  :).

Vector2f RenderTarget::mapPixelToCoords(const Vector2i& point, const View& view) const
{
    // First, convert from viewport coordinates to homogeneous coordinates
    Vector2f normalized;
    FloatRect viewport = FloatRect(getViewport(view));
    normalized.x = -1.f + 2.f * (static_cast<float>(point.x) - viewport.left) / viewport.width;
    normalized.y =  1.f - 2.f * (static_cast<float>(point.y) - viewport.top)  / viewport.height;

    // Then transform by the inverse of the view matrix
    return view.getInverseTransform().transformPoint(normalized);
}

20lbpizza

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Ah sorry! Looks like I also missed the interaction with RenderTarget::getViewport() ...
Maybe I should close this topic since the x/y size of the render target needs to be combined with the view's viewport settings.

Because of that both options, static member function or as public member of sf::View don't make sense!