3
\$\begingroup\$

Let's say I have a 1024x768 SDL_Window, an SDL_Renderer bound to that window and a 768x1024 SDL_Texture into which I do my rendering. How can I render that texture such that it is rotated by 90° to fit the screen exactly (1:1 mapping between texels and pixels)?

I tried using the SDL_RenderCopyEx() function but I couldn't make it work properly with non-square window and texture, probably because it doesn't support scaling.

\$\endgroup\$
4
\$\begingroup\$

I found the solution (or a solution):

If W is the width of the window (e.g. 1024) and H is its height (e.g. 768), and if the texture has width H and height W, then

SDL_Rect dstrect;
dstrect.x = (W - H) / 2 - 1;
dstrect.y = (H - W) / 2;
dstrect.w = H;
dstrect.h = W;
SDL_RenderCopyEx(renderer, texture, nullptr, &dstrect, 90, nullptr, SDL_FLIP_NONE);

does the trick.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ This works indeed and produced pixel-perfect result here (not tested all combinations even/odd). Thanks for sharing! User should just beware that W and H are width and height of destination/new/rotated texture, not source, that's why text counterintuitively says "(source) texture has width H and height W". \$\endgroup\$ Aug 26 '18 at 7:20
  • \$\begingroup\$ I am trying to use this method to display iOS screens for our rPlay desktop application, but failed to get it to work correctly. Basically it's 886 x 1920 texture, but needs to display as 1920 x 886 (90 degree rotation). If I still use 886 x 1920 window, rotation works very nicely, but of course it gets cut off. However, once I resize the window first to 1920 * 886, then rotation does not work correctly, only a small portion is displayed. \$\endgroup\$
    – huisinro
    Feb 18 '19 at 19:38
  • \$\begingroup\$ I figured it out, need to change width and height inside SDL_RenderSetLogicalSize() accordingly, then works perfectly for the rotation \$\endgroup\$
    – huisinro
    Feb 19 '19 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.