Draw to a RenderTarget to zoom and scroll

Hello : )

I would like to create multiple views and to support zooming or scrolling in individual views.

However it seems to be recommended that when I want to zoom in and out, I shall modify the texture’s destination rectangle, is there no way to first draw the scene to a render target, then transform it, and then actually draw that transformation?
I would be very glad if you could - if it is possible - give me some pointers into the right direction!

E.g. scene (sprites/multiple textures) is drawn to a 1280x720 render target and then scaled up to game’s selected resolution of 1920x1080.

Thanks for your time! : )

If I’m understanding correctly, it’s straightforward. Render the scene to a 1280x720 target texture, then SDL_RenderCopy() that texture to the 1920x1080 screen, which will scale it. It’s the same approach as discussed here except that the target texture and the backbuffer are different sizes.

1 Like

Thanks a lot! It works : ) SDL2 mentions that some computers may not support this. Is there a list to get an idea? I would like to inform people before they try and test it.
I never encountered these “may not work” on e.g. SFML or other libraries. Can I assume that this a remain from very early times of SDL?

In my experience it’s very rare for target textures not to be supported. My app relies on it, and I’ve only ever received one such report, which was for a Dell Latitude D620 running Windows 7 (a 13-year-old PC).

1 Like

I’m really failing to recreate my scaling behaviour as I have done it in SFML with Views/RenderTargets.
Just like here: https://www.sfml-dev.org/tutorials/2.5/graphics-view.php

My zoom extends the view, so if a player zooms out, they see more. However if I change the render texture’s size times the zoom scale factor, it really starts to kill the framerate at some point.
While in SFML I simply see more of the game’s world the further I zoom out while barely affecting the framerate at all. SDL goes down from 5000 to 4, due to drawing to a 19800x10200 at some point (so times 10 zoom out).

Am I doing something wrong or is this simply impossible with SDL2? I do not want to use OpenGL calls.

There is no point drawing to a destination that is bigger than your screen, that just wastes time. If you are currently changing the zoom ratio by altering the size of the ‘destination’ rectangle, change the size of the ‘source’ rectangle instead.

However since SDL2 supports only integer sizes it is possible that you may need to adjust both source and destination rectangles to achieve sufficiently fine control over the zoom.

Yes, but if the original texture is only 1920x1080 big and the actual window is 1280x720, it can only zoom out the source up to 1920x1080, anything larger will have no effect.
I was able to zoom in as far as I want but zooming out stops at the point I reach the render texture’s 1920x1080.
However I want to extend the view field, the level itself is bigger, some textures are quite large, the overall scene rendered in its original size would exceed 1920x1080 by far.

If you have a source ‘scene’ that is larger than the maximum texture size then of course you will need to tile it (this is similar to the other thread in which the OP is asking about scrolling a large Tiff file). If you want your program to run on a range of different target platforms, bear in mind that the maximum texture size varies. You can discover what it is by calling SDL_GetRendererInfo().