General => SFML wiki => Topic started by: AFS on November 27, 2014, 01:59:10 am
Title: Letterbox effect using a view
Post by: AFS on November 27, 2014, 01:59:10 am
Hello.
I added a little code example to archieve a letterbox effect when resizing the window, having black bars on the sides in case the aspect ratio of the window is different than the aspect ratio of the view.
I was unsure whether this is "wiki material" or not, but I decided to post it regardless. Maybe someone finds it useful.
Any corrections or suggestions are welcome, of course.
Cheers!
Edit: typo.
Title: Re: Letterbox effect using a view
Post by: G. on November 27, 2014, 11:28:08 am
Good idea! ;)
Title: Re: Letterbox effect using a view
Post by: Hapax on November 28, 2014, 01:16:01 am
Aha! You beat me to it! I was going to do just this ;D
Title: Re: Letterbox effect using a view
Post by: santiaboy on November 28, 2014, 08:49:45 pm
Nice idea!
Why don't you use
void getLetterboxView(sf::View& view, int windowWidth, int windowHeight)
instead of
sf::View getLetterboxView(sf::View view, int windowWidth, int windowHeight)
? It will avoid unnecessary copying.
Title: Re: Letterbox effect using a view
Post by: Hapax on November 28, 2014, 10:31:57 pm
That would alter the original view (or try to). That might not be desired, or may be a const view (renderWindow.getView() for example).
Title: Re: Letterbox effect using a view
Post by: Cirrus Minor on December 21, 2014, 12:15:05 pm
Great :D I've just integrated it in my current project, it's working fine!
Title: Re: Letterbox effect using a view
Post by: Nexus on December 26, 2014, 03:10:17 am
santiaboy, this approach has several disadvantages. Such a function cannot be used for initialization, it's impossible to initialize const variables, it needs 2 lines instead of one. The advantage is very questionable: copying small classes is cheap (and assignment is needed anyway, also for output parameters), larger classes can provide move semantics.
Avoid premature optimization, especially if it complicates code.