SDL2 double buffer not working, still tearing

这一生的挚爱 提交于 2019-12-10 21:02:57

问题


I need a double buffer because i'm starting to notice tearing when I want to move my texture made tile map around the screen via mouse click.

I'm using SDL2 and this is a SDL2 specific question, check out my code that produces tearing, whats wrong?

//set up buffer and display
bufferTexture = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBA8888,SDL_TEXTUREACCESS_STREAMING,800,600);
displayTexture = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBA8888,SDL_TEXTUREACCESS_TARGET,800,600);


while(running)
{
    handleEvents();

    SDL_SetRenderTarget(renderer,bufferTexture); // everything goes into this buffer
    SDL_RenderClear(renderer); // clear buffer before draw

    gTileMovement.updateMapCoordinates();

    for(int i = 0; i < MAP_ROWS; i++)//rows
    {
        for(int j = 0; j < MAP_COLUMNS; j++)//columns
        {
            x = (j * 100) - (i * 100);
            y = ((i * 100) + (j * 100)) / 2;
            drawTiles(i,j,x,y);
        }
    }
    //move from buffer to display texture
    memcpy(&displayTexture,&bufferTexture,sizeof((&bufferTexture)+1));

    //change render target back to display texture
    SDL_SetRenderTarget(renderer,displayTexture);

    //show it all on screen
    SDL_RenderPresent(renderer);
}

for all it matters here is my drawTiles function too, is this not conventional? :

void drawTiles(int i,int j,int x,int y)
{
    //updates based on a mouse clicks xy coords
    gTileMovement.updateMapCoordinates();

    if(tileMap[i][j] == 1) // grass?
    {
         gSpriteSheetTexture.render(x+gTileMovement.getUpdatedX(),y+gTileMovement.getUpdatedY(),&gSpriteClips[1]);
    }
    if(tileMap[i][j] == 0) // wall?
    {
           gSpriteSheetTexture.render(x+gTileMovement.getUpdatedX(),y+gTileMovement.getUpdatedY(),&gSpriteClips[0]);
    }
    if(tileMap[i][j] == 2) // tree?
    {
       gSpriteSheetTexture.render(x+gTileMovement.getUpdatedX(),y+gTileMovement.getUpdatedY(),&gSpriteClips[2]);
    }
}

Which followes into how I SDL_RenderCopy the tiles through a class. This copies the textures onto the current targeted renderer does it not? Which is the buffer texture if i'm not mistaken.

void LTexture::render(int x, int y, SDL_Rect * clip)
{
SDL_Rect renderQuad = {x, y, mWidth, mHeight};

if(clip != NULL)
{
    renderQuad.w = clip->w;
    renderQuad.h = clip->h;
}

SDL_RenderCopy(renderer, mTexture, clip, &renderQuad); 

}

来源:https://stackoverflow.com/questions/28334892/sdl2-double-buffer-not-working-still-tearing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!