Skip to content

SDL_RenderCopyExF does not have subpixel precision when SDL_RendererFlip argument is anything but SDL_FLIP_NONE #11783

@TheRealNoobytube

Description

@TheRealNoobytube

In SDL2, when using the SDL_RenderCopyExF function to render a texture to the screen, if the SDL_RendererFlip argument is anything other than SDL_FLIP_NONE e.g passed in SDL_FLIP_HORIZONTAL, the function loses its subpixel precision and renders the textures position in the window rounded to the nearest pixel.
Here is the code where the function is used:

void Texture::draw(float posX, float posY, float scaleX, float scaleY, int width, int height, int startX, int startY, int sizeX, int sizeY, SDL_RendererFlip flip) {
	SDL_FRect dstRect;

	dstRect.x = posX;
	dstRect.y = posY;

	std::cout << dstRect.x << "\n";

	//if width is less than 0, use the retrieved width values from SDL_QueryTexture. otherwise use the passed in width values
	dstRect.w = (width <= 0) ? dimensions.x * scaleX : width * scaleX;
	//if height is less than 0, use the retrieved height values from SDL_QueryTexture. otherwise use the passed in height values
	dstRect.h = (height <= 0) ? dimensions.y * scaleY : height * scaleY;

	SDL_Rect srcRect;
	srcRect.x = (startX < 0) ? 0 : startX;
	srcRect.y = (startY < 0) ? 0 : startY;
	srcRect.w = (sizeX < 0) ? dstRect.w / scaleX : sizeX;
	srcRect.h = (sizeY < 0) ? dstRect.h / scaleY : sizeY;

	

	SDL_RenderCopyExF(this->renderer, texture, &srcRect, &dstRect, 0, NULL, flip);
}

Here is an example of what this looks like. Notice how when the sprite moves to the left (which flips the sprite horizontally), that the movement is much more jittery and less precise:
PlayerMovement

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions