SDL2 strange behaviour of hardware rendering

2020-06-25 14:01发布

问题:

I want to create my first game.
Nothing special, just blue rectangle moving when WSAD keys are pressed.

The problem is that when I run my game, there are bugs with rectangle(See image below). Bugs appears only during horizontal movement, and not vertical.


Which is interesting, when I changed line:

renderer = SDL_CreateRenderer(display, -1, SDL_RENDERER_ACCELERATED)

to:

renderer = SDL_CreateRenderer(display, -1, SDL_RENDERER_SOFTWARE)

everything is OK

I am using Windows 10, MinGw with CMake(C++14), and SDL 2.0.8, Intel core i5 7th gen, Radeon M7 R465

Im my code OnRender function is responsible for rendering, maybe I made something wrong in it?(Function in my code posted at end of question)

I am also using SDL_WINDOW_OPENGL flag to create my window, but changing it to SDL_WINDOW_SHOWN doesn't change anything.

#include <SDL2/SDL.h>

class Game
{
private:
    SDL_Surface *display_surf = nullptr;
    SDL_Renderer *renderer = nullptr;
    SDL_Window *display = nullptr;

private:
    bool running, prW = false, prS = false, prD = false, prA = false;
    int x, y;
    int spd_y, spd_x;
    int scr_w, scr_h;

public:
    Game();
    int OnExecute();

public:
    bool OnInit();
    void OnEvent( SDL_Event *event );
    void OnLoop();
    void OnRender();
    void OnCleanup();
};

Game::Game()
{
    running = false;
}

int Game::OnExecute()
{
    if( !OnInit() )
    {
        return -1;
    }

    running = true;
    SDL_Event event;
    while( running )
    {
        while( SDL_PollEvent( &event ) )
        {
            OnEvent( &event );
        }

        OnLoop();
        OnRender();
        SDL_Delay( 1 );
    }

    OnCleanup();
    return 0;
}

bool Game::OnInit()
{
    if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
    {
        return false;
    }

    SDL_DisplayMode dspm;
    if( SDL_GetDesktopDisplayMode( 0, &dspm ) < 0 )
    {
        return false;
    }

    scr_h = dspm.h;
    scr_w = dspm.w;

    if( ( display = SDL_CreateWindow( "Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1920, 1080,
        SDL_WINDOW_OPENGL ) ) == nullptr )
    {
        return false;
    }

    display_surf = SDL_GetWindowSurface( display );


    if( ( renderer = SDL_CreateRenderer( display, -1, SDL_RENDERER_ACCELERATED ) ) == nullptr )
    {
        return false;
    }

    x = 0;
    y = 0;

    spd_x = 0;
    spd_y = 0;

    SDL_SetWindowFullscreen( display, SDL_WINDOW_FULLSCREEN );

    return true;

}

void Game::OnEvent( SDL_Event *event )
{
    if( event->type == SDL_QUIT )
    {
        running = false;
        return;
    }

    switch( event->type )
    {
    case SDL_KEYDOWN:
        switch( event->key.keysym.sym )
        {
        case SDLK_w:
            if( prS )
            {
                spd_y = 0;
            }
            else
            {
                spd_y = -5;
            }
            prW = true;
            break;
        case SDLK_s:
            if( prW )
            {
                spd_y = 0;
            }
            else
            {
                spd_y = 5;
            }
            prS = true;
            break;
        case SDLK_d:
            if( prA )
            {
                spd_x = 0;
            }
            else
            {
                spd_x = 5;
            }
            prD = true;
            break;
        case SDLK_a:
            if( prD )
            {
                spd_x = 0;
            }
            else
            {
                spd_x = -5;
            }
            prA = true;
            break;
        default:
            return;
        }

        break;
    case SDL_KEYUP:
        switch( event->key.keysym.sym )
        {
        case SDLK_w:
            if( !prS )
            {
                spd_y = 0;
            }
            else
            {
                spd_y = 5;
            }
            prW = false;
            break;
        case SDLK_s:
            if( !prW )
            {
                spd_y = 0;
            }
            else
            {
                spd_y = -5;
            }
            prS = false;
            break;
        case SDLK_a:
            if( !prD )
            {
                spd_x = 0;
            }
            else
            {
                spd_x = 5;
            }
            prA = false;
            break;
        case SDLK_d:
            if( !prA )
            {
                spd_x = 0;
            }
            else
            {
                spd_x = -5;
            }
            prD = false;
            break;
        default:
            return;
        }
    default:
        return;
    }
}

void Game::OnLoop()
{
    x += spd_x;
    y += spd_y;

    if( x < 0 )
    {
        x = 0;
    }
    else if( x > scr_w - 100 )
    {
        x = scr_w - 100;
    }

    if( y < 0 )
    {
        y = 0;
    }
    else if( y > scr_h - 100 )
    {
        y = scr_h - 100;
    }
}

void Game::OnRender()
{
    SDL_SetRenderDrawColor( renderer, 0, 0, 0, 0x00 );

    SDL_RenderClear( renderer );

    SDL_Rect charc;
    charc.x = x;
    charc.y = y;
    charc.w = 100;
    charc.h = 100;

    SDL_SetRenderDrawColor( renderer, 0, 0, 0xff, 0 );

    SDL_RenderFillRect( renderer, &charc );

    SDL_RenderPresent( renderer );
}

void Game::OnCleanup()
{
    SDL_DestroyWindow( display );
    SDL_Quit();
}

