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

Author Topic: Only render the contents of a view  (Read 2079 times)

0 Members and 1 Guest are viewing this topic.

TechRogue

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Only render the contents of a view
« on: June 06, 2014, 07:50:16 am »
Yeah, this is another thread about clipping. :P I searched as best as I could but I can't seem to find a solution. I'm not very good at math so I don't doubt what I'm trying to do is really simple, but it's eluded me so far.

Basically, when I resize the window, I want to resize the view so as to fit in the window with proportional scaling, letterboxing when necessary, and center it in the window. That much is done, as you can see in the code below.

What I need to do now is make it so that nothing beyond the extents of the view rect is rendered. I can't figure out how to get the appropriate values for my Viewport.

FP.Width and FP.Height are the dimensions of the screen when it is created.

public void Resize(int width, int height)
{
        _sfmlWindow.Size = new Vector2u((uint) width, (uint) height);
       
        var targetWidth = (float) width;
        var targetHeight = (float) height;
       
        var scale = Math.Min(targetWidth / (float) FP.Width, targetHeight / (float) FP.Height);
       
        var toWidth = width / scale;
        var toHeight = height / scale;
        var x = (toWidth - FP.Width) * -0.5f;
        var y = (toHeight - FP.Height) * -0.5f;
       
        var viewRect = new FloatRect(x, y, toWidth, toHeight);
        var view = new View(viewRect);
  _sfmlWindow.SetView(view);
}
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Only render the contents of a view
« Reply #1 on: June 06, 2014, 08:37:52 am »
It's simple, if you want to use the viewport for clipping then use the exact same rectangle (divided by the window size to get values in [0 .. 1]).
Laurent Gomila - SFML developer

TechRogue

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: Only render the contents of a view
« Reply #2 on: June 06, 2014, 09:17:54 am »
Hmm...that seems like it should work but I'm not getting the results I was hoping for. I think it might have to do with the fact that I'm offsetting the view in order to center it, which causes the viewport to be offset from that.