SDL顯示文本
?? SDL2.0庫本身沒有文本數(shù)據(jù)顯示相關(guān)函數(shù)接口,文本顯示需要編譯安裝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.增加設(shè)置字體大小函數(shù)
??SDL2_ttf庫中提供函數(shù)沒有單獨提供設(shè)置字體大小函數(shù),為了方便設(shè)置字體大小,修改SDL_ttf.c文件,增加設(shè)置字體大小函數(shù)。
[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_ttf.h中聲明函數(shù)
[wbyq@wbyq SDL2_ttf-2.0.15]$ gedit SDL_ttf.h +291
extern void TTF_SetFontSize(TTF_Font *font,int ptsize);/*設(shè)置字體大小*/
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();
	/*創(chuàng)建窗口*/
	SDL_Window *window=SDL_CreateWindow("SDL SHOW TEXT",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,WINDOW_W,WINDOW_H,SDL_WINDOW_SHOWN	);
	/*創(chuàng)建渲染器*/
	SDL_Renderer *render=SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);
	/*設(shè)置渲染器顏色*/
	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*/
	
	/*創(chuàng)建字體顯示表面*/
	SDL_Surface *text1_surface=TTF_RenderUTF8_Blended(ttffont,"hello,world!",color);
	/*創(chuàng)建紋理*/
	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);/*釋放紋理*/
	/*設(shè)置字體大小*/
	TTF_SetFontSize(ttffont,60);
	/*字體加粗*/
	TTF_SetFontStyle(ttffont,TTF_STYLE_BOLD);
	/*創(chuàng)建字體顯示表面*/
	text1_surface=TTF_RenderUTF8_Blended(ttffont,"北京萬邦易嵌科技有限公司",color);
	/*創(chuàng)建紋理*/
	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);
	/*創(chuàng)建字體顯示表面*/
	text1_surface=TTF_RenderUTF8_Blended(ttffont,"www.wanbangee.com",color);
	/*創(chuàng)建紋理*/
	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);
	/*創(chuàng)建字體顯示表面*/
	text1_surface=TTF_RenderUTF8_Blended(ttffont,"SDL_ttf庫顯示測試示例!",color);
	/*創(chuàng)建紋理*/
	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);/*關(guān)閉字庫*/
	TTF_Quit();/*關(guān)閉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.運行效果

- 
                                編譯
                                +關(guān)注
關(guān)注
0文章
682瀏覽量
34885 - 
                                SDL
                                +關(guān)注
關(guān)注
0文章
18瀏覽量
7870 - 
                                Makefile
                                +關(guān)注
關(guān)注
1文章
125瀏覽量
20150 
發(fā)布評論請先 登錄
SDL編譯安裝圖片顯示
    
SDL時間和天氣顯示
Linux下基于SDL庫貪吃蛇游戲
    
文本顯示問題
SDL的交叉編譯問題
移植SDL到JZ2440顯示BMP圖片
如何對基于ART-Pi-smart開發(fā)板的SDL進行測試
【飛凌RK3588開發(fā)板試用】基于SDL進行GUI程序開發(fā)
GUI向?qū)Ь幾g錯誤,找不到“SDL2/SDL.h”文件是怎么回事?
基于SDL的自動售票系統(tǒng)的研發(fā)
SDL1000X系列可編程直流電子負(fù)載的產(chǎn)品介紹
    
SDL下載與配置
    
          
        
        
SDL顯示文本
                
 
    
    
           
            
            
                
            
評論