2

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

  • 1
    At a guess, that looks like interframe "tearing". That is, the drawing code is racing with the flush to the display. So, the code is drawing into the frame that is trying to be displayed. Where is your (e.g.) `SDL_RenderPresent` to flush previous changes to the screen? The usual is something like: `start new frame, draw, draw, draw, flush frame to display, sleep` repeated. Are you double buffering? That is, drawing to a surface, flush that, draw to a different surface, flush that, and flip the buffers? – Craig Estey May 06 '21 at 18:32
  • Or ... It looks like you're drawing an _extra_ half line of a given square. The bottom line of a square [half filled] is on the _same_ raster line as the top line of the next square below it. You also have "black stuff" in the upper left of the screen which shouldn't be there. So, your geometries (screen and square/rectangle) may be off. Now that I think of it, because this is consistent across most squares, this indicates geometry errors rather than tearing. – Craig Estey May 06 '21 at 18:37
  • 3
    Your program seems like it would be small enough to _edit_ your question and post a full MRE that can be downloaded, compiled, and run by us. – Craig Estey May 06 '21 at 18:43
  • @CraigEstey sorry, could you please tell me where to put my code? I have some C files and Header files as well... I always run into the problem when I am asked to show mu full code but I have no idea how :))) – Andrei Mikhov May 06 '21 at 22:03
  • 1
    Edit the [mcve] into the question as text; see the program at the end of [this answer](https://stackoverflow.com/a/57684805/44729) for an example: single-file & self-contained. – genpfault May 06 '21 at 22:29
  • 2
    Create a _single_ `.c` file on your system that has all your code by editing and/or cut-and-paste that only has `#include` for standard things like `stdio.h` or `SDL.h`. You may have to reorder things so that (e.g.) if `funcB` calls `funcA`, that `funcA` is defined _before_ `funcB`. It should compile cleanly with (e.g.): `cc -o single single.c \`pkg-config --cflags --libs sdl2\`` The `single.c` can then be posted in a code block at the bottom of your question. Create another code block with the `{}` icon. Or, wrap the text with `\`\`\`` on separate lines. – Craig Estey May 06 '21 at 23:08
  • @CraigEstey thank you very much for your help. I have left a google drive link for you. I hope this is okay, if not - I will do what you've said! – Andrei Mikhov May 07 '21 at 07:28
  • 1
    @AndreiMikhov: Are the broken edges caused by calling the function `render_grid` or by calling the function `render_board`? Please provide a [mre] which only contains the code necessary to reproduce the problem. – Andreas Wenzel May 07 '21 at 10:38
  • According to the code on your Google Drive, the variables `CELL_WIDTH` and `CELL_HEIGHT` are of type `double`. This may introduce [rounding errors](https://stackoverflow.com/q/588004/12149471). Does the problem disappear if you use `int` instead? – Andreas Wenzel May 07 '21 at 11:14

2 Answers2

4

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.

Nigel Atkinson
  • 660
  • 6
  • 11
0

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

Craig Estey
  • 30,627
  • 4
  • 24
  • 48
  • Why the DV? This is too large for just a comment. DV's for answers are for _egregiously_ wrong answers [per SO guidelines]. – Craig Estey May 07 '21 at 17:31
  • 1
    I didn't DV, but if you can't repro, you can vote to close as no repro. – HolyBlackCat May 07 '21 at 17:56
  • @HolyBlackCat l didn't vote to close because of OP's image. And because l wanted to detail the possibe remedies. I've had issues with `cmake` getting in a funky state in the past. And problems with `libsdl2` [possibly on nVidia Jetson] . If OP can confirm that the full rebuild works or provide additional details, I'll follow up accordingly – Craig Estey May 07 '21 at 18:12