Stray Pixels in SDL2 Program

I’m seeing a lot of stray pixels attached to objects that I draw using SDL2. The objects the extra pixels get added to are lines, filled rectangles, and TTF text turned into textures. The SDL_RenderFillRect() function takes two arguments: an SDL_Renderer* and an SDL_Rect* to specify where to display the rectangle. I use that function for drawing rectangles. I don’t see how I could be doing something wrong. Whenever there is a stray pixel, it’s always one single pixel attached to the object. For instance, my rectangles will look like the following:

XXXXXXXXXXX
XXXXXXXXXXX
XXXXXXXXXXXX   <--- stray pixel on the right-bottom

The above is not the same number of pixels as the real image, nor the same scale, but it’s the same idea. There is an extra attached pixel.

I’ll get something similar with lines. Like this (composed of 3 lines):

                X   <--- stray pixel
XXXXXXXXXXXXXXXXX
X               X
X               X

For text, I’ll get something like this:

XXXXXXXXX
    X
    X
    X

        X   <--- stray pixel

The stray pixels always show up in the same place. For text, they’re always there. For lines and boxes, they are only sometimes there.

Any idea why this is happening? What are the sorts of things that can cause this?

Can you provide some code that reproduce the problem?

Thanks for your response. I have not yet been able to produce a minimal reproducible example. However, I did find at least a workaround (as well as a hint as to what is causing the problem), that is hopefully temporary. I found that the stray pixels went away when I called SDL_CreateRenderer() with flag SDL_RENDERER_SOFTWARE rather than SDL_RENDERER_ACCELERATED. Hopefully, I don’t find my program running super slow as I draw more things on the screen. But doing this has completely gotten rid of the stray pixels. So, I am very happy about that, at least for right now.

Can you try your code on another computer?

The reason I’m asking is that it may be a problem with your graphic card (be it drivers or an hardware failure).

I tried very similar code on a Windows machine, and the problem didn’t happen on that machine. I think it’s just an issue with the video card drivers on the other CentOS machine.

Thanks for your help. I’m good on this issue for now.

I can confirm this bug does exist. I think most people do not notice the tiny stray pixels. I did not even notice until I took a screenshot and zoomed in on my screen. I think we need to file an official bug report. I have multiple computers this bug shows up on.
libsdl2-2.0-0
Intel(R) Core™ i5-4210U CPU
Mesa DRI Intel(R) HD Graphics 4400 (HSW GT2)
Debian GNU/Linux 11 (bullseye)

SDL 2.0.10 has bugs of this sort when drawing lines, I think some of them have been fixed in later versions. Which version do you use?

thanks, SDL version 2.0.14 was the newest in debian 11 stable bullseye which I was using. Someone on github mentioned they believed it to be resolved in SDL 2.26

Yeah, older versions of SDL (SDL 2.0.14 is relatively old) had issues because they would leave the line drawing up to the GPU driver. Some versions of some GPU drivers wouldn’t draw the last pixel of the line, so SDL devs would try to have it draw an extra pixel, but then a different GPU or different driver version wouldn’t have that problem and whoops now it’s drawing an extra pixel, etc.

As of SDL 2.0.20, SDL plots the lines itself. You can control the exact method using the hint SDL_HINT_RENDER_LINE_METHOD. According to the documentation in SDL_hint.h, the values are:

“0” - Use the default line drawing method (Bresenham’s line algorithm as of SDL 2.0.20)
“1” - Use the driver point API using Bresenham’s line algorithm (correct, draws many points)
“2” - Use the driver line API (occasionally misses line endpoints based on hardware driver quirks, was the default before 2.0.20)
“3” - Use the driver geometry API (correct, draws thicker diagonal lines)