支持多种普片格式的加载,可选用SDL_Image库(依赖于SDL和一些其他第三方)。
#include "SDL.h" int _tmain(int argc, _TCHAR* argv[]) { SDL_Window *myWindow = NULL; SDL_Renderer *myRenderer = NULL; SDL_Texture *myTexture = NULL, *hisTexture = NULL; SDL_Surface* picture; int nRenderDrivers; if (SDL_Init(SDL_INIT_VIDEO) < 0) { exit(1); } atexit(SDL_Quit); //Load image picture = SDL_LoadBMP("qqtest.bmp"); myWindow = SDL_CreateWindow("testSdl", 240, 240, picture->w, picture->h, SDL_WINDOW_RESIZABLE|SDL_WINDOW_SHOWN); //获取当前可用画图驱动 window中有3个,第一个为d3d,第二个为opengl,第三个为software nRenderDrivers = SDL_GetNumRenderDrivers(); //创建渲染器,第二个参数为选用的画图驱动,0代表d3d myRenderer = SDL_CreateRenderer(myWindow, 0, SDL_RENDERER_ACCELERATED); SDL_RendererInfo info; SDL_GetRendererInfo(myRenderer, &info); SDL_GetRenderDriverInfo(0, &info); //d3d SDL_GetRenderDriverInfo(1, &info); //opgl SDL_GetRenderDriverInfo(2, &info); //software //创建纹理 myTexture = SDL_CreateTexture(myRenderer, /*SDL_PIXELFORMAT_RGB24*/picture->format->format, SDL_TEXTUREACCESS_STREAMING, picture->w, picture->h); //计算yuv一行数据占的字节数 //int iPitch = picture->w * picture->format->BitsPerPixel / 8;//SDL_BYTESPERPIXEL(SDL_PIXELFORMAT_RGB24); { #if 1 int i = SDL_UpdateTexture(myTexture, NULL, picture->pixels, picture->pitch); SDL_RenderClear(myRenderer); SDL_RenderCopy(myRenderer, myTexture, NULL, NULL); #else hisTexture = SDL_CreateTextureFromSurface(myRenderer, picture); SDL_FreeSurface(picture); //SDL_UpdateTexture(myTexture, &sdlRT, picture->pixels, iPitch); SDL_RenderClear(myRenderer); SDL_RenderCopy(myRenderer, myTexture, NULL, NULL); #endif SDL_RenderPresent(myRenderer); SDL_Delay(4000); } SDL_DestroyTexture(myTexture); SDL_DestroyRenderer(myRenderer); SDL_DestroyWindow(myWindow); return 0; }