Question

I'm having trouble drawing scrollable content within a defined rectangle. What I would like to achieve is the ability to freely scroll the contents within a rectangle, with anything that is not within or partially in being cropped. At present I scroll by the number of items available to make sure nothings being drawn outside of the rectangle.

Looking at what's available in SFML it seems I should be able to achieve this using sf::View but my attempts are not working.

sf::View panelView;

panelView.reset(sf::FloatRect(220,85,815,575));
panelView.setViewport(sf::FloatRect(0, 0, 1.0f, 1.0f));

I'm then setting the viewpoint to this, drawing my items, then going back to the default view. It always seems to draw to the entire screen though instead of the desired rectangle i've set in the Reset method?

My understanding is that this should create the viewport at 220 on the x axis, 85 on the y axis and be 815 wide and 575 high, but it's drawing outside of this boundaries.

Any ideas?

Was it helpful?

Solution

Sorted out the issue looking into it a little bit further - seems I just need to set my position as a factor between 0.0f and 1.0f, where position in my sample code below is where I want the viewport to be positioned:

sf::FloatRect panelRect(position.left / SCREENWIDTH,
(position.top) / SCREENHEIGHT,
(position.width) / SCREENWIDTH,
(position.height) / SCREENHEIGHT);

panelView.reset(sf::FloatRect(position.left,position.top,position.width,position.height));
panelView.setViewport(panelRect);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top