SFML community forums
Help => General => Topic started by: easy on August 20, 2011, 01:06:05 pm
-
Hi!
I've got an issue. When I resize the game window, and all the views including the default view, the text I draw on the default view changes it's position, no matter if I reposition it with "SetPosition(20.f, 20.f);", or not.
The truncated code is:
// Process events
sf::Event Event;
while (window.PollEvent(Event))
{
if (Event.Type == sf::Event::Resized)
{
defaultCamera.SetSize(Event.Size.Width, Event.Size.Height);
window.SetView(defaultCamera); // ... Just trying to make it work
FPSText.SetPosition(20.f, 20.f); // ... Here too
}
}
See two screenshots of the resize:
(http://kepfeltoltes.hu/thumb/110820/sfml2-resize_www.kepfeltoltes.hu_.jpg) (http://kepfeltoltes.hu/view/110820/sfml2-resize_www.kepfeltoltes.hu_.jpg)
(http://kepfeltoltes.hu/thumb/110820/sfml2-resize2_www.kepfeltoltes.hu_.jpg) (http://kepfeltoltes.hu/view/110820/sfml2-resize2_www.kepfeltoltes.hu_.jpg)
Am I missing something?
Thanks for your answers,
easy
-
Answering my own question for that anybody can find this solution:
FPSText.SetPosition(window.ConvertCoords(20, 20, defaultCamera));
And the reason behind it is:
////////////////////////////////////////////////////////////
/// \brief Convert a point from target coordinates to view coordinates
///
/// Initially, a unit of the 2D world matches a pixel of the
/// render target. But if you define a custom view, this
/// assertion is not true anymore, ie. a point located at
/// (10, 50) in your render target (for example a window) may
/// map to the point (150, 75) in your 2D world -- for example
/// if the view is translated by (140, 25).
///
/// For render windows, this function is typically used to find
/// which point (or object) is located below the mouse cursor.
///
/// This version uses a custom view for calculations, see the other
/// overload of the function to use the current view of the render
/// target.
///
/// \param x X coordinate of the point to convert, relative to the render target
/// \param y Y coordinate of the point to convert, relative to the render target
/// \param view The view to use for converting the point
///
/// \return The converted point, in "world" units
///
////////////////////////////////////////////////////////////
Vector2f ConvertCoords(unsigned int x, unsigned int y, const View& view) const;