1
Graphics / Re: Letterbox effect using a view - not on position 0,0
« on: May 20, 2016, 08:44:43 pm »
I sat down and took the time to do the math and solved it.
Here it is for anyone interested.
gameViewPort - the viewport of the letterboxed view (usually the default views's / main view)
gameWindowSize - current game window size (ex: 1280x720)
gameBaseSize - base game window size (ex: 640x480)
viewPosition - position of the view to change (any view within the main view)
viewSize- sizeof the view to change (any view within the main view)
Here it is for anyone interested.
gameViewPort - the viewport of the letterboxed view (usually the default views's / main view)
gameWindowSize - current game window size (ex: 1280x720)
gameBaseSize - base game window size (ex: 640x480)
viewPosition - position of the view to change (any view within the main view)
viewSize- sizeof the view to change (any view within the main view)
const auto& gameViewPort = //<insert letterboxed viewPort for reference>;
auto wndX = std::round(gameViewPort.left * gameWindowSize.x);
auto wndY = std::round(gameViewPort.top * gameWindowSize.y);
auto wndW = std::round(gameViewPort.width * gameWindowSize.x);
auto wndH = std::round(gameViewPort.height * gameWindowSize.y);
auto tmpX = (wndW * viewPosition.x) / gameBaseSize.x;
auto tmpY = (wndH * viewPosition.y) / gameBaseSize.y;
auto tmpW = (wndW * viewSize.x) / gameBaseSize.x;
auto tmpH = (wndH * viewSize.y) / gameBaseSize.y;
auto x = (wndX + tmpX) / gameWindowSize.x;
auto y = (wndY + tmpY) / gameWindowSize.y;
auto w = tmpW / gameWindowSize.x;
auto h = tmpH / gameWindowSize.y;
view.setViewport(sf::FloatRect(x, y, w, h));
auto wndX = std::round(gameViewPort.left * gameWindowSize.x);
auto wndY = std::round(gameViewPort.top * gameWindowSize.y);
auto wndW = std::round(gameViewPort.width * gameWindowSize.x);
auto wndH = std::round(gameViewPort.height * gameWindowSize.y);
auto tmpX = (wndW * viewPosition.x) / gameBaseSize.x;
auto tmpY = (wndH * viewPosition.y) / gameBaseSize.y;
auto tmpW = (wndW * viewSize.x) / gameBaseSize.x;
auto tmpH = (wndH * viewSize.y) / gameBaseSize.y;
auto x = (wndX + tmpX) / gameWindowSize.x;
auto y = (wndY + tmpY) / gameWindowSize.y;
auto w = tmpW / gameWindowSize.x;
auto h = tmpH / gameWindowSize.y;
view.setViewport(sf::FloatRect(x, y, w, h));