0

When trying to center my renderer's viewport, it seems like adding half of the difference between the window-size and viewport-size to the viewport's x and y coords isn't correct. Instead you need to divide this difference by 3? I'm so confused on this and hope someone has some clarification.

The goal is to render a 4:3 viewport on a 16:9 window.

Aside of this, the viewport's aspect ratio starts skewing completely when I resize the window around manually.

SDL_Rect windowRect{ };
SDL_GetWindowSize(m_pWindow, &windowRect.w, &windowRect.h);
SDL_Rect newViewport{ };
float aspectRatio{ 4 / 3.f };
if (static_cast<float>(windowRect.w) / windowRect.h >= aspectRatio)
{
    newViewport.h = windowRect.h;
    newViewport.w = windowRect.h * aspectRatio;
    newViewport.x = (windowRect.w - newViewport.w) / 2; // interchange this between 2 and 3
}
else
{
    newViewport.w = windowRect.w;
    newViewport.h = windowRect.w / aspectRatio;
    newViewport.y = (windowRect.h - newViewport.h) / 2; // interchange this between 2 and 3
}
SDL_RenderSetViewport(m_pRenderer, &newViewport);

Result when divided by 2

Result when divided by 3

Example of skewed viewport after resizing window

Regarding the funky centering, I tried looking around on the internet on why SDL2 does this, to obviously no avail.

And about the skewing, I tried applying a couple of changes to the calculations and looked if the numbers I got were actually correct, and they were.

I was expecting something to change with these adaptations but all I could do was break it some more or change nothing.

3
  • ¿What are the actual values of windowRect and newViewport? ¿Does windowRect match real window size? ¿Where do you call this code snippet? Commented Feb 5, 2024 at 6:28
  • This cope snippet is the content of CenterViewport(), which I call when an SDL_WINDOWEVENT_RESIZED happens. This is called correctly, although twice for some reason everytime I resize the window. windowRect and newViewport are the correct dimensions at all times, and when I don't center them they retain the correct dimensions in the renderer. But when I move newViewport.x to the right for example using the width or height in the calculation, its dimensions skew around. Commented Feb 5, 2024 at 14:31
  • Can't reproduce. The piece of code you show, used with a SDL_RenderFillRect(renderer, nullptr) call, draws the biggest possible 4x3 rectangle centered inside the window. Commented Feb 5, 2024 at 18:21

1 Answer 1

0

The code snippet was right, as in it does make the viewport the largest centered 4:3 rectangle that fits the viewport. The problem was in my rendering method, which passed an SDL_Rect, filled using SDL_GetViewport(), as parameter. Instead you need to pass NULL or any kind of value of zero for it to fill the viewport correctly.

Correct

void RenderViewport() const
{
    SDL_RenderFillRect(m_pRenderer, NULL);
}

Incorrect

void RenderViewport() const
{
    SDL_Rect viewport{};
    SDL_RenderGetViewport(m_pRenderer, &viewport);
    SDL_RenderFillRect(m_pRenderer, &viewport);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.