SDL显示文本
  hwzypeJdeEzq 2023年11月02日 28 0

  SDL(Simple DirectMediaLayer)是一套开放源代码的跨平台多媒体开发库,使用C语言写成。SDL提供了数种控制图像、声音、输出入的函数,让开发者只要用相同或是相似的代码就可以开发出跨多个平台(Linux、Windows、MacOS X等)的应用软件。现SDL多用于开发游戏、模拟器、媒体播放器等多媒体应用领域。

 SDL2.0库本身没有文本数据显示相关函数接口,文本显示需要编译安装SDL_ttf库

1.编译安装SDL2_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 <stdio.h>
#include <SDL.h>
#include <SDL_ttf.h>
#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 failed\n");
		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.运行效果

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
hwzypeJdeEzq