#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
SDL_Surface *screen;
void Init_SDL()
{
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
fprintf(stderr,"cannot init SDL: %s",SDL_GetError());
exit(1);
}
screen=SDL_SetVideoMode(640,480,16,SDL_SWSURFACE);
if(screen==NULL)
{
fprintf(stderr,"Error:%s\n",SDL_GetError());
exit(1);
}
//atexit(SDL_Quit);
}
int main()
{
SDL_Surface *text;
SDL_Rect dest_rect;
TTF_Font *font;
SDL_Color green={0,255,0,0};
int fontsize=38;
Init_SDL();
if(TTF_Init()!=0)
{
fprintf(stderr,"cannot init ttf font!\n");
exit(1);
}
font=TTF_OpenFont("/usr/share/fonts/simsum.ttf",fontsize);
TTF_SetFontStyle(font,TTF_STYLE_NORMAL);
text=TTF_RenderUTF8_Blended(font,"Turetype 中文文字显示",green);
if(text==NULL)
{
fprintf(stderr,"cannot render ttf text\n");
exit(1);
TTF_CloseFont(font);
TTF_Quit();
dest_rect.x=100;
dest_rect.y=200;
dest_rect.w=text->w;
dest_rect.h=text->h;
SDL_BlitSurface(text,NULL,screen,&dest_rect);
SDL_UpdateRect(screen,0,0,0,0);
SDL_FreeSurface(text);
SDL_Delay(5000);
}
}
这是linux下的一段小代码,实现ttf显示中文,我装了ttf库(我也装了ttf的开发工具)
[root@localhost sdl]# gcc a.c -lSDL
/tmp/ccEFNb8t.o: In function `main':
a.c:(.text+0xba): undefined reference to `TTF_Init'
a.c:(.text+0xf0): undefined reference to `TTF_OpenFont'
a.c:(.text+0x102): undefined reference to `TTF_SetFontStyle'
a.c:(.text+0x113): undefined reference to `TTF_RenderUTF8_Blended'
collect2: ld 返回 1
是不是i386的工具rpm包,在x86_86机器上不行阿,如果不行还望提示一个下载地址。
回复 更多评论