当前位置:网站首页>Sdl2 concise tutorial (4): using SDL_ Image library importing pictures
Sdl2 concise tutorial (4): using SDL_ Image library importing pictures
2022-07-28 16:25:00 【Helplessness of mustard】
List of articles
SDL2 A concise tutorial ( One ): Use Cmake and Conan structure SDL2 Programming environment
SDL2 A concise tutorial ( Two ): Create an empty window
SDL2 A concise tutorial ( 3、 ... and ): display picture
List of articles
Integrate SDL_IMAGE
stay SDL2 A concise tutorial ( 3、 ... and ): display picture
in , We use SDL2 Self contained function SDL_LoadBMP() Import bmp picture . But this function can only import bmp Format ,SDL2 No other functions are provided to import pictures in other formats . Fortunately, , There's a man named sdl_image The library of can provide such functions . This article will show you how to use sdl_image Import pictures in other formats .
Integrate in our project sdl_image It's simple ( thank conan Convenience brought by ), You just need to conanfile.txt Add a pair of sdl_image You can rely on :
[requires]
sdl/2.0.20
sdl_image/2.0.5
[generators]
cmake
You can sdl_image - conan center Find out more about sdl_image Package information . next , Please look like SDL2 A concise tutorial ( One ): Use Cmake and Conan structure SDL2 Programming environment Just build our code as in .
Use SDL_IMAGE Import image
sdl_image Very simple to use , Import header file first
#include <SDL_image.h>
next , call IMG_Init() initialization sdl_image
IMG_Init(IMG_INIT_JPG);
at present sdl_image Four formats are supported , Namely :
- IMG_INIT_JPG
- IMG_INIT_PNG
- IMG_INIT_TIF
- IMG_INIT_WEBP
You can initialize multiple formats at the same time , for example
IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG);
Remember , Call before exiting IMG_Quit() To release resources
IMG_Quit();
Now? , Use IMG_Load() Replace SDL_LoadBMP() To import pictures in other formats
SDL_Surface * image = IMG_Load("PICT3159.JPG");
Finally, paste all the codes :
#if defined(__cplusplus)
extern "C" {
#endif
#include <SDL.h>
#include <SDL_image.h>
#if defined(__cplusplus)
};
#endif
#include <iostream>
using namespace std;
int main() {
bool quit = false;
SDL_Event event;
SDL_Init(SDL_INIT_VIDEO);
IMG_Init(IMG_INIT_JPG);
SDL_Window *window = SDL_CreateWindow("My SDL Empty window",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640, 480, 0);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Surface *image = IMG_Load("sdl_image.jpeg");
if (image == nullptr) {
cerr << "SDL_LoadBMP failed\n";
return -1;
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, image);
for (; !quit;) {
SDL_WaitEvent(&event);
switch (event.type) {
case SDL_QUIT: {
quit = true;
break;
}
}
SDL_RenderCopy(renderer, texture, nullptr, nullptr);
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(texture);
SDL_FreeSurface(image);
SDL_DestroyRenderer(renderer);
IMG_Quit();
SDL_Quit();
return 0;
}
summary
excellent !sdl_image It can support pictures in various formats , Need to use IMG_Load Instead of SDL_LoadBMP() that will do . You need to initialize and clean up sdl_image, These are very simple .
All the code in this article can be found in sdl2_tutorial find .
边栏推荐
- 2021 肯特面试题3
- 2021 亚鸿笔试题2
- Notes on October 22, 2021
- Telecommuting can be easily realized in only three steps
- 高精度绝对角度传感器应用高速度角度监测
- Ffmpeg get the first frame
- LwIP development | socket | TCP | client
- Vm501 development kit development version single vibrating wire sensor acquisition module geotechnical engineering monitoring
- MicTR01 Tester 开发套件(振弦采集读数仪)使用说明
- 12V pulse speed measurement to 24V level signal conversion transmitter
猜你喜欢

How to measure the vibrating wire sensor by vibrating wire acquisition module?

js 队列

Telecommuting can be easily realized in only three steps

QT打包

js 优先级队列

Practical development tutorial of software problem repair tracking system (Part 1)

js 链表 02

动态规划 --- 数位统计DP

JS stack

Temperature measurement and imaging accuracy of ifd-x micro infrared imager (module)
随机推荐
Fifth uncle's thinking
js 链表 02
Note: the value is rounded up to ten, hundred, thousand, ten thousand
Where is the RDS MySQL read-only instance of Alibaba cloud created
Remember the common JS methods of projects
R语言ggplot2可视化绘制线图(line plot)、使用gghighlight包突出高亮线图中满足组合判断条件的线图(satisfies both condition A and B)
2021 Yahong pen test questions
2021 肯特面试题2
Rust Getting Started Guide (crite Management)
解决uniapp等富文本图片宽度溢出
el-input限制只能输入规定的数
A good start
One channel encoder, two channels Di speed measurement, RS485 serial port connected to one channel do alarm module ibf151
远距离串口服务器( 适配器)UART 转 1-Wire 应用
LwIP development | socket | TCP | client
资本「断供」两年,我只能把公司卖了
R language uses ggpairs function of ggally package to visualize pairwise relationship graph of grouping multivariable, set alpha parameter to change image transparency, diagonal continuous variable de
Writing of factorial
2021 Kent interview question 3
射频模块无线收发RF63U芯片应用数据传输和基建网络