SFont 库
sfont用大小写英文字母和符号进行内容显示,资源即是字体图片。
sfont下载:http://www.linux-games.com/sfont
我下载SFont 2.03后,解压并make,产生了许多的文件,其中包括三个SFont的测试文件,下面是SFontTest3的效果:
自己来写一个试试,将.h
文件,.c
文件,要使用的图片文件(我就直接使用24P_Copperplate_Blue.png
了)复制到代码文件目录。官网上有更多的字体 http://www.linux-games.com/sfont-fonts/
#include <SDL.h>
#include <SDL_image.h>
#include "SFont.h"
int main(){
SDL_Surface *screen, *font;
SFont_Font* myFont;
if(SDL_Init(SDL_INIT_VIDEO) < 0){
fprintf(stderr,"SDL init error: %s\n",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);
font = IMG_Load("24P_Copperplate_Blue.png");
if(font == NULL){
fprintf(stderr,"IMG_Load Error: %s\n",SDL_GetError());
exit(1);
}
myFont = SFont_InitFont(font);
/* void SFont_Write(SDL_Surface *Surface, const SFont_Font *Font, int x, int y, const char *text); */
SFont_Write(screen,myFont,100,220,"SFont works, hello world");
SDL_UpdateRect(screen,0,0,0,0);
SDL_Delay(5000);
SFont_FreeFont(myFont);
//SDL_FreeSurface(font); /* SDL_FreeFont(myFont) has free font before.*/
//SDL_FreeSurface(screen); /* SDL_quit has handled it.*/
return 0;
}
edemon@ubuntu1:~/workspace$ gcc SFontTest.c SFont.c -o exe -lSDL -lSDL_image
edemon@ubuntu1:~/workspace$ ./exe