Maintaining aspect ratio in fullscreen mode

Started by
3 comments, last by acidleaf 11 years, 7 months ago
Hey guys.

I'm writing a game that has to take into account fullscreen and windowed modes. I'm basically using a fullscreen borderless window for the fullscreen mode. What I want to do for fullscreen mode is to get the user's desktop resolution, and create a viewport that has the same horizontal resolution as the user's desktop, and still maintain the aspect ratio. And to do this, I'm thinking of adding black borders to both sides of the screen which aren't filled. What I know is I need to use glViewport to set a viewport and glScissor to clip the outside of the viewport. But how to I set this up so that the viewport is centered on the screen? And if I do this, do I have to specify the coordinates in my game according to the offset created by the black bars on the sides? Or will OpenGL automatically map my coordinates inside the viewport's rectangle?

Help is very much appreciated.
Thank you very much. smile.png
Advertisement
0,0 is relative to the windows 0,0 and not the desktop. If you didn't know that already, I suggest you just start diving in to opengl. You don't need scissoring for the viewport. If you simply started opengl you would figure that out almost immediately. Not sure what centered on the screen means.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

I'm not quite sure of the gist of what you are looking for, but maintaining aspect ratio shouldn't be that difficult. Supposing you have a onResize() method (or similar), that handles the window resize events, you can take the window width and divide it by window height (supposing the height != 0). That should give you the correct aspect ratio in all circumstances.

If you have, let's say, a square 1:1 ratio when windowed, and want to keep it that way and add black borders to the left and right edges, I suggest to render the scene to a texture in any case. Then, just render a square plane with that texture at the center of the world. Whenever the aspect ratio is changed, the "camera" will automatically add the black background for you. :)
Since you want to make your viewport width match your window width you just need to use the window's height and the desired viewport height to calculate what you should provide in the y parameter for your glViewport call:
http://www.opengl.org/sdk/docs/man/xhtml/glViewport.xml
x, y specify the lower left corner of the viewport.

Your game coordinates should be viewport relative unless you make them so that they aren't.
Hi guys, thanks for the replies!

I finally got what I wanted to do.

if (fullscreen) {
GLfloat aspect = 4.0f / 3.0f;

int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);

int viewWidth = screenWidth;
int viewHeight = screenWidth / aspect;

if (viewHeight > screenHeight) {
viewHeight = screenHeight;
viewWidth = screenHeight * aspect;
}

int vportX = (screenWidth - viewWidth) / 2;
int vportY = (screenHeight - viewHeight) / 2;

glViewport(vportX, vportY, viewWidth, viewHeight);
}


This results in the viewport's height matching the window's height, and the width will be adjusted to match the aspect ratio.

This topic is closed to new replies.

Advertisement