// 代码是雷神写的 我只是拿过来使用而已 #include "stdafx.h" #include <string.h> #include <iostream> extern "C" { #include "SDL.h" }; #undef main // 注意这个 不然SDL库报一个错误 const int bpp = 12; int screen_w = 500, screen_h = 500; const int pixel_w = 320, pixel_h = 180; unsigned char buffer[pixel_w*pixel_h*bpp / 8]; //Refresh Event #define REFRESH_EVENT (SDL_USEREVENT + 1) //Break #define BREAK_EVENT (SDL_USEREVENT + 2) int thread_exit = 0; //每隔40毫秒向SDL系统发送一个REFRESH_EVENT事件 int refresh_video(void *opaque) { thread_exit = 0; while (thread_exit == 0) { SDL_Event event; event.type = REFRESH_EVENT; SDL_PushEvent(&event); SDL_Delay(40); } thread_exit = 0; //Break SDL_Event event; event.type = BREAK_EVENT; SDL_PushEvent(&event); return 0; } int main(int argc, char* argv[]) { if (SDL_Init(SDL_INIT_VIDEO)) { printf("Could not initialize SDL - %s ", SDL_GetError()); return -1; } SDL_Window *screen; //SDL 2.0 Support for multiple windows screen = SDL_CreateWindow("Simplest Video Play SDL2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen_w, screen_h, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); if (!screen) { printf("SDL: could not create window - exiting:%s ", SDL_GetError()); return -1; } SDL_Renderer* sdlRenderer = SDL_CreateRenderer(screen, -1, 0); Uint32 pixformat = 0; //IYUV: Y + U + V (3 planes) //YV12: Y + V + U (3 planes) pixformat = SDL_PIXELFORMAT_IYUV; SDL_Texture* sdlTexture = SDL_CreateTexture(sdlRenderer, pixformat, SDL_TEXTUREACCESS_STREAMING, pixel_w, pixel_h); FILE *fp = NULL; fp = fopen("D:\pthread\Pre-built.2\111.yuv", "rb+"); if (fp == NULL) { while(1) { printf("cannot open this file "); } return -1; } SDL_Rect sdlRect; SDL_Thread *refresh_thread = SDL_CreateThread(refresh_video, NULL, NULL); SDL_Event event; while (1) { //收取SDL系统里面的事件 SDL_WaitEvent(&event); if (event.type == REFRESH_EVENT) { if (fread(buffer, 1, pixel_w*pixel_h*bpp / 8, fp) != pixel_w*pixel_h*bpp / 8) { // Loop fseek(fp, 0, SEEK_SET); fread(buffer, 1, pixel_w*pixel_h*bpp / 8, fp); } SDL_UpdateTexture(sdlTexture, NULL, buffer, pixel_w); //FIX: If window is resize sdlRect.x = 0; sdlRect.y = 0; sdlRect.w = screen_w; sdlRect.h = screen_h; SDL_RenderClear(sdlRenderer); SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, &sdlRect); SDL_RenderPresent(sdlRenderer); } else if (event.type == SDL_WINDOWEVENT) { //If Resize SDL_GetWindowSize(screen, &screen_w, &screen_h); } else if (event.type == SDL_QUIT) { thread_exit = 1; } else if (event.type == BREAK_EVENT) { break; } } SDL_Quit(); return 0; }