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

Author Topic: Letterbox effect using a view - not on position 0,0  (Read 6513 times)

0 Members and 1 Guest are viewing this topic.

sfUser

  • Newbie
  • *
  • Posts: 12
    • View Profile
Letterbox effect using a view - not on position 0,0
« on: May 15, 2016, 12:45:41 pm »
Greetings,

Letterboxing a view when resized to keep the aspect ratio can be done as in the wiki example:
https://github.com/SFML/SFML/wiki/Source:-Letterbox-effect-using-a-view

However, this code assumes the view takes the whole screen and is at position 0, 0, as seen by these variables:
    float sizeX = 1;
    float sizeY = 1;
    float posX = 0;
    float posY = 0;

If I have a smaller view, such as a small map on the bottom right corner (therefore, a view not on 0,0 and with a size < window size),
what would be the changes to the code to make that work (it doesn't).

The best I could get was a function that added small increments as the window grew, which would shift it slightly to the right, bottom by changing the initial values (size, pos) to the viewport as expected on the normal screen:

        sizeX *= viewRatio / windowRatio;
        posX += (1 - sizeX) / 2.0;

Thank you. :)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Letterbox effect using a view - not on position 0,0
« Reply #1 on: May 16, 2016, 02:01:16 am »
Greetings,
Hi!

this code assumes the view takes the whole screen and is at position 0, 0, as seen by these variables:
    float sizeX = 1;
    float sizeY = 1;
    float posX = 0;
    float posY = 0;
This is not true. These are the initial values assigned to these variables but are not the final values associated with them. In fact, all four of these values are overwritten and never read (so don't necessarily need to be initialised).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

sfUser

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Letterbox effect using a view - not on position 0,0
« Reply #2 on: May 16, 2016, 09:20:24 pm »
Sorry, my mistake. I rushed the post.

Still, the function doesn't work with views that not on 0, 0.

If anyone has done something similar, please share how.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Letterbox effect using a view - not on position 0,0
« Reply #3 on: May 17, 2016, 12:15:25 am »
That functions sets the view's viewport to position the view with letterboxing. To have a view that displays elsewhere, you will need to put in a bit of work as the viewport will need to match your mini-map.
You could try scaling the viewport size attributes from the window size to match your mini-map. Note that the viewport position should be changed to place it where you need it. I am assuming you are using a viewport to display and position your map. Is this correct?

What is it that you're trying to achieve exactly? Why does a mini-map (assuming that's what it is) need letterboxing?
What is it that "isn't working"?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

sfUser

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Letterbox effect using a view - not on position 0,0
« Reply #4 on: May 18, 2016, 12:12:16 am »
I use views for displaying the map (the visible part) and for scrolling texts. None of these views occupy the whole screen.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Letterbox effect using a view - not on position 0,0
« Reply #5 on: May 18, 2016, 09:57:19 pm »
You only need viewports for this. Why are you using letterboxing for your minimap?

Some information that could help:
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

AFS

  • Full Member
  • ***
  • Posts: 115
    • View Profile
Re: Letterbox effect using a view - not on position 0,0
« Reply #6 on: May 19, 2016, 08:22:10 am »
You only need viewports for this. Why are you using letterboxing for your minimap?

I think he means that he wants to apply letterboxing to his game, but it doesn't work for the HUD because it uses a view with a different size. So, when he resizes the window, his world view scales at a different rate than his HUD view, having the HUD all over the place.

Now, I can confirm that the position of the view is irrelevant, so it doesn't matter if it's at 0,0 or not (I just tested it), and it also doesn't matter if you are using views of different size than the window (because that's what letterboxing is for), but I can confirm that it doesn't work properly if you are using two views that are of different aspect ratio at the same time, because that's what the function uses for comparison to make the letterbox effect: the aspect ratio of the view vs. the aspect ratio of the window, without taking into account the other views. So, view1 may behave like having having black bars left and right, but view2 may behave like having black bars up and down, messing everything up.

So, a solution could simply be making all your views to have the same aspect ratio (not necessarily same size, mind you), but that may screw the size of your HUD, so you'll need to readjust accordingly

Also, keep in mind that if you are modifying the viewport of your views (to make something like, say, a minimap, splitscreen or something like that), this function will simply not work, because it modifies the viewports, so if that's the case you will need to find another solution.

SonyUSA

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Letterbox effect using a view - not on position 0,0
« Reply #7 on: May 20, 2016, 07:22:03 am »
Sorry to kinda hijack this thread, but I found what you linked very useful for my game, but do you get warnings at build time about changing 'double' to 'float' ? Have you found a way to stop that? The game still works fine but I don't like errors >_>

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Letterbox effect using a view - not on position 0,0
« Reply #8 on: May 20, 2016, 12:23:05 pm »
Yes, this function is designed to convert a window's view to its letter-boxed equivalent, not convert any extra views based upon it. Try converting the main window's view using the function and then changing the viewport of your other views (minimap etc.) by using the aspect ratio adjustment.

