SDL_image扩展库使用(显示jpg图片)
基本流程
- 1.初始化SDL 、IMG SDL_Init IMG_Init
- 2.创建窗口 SDL_CreateWindow
- 3.基于窗口创建渲染器 SDL_CreateRenderer
- 4.加载图片到surface IMG_Load
- 5.根据图片surface创建纹理 SDL_CreateTextureFromSurface
6.绘制三部曲
清空渲染器 SDL\_RenderClear 拷贝纹理数据到渲染器 SDL\_RenderCopy 刷新渲染器显示 SDL\_RenderPresent
7.退出SDL、IMG SDL_Quit IMG_Quit
#include <iostream>
/* SDL_image 扩展库使用 */
extern "C"
{
#include <SDL.h>
#include <SDL_image.h>
}
#pragma comment(lib, "SDL2.lib")
#pragma comment(lib, "x86/SDL2_image.lib")
#undef main
int main()
{
// 1. 初始化
int nRet = SDL_Init(SDL_INIT_EVERYTHING);
if (nRet < 0)
{
std::cout << "SDL Error: " << SDL_GetError() << std::endl;
return -1;
}
nRet = IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG);
if (nRet < 0)
{
std::cout << "SDL Error: " << SDL_GetError() << std::endl;
return -1;
}
// 2. 创建窗口
SDL_Window* pWnd = SDL_CreateWindow("Title",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
640, 480,
SDL_WINDOW_SHOWN);
if (NULL == pWnd)
{
std::cout << "SDL Error: " << SDL_GetError() << std::endl;
return -1;
}
// 3. 创建渲染器
SDL_Renderer* pRender = SDL_CreateRenderer(pWnd, -1, SDL_RENDERER_ACCELERATED);
if (NULL == pRender)
{
SDL_DestroyWindow(pWnd);
std::cout << "SDL Error: " << SDL_GetError() << std::endl;
return -1;
}
// 4. 加载图片
SDL_Surface* pSurface = IMG_Load("./longong.jpg");
if (NULL == pSurface)
{
SDL_DestroyRenderer(pRender);
SDL_DestroyWindow(pWnd);
std::cout << "SDL Error: " << SDL_GetError() << std::endl;
return -1;
}
// 5. 根据表面创建纹理
SDL_Texture* pTexture = SDL_CreateTextureFromSurface(pRender, pSurface);
if (NULL == pTexture)
{
SDL_FreeSurface(pSurface);
SDL_DestroyRenderer(pRender);
SDL_DestroyWindow(pWnd);
std::cout << "SDL Error: " << SDL_GetError() << std::endl;
return -1;
}
// 6. 绘制三部曲
SDL_RenderClear(pRender); // 清空一下(防止有残留的数据)
SDL_RenderCopy(pRender, pTexture, NULL, NULL); // 拷贝纹理数据到渲染器
SDL_RenderPresent(pRender); // 刷新渲染器显示
SDL_Delay(2000);
// 7. 释放资源
if (pSurface)
{
SDL_FreeSurface(pSurface);
}
if (pTexture)
{
SDL_DestroyTexture(pTexture);
}
if (pRender)
{
SDL_DestroyRenderer(pRender);
}
if (pWnd)
{
SDL_DestroyWindow(pWnd);
}
IMG_Quit();
SDL_Quit();
return 0;
}
注意坑
- 放置dll文件时还需要其它的dll文件 zlib1.dll(解压算法库) SDL2_ttf.dll SDL2_image.dll libpng16-16.dll zlib1.dll等
- 没有zlib1.dll会导致图像显示不出来
SDL_mixer扩展库使用(播放wav音频文件)
基本流程
- 1.初始化 SDL_Init
- 2.打开音频 Mix_OpenAudio
- 3.加载音频文件 Mix_LoadWAV
- 4.创建窗口 SDL_CreateWindow
- 5.根据窗口获取surface SDL_GetWindowSurface
- 6.播放音频文件 Mix_PlayChannel
- 7.循环将数据喂给声卡播放 Mix_Playing
- 8.释放资源退出 Mix_FreeChunk Mix_CloseAudio SDL_DestroyWindow SDL_Quit
#include <iostream>
/* 使用 SDL_mixer 播放wav格式音频(内部音频为pcm)文件 */
extern "C"
{
#include <SDL.h>
#include <SDL_mixer.h>
}
#pragma comment(lib, "SDL2.lib")
#pragma comment(lib, "x86/SDL2_mixer.lib")
#undef main
int main()
{
int channel = -1;
// 1. 初始化
int nRet = SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO);
if (nRet < 0)
{
std::cout << "SDL Error: " << SDL_GetError() << std::endl;
return -1;
}
int audio_rate = 22050; // 音视频播放频率
int audio_format = AUDIO_S16SYS; // 播放格式
int audio_channels = 2; // 通道数
int audio_chunkSize = 4096; // 每次读取数据的大小
// 2. 打开音频
nRet = Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_chunkSize);
// 3. 加载音频文件
Mix_Chunk* sound = Mix_LoadWAV("./sound.wav"); // 存储音频文件在内存中
if (NULL == sound)
{
std::cout << "SDL Error: " << SDL_GetError() << std::endl;
return -1;
}
SDL_Window* pWnd = SDL_CreateWindow("Title",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
640, 480,
SDL_WINDOW_SHOWN);
if (NULL == pWnd)
{
std::cout << "SDL Error: " << SDL_GetError() << std::endl;
return -1;
}
SDL_Surface* pSurface = SDL_GetWindowSurface(pWnd);
if (NULL == pSurface)
{
std::cout << "SDL Error: " << SDL_GetError() << std::endl;
return -1;
}
// 4. 播放音频文件
channel = Mix_PlayChannel(-1, sound, 0); // 返回声道数
if (-1 == channel)
{
std::cout << "SDL Error: " << SDL_GetError() << std::endl;
return -1;
}
while (Mix_Playing(channel) != 0); // 将音频数据喂给声卡播放
// 5. 释放资源
Mix_FreeChunk(sound);
Mix_CloseAudio();
if (pWnd)
{
SDL_DestroyWindow(pWnd);
}
SDL_Quit();
return 0;
}