Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SDL_RenderSetScale incorrectly applies to rotated bitmaps in SDL2 (2.0.3)

SDL_RenderSetScale will scale co-ordinates accordingly until rotation is used with SDL_RenderCopyEx. SDL seems to apply the scaling pre-rotation, so the rotated texture's aspect ratio is wrong. For example, a texture rotated 90 degrees will have the x and y scaling reversed. I've tried to calculate a formula to counteract this scaling, but the result isn't great.

I've also noticed that straight lines drawn with SDL_RenderDrawLine are drawn a different thickness according to scaling, but diagonal lines are still drawn per screen pixel.

Has anyone else had this problem? Is there another way around it to get the desired result? Is there any plan to fix SDL2 so the scaling effect is consistent throughout?

like image 570
Ant the Alchemist Avatar asked Jan 24 '15 07:01

Ant the Alchemist


3 Answers

Alas, not. The issue becomes apparent with different width and height scaling factors. Try:

SDL_RenderSetScale(renderer, 4.0f, 2.0f);
SDL_Rect rect = { 16, 16, 128, 128 };
SDL_RenderCopyEx(renderer, tex, NULL, &rect, 90.0, NULL, SDL_FLIP_NONE);

...and you'll see the problem.

You would expect an originally square texture to now be a wide rectangle, but instead it is a tall rectangle.

The scaling is applied before the rotation - whereas I personally would expect the scaling to be applied after the rotation, as the whole point of SDL_RenderSetScale() was seemingly to allow an independent coordinate system to be chosen, but if the aspect ratios of the two coordinate systems aren't the same, then any rotated textures will be displayed wrong.

Ronnie

like image 97
Ronnie Cassinello Avatar answered Nov 10 '22 04:11

Ronnie Cassinello


This question has been asked four years ago but I'm still confronted to this problem. So, if anyone drops by, here is my answer.

Lignum's assumption about SDL versioning is wrong. Indeed, I'm working with SDL 2.0.9 and this question is still relevant. As Ronnie pointed out, this problem only comes when the aspect ratio is not preserved (in Lignum code example, the aspect ratio is preserved as the scale factors along x and y axis are equal).

The only solution, I'm afraid, is to simply avoid SDL_RenderSetScale and manually scale the coordinates of the destination rectangle in SDL_RenderCopyEx.

like image 45
Vost Avatar answered Nov 10 '22 05:11

Vost


Using this code, I'm afraid I wasn't able to reproduce the issue:

SDL_RenderSetScale(renderer, 4.0f, 4.0f);

SDL_Rect rect = { 16, 16, 128, 128 };
SDL_RenderCopyEx(renderer, tex, NULL, &rect, 90.0, NULL, SDL_FLIP_NONE);

This works perfectly fine, the image is rotated 90°, scaled by a factor of 4. Since this was asked 2 years before my reply, I'd assume this might've been a bug in SDL 2.0.3, since I'm using SDL 2.0.5.

Should this happen on the latest version anyway, not all hope is lost. An alternative to using SDL_RenderSetScale is to simply multiply your destination rectangle's width and height by your desired scale, which, from my experience, works just as well. Considering the fact that you'd be passing the rectangle into SDL_RenderCopyEx directly, the function should most definitely keep this in mind when rotating.

like image 1
Emily Avatar answered Nov 10 '22 04:11

Emily