6
\$\begingroup\$

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.

\$\endgroup\$

1 Answer 1

8
\$\begingroup\$

Yes, in the situation you describe, you want to use SDL_RenderSetLogicalSize().

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 SDL_RenderSetIntegerScale.

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.

\$\endgroup\$
4
  • \$\begingroup\$ As I see it could be the function I'm looking for, but I'm still not sure how to use it. I create my window at the bigger size, then call SDL_RenderSetLogicalSize() with the smaller size, and it indeed scales up everything, but in the game window, I only see the top left portion of the game. Maybe I call it at the wrong time? \$\endgroup\$
    – Letokteren
    Commented Jul 30, 2016 at 10:01
  • \$\begingroup\$ I would need to see your (relevent) code to answer what you are doing wrong. I can't really guess that. \$\endgroup\$
    – user35344
    Commented Jul 30, 2016 at 10:02
  • \$\begingroup\$ Sorry, my bad. The fault was in my code. I still based the camera following on the window size and not on the logical size. Now it works like a charm. Thank you very much! \$\endgroup\$
    – Letokteren
    Commented Jul 30, 2016 at 10:10
  • \$\begingroup\$ All right! Happy to hear that it's working and that I managed to clarify something for you. \$\endgroup\$
    – user35344
    Commented Jul 30, 2016 at 10:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .