Is there any simple way make size of view equal to size of viewport to get 1:1 scale?
Say i want split my 1000x1000 window to two vertical parts. I make view with 0, 0, 0.5f, 0.5f, but then i have to manualy calculate size of this view depending on resolution of whole window and viewport size ratios like that
void OperatingSystem::adjustPanel(const sf::RenderWindow& win, sf::View& view)
{
view.reset({std::round(view.getViewport().width / 2),
std::round(view.getViewport().height / 2),
std::round(win.getSize().x * view.getViewport().width),
std::round(win.getSize().y * view.getViewport().height)});
}
I extended the RenderTarget-Class now by a new function:
void RenderTarget::setViewportt(const sf::FloatRect& rect)
{
sf::Vector2u oldSize = getSize();
m_view.reset(sf::FloatRect(0.f, 0.f, rect.width, rect.height));
m_view.setViewport(sf::FloatRect(rect.left / oldSize.x, rect.top / oldSize.y,
rect.width / oldSize.x, rect.height / oldSize.y));
m_cache.viewChanged = true;
}
Now i exactly have what i want, i can make offsets for my sprites with a cutting off at the edge. But my question now is, is that a good coding style? Perhaps i do something wrong to reach my goal? Or i do something wrong when i think i have to reach this goal?
My Plan is it, to create Surface-Classes for everything i want draw. For example a Field Surface-Class called SurfaceField needs to dispaly a Field and the figures on it, then i can simply write:
int main()
{
SurfaceField sf;
SurfaceChat sc;
renderWindow.setViewportt(sf::FloatRect(50, 50, 200, 200));
sf.draw(renderWindow);
renderWindow.setViewportt(sf::FloatRect(100, 100, 200, 200));
sc.draw(renderWindow);
}
So i simply can move my chat and my field without have to worry about the offsets in the different Objects.