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.pc

4 directories, 8 files

2.增加设置字体大小函数

  SDL2_ttf库中提供函数没有单独提供设置字体大小函数,为了方便设置字体大小,修改SDL_ttf.c文件,增加设置字体大小函数。

[wbyq@wbyq SDL2_ttf-2.0.15]$ gedit SDL_ttf.c +2135
void 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 < 1 ) {
		font->underline_height = 1;
	}
	font->glyph_italics *= font->height;
	Flush_Cache(font); //这个非常重要
}
SDL

  在sdl_ttf.h中声明函数

[wbyq@wbyq SDL2_ttf-2.0.15]$ gedit SDL_ttf.h +291
extern void TTF_SetFontSize(TTF_Font *font,int ptsize);/*设置字体大小*/

3.SDL文本显示

#include 
#include 
#include 
#define WINDOW_W 800
#define WINDOW_H 480
int 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/lib
CFLAGS +=-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/SDL2

CFLAGS +=-I/home/wbyq/src_pack/SDL2_ttf-2.0.15/_install/include/SDL2 -L/home/wbyq/src_pack/SDL2_ttf-2.0.15/_install/lib
CFLAGS+=-lSDL2 -lpthread -lm -ldl -lSDL2_image -lSDL2_ttf
app:
	gcc sdl_test.c  $(CFLAGS)

5.运行效果

SDL

 

  审核编辑:汤梓红
 
打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分