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

Author Topic: Game is half off screen when using a view at a lower resolution  (Read 999 times)

0 Members and 1 Guest are viewing this topic.

Dilloid

  • Newbie
  • *
  • Posts: 4
  • bad at this
    • View Profile
    • dilloid.dev
    • Email
This is how my game is supposed to look (so far):
(click to show/hide)

That's when creating a view of size 1920x1080 and displaying it in a fullscreen 1080p window.

When I change the resolution to 720p and re-set the view to a FloatRect(0, 0, 1920, 1080), it looks like this:
(click to show/hide)

This is the code that sets up the window and view:
const std::string& gameName, Scene* scn) {
RenderWindow rw(VideoMode(Vector2u(1280.f, 720.f)), gameName, sf::Style::Fullscreen);
Engine::gameName = gameName;
window = &rw;

View view(FloatRect(Vector2f(0.f, 0.f), Vector2f(1920.f, 1080.f)));
rw.setView(view);
 

I have looked at the code that places the tiles over and over and theres no reason it should be displaying like this. And then the debug text still lines up but the pause menu text doesn't. I am so confused and this is for an assignment that's due in a few days so I don't have time to start over. Is this a common problem with a known solution? I did try googling and found nothing.

Please let me know what you think. I am brand new to SFML and don't want to lose marks over this <3

Edit: Updated to fix images
« Last Edit: May 05, 2023, 01:34:42 am by Dilloid »

kojack

  • Sr. Member
  • ****
  • Posts: 310
  • C++/C# game dev teacher.
    • View Profile
Re: Game is half off screen when using a view at a lower resolution
« Reply #1 on: May 05, 2023, 04:44:02 am »
The code posted looks like its in a function that takes a name and a scene, but with the start cut off. Is all of the game in that function?
The reason I ask is you are making a local variable render window, then getting its address to put in the window variable. But at the end of that function the render window will stop existing, and window will point to left over memory that might get corrupted.

Otherwise the view should be correct (I just did a test, it works as expected). Maybe something else is messing with views (the scene class might have a render method that resets the view?).

Dilloid

  • Newbie
  • *
  • Posts: 4
  • bad at this
    • View Profile
    • dilloid.dev
    • Email
Re: Game is half off screen when using a view at a lower resolution
« Reply #2 on: May 06, 2023, 04:05:04 pm »
The game runs entirely in that function, yes. As far as I can tell (I didn't write all of the code, we were provided with custom libraries), the window/view never changes aside from there and the settings scene where you can change it, in which case I make sure to preserve the view:
View view = Engine::getWindow().getView();
Vector2u resolution = resolutionToVector(ents.find("resolution"
    + to_string(resolutionCounter))[0]->getComponents<TextComponent>()[0]->getText().getString());
Engine::getWindow().create(VideoMode({ resolution.x, resolution.y }),
    Engine::gameName, (currentWindowMode == 0) ? Style::Fullscreen : Style::Default);
Engine::getWindow().setView(view);
 

It may be worth noting that the level is offset by exactly the difference between 720 and 1080, and that I can manually add an offset to put it back in the right place. However, the debug text in the top left then moves down with it. It's confusing because some text is in the correct place and some text isn't, so I can't exactly isolate it to one element that's positioned wrongly.

MrMisterson

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Game is half off screen when using a view at a lower resolution
« Reply #3 on: May 06, 2023, 08:05:44 pm »
Do you setCenter for the View?

Something like:
view.setCenter(resolution.x/2.f, resolution.y/2.f);
 

Dilloid

  • Newbie
  • *
  • Posts: 4
  • bad at this
    • View Profile
    • dilloid.dev
    • Email
Re: Game is half off screen when using a view at a lower resolution
« Reply #4 on: May 06, 2023, 10:30:39 pm »
Do you setCenter for the View?

I wasn't before, but here's the result after doing that:
(click to show/hide)
« Last Edit: May 06, 2023, 10:38:38 pm by Dilloid »

MrMisterson

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Game is half off screen when using a view at a lower resolution
« Reply #5 on: May 07, 2023, 01:50:20 am »
Yes I see *Cartman voice*

It's a bit hard to tell exactly what's going without seeing the whole code or knowing which parts are the provided library. Also, I've only been using SFML for a few months and I haven't delved too deep into the realm of dealing with different video resolutions yet.

However I noticed in your first post the window size is 1280 x 720 while the View size is 1920 x 1080. I would try keeping the View size the same as the window size.

So in a pure SFML environment it might look something like:

sf::RenderWindow window;
sf::View view;

window.create(
  sf::VideoMode(1920, 1080),
  "Game Name",
  sf::Style::Fullscreen
);
// As far as I can tell, the initial size for video mode above kind of doesnt matter if youre doing Style::Fullscreen or Style::Default because the window will be automatically resized based on the operating system environment.

sf::Vector2u windowSize = window.getSize(); // Get the actual size of the window now that it exists.

view.setSize(windowSize.x, windowSize.y);
 

And then maybe even have it readjust every time the window resizes. So somewhere inside the game loop you would have:
sf::Event event;
while (window.pollEvent(event)) {
  if (event.type == sf::Event::Resized)
    view.setSize(event.size.width, event.size.height);
}
 

Somehow I doubt this solves everything, but maybe it might help reveal the ultimate solution.

kojack

  • Sr. Member
  • ****
  • Posts: 310
  • C++/C# game dev teacher.
    • View Profile
Re: Game is half off screen when using a view at a lower resolution
« Reply #6 on: May 07, 2023, 05:16:24 am »
What's odd is the screenshots show several different offsets. The background is offset vertically. The text is offset horizontally and the box around the text is offset diagonally.

Something to try, go to where you are rendering and just before check the values of the view to see if its changed. Something like this:
                sf::View view = g_window.getView();
                sf::Vector2f center = view.getCenter();
                sf::Vector2f size = view.getSize();
                std::cout << center.x << " " << center.y << std::endl;
                std::cout << size.x << " " << size.y << std::endl;
 
It should give 960 540 and 1920 1080.
(If you don't have a console window to cout to, write it to a file or use some onscreen debug text, etc)

As MrMisteron said, without seeing the whole engine it is hard to tell if something else in there is affecting it.

Dilloid

  • Newbie
  • *
  • Posts: 4
  • bad at this
    • View Profile
    • dilloid.dev
    • Email
Re: Game is half off screen when using a view at a lower resolution
« Reply #7 on: May 07, 2023, 03:34:07 pm »
I would certainly love to show you the whole engine but it is split into like 5 different libraries so it would be ridiculous to attempt to show it all. Thank you guys for your help, I will try some of this stuff when I can and get back to you.

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Game is half off screen when using a view at a lower resolution
« Reply #8 on: May 08, 2023, 06:25:11 pm »
Are you maybe using the window's size for some position calculations?

For example, something like:
// get window centre
sf::Vector2f windowCenter{ rw.getSize() / 2.f };
or to calculate the sizes of tiles etc..

Instead, consider using the size of the view (presuming that the view's centre is its size/2) to make these calculations. Something like:
// get window centre
sf::Vector2f windowCenter{ view.getSize() / 2.f };


You can also get the window's current view and either 'watch' them using a debugger or output the values somewhere. You should note the size and the centre.
sf::View viewForDebug{ rw.getView(); };
sf::Vector2f viewForDebugSize{ viewForDebug.getSize(); };
sf::Vector2f viewForDebugCenter{ viewForDebug.getCenter(); };
std::clog << "View size: " << viewForDebugSize.x << "x" << viewForDebugSize.y << "\n";
std::clog << "View centre: (" << viewForDebugCenter.x << ", " << viewForDebugCenter.y << "\n";
Track it as much as possible and at different points. First place would be at the point of drawing where it is not what you expect.

Then, once you know the view is what you expect - size: 1920x1080, center: (960, 540) - you can be certain that it's not being used correctly elsewhere.


Also, note that if you're using multiple views, 'getting the view' will only get the currently set one.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything