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

Author Topic: Aspect ratio confusion  (Read 3733 times)

0 Members and 1 Guest are viewing this topic.

Putarda

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Aspect ratio confusion
« 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();

                }

        }

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: Aspect ratio confusion
« Reply #1 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.
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

Hapax

  • Hero Member
  • *****
  • Posts: 3058
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: Aspect ratio confusion
« Reply #2 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.
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter

Putarda

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: Aspect ratio confusion
« Reply #3 on: October 01, 2016, 01:07:43 pm »
Thank you for your answer. But I was more thinking about this

fallahn

  • Sr. Member
  • ****
  • Posts: 469
  • Buns.
    • View Profile
    • Trederia
Re: Aspect ratio confusion
« Reply #4 on: October 01, 2016, 11:38:03 pm »
Like this?

Putarda

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: Aspect ratio confusion
« Reply #5 on: October 02, 2016, 11:31:29 am »
Yes, just like that :D. Thanks.

Putarda

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: Aspect ratio confusion
« Reply #6 on: October 02, 2016, 02:46:02 pm »
And it's not working.... nothing changes, there is not letterbox.