Sorry to kinda hijack this thread, but I found what you linked very useful for my game, but do you get warnings at build time about changing 'double' to 'float' ? Have you found a way to stop that? The game still works fine but I don't like errors >_>
This is because the code uses doubles to perform calculations on floats without explicit casting. Change the functions doubles to floats and that should fix those warnings. (i.e. 2.0 -> 2.f)
EDIT: I've updated that code to use floats so you can copy it again if you'd like ;)
« Last Edit: May 20, 2016, 12:29:20 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

sfUser

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Letterbox effect using a view - not on position 0,0
« Reply #9 on: May 20, 2016, 08:44:43 pm »
I sat down and took the time to do the math and solved it.
Here it is for anyone interested.

gameViewPort - the viewport of the letterboxed view (usually the default views's / main view)
gameWindowSize - current game window size (ex: 1280x720)
gameBaseSize - base game window size (ex: 640x480)
viewPosition - position of the view to change (any view within the main view)
viewSize- sizeof the view to change (any view within the main view)

const auto& gameViewPort = //<insert letterboxed viewPort for reference>;

auto wndX = std::round(gameViewPort.left * gameWindowSize.x);
auto wndY = std::round(gameViewPort.top * gameWindowSize.y);
auto wndW = std::round(gameViewPort.width * gameWindowSize.x);
auto wndH = std::round(gameViewPort.height * gameWindowSize.y);

auto tmpX = (wndW * viewPosition.x) / gameBaseSize.x;
auto tmpY = (wndH * viewPosition.y) / gameBaseSize.y;
auto tmpW = (wndW * viewSize.x) / gameBaseSize.x;
auto tmpH = (wndH * viewSize.y) / gameBaseSize.y;

auto x = (wndX + tmpX) / gameWindowSize.x;
auto y = (wndY + tmpY) / gameWindowSize.y;
auto w = tmpW / gameWindowSize.x;
auto h = tmpH / gameWindowSize.y;

view.setViewport(sf::FloatRect(x, y, w, h));
 

SonyUSA

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Letterbox effect using a view - not on position 0,0
« Reply #10 on: May 21, 2016, 12:56:35 am »
This is because the code uses doubles to perform calculations on floats without explicit casting. Change the functions doubles to floats and that should fix those warnings. (i.e. 2.0 -> 2.f)
EDIT: I've updated that code to use floats so you can copy it again if you'd like ;)
Yeah the awesome guys in #sfml helped me fix it by changing it to floats and then casting INT to the stuff x3 Thanks though!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Letterbox effect using a view - not on position 0,0
« Reply #11 on: May 21, 2016, 01:19:01 am »
Where is the casting to ints needed? Is it the window size stuff at the beginning? I'm thinking it might need casting to float first and it should be okay then (second will be automatically "upgraded" to float).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

SonyUSA

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Letterbox effect using a view - not on position 0,0
« Reply #12 on: May 21, 2016, 03:25:06 am »
        // Create the main window w/ Viewscope Function
        float resX = 800.f;
        float resY = 600.f;
        sf::RenderWindow window(sf::VideoMode(static_cast<int>(resX), static_cast<int>(resY)), "RE:Union", (sf::Style::Resize | sf::Style::Close));
        // Slow Down Input
        window.setKeyRepeatEnabled(false);

        // Create a view. This can be of any size, but in this example it will be the same size as the window.
        // After creating it, it applies the letterbox effect to it; this is not neccesary if the view
        // is the same size as the window initially, but let's do this just in case.
        sf::View view;
        view.setSize(resX, resY);
        view.setCenter(view.getSize().x / 2, view.getSize().y / 2);
        view = getLetterboxView(view, static_cast<int>(resX), static_cast<int>(resY));

Those two lines with the static_cast for resX and resY :)

I converted resX and resY to floats to stop the compiler from warning then casted them in those two instances to stop the 2 new warning errors :P
« Last Edit: May 21, 2016, 03:27:38 am by SonyUSA »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Letterbox effect using a view - not on position 0,0
« Reply #13 on: May 21, 2016, 05:27:03 am »
I think it would be semantically better to store the width and height of the window as ints/unsigned ints and then cast to floats when using them for views.
e.g.
unsigned int resX = 800u;
unsigned int resY = 600u;
although using a vector makes more sense (that's what they're for!):
sf::Vector2u res(800u, 600u);
sf::RenderWindow window(sf::VideoMode(res.x, res.y), "RE:Union", (sf::Style::Resize | sf::Style::Close));
// ...
sf::View view;
view.setSize(sf::Vector2f(res));
view.setCenter(view.getSize() / 2.f); // this can be divided as one as long as the scalar is the same type as the components
view = getLetterboxView(view, res.x, res.y);
Doesn't this look cleaner and easier to read?  :D

tip: you could also change the letterboxing code to take a vector2u as a parameter instead of individual x and y, if you'd like it to be even cleaner!
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

SonyUSA

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Letterbox effect using a view - not on position 0,0
« Reply #14 on: May 21, 2016, 05:30:28 am »
Yeah that looks very nice :D

 

anything