SDL显示文本

sdl显示文本    sdl2.0库本身没有文本数据显示相关函数接口,文本显示需要编译安装sdl_ttf库。
1.编译安装sdl2_ttf库 sdl_ttf下载地址:sdl_ttf
  (1)将下载的sdl2-2.0.14.tar.gz压缩包拷贝至虚拟机解压。
[wbyq@wbyq src_pack]$ tar xvf /mnt/hgfs/ubuntu/software_pack/sdl2_ttf-2.0.15.tar.gz      (2)配置编译安装路径。
[wbyq@wbyq sdl2_ttf-2.0.15]$ ./autogen.sh [wbyq@wbyq sdl2_ttf-2.0.15]$ ./configure --prefix=$pwd/_install[wbyq@wbyq sdl2_ttf-2.0.15]$ make && make install    (3)编译成功生成文文件。
[wbyq@wbyq _install]$ tree.├── include│ └── sdl2│ ├── freetype.h│ └── sdl_ttf.h└── lib ├── libsdl2_ttf-2.0.so.0 -> libsdl2_ttf-2.0.so.0.14.1 ├── libsdl2_ttf-2.0.so.0.14.1 ├── libsdl2_ttf.a ├── libsdl2_ttf.la ├── libsdl2_ttf.so -> libsdl2_ttf-2.0.so.0.14.1 └── pkgconfig └── sdl2_ttf.pc4 directories, 8 files 2.增加设置字体大小函数   sdl2_ttf库中提供函数没有单独提供设置字体大小函数,为了方便设置字体大小,修改sdl_ttf.c文件,增加设置字体大小函数。
[wbyq@wbyq sdl2_ttf-2.0.15]$ gedit sdl_ttf.c +2135void ttf_setfontsize(ttf_font *font,int ptsize){ ft_fixed scale; ft_error error; ft_face face; face = font->face; /* make sure that our font face is scalable (global metrics) */ if ( ft_is_scalable(face) ) { /* set the character size and use default dpi (72) */ error = ft_set_char_size( font->face, 0, ptsize * 64, 0, 0 ); if( error ) { ttf_setfterror( couldn't set font size, error ); ttf_closefont( font ); return ; } /* get the scalable font metrics for this font */ scale = face->size->metrics.y_scale; font->ascent = ft_ceil(ft_mulfix(face->ascender, scale)); font->descent = ft_ceil(ft_mulfix(face->descender, scale)); font->height = font->ascent - font->descent + /* baseline */ 1; font->lineskip = ft_ceil(ft_mulfix(face->height, scale)); font->underline_offset = ft_floor(ft_mulfix(face->underline_position, scale)); font->underline_height = ft_floor(ft_mulfix(face->underline_thickness, scale)); } else { /* non-scalable font case. ptsize determines which family * or series of fonts to grab from the non-scalable format. * it is not the point size of the font. * */ if ( ptsize >= font->face->num_fixed_sizes ) ptsize = font->face->num_fixed_sizes - 1; font->font_size_family = ptsize; error = ft_set_pixel_sizes( face, face->available_sizes[ptsize].height, face->available_sizes[ptsize].width ); /* with non-scalale fonts, freetype2 likes to fill many of the * font metrics with the value of 0. the size of the * non-scalable fonts must be determined differently * or sometimes cannot be determined. * */ font->ascent = face->available_sizes[ptsize].height; font->descent = 0; font->height = face->available_sizes[ptsize].height; font->lineskip = ft_ceil(font->ascent); font->underline_offset = ft_floor(face->underline_position); font->underline_height = ft_floor(face->underline_thickness); } if ( font->underline_height underline_height = 1; } font->glyph_italics *= font->height; flush_cache(font); //这个非常重要}   在sdl_ttf.h中声明函数
[wbyq@wbyq sdl2_ttf-2.0.15]$ gedit sdl_ttf.h +291extern void ttf_setfontsize(ttf_font *font,int ptsize);/*设置字体大小*/ 3.sdl文本显示#include #include #include #define window_w 800#define window_h 480int main(int argc,char *argv[]){ /*sdl初始化*/ sdl_init(sdl_init_video); /*ttf初始化*/ ttf_init(); /*创建窗口*/ sdl_window *window=sdl_createwindow(sdl show text,sdl_windowpos_centered,sdl_windowpos_centered,window_w,window_h,sdl_window_shown ); /*创建渲染器*/ sdl_renderer *render=sdl_createrenderer(window,-1,sdl_renderer_accelerated); /*设置渲染器颜色*/ sdl_setrenderdrawcolor(render, 255, 255, 255, 255); /*清空渲染器*/ sdl_renderclear(render); /*打开字库*/ ttf_font *ttffont=ttf_openfont(simkai.ttf,50); if(ttffont==null) { printf(simkai.ttf open failedn); return 0; } sdl_color color={52,203,120,255};/*字体颜色rgba*/ /*创建字体显示表面*/ sdl_surface *text1_surface=ttf_renderutf8_blended(ttffont,hello,world!,color); /*创建纹理*/ sdl_texture * texture=sdl_createtexturefromsurface(render,text1_surface); /*将surface拷贝到渲染器*/ sdl_rect dstrect; dstrect.x=window_w/2-text1_surface->w/2;/*显示的起始位置*/ dstrect.y=100;/*显示的起始位置*/ dstrect.w=text1_surface->w;/*显示的宽度*/ dstrect.h=text1_surface->h;/*显示的高度*/ sdl_rendercopy(render,texture,null,&dstrect); sdl_freesurface(text1_surface);/*释放surface*/ sdl_destroytexture(texture);/*释放纹理*/ /*设置字体大小*/ ttf_setfontsize(ttffont,60); /*字体加粗*/ ttf_setfontstyle(ttffont,ttf_style_bold); /*创建字体显示表面*/ text1_surface=ttf_renderutf8_blended(ttffont,北京万邦易嵌科技有限公司,color); /*创建纹理*/ texture=sdl_createtexturefromsurface(render,text1_surface); /*将surface拷贝到渲染器*/ dstrect.x=window_w/2-text1_surface->w/2;/*显示的起始位置*/ dstrect.y=160;/*显示的起始位置*/ dstrect.w=text1_surface->w;/*显示的宽度*/ dstrect.h=text1_surface->h;/*显示的高度*/ sdl_rendercopy(render,texture,null,&dstrect); sdl_freesurface(text1_surface);/*释放surface*/ sdl_destroytexture(texture);/*释放纹理*/ /*正常字体*/ ttf_setfontstyle(ttffont,ttf_style_normal); /*创建字体显示表面*/ text1_surface=ttf_renderutf8_blended(ttffont,www.wanbangee.com,color); /*创建纹理*/ texture=sdl_createtexturefromsurface(render,text1_surface); /*将surface拷贝到渲染器*/ dstrect.x=window_w/2-text1_surface->w/2;/*显示的起始位置*/ dstrect.y=230;/*显示的起始位置*/ dstrect.w=text1_surface->w;/*显示的宽度*/ dstrect.h=text1_surface->h;/*显示的高度*/ sdl_rendercopy(render,texture,null,&dstrect); sdl_freesurface(text1_surface);/*释放surface*/ sdl_destroytexture(texture);/*释放纹理*/ /*正常字体*/ ttf_setfontstyle(ttffont,ttf_style_normal); /*创建字体显示表面*/ text1_surface=ttf_renderutf8_blended(ttffont,sdl_ttf库显示测试示例!,color); /*创建纹理*/ texture=sdl_createtexturefromsurface(render,text1_surface); /*将surface拷贝到渲染器*/ dstrect.x=window_w/2-text1_surface->w/2;/*显示的起始位置*/ dstrect.y=300;/*显示的起始位置*/ dstrect.w=text1_surface->w;/*显示的宽度*/ dstrect.h=text1_surface->h;/*显示的高度*/ sdl_rendercopy(render,texture,null,&dstrect); sdl_freesurface(text1_surface);/*释放surface*/ sdl_destroytexture(texture);/*释放纹理*/ sdl_renderpresent(render);/*刷新显示*/ sdl_event event; while(1) { if(sdl_pollevent(&event))/*获取事件*/ { if(event.type==sdl_quit)break; } } ttf_closefont(ttffont);/*关闭字库*/ ttf_quit();/*关闭ttf*/ sdl_destroyrenderer(render);/*注销渲染器*/ sdl_destroywindow(window);/*注销窗口*/ sdl_quit(); return 0;} 4.makefile文件cflags =-i/home/wbyq/src_pack/sdl2-2.0.14/_install/include -i/home/wbyq/src_pack/sdl2-2.0.14/_install/include/sdl2 -l/home/wbyq/src_pack/sdl2-2.0.14/_install/libcflags +=-l/home/wbyq/src_pack/sdl2_image-2.0.5/_install/lib -i/home/wbyq/src_pack/sdl2_image-2.0.5/_install/include -i/home/wbyq/src_pack/sdl2_image-2.0.5/_install/include/sdl2cflags +=-i/home/wbyq/src_pack/sdl2_ttf-2.0.15/_install/include/sdl2 -l/home/wbyq/src_pack/sdl2_ttf-2.0.15/_install/libcflags+=-lsdl2 -lpthread -lm -ldl -lsdl2_image -lsdl2_ttfapp: gcc sdl_test.c $(cflags) 5.运行效果  


从打压到认可,LoRa为何能够实现逆袭
aigo国民好物固态U盘U393到底好在哪?看完测评你就明白了
西部数据扩展闪存产品组合,助力ZB时代以数据为中心的存储架构不断发展
西门子变频器更换主板后怎样重新设置参数
如何设置RT-AC87R路由器
SDL显示文本
什么是杂散干扰、互调干扰、阻塞干扰?
真无线耳机哪个延迟低?吃鸡低延迟真无线蓝牙耳机榜单
诺基亚8现身京东,初于情怀,你会购买吗?
在ANSYS软件中对单相三柱式电压互感器的耦合进行分析与研究
10nm工艺之战又升级 联发科刷存在感HelioX30或采用
验电器使用方法及注意事项
高通骁龙888的特性如何?
诺基亚获得美国政府资金支持,用于在月球上提供4G LTE连接
0Ω电阻到底能过多大电流?电路中0Ω电阻的使用方法
P5200A系列示波器高压差分探头的产品解析
医疗废物在线监测系统医废监管好帮手
HDZF智能蓄电池充电放电放电综合测试仪使用与接线说明
泵吸式氨气检测仪有哪些功能
linux makefile教程