Hey guys,
I am stuck with certain problem: I manage stuff inside of sth you can call a scene and I want to keep the aspect ratio of the scene when the window is resized. I want to do this by changing the viewport. I am used to the fact that the viewport is given in pixels as sf::View seems to use percentage values of the window size I scale my calculated viewport down with these lines:
// down scale to % units ; windoww/h are from the resize event
viewport.width = viewport.width / windoww;
viewport.height = viewport.height / windowh;
// center
viewport.left = ( 1.0 - viewport.width ) * 0.5;
viewport.top = ( 1.0 - viewport.height ) * 0.5;
But it seems that my calculations are always wrong :/ and I guess I am stuck in my head so I can't see the problem. I tried this way to get the right upscaled/cropped size:
// in = ( scenew, sceneh ); clip = ( windoww, windowh )
sf::Vector2f scaleToFit( const sf::Vector2f& in, const sf::Vector2f& clip )
{
sf::Vector2f ret( in );
if ( ( clip.y * in.x ) / in.y >= clip.x )
{
ret.y = ( clip.x * in.y ) / in.x;
ret.x = clip.x;
}
else if ( ( clip.x * in.y ) / in.x >= clip.y )
{
ret.x = ( clip.y * in.x ) / in.y;
ret.y = clip.y;
}
else
ret = clip;
return ret;
}
All I get is strange resizing. The scene keeps in the center but if I extend the width of the window (there should be black panels on the left and right) the black panels get very big so the scene does not keep its ratio and I don't know why. I hope I can get some help here.
Just for your information: This is what all happens on the resize event but I don't expect an error there:
sf::View v( sf::FloatRect( 0, 0, scenew, sceneh ) );
sf::FloatRect viewport( sf::Vector2f( 0, 0 ), scaleToFit( sf::Vector2f( screenw, screenh ), sf::Vector2f( windoww, windowh ) ) );
viewport.width = viewport.width / basicSettings.windoww;
viewport.height = viewport.height / basicSettings.windowh;
viewport.left = ( 1.0 - viewport.width ) * 0.5;
viewport.top = ( 1.0 - viewport.height ) * 0.5;
v.setViewport( viewport );
window.setView( v );