Skip to content

Instantly share code, notes, and snippets.

@southwolf
Created October 11, 2013 03:20
Show Gist options
  • Save southwolf/6929091 to your computer and use it in GitHub Desktop.
Save southwolf/6929091 to your computer and use it in GitHub Desktop.
SDL YUV display, screen freezes but code still running
int _tmain(int argc, _TCHAR* argv[])
{
FILE *in_fp;
int frameNum = 10000;
Uint32 screenWidth = 0, screenHeight = 0;
Uint32 frameSize = 0;
Uint8 *frame, *displayFrame;
SDL_RWops *handle = nullptr;
Uint32 readStart, readEnd, readTime, showStart, showEnd, showTime;
Uint32 pixfmt = SDL_PIXELFORMAT_IYUV;
SDL_DisplayMode *mode = new SDL_DisplayMode;
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
SDL_Rect displayrect;
//SDL_Event event;
//SDL_RendererInfo *rendererInfo = new SDL_RendererInfo;
if(SDL_Init(SDL_INIT_EVERYTHING)!=0)
{
cout << SDL_GetError() << endl;
SDL_Quit();
return 1;
}
SDL_GetDesktopDisplayMode(0, mode);
screenWidth = mode->w;
screenHeight = mode->h;
frameSize = screenHeight * screenWidth * 3 / 2;
//SDL_CreateWindowAndRenderer(0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP, &window, &renderer);
//SDL_CreateWindowAndRenderer(1600,900, SDL_WINDOW_SHOWN, &window, &renderer);
//if(window == NULL || renderer == NULL)
//{
// cout << SDL_GetError() << endl;
// SDL_Quit();
// return 1;
//}
//SDL_GetRendererInfo(renderer, rendererInfo);
window = SDL_CreateWindow("Test", 0, 0, screenWidth, screenHeight, SDL_WINDOW_FULLSCREEN_DESKTOP);
if(window == NULL)
{
cout << SDL_GetError() << endl;
SDL_Quit();
return 1;
}
renderer = SDL_CreateRenderer(window, -1 , SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
if(renderer == NULL)
{
cout << SDL_GetError() << endl;
SDL_Quit();
return 1;
}
texture = SDL_CreateTexture(renderer, pixfmt, SDL_TEXTUREACCESS_STREAMING, screenWidth, screenHeight);
if(texture == NULL)
{
cout << SDL_GetError() << endl;
SDL_Quit();
return 1;
}
fopen_s(&in_fp, "C:\\air\\air.yuv", "rb");
displayFrame = (Uint8 *)malloc((screenWidth * screenHeight) * 3 / 2);
frame = (Uint8 *)malloc((screenWidth * screenHeight) * 3 / 2);
for(int fn = 0; fn < frameNum; fn++)
{
readStart = SDL_GetTicks();
if(fread(frame, frameSize, 1, in_fp) < 1)
{
fseek(in_fp, 0, SEEK_SET);
fread(frame, frameSize, 1, in_fp);
}
handle = SDL_RWFromMem(frame, frameSize);
if(handle == NULL)
{
cout << SDL_GetError() << endl;
SDL_Quit();
return 1;
}
SDL_RWread(handle, displayFrame, frameSize, 1);
readEnd = SDL_GetTicks();
readTime = readEnd - readStart;
//cout << readTime << endl;
/* if(readTime < 20)
{
SDL_Delay(20 - readTime);
}
*/
displayrect.x = 0;
displayrect.y = 0;
displayrect.w = screenWidth;
displayrect.h = screenHeight;
if(SDL_UpdateTexture(texture, NULL, displayFrame, screenWidth*SDL_BYTESPERPIXEL(pixfmt)) !=0)
{
cout << SDL_GetError() << endl;
SDL_Quit();
return 1;
}
if(SDL_RenderClear(renderer) !=0)
{
cout << SDL_GetError() << endl;
SDL_Quit();
return 1;
}
if(SDL_RenderCopy(renderer, texture, NULL, &displayrect)!=0)
{
cout << SDL_GetError() << endl;
SDL_Quit();
return 1;
}
showStart = SDL_GetTicks();
SDL_RenderPresent(renderer);
showEnd = SDL_GetTicks();
showTime = showEnd - showStart;
if(showTime > 2)
{
cout << fn << ", " << showTime << endl;
}
}
SDL_DestroyRenderer(renderer);
SDL_RWclose(handle);
fclose(in_fp);
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment