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:
https://imgur.com/a/2zhiV7S
It creates the correct gap length, 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.
I am just not sure what you mean by setting the view's Y position. Are you reseting the view position after the viewport has been set?
Here's the code I used:
static float vw = 560; // view width
static float vh = 560; // view height
static float vpw = 560.0; // viewport width
static float vph = 560.0; // viewport height
static float vpy = 160.0; // viewport y offset
sf::FloatRect vp(sf::Vector2f(0.0f, vpy / 720.0), sf::Vector2f(vpw / 1280.0, vph / 720.0));
sf::View v(sf::FloatRect(sf::Vector2f(0, 0),sf::Vector2f((float)vw, (float)vh)));
v.setViewport(vp);
g_window.setView(v);
There was ImGui stuff in the middle (to let me adjust it at run time), which is why I had static variables there.
So the viewport values define where it is in the window and how big it is, using values of 0-1.
The view then defines how the rendering coordinates are stretched into the viewport.
I've been using SFML for many years, I still had to write a test program to make sure I understood what was going on before posting. It's a bit confusing. :)
(Actually I'd never used a viewport. Views all the time, never a viewport that I can remember).