纠结了很久,终于把它搞定了- // 写字符串
- // x,y 坐标
- // str 字符串
- // color 颜色
- // size 字体大小,字形为宋体。
- // fontname 字体名
- // charset 字符集 0 GBK 1 big5
- // OScharset 0 简体显示 1 繁体显示
- int JY_DrawStr(int x, int y, const char *str,int color,int size,const char *fontname,
- int charset, int OScharset)
- {
- SDL_Color c,c2;
- SDL_Surface *fontSurface=NULL, *fontSurface1=NULL;
- int w,h;
- SDL_Rect rect1,rect2,rect_dest;
- SDL_Rect rect;
- char tmp1[256],tmp2[256];
- TTF_Font *myfont;
- SDL_Surface *tempSurface;
- if(strlen(str)>127){
- JY_Error("JY_DrawStr: string length more than 127: %s",str);
- return 0;
- }
- myfont=GetFont(fontname,size);
- if(myfont==NULL)
- return 1;
- c.r=(Uint8) ((color & 0xff0000) >>16);
- c.g=(Uint8) ((color & 0xff00)>>8);
- c.b=(Uint8) ((color & 0xff));
- c2.r=c.r>>1; //阴影色
- c2.b=c.b>>1;
- c2.g=c.g>>1;
- if(charset==0 && OScharset==0){//GBK -->unicode简体
- JY_CharSet(str,tmp2,3);
- }
- else if(charset==0 && OScharset==1){//GBK -->unicode繁体
- JY_CharSet(str,tmp1,1);
- JY_CharSet(tmp1,tmp2,2);
- }
- else if(charset==1 && OScharset==0){ //big5-->unicode简体
- JY_CharSet(str,tmp1,0);
- JY_CharSet(tmp1,tmp2,3);
- }
- else if(charset==1 && OScharset==1){ ////big5-->unicode繁体
- JY_CharSet(str,tmp2,2);
- }
- else{
- strcpy(tmp2,str);
- }
-
- if(g_Rotate==0){
- rect=g_Surface->clip_rect;
- }
- else{
- rect=RotateReverseRect(&g_Surface->clip_rect);
- }
- TTF_SizeUNICODE(myfont, (Uint16*)tmp2, &w, &h);
-
- if( (x>=rect.x+rect.w) || (x+w+1) <=rect.x ||
- (y>=rect.y+rect.h) || (y+h+1) <=rect.y){ // 超出裁剪范围则不显示
- return 1;
- }
- fontSurface=TTF_RenderUNICODE_Blended(myfont, (Uint16*)tmp2, c); //生成表面
- fontSurface1=TTF_RenderUNICODE_Blended(myfont, (Uint16*)tmp2, c2); //生成阴影
-
- if(fontSurface==NULL)
- return 1;
-
- rect1.x=(Sint16)x;
- rect1.y=(Sint16)y;
- rect1.w=(Uint16)fontSurface->w;
- rect1.h=(Uint16)fontSurface->h;
- if(g_Rotate==0){
- rect2=rect1;
- SDL_SetAlpha(fontSurface1,SDL_SRCALPHA,56); //设置阴影透明度
- rect_dest.x=rect2.x-1;
- rect_dest.y=rect2.y-1;
- SDL_BlitSurface(fontSurface1, NULL, g_Surface, &rect_dest); //表面写到游戏表面--阴影色
- rect_dest.x=rect2.x-1;
- rect_dest.y=rect2.y+1;
- SDL_BlitSurface(fontSurface1, NULL, g_Surface, &rect_dest); //表面写到游戏表面--阴影色
- rect_dest.x=rect2.x+1;
- rect_dest.y=rect2.y-1;
- SDL_BlitSurface(fontSurface1, NULL, g_Surface, &rect_dest); //表面写到游戏表面--阴影色
- rect_dest.x=rect2.x+1;
- rect_dest.y=rect2.y+1;
- SDL_BlitSurface(fontSurface1, NULL, g_Surface, &rect_dest); //表面写到游戏表面--阴影色
-
- rect_dest.x=rect2.x;
- rect_dest.y=rect2.y;
- SDL_BlitSurface(fontSurface, NULL, g_Surface, &rect_dest); //表面写到游戏表面
-
- }
- else if(g_Rotate==1){
- tempSurface=RotateSurface(fontSurface);
- SDL_FreeSurface(fontSurface);
- fontSurface=tempSurface;
- rect2=RotateRect(&rect1);
- SDL_SetAlpha(fontSurface1,SDL_SRCALPHA,56); //设置阴影透明度
- rect_dest.x=rect2.x-1;
- rect_dest.y=rect2.y-1;
- SDL_BlitSurface(fontSurface1, NULL, g_Surface, &rect_dest); //表面写到游戏表面--阴影色
- rect_dest.x=rect2.x-1;
- rect_dest.y=rect2.y+1;
- SDL_BlitSurface(fontSurface1, NULL, g_Surface, &rect_dest); //表面写到游戏表面--阴影色
- rect_dest.x=rect2.x+1;
- rect_dest.y=rect2.y-1;
- SDL_BlitSurface(fontSurface1, NULL, g_Surface, &rect_dest); //表面写到游戏表面--阴影色
- rect_dest.x=rect2.x+1;
- rect_dest.y=rect2.y+1;
- SDL_BlitSurface(fontSurface1, NULL, g_Surface, &rect_dest); //表面写到游戏表面--阴影色
-
- rect_dest.x=rect2.x;
- rect_dest.y=rect2.y;
- SDL_BlitSurface(fontSurface, NULL, g_Surface, &rect_dest); //表面写到游戏表面
-
- }
- SDL_FreeSurface(fontSurface); //释放表面
- SDL_FreeSurface(fontSurface1); //释放阴影表面
- return 0;
- }
复制代码
|