Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2716471
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2010-09-22 21:39:23

说明:本程序是在xp, vs2008下编译通过,是在SDL_ttf-2.0.10中的showfont.c裁剪的基本上而改来的,为了把代码精简方便大家阅读。网上好多样例是要加入GLut库才支持或用OpenGl,这个例字说明只需要SDL_ttf库即可

#include
#include
#include
#include
#include

#include "SDL.h"
#include "SDL_ttf.h"

#define DEFAULT_PTSIZE 18
#define DEFAULT_TEXT "The quick brown fox jumped over the lazy dog"
#define NUM_COLORS      256

static void cleanup(int exitcode)
{
 TTF_Quit();
 SDL_Quit();
 exit(exitcode);
}
int main(int argc, char *argv[])
{
 char *argv0 = argv[0];
 SDL_Surface *screen;
 TTF_Font *font;
 SDL_Surface *text, *temp;
 int ptsize;
 int i, done;
 int rdiff, gdiff, bdiff;
 SDL_Color colors[NUM_COLORS];
 SDL_Color white = { 0xFF, 0xFF, 0x00, 0 };
 SDL_Color black = { 0x00, 0x00, 0x00, 0 };
 SDL_Color *forecol;
 SDL_Color *backcol;
 SDL_Rect dstrect;
 SDL_Event event;
 int rendersolid;
 int renderstyle;
 int outline;
 int hinting;
 int kerning;
 int dump;

 enum {
  RENDER_LATIN1,
  RENDER_UTF8,
  RENDER_UNICODE
 } rendertype;
 char *message, string[128];

 wchar_t wText[10] = {L"函数示例"};
 /* Look for special execution mode */
 dump = 0;
 /* Look for special rendering types */
 rendersolid = 0;
 renderstyle = TTF_STYLE_NORMAL;
 rendertype = RENDER_LATIN1;
 outline = 0;
 hinting = TTF_HINTING_NORMAL;
 kerning = 1;
 /* Default is black and white */
 forecol = &black;
 backcol = &white;

 /* Initialize SDL */
 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
  fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
  return(2);
 }

 /* Initialize the TTF library */
 if ( TTF_Init() < 0 ) {
  fprintf(stderr, "Couldn't initialize TTF: %s\n",SDL_GetError());
  SDL_Quit();
  return(2);
 }

 /* Open the font file with the requested point size */
 ptsize = 0;
 i = 2;
 ptsize = DEFAULT_PTSIZE;
 
 font = TTF_OpenFont("F:/dev/vc/006SDL/lib2/simsun.ttc", ptsize);
 if ( font == NULL ) {
  fprintf(stderr, "Couldn't load %d pt font from %s: %s\n", ptsize, argv[0], SDL_GetError());
  cleanup(2);
 }
 TTF_SetFontStyle(font, renderstyle);
 TTF_SetFontOutline(font, outline);
 TTF_SetFontKerning(font, kerning);

 /* Set a 640x480x8 video mode */
 screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
 if ( screen == NULL ) {
  fprintf(stderr, "Couldn't set 640x480x8 video mode: %s\n", SDL_GetError());
  cleanup(2);
 }

 /* Set a palette that is good for the foreground colored text */
 rdiff = backcol->r - forecol->r;
 gdiff = backcol->g - forecol->g;
 bdiff = backcol->b - forecol->b;
 for ( i=0; i  colors[i].r = forecol->r + (i*rdiff)/4;
  colors[i].g = forecol->g + (i*gdiff)/4;
  colors[i].b = forecol->b + (i*bdiff)/4;
 }
 SDL_SetColors(screen, colors, 0, NUM_COLORS);

 /* Clear the background to background color */
 SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, backcol->r, backcol->g, backcol->b));
 SDL_UpdateRect(screen, 0, 0, 0, 0);

 text =TTF_RenderUNICODE_Solid(font,wText, *forecol);   
 
 if ( text != NULL ) {
  dstrect.x = 4;
  dstrect.y = 4;
  dstrect.w = text->w;
  dstrect.h = text->h;
  SDL_BlitSurface(text, NULL, screen, &dstrect);
  SDL_FreeSurface(text);
 }
 
 message = wText;// DEFAULT_TEXT;
 
 rendertype = RENDER_UNICODE;
 
 {
  Uint16 unicode_text[BUFSIZ];
  int index;

  /* Convert the message from ascii into utf-16.
   * This is unreliable as a test because it always
   * gives the local ordering. */
  for (index = 0; wText[index]; index++) {
   unicode_text[index] = wText[index];
  }
  unicode_text[index] = 0;

  if ( rendersolid ) {
   text = TTF_RenderUNICODE_Solid(font, unicode_text, *forecol);
  } else {
   text = TTF_RenderUNICODE_Shaded(font, unicode_text, *forecol, *backcol);
  }
 }
 
 if ( text == NULL ) {
  fprintf(stderr, "Couldn't render text: %s\n", SDL_GetError());
  TTF_CloseFont(font);
  cleanup(2);
 }
 dstrect.x = (screen->w - text->w)/2;
 dstrect.y = (screen->h - text->h)/2;
 dstrect.w = text->w;
 dstrect.h = text->h;

 /* Blit the text surface */
 if ( SDL_BlitSurface(text, NULL, screen, &dstrect) < 0 ) {
  fprintf(stderr, "Couldn't blit text to display: %s\n",  SDL_GetError());
  TTF_CloseFont(font);
  cleanup(2);
 }
 SDL_UpdateRect(screen, 0, 0, 0, 0);

 /* Set the text colorkey and convert to display format */
 if ( SDL_SetColorKey(text, SDL_SRCCOLORKEY|SDL_RLEACCEL, 0) < 0 ) {
  fprintf(stderr, "Warning: Couldn't set text colorkey: %s\n", SDL_GetError());
 }
 temp = SDL_DisplayFormat(text);
 if ( temp != NULL ) {
  SDL_FreeSurface(text);
  text = temp;
 }

 /* Wait for a keystroke, and blit text on mouse press */
 done = 0;
 while ( ! done ) {
  if ( SDL_WaitEvent(&event) < 0 ) {
   fprintf(stderr, "SDL_PullEvent() error: %s\n", SDL_GetError());
   done = 1;
   continue;
  }
  switch (event.type) {
   case SDL_MOUSEBUTTONDOWN:
    dstrect.x = event.button.x - text->w/2;
    dstrect.y = event.button.y - text->h/2;
    dstrect.w = text->w;
    dstrect.h = text->h;
    if ( SDL_BlitSurface(text, NULL, screen, &dstrect) == 0 ) {
     SDL_UpdateRects(screen, 1, &dstrect);
    } else {
     fprintf(stderr, "Couldn't blit text to display: %s\n", SDL_GetError());
    }
    break;
    
   case SDL_KEYDOWN:
   case SDL_QUIT:
    done = 1;
    break;
   default:
    break;
  }
 }
 SDL_FreeSurface(text);
 TTF_CloseFont(font);
 cleanup(0);

 /* Not reached, but fixes compiler warnings */
 return 0;
}

阅读(3536) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-09-26 15:43:59

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com