0
\$\begingroup\$

I am trying to do something really basic with sf::View.

On a 1280x720 pixel window. I need to place a viewport which is 560x560 with a 160px gap from the top.

What I get is this:

screenshot of problem

It creates the correct gap length on the top, but it also applies a gap at the bottom.

Code I wrote for the viewport:

int viewport_bl_OffsetX = 0;
int viewport_bl_OffsetY = 160;
int viewport_bl_W = 560;
int viewport_bl_H = 560;

sf::View viewBL(sf::FloatRect(
    static_cast<float>(viewport_bl_OffsetX),
    static_cast<float>(viewport_bl_OffsetY),
    static_cast<float>(viewport_bl_W),
    static_cast<float>(viewport_bl_H)
));

float viewport_bl_OffsetXRatio = static_cast<float>(viewport_bl_OffsetX) / window_W;
float viewport_bl_OffsetYRatio = static_cast<float>(viewport_bl_OffsetY) / window_H;

float viewport_bl_WRatio = static_cast<float>(viewport_bl_W) / window_W;
float viewport_bl_HRatio = static_cast<float>(viewport_bl_H) / window_H;

viewBL.setViewport(sf::FloatRect(
    viewport_bl_OffsetXRatio, // 0.00000000 
    viewport_bl_OffsetYRatio, // 0.222222224 
    viewport_bl_WRatio, // 0.437500000 
    viewport_bl_HRatio // 0.777777791 
));
 

The ratios seem to be correct, but I am not sure why it leaves a gap at the bottom.

\$\endgroup\$
1
  • \$\begingroup\$ The code that shows what is displayed in the view (your scene) is not shown. \$\endgroup\$ Commented Nov 1, 2022 at 12:47

1 Answer 1

0
\$\begingroup\$

You don't provide much details about how you set up your scene but it seems to me, from what I remember about SFML (and the documentation), that there is a bit of confusion about how to set up your code.

The View is a view on your scene, it defines what portion of your scene you want to show to your users.

The Viewport is about where you'll show this on your window.

So the engine will try to "fit" the content of the View into the Viewport.

I assume the image you show is actually 560x560, and that you have set it at 0x0 in your scene; analyzing what we see on it, it seems that the "top" part of the image is cropped.

This would match the fact that you set the View's y at 160: you show 560 px of your scene, but starting at 160 instead of 0. The black part you see at the bottom is simply because there is nothing to show from your scene.

So the correct way to set your view is actually:

sf::View viewBL(sf::FloatRect(
    0.0f,
    0.0f,
    static_cast<float>(viewport_bl_W),
    static_cast<float>(viewport_bl_H)
));

(That is, until you want to show some other parts of your scene.)

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.