sdl实现texture透明
来源:互联网 发布:mysql oxc000007b 编辑:程序博客网 时间:2024/05/29 04:53
sdl可以实现一些图片,文字透明,实现renderer透明。这里说的是texture透明,关于其他透明函数,请查看:点击打开链接
如果闪退,尝试更改字体路径。
效果图:
代码:
#include <SDL2/SDL.h>#include <SDL2/SDL_ttf.h>//显示的texture的数量const int TexNum =3;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; //构造函数:size为字体大小 Font(int size=70) { TTF_Init();font=TTF_OpenFont("./resource/DroidSansFallback.ttf", size); } //析构函数 ~Font() { TTF_CloseFont(font); font=0; TTF_Quit(); } //把文字加载成textureSDL_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[TexNum]; SDL_Rect box[TexNum]; Font font; //特效texture SDL_Texture*texture; SDL_Rect TexBox; Uint8 alpha=255; //构造函数 Tex() { char *str[TexNum]={"逐渐透明", "逐渐显现", "退出"}; for (int i=0; i<TexNum; i++) {tex[i]=font.loadTex(str[i]); int w,h;//获取纹理大小 SDL_QueryTexture(tex[i], NULL, NULL, &w, &h);//设置纹理的大小 box[i].w=w; box[i].h=h; }//设置纹理的坐标 box[0].x=50; box[0].y=800; box[1].x=400; box[1].y=800; box[2].x=270; box[2].y=950;//设置特效textureFont BigFont(150);texture=BigFont.loadTex("texture"); int w,h;//获取纹理大小 SDL_QueryTexture(texture, NULL, NULL, &w, &h);TexBox.w=w;TexBox.h=h; } //析构函数 ~Tex() { for (int i=0; i<TexNum; i++) { if (tex[i]!=NULL) SDL_DestroyTexture(tex[i]); }SDL_DestroyTexture(texture); } //显示tex void showTex(int n) {SDL_RenderCopy(renderer, tex[n], NULL, &box[n]); }//显示特效texturevoid showTexture(int x=100, int y=250){TexBox.x=x; TexBox.y=y;SDL_SetTextureBlendMode(texture,SDL_BLENDMODE_BLEND);//设置texture透明度模式SDL_SetTextureAlphaMod(texture,alpha);//设置texture透明度SDL_RenderCopy(renderer, texture, NULL, &TexBox);}};//按钮类class Button{ SDL_Event event; SDL_Rect box[TexNum]; //屏幕分辨率 int win_w,win_h;public :Tex tex;int m=-1; //按下和移动时按钮索引int n=-1; //抬起时按钮索引//构造函数Button(){//获取屏幕分辨率SDL_GetWindowSize(window, &win_w, &win_h);//获取tex文字大小和坐标for (int i=0; i<TexNum; 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~1SDL_Point point={event.tfinger.x*win_w, event.tfinger.y*win_h};for (int i=0; i<TexNum; i++)//判断触点是否在box矩形中中if (SDL_PointInRect(&point, &box[i]))m=i;}break;//手指抬起case SDL_FINGERUP:{//event.tfinger.x和event.tfinger.y值范围:0~1SDL_Point point={event.tfinger.x*win_w, event.tfinger.y*win_h};for (int i=0; i<TexNum; i++)//判断触点是否在box矩形中if (SDL_PointInRect(&point, &box[i]))n=i;m=-1;}break;}}}void show(){//绘画填充矩形for (int i=0; i<TexNum; 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<TexNum; i++)tex.showTex(i);}};//主函数int main(int argc, char**argv){ Window win("多个按钮"); Button button; while (win.load) { button.touch(); button.show(); switch (button.n) { case -1:{button.tex.showTexture(); }break; case 0:{if (button.tex.alpha>0) //alpha值在0~255,要限制一下button.tex.alpha--;button.tex.showTexture(); }break; case 1:{if (button.tex.alpha<255)button.tex.alpha++;button.tex.showTexture(); }break; case 2:{ win.load=false; }break;
阅读全文
0 0
- sdl实现texture透明
- sdl实现texture旋转
- sdl实现texture拖动
- SDL Game Development-第2章-5.实现纹理管理器Texture Manager
- sdl renderer window texture surface更新关系
- android OpenGL ES实现渲染到透明的纹理 render to transparent texture
- Tamura texture C++实现
- SDL实现生产者消费者
- sdl实现计时器
- SDL 实现马赛克
- 用GPU实现Cellular Texture
- Texture
- texture
- 嵌入式 Color Keying(抠色)SDL背景透明
- SDL 实现五子棋 GUI (一)
- SDL 实现五子棋 GUI (二)
- SDL 实现五子棋 GUI (三)
- SDL实现多个按钮
- 权限管理框架
- 自然语言处理技术
- Notepad++插件添加方法
- 软图像抽象的有效显着区域检测-GC
- Java反射机制
- sdl实现texture透明
- 原生JavaScript操作cookies
- WebRTC之AGC自动增益控制
- 二叉树打印
- C Primer Plus 结构体 struct 详解 常见用法
- js知识
- Java泛型详解,通俗易懂只需5分钟
- php编译安装与配置
- ASP.NET 网页中的跨页发送PreviousPage