Jump to content
• Advertisement

# SFML: Dealing with Aspect Ratio?

This topic is 2944 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

I'm setting up a top down 2D game with C++, sfml 2.0, and boost::python. I have it set up to parse an ini to set the resolution and bit depth when the app runs. But how do you manage, conceptually and programmatically, different aspect ratios? At first I though about working with one resolution and just scaling it all to the appropriate resolution, either down or up, but I realized that would result in the screen looking stretched. So how can I, with the least amount of effort, compensate for different aspect ratios?

#### Share this post

##### Share on other sites
Advertisement
And they said you'd never use this thing called math XD...

 void GameStateManager::Init(int resX, int resY, int bitDepth){ /// Initialize Window sf::VideoMode VMode(resX, resY, bitDepth); window.Create(VMode, "Title of the Game", sf::Style::Titlebar); window.SetFramerateLimit(60); int newH = (1920*resY)/resX; int displace = (newH - 1080)/(-2); window.SetView(sf::View(sf::FloatRect(0, displace, 1920, newH))); running = true; } 

So I have my game set up to draw things to a 1920 by 1080 space. This is assuming your game is widescreen mind you, otherwise you'd have to flip the math around. Since it *should* be too wide for different aspect ratios, we keep the desired width of the view as the draw space width. Then we calculate a height that would fit the user's aspect ratio:

resX/resY = 1920/t
resX(t) = 1920(resY)
t = [1920(resY)]/resX

Now there should be a black bar at the bottom but you'd most likely want to split that up. So what I do is calculate the difference in the new height (which should be bigger) and the draw height (1080 in this case) then divide it by negative two (negative so the camera moves the correct direction).

I've tested it with 5:4, 4:3, 16:9, and 16:10 resolutions and the display is always correct. [s]Now to fix the mouse when in windowed mode...[/s]

#### Share this post

##### Share on other sites

• Advertisement
• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• Advertisement

• ### Popular Now

• 16
• 11
• 24
• 43
• 75
• Advertisement
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!