SDL顯示文本
?? SDL2.0庫(kù)本身沒(méi)有文本數(shù)據(jù)顯示相關(guān)函數(shù)接口,文本顯示需要編譯安裝SDL_ttf庫(kù)。
1.編譯安裝SDL2_ttf庫(kù)
SDL_ttf下載地址:SDL_ttf
??(1)將下載的SDL2-2.0.14.tar.gz壓縮包拷貝至虛擬機(jī)解壓。
[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庫(kù)中提供函數(shù)沒(méi)有單獨(dú)提供設(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); //這個(gè)非常重要
}
??在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);
/*打開(kāi)字庫(kù)*/
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,"北京萬(wàn)邦易嵌科技有限公司",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庫(kù)顯示測(cè)試示例!",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)閉字庫(kù)*/
TTF_Quit();/*關(guān)閉ttf*/
SDL_DestroyRenderer(render);/*注銷(xiāo)渲染器*/
SDL_DestroyWindow(window);/*注銷(xiāo)窗口*/
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.運(yùn)行效果
-
編譯
+關(guān)注
關(guān)注
0文章
648瀏覽量
32774 -
SDL
+關(guān)注
關(guān)注
0文章
18瀏覽量
7379 -
Makefile
+關(guān)注
關(guān)注
1文章
125瀏覽量
19158
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論