SDL 显示图片
Posted lvdongjie
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SDL 显示图片相关的知识,希望对你有一定的参考价值。
来源:
http://adolfans.github.io/sdltutorialcn/ (中文教程)
http://www.willusher.io/pages/sdl2/ (英文教程)
环境:SDL2 + VC++2015
下面的代码将打开一个窗口显示hello.bmp
1 #include <iostream>
2 #include <SDL.h>
3
4 int main(int argc, char** argv)
5 {
6 //初始化SDL
7 if (SDL_Init(SDL_INIT_VIDEO) != 0)
8 {
9 std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
10 return 1;
11 }
12
13 //创建窗口
14 SDL_Window *win = nullptr;
15 win = SDL_CreateWindow("Hello World!",
16 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
17 640, 480, SDL_WINDOW_SHOWN);
18 if (win == nullptr)
19 {
20 std::cout << SDL_GetError() << std::endl;
21 return 1;
22 }
23
24 //创建渲染器
25 SDL_Renderer *ren = nullptr;
26 ren = SDL_CreateRenderer(win, -1,
27 SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
28 if (ren == nullptr)
29 {
30 std::cout << SDL_GetError() << std::endl;
31 return 1;
32 }
33
34 //创建表面
35 SDL_Surface *bmp = nullptr;
36 bmp = SDL_LoadBMP("hello.bmp");
37 if (bmp == nullptr) {
38 std::cout << SDL_GetError() << std::endl;
39 return 1;
40 }
41
42 //创建材质
43 SDL_Texture *tex = nullptr;
44 tex = SDL_CreateTextureFromSurface(ren, bmp);
45 SDL_FreeSurface(bmp);
46
47 //清空渲染器
48 SDL_RenderClear(ren);
49 //将材质复制到渲染器
50 SDL_RenderCopy(ren, tex, NULL, NULL);
51 //呈现渲染器
52 SDL_RenderPresent(ren);
53 //窗口延时
54 SDL_Delay(2000);
55
56 //释放资源
57 SDL_DestroyTexture(tex);
58 SDL_DestroyRenderer(ren);
59 SDL_DestroyWindow(win);
60 //退出SDL
61 SDL_Quit();
62
63 return 0;
64 }
https://i-beta.cnblogs.com/posts/edit
以上是关于SDL 显示图片的主要内容,如果未能解决你的问题,请参考以下文章