What is the easiest way to set different resolutions with SDL2?
I was trying to use the
SDL_RenderSetScale() function first, but for some reason it alters the physics of the game (jumps are higher or smaller for example). My other guess was to use
SDL_RenderSetLogicalSize(), since it was mentioned in another similar question here, but I'm not quite sure how it is supposed to work.
What I would simply like to do:
Let's say I have the game world seen in 320x240. Then no matter the resolution of the window, what we see in 320x240 is simply "scaled up". So for example: what is supposed to be 1 pixel in the "original view", becomes 4 pixel in 640x480 and so on, but all the logic still happens in 320x240. So when I move something by 1 pixel, it will move seemingly 4 pixels in 640x480, but only 1 logically.
Yes, in the situation you describe, you want to use
The function takes in the width and height of the logical viewport, which in your case is the resolution you want to program your game logic in. You would call
SDL_RenderSetLogicalSize(renderer, 320, 240);, and after that you don't need to worry about resolution scaling. SDL will scale the rendering for you automatically, and you get resolution independence.
Now, there are a couple of downsides to using
SDL_RenderSetLogicalSize: It will letter box the viewport, meaning that in certain resolutions you will get black boxes either to the top and bottom or left and right of the window. I also had some issues with rounding errors when using
SDL_RenderSetLogicalSize, which created ugly artifacts between tile borders. Those could however be fixed using
I myself use in my SDL2 game which gets upscaled while rendering custom scaling, meaning that I scale all textures that get rendered using destination rectangles. However, using the SDL scaling pipeline might just do the job for you fine, if you are okay with the limitations it sets. Feel free to ask if you are more interested in implementing scaling yourself.