SDL实现多个按钮
程序员文章站
2022-07-14 11:32:20
...
如出现无法运行,尝试更改字体路径。
废话不多说,先上效果图:
代码:
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
SDL_Window*window=NULL;
SDL_Renderer*renderer=NULL;
//窗口类
class Window
{
public:
//记录是否加载成功
bool load;
//构造函数
Window(char title[20], Uint32 w=720, Uint32 h=1280, int flag=0)
{
load=false;
if (!SDL_Init(SDL_INIT_VIDEO))
if (window=SDL_CreateWindow(title, 0, 0, w, h, flag))
if (renderer=SDL_CreateRenderer(window, -1, 0))
{
//加载窗口成功
load=true;
}
}
//析构函数
~Window()
{
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
//填充窗口:白色
void fullWindow()
{
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, NULL);
}
//刷新窗口
void reflush()
{
SDL_RenderPresent(renderer);
SDL_RenderClear(renderer);
}
};
//文字类
class Font
{
public:
TTF_Font*font;
//构造函数
Font(int size=100)
{
TTF_Init();
font=TTF_OpenFont("/system/fonts/DroidSansFallback.ttf", size);
}
//析构函数
~Font()
{
TTF_CloseFont(font);
font=0;
TTF_Quit();
}
//把文字加载成texture
SDL_Texture*loadTex(char*str, int type=4)
{
switch (type)
{
case 0:TTF_SetFontStyle(font,TTF_STYLE_BOLD);break; //粗体
case 1:TTF_SetFontStyle(font,TTF_STYLE_ITALIC);break; //斜体
case 2:TTF_SetFontStyle(font,TTF_STYLE_UNDERLINE);break; //下划线
case 3:TTF_SetFontStyle(font,TTF_STYLE_STRIKETHROUGH);break; //删除线
case 4:TTF_SetFontStyle(font,TTF_STYLE_NORMAL);break; //正常
}
//文字颜色:黑色
SDL_Color color={0, 0, 0, 255};
SDL_Surface*surface=TTF_RenderUTF8_Blended(font, str, color);
SDL_Texture*texture=SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
return texture;
}
};
//纹理类
class Tex
{
public:
SDL_Texture*tex[6];
SDL_Rect box[6];
Font font;
//构造函数
Tex()
{
char*str[6]={"按钮0", "按钮1", "按钮2", "按钮3", "按钮4", "退出"};
for (int i=0; i<6; i++)
{
tex[i]=font.loadTex(str[i], 4);
int w,h;
//获取纹理大小
SDL_QueryTexture(tex[i], NULL, NULL, &w, &h);
//设置纹理的显示坐标
box[i].x=250;
box[i].y=i*150+100;
box[i].w=w;
box[i].h=h;
}
}
//析构函数
~Tex()
{
for (int i=0; i<6; i++)
{
if (tex[i]!=NULL)
SDL_DestroyTexture(tex[i]);
}
}
//显示tex
void showTex(int n)
{
SDL_RenderCopy(renderer, tex[n], NULL, &box[n]);
}
};
//按钮类
class Button
{
SDL_Event event;
SDL_Rect box[6];
Tex tex;
//屏幕分辨率
int win_w,win_h;
public :
int m=-1; //按下和移动时按钮索引
int n=-1; //抬起时按钮索引
//构造函数
Button()
{
//获取屏幕分辨率
SDL_GetWindowSize(window, &win_w, &win_h);
//获取tex文字大小和坐标
for (int i=0; i<6; i++)
{
box[i].x=tex.box[i].x;
box[i].y=tex.box[i].y;
box[i].w=tex.box[i].w;
box[i].h=tex.box[i].h;
}
}
void touch()
{
while (SDL_PollEvent(&event))
{
switch (event.type)
{
//手指按下
case SDL_FINGERDOWN:
//手指滑动
case SDL_FINGERMOTION:{
//event.tfinger.x和event.tfinger.y值范围:0~1
SDL_Point point={event.tfinger.x*win_w, event.tfinger.y*win_h};
for (int i=0; i<6; i++)
//判断触点是否在box矩形中中
if (SDL_PointInRect(&point, &box[i]))
m=i;
}break;
//手指抬起
case SDL_FINGERUP:{
//event.tfinger.x和event.tfinger.y值范围:0~1
SDL_Point point={event.tfinger.x*win_w, event.tfinger.y*win_h};
for (int i=0; i<6; i++)
//判断触点是否在box矩形中
if (SDL_PointInRect(&point, &box[i]))
n=i;
m=-1;
}break;
}
}
}
void show()
{
//绘画填充矩形
for (int i=0; i<6; i++)
{
SDL_SetRenderDrawColor(renderer, 0, 255, 255, 0);
SDL_RenderFillRect(renderer, &box[i]);
}
if (m!=-1)
{
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 0);
SDL_RenderFillRect(renderer, &box[m]);
}
//显示文字
for (int i=0; i<6; i++)
tex.showTex(i);
}
};
//主函数
int main(int argc, char**argv)
{
Window win("多个按钮");
Button button;
while (win.load)
{
button.touch();
button.show();
if (button.n==5)
win.load=false;
win.reflush();
win.fullWindow();
}
}
上一篇: sdl实现texture旋转
下一篇: SDL游戏开发之六-简单的SDL程序