int main( int argc, char** argv )
{
    Game game;
    return game.OnExecute();
}

回答1:

Looks a lot like tearing caused by a high frame-rate & lack of vsync.

I can get tear-less drawing by passing SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC to flags on SDL_CreateRenderer():

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

class Game
{
private:
    SDL_Renderer *renderer = nullptr;
    SDL_Window *display = nullptr;

private:
    bool running, prW = false, prS = false, prD = false, prA = false;
    int x, y;
    int spd_y, spd_x;
    int scr_w, scr_h;

public:
    Game();
    int OnExecute();

public:
    bool OnInit();
    void OnEvent( SDL_Event *event );
    void OnLoop();
    void OnRender();
    void OnCleanup();
};

Game::Game()
{
    running = false;
}

int Game::OnExecute()
{
    if( !OnInit() )
    {
        return -1;
    }

    running = true;
    SDL_Event event;

    Uint32 beg = SDL_GetTicks();
    size_t frames = 0;
    while( running )
    {
        while( SDL_PollEvent( &event ) )
        {
            OnEvent( &event );
        }

        OnLoop();
        OnRender();

        frames++;
        Uint32 end = SDL_GetTicks();
        if( end - beg > 1000 )
        {
            std::cout << "Frame time: " << ( end - beg ) / frames << " ms" << std::endl;
            beg = end;
            frames = 0;
        }
    }

    OnCleanup();
    return 0;
}

bool Game::OnInit()
{
    if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
    {
        return false;
    }

    if( ( display = SDL_CreateWindow( "Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, 0 ) ) == nullptr )
    {
        return false;
    }

    scr_w = 1280;
    scr_h = 720;

    Uint32 rflags = SDL_RENDERER_ACCELERATED;
    rflags |= SDL_RENDERER_PRESENTVSYNC;
    if( ( renderer = SDL_CreateRenderer( display, -1, rflags ) ) == nullptr )
    {
        return false;
    }

    x = 0;
    y = 0;

    spd_x = 0;
    spd_y = 0;

    return true;
}

void Game::OnEvent( SDL_Event *event )
{
    if( event->type == SDL_QUIT )
    {
        running = false;
        return;
    }

    switch( event->type )
    {
    case SDL_KEYDOWN:
        switch( event->key.keysym.sym )
        {
        case SDLK_w:
            if( prS )
            {
                spd_y = 0;
            }
            else
            {
                spd_y = -5;
            }
            prW = true;
            break;
        case SDLK_s:
            if( prW )
            {
                spd_y = 0;
            }
            else
            {
                spd_y = 5;
            }
            prS = true;
            break;
        case SDLK_d:
            if( prA )
            {
                spd_x = 0;
            }
            else
            {
                spd_x = 5;
            }
            prD = true;
            break;
        case SDLK_a:
            if( prD )
            {
                spd_x = 0;
            }
            else
            {
                spd_x = -5;
            }
            prA = true;
            break;
        default:
            return;
        }

        break;
    case SDL_KEYUP:
        switch( event->key.keysym.sym )
        {
        case SDLK_w:
            if( !prS )
            {
                spd_y = 0;
            }
            else
            {
                spd_y = 5;
            }
            prW = false;
            break;
        case SDLK_s:
            if( !prW )
            {
                spd_y = 0;
            }
            else
            {
                spd_y = -5;
            }
            prS = false;
            break;
        case SDLK_a:
            if( !prD )
            {
                spd_x = 0;
            }
            else
            {
                spd_x = 5;
            }
            prA = false;
            break;
        case SDLK_d:
            if( !prA )
            {
                spd_x = 0;
            }
            else
            {
                spd_x = -5;
            }
            prD = false;
            break;
        default:
            return;
        }
    default:
        return;
    }
}

void Game::OnLoop()
{
    x += spd_x;
    y += spd_y;

    if( x < 0 )
    {
        x = 0;
    }
    else if( x > scr_w - 100 )
    {
        x = scr_w - 100;
    }

    if( y < 0 )
    {
        y = 0;
    }
    else if( y > scr_h - 100 )
    {
        y = scr_h - 100;
    }
}

void Game::OnRender()
{
    SDL_SetRenderDrawColor( renderer, 0, 0, 0, 0x00 );

    SDL_RenderClear( renderer );

    SDL_Rect charc;
    charc.x = x;
    charc.y = y;
    charc.w = 100;
    charc.h = 100;

    SDL_SetRenderDrawColor( renderer, 0, 0, 0xff, 0 );

    SDL_RenderFillRect( renderer, &charc );

    SDL_Delay( 1 );
    SDL_RenderPresent( renderer );
}

void Game::OnCleanup()
{
    SDL_DestroyWindow( display );
    SDL_Quit();
}

int main( int argc, char** argv )
{
    Game game;
    return game.OnExecute();
}

If I just pass SDL_RENDERER_ACCELERATED I get tearing and a vastly higher frame-rate.

Make sure your OS isn't configured to disable vsync by default.



回答2:

Many developers seem to get some unwanted behaviours when enabling SDL_RENDERER_ACCELERATED flag on SDL 2.0.8.

A ticket has been opened in libsdl's bugzilla (https://bugzilla.libsdl.org/show_bug.cgi?id=4110). It's about another problem but issue with hardware rendering is mentioned in description.

For now, I use software rendering (SDL_RENDERER_SOFTWARE) as a fallback. Not really what I wanted to do but now I get the expected result.

I'll try PREVENTSYNC...