当前位置:网站首页>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 .
边栏推荐
- Rust 入门指南(crate 管理)
- 2-channel Di high-speed pulse counter, 1-channel encoder to Modbus TCP wired wireless module ibf161
- Automatic conversion and cast
- LwIP development | socket | DNS domain name resolution
- 2021 肯特面试题3
- js 队列
- Notes on October 22, 2021
- Telecommuting can be easily realized in only three steps
- Implementation of skip table
- Roson的Qt之旅#102 ListModel
猜你喜欢

Mlx90640 infrared thermal imager temperature sensor module development notes (VIII)

flashfxp 530 User cannot log in. ftp

Basic structure and operation principle of solar street lamp

Wei Jianjun couldn't catch up with Li Shufu by riding a BMW

2021 Yahong pen test questions

KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书

The epidemic dividend disappeared, and the "home fitness" foam dissipated

Dynamic programming -- digital statistics DP

两种特殊函数(箭头函数和方法)

js 链表 02
随机推荐
Writing of factorial
Detectron2 installation and testing
Connection and application of portable borehole inclinometer data acquisition instrument and inclinometer probe
正大杯黑客马拉松数据解析竞赛
Remember the common JS methods of projects
NTC, PT100 thermal resistance to 4-20mA temperature signal converter
Summary of for loop in JS
Application of optical rain gauge to rainfall detection
激光测距仪非接触式地表裂缝监测仪
mysql查询 limit 1000,10 和limit 10 速度一样快吗?如果我要分页,我该怎么办?
Installation points and precautions of split angle probe
Automatic conversion and cast
flashfxp 530 User cannot log in. ftp
Automatically pack compressed backup download and delete bat script commands
食品安全 | 这两类瓜果宜改善便秘 孕妇人群尤其建议
Telecommuting can be easily realized in only three steps
仅需三步 轻松实现远程办公
【Multisim仿真】LM339过零电路仿真
R language ggplot2 visually draws line plots, and uses gghighlight package to highlight the lines that meet the combination judgment conditions in the line graphs (satisfies both condition a and b)
Where is the RDS MySQL read-only instance of Alibaba cloud created