[tsc-devel] Re: TSC3 native resolution

Marvin Gülker | Thu, 05 Apr 2018 08:05:52 UTC

I found that a native aspect ratio of 16:9 results in the following
effective game play dimensions for typical non-native resolutions:

* For a resolution of 1024x768, the game play will be rendered as
  1024x576.
* For a resolution of 1280x1024, the game play will be rendered as
  1024x720.

I think this is acceptable.

On a technical point, I tried to implement the "black bars" using an
SFML view (sf::View) as a kind of "master" view. I wrote a function that
calculated the view's viewport like this (note that sf::View's
setViewport() takes fractions of the window rather than pixels):

    static sf::FloatRect calc_master_viewport(sf::Vector2u winsize)
    {
        // Native aspect ratio is 16:9
        if (winsize.x > winsize.y) {
            // Calculate fraction of the height that can be used
            // w/(hx)=16/9 <=> x=(9w)/(16h)
            float heightfrac = (9.0f * static_cast<float>(winsize.x)) / (16.0f * static_cast<float>(winsize.y));
            // Calculate height of one black bar (= start Y of the viewport)
            float starty     = (1.0f - heightfrac) / 2.0f;

            return sf::FloatRect(0.0f, starty, 1.0f, heightfrac);
        } else {
            // Calculate fraction of the width that can be used
            // (wx)/h=16/9 <=> x=(16h)/(9w)
            float widthfrac = (16.0f * static_cast<float>(winsize.y)) / (9.0f * static_cast<float>(winsize.x));
            // Calculate width of one black bar (= start X of the viewport)
            float startx    = (1.0f - widthfrac) / 2.0f;

            return sf::FloatRect(startx, 0.0f, widthfrac, 1.0f);
        }
    }

I set the view before drawing the GUI as:

   view.setViewport(calc_master_viewport(windowsize));
   view.reset(0.0f, 0.0f, 1920.0f, 1080.0f);
   window.setView(view);

The idea was that the GUI effectively draws a 2D "world" with the exact
size of the native resolution (1920x1080px). The view views the entire
thing, which is then scaled down to the "master" viewport. Normal
scenes, most notably the level scene, would use their own view anyway as
they need to implement level scrolling.

While the idea was certainly fine, it revealed a major problem of this
approach. Since SFML is scaling everything when it works through the
view, the GUI elements are distored. Especially text looks very bad once
gone through this process, and GUI mouse event handling gets set
off. The consequence is that the GUI itself can't be constrained via an
SFML view, but has to work on the window directly and manually adapt to
the window size. More work, but results in proper sharp GUI output and
no problems in mouse event handling.

One could now debate whether the GUI should respect the gameplay's 16:9
aspect ratio or whether it is okay to draw GUI elements onto those "black
bars", using the additional screen estate. Given that TSC has always
featured a HUD, I would find it irritating if the HUD would be placed
onto the top "black bar" and would thus lean towards the GUI respecting
the rest of the game's dimensions.

Greetings
Marvin

-- 
Blog: https://mg.guelker.eu
PGP/GPG ID: F1D8799FBCC8BC4F
_______________________________________________
tsc-devel mailing list -- …l@l…
To unsubscribe send an email to …e@l…
By Thread
2018-04-02 18:54:46Marvin Gülker[tsc-devel] TSC3 native resolution
2018-04-05 08:05:52Marvin Gülker[tsc-devel] Re: TSC3 native resolution
By Date
[tsc-devel] TSC3 native resolutionMarvin Gülker2018-04-02 18:54:46
[tsc-devel] Re: TSC3 native resolutionMarvin Gülker2018-04-05 08:05:52