Problem render tile map

I have create a texture with my game map

SDL_SetRenderTarget(_game->renderer(), _texture);

for(int x = 0; x < _width; ++x)
{
    for(int y = 0; y < _height; ++y)
    {
        int tile = _data[(x * _width) + y];
        SDL_Rect target {static_cast<int>((x * 32)), static_cast<int>((y * 32)), 32, 32 };
        SDL_RenderCopyEx(_game->renderer(), _tiles[tile], &_source, &target, 0, 0, SDL_FLIP_NONE);
    }
}
SDL_SetRenderTarget(_game->renderer(), 0)

Then each time I render I copy the relevant portion

void render()
{
    auto camera = _game->getCamera()->position();
    SDL_Rect source { static_cast<int>(camera.x), static_cast<int>(camera.y), 800, 600};
    SDL_Rect target { 0, 0, 800, 600 };
   SDL_RenderCopyEx(_game->renderer(), _texture, &source, &target, 0, 0, SDL_FLIP_NONE);
}

But when the player moves horizontally, there is an odd effect in vertical lines, like if you notice they are redrawn and they are not totally vertical.

Any ideas how to avoid this effect

Please make a recording of the game and the issue you are facing.

So far I did a screen recording with QuickTime on macOS but looks better than the actual play, not sure what is going on, maybe the recording doesn’t capture 60 fps.

Locally I see a zigzag in the vertical lines when the player block is moving, my render method just uses SDL_RenderCopyEx to copy the relevant part of the tilemap texture, that was created during initialization.

Isn’t this just a screen tearing due to lack of V-Sync?

1 Like

It can be, how can I fix it? passing SDL_RENDERER_PRESENTVSYNC to SDL_CreateRenderer seems to solve it

Yea, that’s the way to do it :wink: