SFML community forums

Help => Window => Topic started by: Putarda on September 28, 2016, 12:50:04 am

Title: Aspect ratio confusion
Post by: Putarda on September 28, 2016, 12:50:04 am
Currently i'm at the point where I don't know where to start. I want to make game where view will be equal on different screen resolutions. I don't know how and where to start and how other games do it. How do i calculate that. For good explanation i will be so thank thankful  :).

Btw here is my window code and render code:

Window.cpp:
        void Window::createWindow(Game::Game* game, sf::RenderWindow* game_window) {

                sf::ContextSettings window_settings;

                window_settings.antialiasingLevel = 16;

                game_window->create(sf::VideoMode::getDesktopMode(), *game->getName(), sf::Style::Fullscreen, window_settings);

                game_window->setVerticalSyncEnabled(true);
               
                game_window->setActive(false);

        }

Game.cpp (render code):
        void Game::startDraw() {

                window.setActive(true);

                sf::RenderTexture screen_layer_rendertexture;

                sf::Sprite screen_layer_sprite;

                if (!screen_layer_rendertexture.create(1920, 1080)) {

                        Main::printError("Can't create the texture.");

                        exit(-1);

                }

                screen_layer_rendertexture.setSmooth(true);

                screen_layer_sprite.setTexture(screen_layer_rendertexture.getTexture());

                while (isOpen()) {

                        sf::Event event;

                        while (window.pollEvent(event))
                        {

                                switch (event.type) {

                                case sf::Event::Closed:

                                        setRun(STOP);

                                        window.close();

                                        return;
                                }

                        }

                        screen_layer_rendertexture.clear(sf::Color::White);

                        for (auto object : getObjects()) {

                                screen_layer_rendertexture.draw(*object->getSprite());

                        }

                        screen_layer_rendertexture.display();

                        window.clear(sf::Color::Red);

                        window.draw(screen_layer_sprite);

                        window.display();

                }

        }
Title: Re: Aspect ratio confusion
Post by: AlexxanderX on September 28, 2016, 03:19:00 pm
You can use sf::View:

sf::View view( you fixed aspect ratio size );
window.setView(view);

The last thing to do is to think what will you do with the extra space which will remain on the different aspect ratio screens. I met this problem when working on mobile applications: maybe will help you (http://v-play.net/doc/vplay-different-screen-sizes/).
Title: Re: Aspect ratio confusion
Post by: Hapax on September 29, 2016, 09:29:56 pm
Do you want the view to be the same regardless of the window's resolution? You can use a fixed view and it will stretch to fit the window. This has an added benefit that all of the co-ordinates that you use don't need to be altered depending on the window's size. A down-side is that you no longer have co-ordinates that match the pixels exactly. However, you can use multiple views in one window so you can use a separate view for things that must have "pixel perfect" positioning/sizing, for example.

If all you want to do is match the view's aspect ratio to the window resolution's ratio, you can divide the window width by the window height to get the window ratio and then multiply that ratio by the view's height to get a new view width.
For example (in code):
// get view from window although you may want to use a specific view instead.
sf::View theView = window.getView();

// calculate window's ratio
const float windowRatio = static_cast<float>(window.getSize().x) / window.getSize().y;

// calculate a new view width so that the view's ratio would match the window's ratio
const float newViewWidth = theView.getSize().y * windowRatio;

// apply the new width. You may wish to also adjust the view's centre; this depends on how you're using the view.
theView.setSize({ newViewWidth, theView.getSize().y });

// use the new, adjusted view
window.setView(theView);

Note that you may wish to do this the other way around - that is, to calculate a new height from the width instead. That depends on your preference.
Title: Re: Aspect ratio confusion
Post by: Putarda on October 01, 2016, 01:07:43 pm
Thank you for your answer. But I was more thinking about this (https://media.licdn.com/mpr/mpr/shrinknp_800_800/p/1/005/0b2/1b4/26576f5.jpg)
Title: Re: Aspect ratio confusion
Post by: fallahn on October 01, 2016, 11:38:03 pm
Like this (https://github.com/SFML/SFML/wiki/Source%3A-Letterbox-effect-using-a-view)?
Title: Re: Aspect ratio confusion
Post by: Putarda on October 02, 2016, 11:31:29 am
Yes, just like that :D. Thanks.
Title: Re: Aspect ratio confusion
Post by: Putarda on October 02, 2016, 02:46:02 pm
And it's not working.... nothing changes, there is not letterbox.