当前位置:网站首页>SDL2 简明教程(四):用 SDL_IMAGE 库导入图片
SDL2 简明教程(四):用 SDL_IMAGE 库导入图片
2022-07-28 15:10:00 【芥末的无奈】
系列文章目录
SDL2 简明教程(一):使用 Cmake 和 Conan 构建 SDL2 编程环境
集成 SDL_IMAGE
在 SDL2 简明教程(三):显示图片
中,我们使用 SDL2 自带的函数 SDL_LoadBMP() 导入 bmp 图片。但这个函数只能导入 bmp 格式,SDL2 也没有提供其他函数来导入其他格式的图片。幸运的是,有个叫 sdl_image 的库可以提供此类功能。这篇文章将向你展示如何使用 sdl_image 导入其他格式的图片。
在我们的工程中集成 sdl_image 非常简单(感谢 conan 带来的便利),你只需在 conanfile.txt 中添加对 sdl_image 的依赖即可:
[requires]
sdl/2.0.20
sdl_image/2.0.5
[generators]
cmake
你可以在 sdl_image - conan center 找到更多关于 sdl_image 包的信息。接着,请像 SDL2 简明教程(一):使用 Cmake 和 Conan 构建 SDL2 编程环境 中一样构建我们的代码即可。
使用 SDL_IMAGE 导入图片
sdl_image 使用非常简单,首先引入头文件
#include <SDL_image.h>
接着,调用 IMG_Init() 初始化 sdl_image
IMG_Init(IMG_INIT_JPG);
目前 sdl_image 支持四种格式,分别是:
- IMG_INIT_JPG
- IMG_INIT_PNG
- IMG_INIT_TIF
- IMG_INIT_WEBP
你可以同时初始化多种格式,例如
IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG);
记得,在退出前调用 IMG_Quit() 来释放资源
IMG_Quit();
现在,使用 IMG_Load() 替换 SDL_LoadBMP() 来导入其他格式图片
SDL_Surface * image = IMG_Load("PICT3159.JPG");
最后贴上所有代码:
#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;
}
总结
非常棒!sdl_image 可以支持多种格式图片,需要用 IMG_Load 代替 SDL_LoadBMP() 即可。你需要初始化和清理 sdl_image,这些都非常简单。
本文所有代码可以在 sdl2_tutorial 找到。
边栏推荐
- R语言使用fs包的file_delete函数删除指定文件夹下的指定文件、举一反三、dir_delete函数、link_delete函数可以用来删除文件夹和超链接
- 电压频率的变换原理
- Multifunctional mixed signal AI acquisition / switching value di/do acquisition to rs485/232/modbus module
- Redis系列4:高可用之Sentinel(哨兵模式)
- Proportional solenoid valve control valve 4-20mA to 0-165ma/330ma signal isolation amplifier
- STM32F103C8T6 + 0.96“ I2C OLED显示3D_Cube
- Temperature measurement and imaging accuracy of ifd-x micro infrared imager (module)
- 两种特殊函数(箭头函数和方法)
- A failed cracking experience
- 激光测距仪非接触式地表裂缝监测仪
猜你喜欢

Principle and application of low cost / small volume module rs485/232 to analog signal ibf33

JS priority queue

2021 Kent interview question 1

高精度绝对角度传感器应用高速度角度监测

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

2021 肯特面试题1

Learn about the native application management platform of rainbow cloud

2021 肯特面试题2

Instructions for mictr01 Tester development kit (vibrating wire acquisition reader)

JS stack
随机推荐
I came across Digital Phoenix coordinate Xuhui Meiluo city in Shanghai
不懂就问,快速成为容器服务进阶玩家!
MicTR01 Tester 开发套件(振弦采集读数仪)使用说明
MySQL add and delete indexes
以太网转RS485串口计数器WiFI模块 LED灯光控制器IBF165
js 双向链表 01
Set static IP in NAT mode of virtual machine
Pytorch - optimize model parameters
Note: numerical accumulation animation
[live broadcast reservation] a new challenge under the evolution of data architecture - Shanghai railway station
Pyqt5 rapid development and practice 5.1 tables and trees
Have you seen the management area decoupling architecture? Can help customers solve big problems
跳表的实现
2-channel Di high-speed pulse counter, 1-channel encoder to Modbus TCP wired wireless module ibf161
js 优先级队列
光学雨量计对比翻斗式雨量计的优势
Ethernet to RS485 serial port counter WiFi module LED light controller ibf165
开光量输入/继电器输出rs485/232远程数据采集IO模块IBF70
比例电磁阀控制阀4-20mA转0-165mA/330mA信号隔离放大器
太阳能路灯的根本结构及作业原理