How to enforce a user-defined aspect ratio?

Hello,

I’m trying to find a callback or something similar that occurs when the user resizes the game window (in windowed mode) so that I can force the window to stretch in a way that it maintains their originally picked Aspect Ratio from settings. So if someone had picked a 16:9 resolution and tried to stretch the side of the window, the bottom would also stretch to end up with a 16:9 window.

Also, if someone could point me towards a way to set the viewport size from code (assuming “viewport” and “window” are the same thing?) to an arbitrary value, that’d be appreciated.

Thanks.

If you want to add some code to viewport override this class:

and set it in project configuration. You can definitly find size from there (without overriding), not sure about keeping ratio but there GetDesiredAspectRatio() in FViewport, not sure how to override it. So try searching around there

You can set the aspect ratio of the camera component:

81328-untitled.png

But that will introduce black bars rather than force-sizing the window.

As for setting the resolution:

()->Exec((), TEXT("setres 1280x720w"));

Where the w means windowed. Can be replaced by f for fullscreen.

I was aware of the Aspect Ratio setting on the camera, but was hoping to avoid using that, since ideally if the window is always the correct size, we don’t need the black bars.

However the “setres” is new information, so thanks for that.

Now I just need to find some sort of event that triggers when the viewport is manually resized (if one exists.)

Yeah, I looked through it; didn’t see any sort of callback. Looks like I’ll just have to check the viewport size every draw against the previous size and do something if it changed.

Did you look at the UGameViewportClient as ** suggested?

You could also try to explore UEngine and UGameEngine, it’s general engine class (which btw you can override too, but you need set class in ini config) maybe you might find somewhere there

I found what I was looking for in WindowsApplication.cpp’s handler for the wm_sizing event. It can be controlled by calling the SetFixedWindowAspectRatio function from SlateApplicationBase.h.

1 Like

For Unreal 4.11:

GSystemResolution.RequestResolutionChange(1000, 500, EWindowMode::Windowed);
GSystemResolution.bForceRefresh = true;