SDL2.0在mfc窗口中显示yuv的一种方法

DWORD ThreadFun()
{
    //用mfc窗口句柄创建一个sdl window
    SDL_Window * pWindow = SDL_CreateWindowFrom( (void *)( GetDlgItem(IDC_STATIC1)->GetSafeHwnd() ) );

    SDL_Rect sdlRT;
    sdlRT.h = 288;
    sdlRT.w = 352;
    sdlRT.x = 0;
    sdlRT.y = 0;

    SDL_Rect dstRT;
    dstRT.h = 288;
    dstRT.w = 352;
    dstRT.x = 0;
    dstRT.y = 0;

    int iW = 352;
    int iH = 288;
    char szData[352*288*2] = {0};

    char szPrint[256] = {0};
    
    //计算yuv一行数据占的字节数
    int iPitch = iW*SDL_BYTESPERPIXEL(SDL_PIXELFORMAT_YV12);    

    int iWidth = 0;
    int iHeight = 0;
    SDL_GetWindowSize( pWindow, &iWidth, &iHeight );
    dstRT.h = iHeight;
    dstRT.w = iWidth;

    //获取当前可用画图驱动 window中有3个,第一个为d3d,第二个为opengl,第三个为software
    int iii = SDL_GetNumRenderDrivers();
    //创建渲染器,第二个参数为选用的画图驱动,0代表d3d
    SDL_Renderer * pRender = SDL_CreateRenderer( pWindow, 0, SDL_RENDERER_ACCELERATED );
    sprintf( szPrint, "%s", SDL_GetError());

    SDL_RendererInfo info;
    SDL_GetRendererInfo(pRender, &info);

    SDL_GetRenderDriverInfo(0, &info);    //d3d
    SDL_GetRenderDriverInfo(1, &info);    //opgl
    SDL_GetRenderDriverInfo(2, &info);    //software

    char szInfo[256] = {0};
    sprintf(szInfo, "%s", info.name);
    sprintf( szPrint, "%s", SDL_GetError());

    //创建纹理
    SDL_Texture * pTexture = SDL_CreateTexture( pRender,SDL_PIXELFORMAT_YV12, SDL_TEXTUREACCESS_STREAMING, iW, iH );


    FILE * pFile = fopen( "d:\\cap1.yuv", "rb" );

    if ( pFile == NULL )
    {
        int u = 0;
    }


    //读yuv文件,该文件中存放的数据尺寸为 352*288的yv12数据
    while ( fread( szData, 1, iW*iH*3/2, pFile ) != NULL )
    {
        int i = SDL_UpdateTexture( pTexture, &sdlRT, szData, iPitch );
        SDL_RenderClear( pRender );
        SDL_RenderCopy( pRender, pTexture, &sdlRT, &dstRT );
        SDL_RenderPresent( pRender );

        Sleep(40);
    }

    fclose(pFile);


    if ( pTexture != NULL )
    {
        SDL_DestroyTexture( pTexture );
        pTexture = NULL    ;
    }

    if ( pRender != NULL )
    {
        SDL_DestroyRenderer( pRender );
        pRender = NULL;
    }


    if ( NULL != pWindow )
    {
        SDL_DestroyWindow( pWindow );
        pWindow = NULL;
    }

}
注意在SDL_CreateWindowFrom中sdl将窗口的消息处理函数地址给改写成sdl的函数了(在sdl源文件中SetupWindowData()此函数内修改),
SDL2.0在mfc窗口中显示yuv的一种方法_第1张图片

同时在SDL_Destroy函数中会恢复hwnd默认消息处理函数,如果不希望sdl参与消息处理,可将SetWindowLongPtr注销掉

对SDL的使用还在摸索中,不能保证上述知识全部正确,如哪位仁兄发现问题,请留言更正,再次谢过了!

你可能感兴趣的:(SDL2.0在mfc窗口中显示yuv的一种方法)