当前位置:网站首页>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 找到。
边栏推荐
- Food safety | these two kinds of melons and fruits should be improved, especially for pregnant women with constipation
- IFD-x 微型红外成像仪(模块)的温度测量和成像精度
- Summary of for loop in JS
- 12V pulse speed measurement to 24V level signal conversion transmitter
- 光学雨量计应用降雨量检测
- 2021 亚鸿笔试题2
- Data real-time feedback technology
- What are the process specifications of software testing? How to do it?
- 5路DI/DO继电器输出远程IO采集模块Modbus TCP/IBF95
- 【微信小程序开发(七)】订阅消息
猜你喜欢

Duty cycle switch output high speed pulse counter rtumodbus module ibf63

Redis series 4: sentinel (sentinel mode) with high availability

LabVIEW Linx toolkit controls Arduino equipment (expansion-1)

为什么学编程的人大多数都去了深圳和北京?

Rust Getting Started Guide (crite Management)

NTC,PT100热电阻转4-20mA温度信号转换器

2021 Yahong pen test question 2

High precision absolute angle sensor application high speed angle monitoring

2021 Yahong pen test questions

JS array (summary)
随机推荐
Thermistor PT100, NTC to 0-10v/4-20ma converter
One channel encoder, two channels Di speed measurement, RS485 serial port connected to one channel do alarm module ibf151
[channel attention mechanism] senet
JS linked list 01
Remote serial port server (adapter) UART to 1-wire application
比例电磁阀控制阀4-20mA转0-165mA/330mA信号隔离放大器
Mlx90640 infrared thermal imager temperature sensor module development notes (VIII)
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)
5-channel di/do relay output remote IO acquisition module Modbus tcp/ibf95
Software architecture and design (x) -- Architecture Technology
Learn about the native application management platform of rainbow cloud
2021 Kent interview question 2
【微信小程序开发(七)】订阅消息
记:数值向上取整十,整百,整千,整万
A tour of gRPC:05 - gRPC server straming 服务端流
Temperature measurement and imaging accuracy of ifd-x micro infrared imager (module)
High speed counter to rs485modbus RTU module ibf150
激光测距仪非接触式地表裂缝监测仪
软考 系统架构设计师 简明教程 | 软件调试
如何快速接入统一的认证鉴权体系