美文网首页
SDL 加载一张图片

SDL 加载一张图片

作者: 大批 | 来源:发表于2017-07-29 13:36 被阅读34次

原文地址


加载图片的流程仅仅只是在前面的创建窗口流程中加了一个加载图片

  • 读取一张图片到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

相关文章

网友评论

      本文标题:SDL 加载一张图片

      本文链接:https://www.haomeiwen.com/subject/iozqlxtx.html