Search code examples
c++c++11centos7sdl-2

What could be causing stray pixels to appear in my 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?


Solution

  • 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.

    If anyone has anymore insight on the problem and why I am having to use software rendering to avoid stray pixels, please edit this answer or add a comment.