Bottom edge of a rectangle is not straight in SDL2

Viewed 247

I am making rectangles using this simple function:

int render_cell(SDL_Renderer *renderer, int x, int y, const SDL_Color *color) {
    SDL_Rect rect;
    rect.x = x * CELL_WIDTH;
    rect.y = y * CELL_HEIGHT + 30;
    rect.w = CELL_HEIGHT;
    rect.h = CELL_WIDTH;

    SDL_SetRenderDrawColor(renderer, color->r, color->g, color->b, 255);
    SDL_RenderDrawRect(renderer, &rect);
    SDL_RenderFillRect(renderer, &rect);
}

As a result, I get the rectangles with 'broken' edge on the bottom side: enter image description here

I have tried avoiding the collision with other objects but this did not make any difference.

Also, the lines are straight as you can see and the other sides of the rectangles are as well.

Here is a google drive link to download my project: you need to navigate to build and type "make" into the terminal and then "./game" and it will run. This will work if you have got CMake https://drive.google.com/file/d/1txmb2IKEYcJOcFSUBVSZyqWyCZQDp2uR/view?usp=sharing

2 Answers

Looks like a known bug in the library: https://github.com/libsdl-org/SDL/issues/4001 which only affects openGL.

I'm seeing the same broken bottom line on rects also. I'm developing on Fedora 34. Below is a minimal example that shows the problem:

#include <SDL2/SDL.h>
#include <iostream>
#include <string>

int main(int argc, char** argv)
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* window = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 480, 0);
    SDL_Renderer* renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED);
    SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 0xff );
    SDL_Rect r = { 10, 10, 100, 100 };
    SDL_RenderDrawRect(renderer, &r);
    SDL_RenderPresent(renderer);

    // Type any text and enter to continue
    std::string dummy;
    std::cin >> dummy;

    SDL_DestroyRenderer(renderer);
    SDL_Quit();
}

I'm compiling the above with:

g++ example.cpp -o example -lSDL2

With the above, I get a window with a single white rectangle but the bottom line is not straight just as the OP has in their screenshot. It looks like the right side bottom corner is one pixel up, from the left side.

Caveat: This is prefaced by my top comments.

I'm unable to reproduce your problem.

But, I'll detail what I did and make some suggestions for how you can proceed.

I downloaded your source .zip from Google Drive. I extracted the .zip file and put all files under (e.g.) /home/me/cw2

In the /home/me/cw2/build directory, I did:

  1. rm -f CMakeCache.txt
  2. cmake /home/me/cw2
  3. make clean
  4. make
  5. ./game

The output window is below.

In other words, I did a full rebuild.

I also copied the files to another directory, did cleanup and rearranged the files to be compatible with the way I build SDL2 programs with my personal build environment. I got the same results.

The above was done on a fedora 29 system.

I also tried this on an ubuntu 18.04.5 system I have

The only things I can suggest:

  1. Do a full rebuild (including the make clean)
  2. Look at the output of pkg-config as I outlined in my top comments
  3. Rebuild on a different system.

If you're using ubuntu, I've had problems with the prebuilt SDL2 library package in the past. So, on my ubuntu system, I rebuilt SDL2 from the source package. See: Compile an SDL project using gcc? Consider this as a last resort.


Here is the output of your program on my system.

  1. It is clean with no half line overlap.
  2. And, no "black area" in the top left:

enter image description here

Related