当前位置:网站首页>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 .
边栏推荐
- 为什么学编程的人大多数都去了深圳和北京?
- Huada chip hc32f4a0 realizes RS485 communication DMA transceiver
- leetcode 题目
- 2-channel Di high-speed pulse counter, 1-channel encoder to Modbus TCP wired wireless module ibf161
- Two special functions (arrow function and method)
- Zhaoqi science and technology innovation and entrepreneurship competition talent introduction platform, mass entrepreneurship and entrepreneurship competition high-level talent introduction
- RF module wireless transceiver rf63u chip application data transmission and infrastructure network
- 疫情红利消失,「居家健身」泡沫消散
- Numpy ndarray learning < I > Foundation
- VM501开发套件开发版单振弦式传感器采集模块岩土工程监测
猜你喜欢

我在上海偶遇数字凤凰#坐标徐汇美罗城

一大早支付宝来短信说你中“奖”了?处理服务器挖矿病毒 - kthreaddi

LwIP development | realize TCP server through socket
![[wechat applet development (VII)] subscription message](/img/aa/f0c68bc0c068ac208820df42cce3db.png)
[wechat applet development (VII)] subscription message

解决电脑恶意广告弹窗的思路

flashfxp 530 User cannot log in. ftp

The deep displacement monitoring system wk813 is used to measure the deep displacement of slopes, dams, embankments, railways and building foundation pit excavation

High precision absolute angle sensor application high speed angle monitoring

NTC, PT100 thermal resistance to 4-20mA temperature signal converter

Food safety | these two kinds of melons and fruits should be improved, especially for pregnant women with constipation
随机推荐
R语言使用fs包的file_delete函数删除指定文件夹下的指定文件、举一反三、dir_delete函数、link_delete函数可以用来删除文件夹和超链接
Redis系列4:高可用之Sentinel(哨兵模式)
mysql查询 limit 1000,10 和limit 10 速度一样快吗?如果我要分页,我该怎么办?
Remember the common JS methods of projects
2021 Yahong pen test question 2
A tour of grp:05 - GRP server streaming service end stream
Remote serial port server (adapter) UART to 1-wire application
LwIP development | socket | TCP | client
I'll show you a little chat! Summary of single merchant function modules
KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
CoDeSys realizes bubble sorting
2021 肯特面试题3
Stm32cube infrared remote control: input capture
I came across Digital Phoenix coordinate Xuhui Meiluo city in Shanghai
PHP mb_ Substr Chinese garbled code
Mlx90640 infrared thermal imager temperature sensor module development notes (VIII)
射频模块无线收发RF63U芯片应用数据传输和基建网络
el-input限制只能输入规定的数
使用py,根据日志记录自动生成周报
JS linked list 01