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;
if ( FT_IS_SCALABLE(face) ) {
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 ;
}
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 + 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 {
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 );
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中声明函数
[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_Init(SDL_INIT_VIDEO);
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};
SDL_Surface *text1_surface=TTF_RenderUTF8_Blended(ttffont,"hello,world!",color);
SDL_Texture * texture=SDL_CreateTextureFromSurface(render,text1_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);
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);
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);
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);
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);
SDL_DestroyTexture(texture);
TTF_SetFontStyle(ttffont,TTF_STYLE_NORMAL);
text1_surface=TTF_RenderUTF8_Blended(ttffont,"SDL_ttf库显示测试示例!",color);
texture=SDL_CreateTextureFromSurface(render,text1_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);
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();
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.运行效果
|