1、SDL 介绍
- SDL(Simple DirectMedia Layer):一个简单的多媒体层
- 由 C 语言实现的跨平台的媒体开源库
- 多用于开发游戏、模拟器、媒体播放器等多媒体应用领域
- 版本:SDL1.2(老)、SDL2.0(新),以 SDL2.0 为基础
- 官网:https://www.libsdl.org/index.php
2、SDL 编译与安装
https://www.libsdl.org/release/SDL2-2.0.20.tar.gz
tar SDL2-2.0.20.tar.gz
cd SDL2-2.0.20 目录,并执行 ./configure --prefix=/usr/local
sudo make -j 8 && make install
3、SDL 基本使用步骤
<!-- 头文件 -->
#include
SDL_Init()
SDL_Quit()
<!-- SDL 渲染窗口 -->
SDL_CreateWindow()
SDL_DestroyWindow()
<!-- SDL 渲染器 -->
SDL_CreateRenderer()
SDL_DestroyRenderer()
SDL_SetRenderDrawColor()
SDL_RenderClear()
SDL_RenderPresent()
#include
int main(int argc, char *argv[]) {
SDL_Window *window = NULL;
SDL_Renderer *render = NULL;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("SDL2 Window", 200, 200, 640, 480, SDL_WINDOW_SHOWN);
if (!window) {
SDL_Log("Failed to create window!");
goto __EXIT;
}
render = SDL_CreateRenderer(window, -1, 0);
if (!render) {
SDL_Log("Failed to create render!");
goto __DWINODW;
}
SDL_SetRenderDrawColor(render, 255, 0, 0, 255);
SDL_RenderClear(render);
SDL_RenderPresent(render);
SDL_Delay(10000);
SDL_DestroyRenderer(render);
__DWINODW:
SDL_DestroyWindow(window);
__EXIT:
SDL_Quit();
return 0;
}
clang -g -o 13_ff_hello_sdl 13_ff_hello_sdl.c `pkg-config --cflags --libs sdl2`
./13_ff_hello_sdl
4、SDL 事件基本原理
- SDL 将所有事件都存放在一个队列中
- SDL 所有对事件的操作,其实就是对队列的操作
- SDL 事件种类 API:
SDL_WindowEvent
SDL_KeyboardEvent
SDL_MouseMotionEvent
SDL_PollEvent
SDL_WaitEvent
#include
int main(int argc, char *argv[]) {
SDL_Window *window = NULL;
SDL_Renderer *render = NULL;
int quit = 1;
SDL_Event event;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("SDL2 Window", 200, 200, 640, 480, SDL_WINDOW_SHOWN);
if (!window) {
SDL_Log("Failed to create window!");
goto __EXIT;
}
render = SDL_CreateRenderer(window, -1, 0);
if (!render) {
SDL_Log("Failed to create render!");
goto __DWINODW;
}
SDL_SetRenderDrawColor(render, 255, 0, 0, 255);
SDL_RenderClear(render);
SDL_RenderPresent(render);
do {
SDL_WaitEvent(&event);
switch (event.type) {
case SDL_QUIT:
quit = 0;
break;
default:
SDL_Log("event type is %d", event.type);
}
} while (quit);
SDL_DestroyRenderer(render);
__DWINODW:
SDL_DestroyWindow(window);
__EXIT:
SDL_Quit();
return 0;
}
clang -g -o 14_ff_event_sdl 14_ff_event_sdl.c `pkg-config --cflags --libs sdl2`
./14_ff_event_sdl
5、SDL 纹理渲染
- 基本原理流程图:
- API:
#include
int main(int argc, char *argv[]) {
SDL_Window *window = NULL;
SDL_Renderer *render = NULL;
SDL_Texture *texture = NULL;
int quit = 1;
SDL_Event event;
SDL_Rect rect;
rect.w = 30;
rect.y = 30;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("SDL2 Window", 200, 200, 640, 480, SDL_WINDOW_SHOWN);
if (!window) {
SDL_Log("Failed to create window!");
goto __EXIT;
}
render = SDL_CreateRenderer(window, -1, 0);
if (!render) {
SDL_Log("Failed to create render!");
goto __DWINODW;
}
SDL_SetRenderDrawColor(render, 255, 0, 0, 255);
SDL_RenderClear(render);
SDL_RenderPresent(render);
texture = SDL_CreateTexture(render, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 640, 480);
if (!texture) {
SDL_Log("Failed to create Texture!");
goto __RENDER;
}
do {
SDL_WaitEvent(&event);
switch (event.type) {
case SDL_QUIT:
quit = 0;
break;
default:
SDL_Log("event type is %d", event.type);
}
rect.x = rand() % 600;
rect.y = rand() % 450;
SDL_SetRenderTarget(render, texture);
SDL_SetRenderDrawColor(render, 0, 0, 0, 0);
SDL_RenderClear(render);
SDL_RenderDrawRect(render, &rect);
SDL_SetRenderDrawColor(render, 255, 0, 0, 0);
SDL_RenderFillRect(render, &rect);
SDL_SetRenderTarget(render, NULL);
SDL_RenderCopy(render, texture, NULL, NULL);
SDL_RenderPresent(render);
} while (quit);
SDL_DestroyTexture(texture);
__RENDER:
SDL_DestroyRenderer(render);
__DWINODW:
SDL_DestroyWindow(window);
__EXIT:
SDL_Quit();
return 0;
}
clang -g -o 15_ff_texture_sdl 15_ff_texture_sdl.c `pkg-config --cflags --libs sdl2`
./15_ff_texture_sdl
5、实现 YUV 视频播放器
<!-- SDL 创建线程 -->
SDL_CreateThread()
fn:线程执行函数
name:线程名
data:执行函数参数
<!-- 更新纹理 -->
SDL_UpdateTexture()
SDL_UpdateYUVTexture()