SDL显示中文(一个函数而已)


其实让SDL显示中文很简单,刚开始我也不知道,苦恼了好久,但是知道后发现他太简单了
想要显示的中文就是一个函数的事情,用TTF_RenderUTF8_Solid( font,"大家好!!", textColor )函数就行了,什么也不用做。



大家可以试试


下面是我写的一个测试的代码:


#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"


const int SCREEN_WIDTH=1000;
const int SCREEN_HEIGHT=750;
const int SCREEN_BPP=32;


SDL_Surface *background=NULL;
SDL_Surface *screen=NULL;
SDL_Surface *message=NULL;


SDL_Event event;


TTF_Font *font=NULL;
SDL_Color textColor={ 255, 0, 0 };//设置颜色
/*加载图片*/
SDL_Surface *load_image( char filename[20] )
{
    SDL_Surface* loadedImage = NULL;
    SDL_Surface* optimizedImage = NULL;
    loadedImage = SDL_LoadBMP(filename);
    if( loadedImage != NULL )
    {
        optimizedImage = SDL_DisplayFormat( loadedImage );   
        SDL_FreeSurface( loadedImage );
    }
    return optimizedImage;
}


void apply_surface( int x, int y, int w, int h, SDL_Surface* picture,SDL_Surface* Screen)
{
    SDL_Rect offset;


    offset.x = x;
    offset.y = y;
    offset.w = w;
    offset.h = h;
    SDL_BlitSurface( picture, NULL,Screen, &offset );
}
/*加载图片*/


int init()//初始化函数
{
if(TTF_Init()==-1)
{
exit(1);
}
font = TTF_OpenFont( "simfang.ttf", 50 );
if(font==NULL)
    {
exit(1);
}
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        exit(1);
    }
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
    if( screen == NULL )
    {
        exit (1);
    }
    SDL_WM_SetCaption( "测试", NULL );
    
    return 1;
}


void FreeSDL()//  释放函数
{
SDL_FreeSurface( background );
TTF_CloseFont( font );
    TTF_Quit();
SDL_Quit();
}


int main()
{
if(init()!=1)
{
exit(1);
}
background=load_image("background.bmp");
apply_surface(0,0,0,0,background,screen);
message=TTF_RenderUTF8_Solid( font,"大家好!!", textColor );//加在成中文
apply_surface(300,300,0,0,message,screen);

if(SDL_Flip(screen)==-1)
{
exit(1);
}
int quit=0;
int sno;
while(!quit)
{
while(SDL_PollEvent(&event))
{
if(event.type==SDL_QUIT)
{
quit=1;
}
SDL_Flip(screen);
}
}
FreeSDL();
return 0;
}

效果如下:


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值