SDL入门 - 常用功能简明笔记
常用第三方库:
SDL2_ttf 用来处理文本显示SDL2_mixer 用来处理扩展格式的音乐(譬如MP3等)SDL2_image 用来加载带有透明通道的图片等
初始化
#include <SDL.h>
SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_AUDIO
);
创建窗口
#include <SDL.h>
#define WINDOW_WIDTH 窗口宽度
#define WINDOW_HEIGHT 窗口高度
SDL_Window
* window
= SDL_CreateWindow(
"窗口标题",
SDL_WINDOWPOS_UNDEFINED
, SDL_WINDOWPOS_UNDEFINED
,
WINDOW_WIDTH
, WINDOW_HEIGHT
,
SDL_WINDOW_SHOWN
);
释放资源并退出
#include <SDL.h>
#define WINDOW_WIDTH 窗口宽度
#define WINDOW_HEIGHT 窗口高度
SDL_Window
* window
= SDL_CreateWindow("窗口标题", SDL_WINDOWPOS_UNDEFINED
, SDL_WINDOWPOS_UNDEFINED
, WINDOW_WIDTH
, WINDOW_HEIGHT
, SDL_WINDOW_SHOWN
);
SDL_Renderer
* rend
= SDL_CreateRenderer(window
, -1, SDL_RENDERER_ACCELERATED
);
SDL_Surface
* surfaceImage
= IMG_Load("图片文件路径");
SDL_Texture
* image
= SDL_CreateTextureFromSurface(rend
, surfaceImage
);
SDL_DestroyTexture(image
);
image
= NULL;
SDL_FreeSurface(surfaceImage
);
surfaceImage
= NULL;
SDL_DestroyRenderer(rend
);
rend
= NULL;
SDL_DestroyWindow(window
);
window
= NULL;
SDL_Quit();
延时
#include <SDL.h>
SDL_Delay(时长
);
调整图片透明效果
#include <SDL.h>
#include <SDL_image.h>
SDL_Surface
* surfaceImage
= IMG_Load("图片文件路径");
SDL_Texture
* image
= SDL_CreateTextureFromSurface(rend
, surfaceImage
);
SDL_SetTextureBlendMode(image
, SDL_BLENDMODE_BLEND
);
SDL_SetTextureAlphaMod(image
, α通道值
);
常用事件
#include <SDL.h>
SDL_Event event
;
while (true)
{
while (SDL_PollEvent(&event
))
{
if (event
.type
== SDL_QUIT
)
{
}
if (event
.type
== SDL_MOUSEMOTION
)
{
int x
= event
.motion
.x
;
int y
= event
.motion
.y
;
}
if (event
.type
== SDL_MOUSEBUTTONDOWN
)
{
if (event
.button
== SDL_BUTTON_RIGHT
)
{
}
if (event
.button
== SDL_BUTTON_LEFT
)
{
}
}
if (event
.type
== SDL_KEYDOWN
)
{
int keyCode
= event
.key
.keysym
.sym
;
if (event
.key
.keysym
.sym
== SDLK_F10
)
{
}
}
}
}
清空绘图窗口
#include <SDL.h>
#define WINDOW_WIDTH 窗口宽度
#define WINDOW_HEIGHT 窗口高度
SDL_Window
* window
= SDL_CreateWindow("窗口标题", SDL_WINDOWPOS_UNDEFINED
, SDL_WINDOWPOS_UNDEFINED
, WINDOW_WIDTH
, WINDOW_HEIGHT
, SDL_WINDOW_SHOWN
);
SDL_Renderer
* rend
= SDL_CreateRenderer(window
, -1, SDL_RENDERER_ACCELERATED
);
SDL_RendClear(rend
);
显示图片
#include <SDL.h>
#include <SDL_image.h>
#define WINDOW_WIDTH 窗口宽度
#define WINDOW_HEIGHT 窗口高度
SDL_Window
* window
= SDL_CreateWindow("窗口标题", SDL_WINDOWPOS_UNDEFINED
, SDL_WINDOWPOS_UNDEFINED
, WINDOW_WIDTH
, WINDOW_HEIGHT
, SDL_WINDOW_SHOWN
);
SDL_Renderer
* rend
= SDL_CreateRenderer(window
, -1, SDL_RENDERER_ACCELERATED
);
SDL_Surface
* surfaceImage
= IMG_Load("图片文件路径");
SDL_Texture
* textureImage
= SDL_CreateTextureFromSurface(rend
, surfaceImage
);
SDL_Rect rect
;
rect
.x
= 坐标
;
rect
.y
= 坐标
;
rect
.w
= surfaceImage
-> w
;
rect
.h
= surfaceImage
-> h
;
SDL_RenderClear(rend
);
SDL_RenderCopy(rend
, textureImage
, NULL, &rect
);
SDL_RenderPresent(rend
);
简单的几何绘制
#include <SDL.h>
#define WINDOW_WIDTH 窗口宽度
#define WINDOW_HEIGHT 窗口高度
SDL_Window
* window
= SDL_CreateWindow("窗口标题", SDL_WINDOWPOS_UNDEFINED
, SDL_WINDOWPOS_UNDEFINED
, WINDOW_WIDTH
, WINDOW_HEIGHT
, SDL_WINDOW_SHOWN
);
SDL_Renderer
* rend
= SDL_CreateRenderer(window
, -1, SDL_RENDERER_ACCELERATED
);
SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_BLEND
);
SDL_SetRenderDrawColor(rend
, R
, G
, B
, A
);
SDL_RenderDrawPoint(rend
, x坐标
, y坐标
);
SDL_RenderDrawLine(rend
, 起点x坐标
, 起点y坐标
, 终点x坐标
, 终点y坐标
);
SDL_Rect rect
;
rect
.x
= 坐标
;
rect
.y
= 坐标
;
rect
.w
= 宽度
;
rect
.h
= 高度
;
SDL_RenderDrawRect(rend
, &rect
);
SDL_RenderFillRect(rend
, &rect
);
播放音频
#include <SDL.h>
#include <SDL_mixer.h>
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT
, 2, 2048);
Mix_Music
* sound
= Mix_LoadMUS("音频文件路径");
Mix_PlayMusic(sound
, 1);
显示文本
#include <SDL.h>
#include <SDL_ttf.h>
TTF_Init();
TTF_Font
* font
= TTF_OpenFont("字体文件路径", 16);
SDL_Color red
= { R
, G
, B
};
SDL_Surface
* ttf_surface
= TTF_RenderText_Solid(font
, "文字内容", red
);
SDL_FreeSurface(ttf_surface
);
ttf_surface
= NULL;
TTF_CloseFont(font
);
font
= NULL;
TTF_Quit();
参考资料:
B站大佬“正月点灯笼”SDL系列教程:https://space.bilibili.com/24014925/video?keyword=SDL博客园-SDL显示文字及中文 :https://www.cnblogs.com/lvdongjie/p/12455462.html