Player: Removing Letterboxing in Landscape

Created on 28 Mar 2017  路  4Comments  路  Source: EasyRPG/Player

I love playing with EasyRPG, and kudos to all those involved.

I'm playing with the surface in Android, and I can't seem to figure out how to stretch the game's screen (variable aspect ratio) to the width of the device in landscape. Since the buttons are overlaid on the same RelativeLayout, I cannot extend the layout itself.

I came across this a few SDL APIs, but it's out of my expertise to pass the device's screen width and height to something like SDL_RenderSetLogicalSize or SDL_RenderSetScale.

Here is a example in practice:
https://www.gamedev.net/resources/_/technical/apis-and-tools/stretching-your-game-to-fit-the-screen-without-letterboxing-sdl2-r3547

Is this possible without lots of headache?

Android Enhancement Feature Request

Most helpful comment

Great, In that case we can even add an optimized codepath which directly returns at the beginning ;)

All 4 comments

I'm quite sure I have code somewhere where I tried to achive this with SDL2 on Windows. Will try to find it again when I have some time. (this was more about Multiple of 2 scaling to get rid of pixel artifacts but ignoring aspect ratio is very similiar)

(Will also need an Android configuration option for this...)

Just dropping this here for later use. SDL_SetLogicalRenderSize must be removed:

constexpr float aspect_ratio = (float)SCREEN_TARGET_WIDTH / SCREEN_TARGET_HEIGHT;

void SdlUi::UpdateDisplay() {
    [...]
    SDL_RenderClear(sdl_renderer);

// NEW
    int window_width, window_height;
    SDL_GetWindowSize(sdl_window, &window_width, &window_height);

    float w = (float)window_width;
    float h = (float)window_height;

    SDL_Rect dst_rect;
    dst_rect.x = 0;
    dst_rect.y = 0;

    if (w / h > aspect_ratio) {
        // Landscape
        if (IsFullscreen()) {
            // Reduce to closest multiple of 240 * n
            if (h > SCREEN_TARGET_HEIGHT) {
                for (int i = SCREEN_TARGET_HEIGHT; i < INT16_MAX; i += SCREEN_TARGET_HEIGHT) {
                    if (h > i && h < i + SCREEN_TARGET_HEIGHT) {
                        h = i;
                        break;
                    }
                }
            }
        }

        // Adjust aspect ratio
        w += (aspect_ratio * h - w);
    } else {
        // Portrait
        if (IsFullscreen()) {
            // Reduce to closest multiple of 320 * n
            if (w > SCREEN_TARGET_WIDTH) {
                for (int i = SCREEN_TARGET_WIDTH; i < INT16_MAX; i += SCREEN_TARGET_WIDTH) {
                    if (w > i && w < i + SCREEN_TARGET_WIDTH) {
                        w = i;
                        break;
                    }
                }
            }
        }

        // Adjust aspect ratio
        h += (1 / aspect_ratio * w - h);
    }
    dst_rect.x = window_width / 2 - w / 2;
    dst_rect.y = window_height / 2 - h / 2;
    dst_rect.w = w;
    dst_rect.h = h;

// OLD
    SDL_RenderCopy(sdl_renderer, sdl_texture, NULL, &dst_rect);
    SDL_RenderPresent(sdl_renderer);
#endif
}

This will crash when I am allowed to resize the SDL window to 0 pixels height... 馃嵀

Great, In that case we can even add an optimized codepath which directly returns at the beginning ;)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ghabry picture Ghabry  路  4Comments

carstene1ns picture carstene1ns  路  4Comments

fdelapena picture fdelapena  路  4Comments

scoot404 picture scoot404  路  4Comments

fmatthew5876 picture fmatthew5876  路  3Comments