加载图片的流程仅仅只是在前面的创建窗口流程中加了一个加载图片
- 读取一张图片到Surface(bmp格式的图片,bmp里面保存的就是原始的像素数据)
- 将读取的Surface复制到屏幕的Surface
- 更新屏幕
读取图片以及赋值给屏幕的Surface
helloWorldSurface = SDL_LoadBMP("demo.bmp");//读取图片
SDL_BlitSurface(helloWorldSurface,NULL, screenSurface, NULL);//赋值给屏幕的Surface
源码
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//SDL loadImage
int main(int argc,char* argv[]) {
SDL_Window *window = NULL;
SDL_Surface *screenSurface = NULL;
SDL_Surface *helloWorldSurface = NULL;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("初始化SDL失败 %s",SDL_GetError());
return -1;
}
window = SDL_CreateWindow(
"Load Image",
SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,SCREEN_HEIGHT,SDL_WINDOW_SHOWN
);
if (window == NULL) {
printf("初始化窗口失败");
return -1;
}
screenSurface = SDL_GetWindowSurface(window);
helloWorldSurface = SDL_LoadBMP("demo.bmp");
if (helloWorldSurface == NULL) {
printf("加载图片数据失败 %s",SDL_GetError());
return -1;
}
//hello --> screen
SDL_BlitSurface(helloWorldSurface,NULL, screenSurface, NULL);
SDL_UpdateWindowSurface(window);
SDL_Delay(2 * 1000);
SDL_FreeSurface(helloWorldSurface);
helloWorldSurface = NULL;
SDL_DestroyWindow(window);
window = NULL;
SDL_Quit();
return 0;
}
Nothing is certain in this life. The only thing i know for sure is that. I love you and my life. That is the only thing i know. have a good day