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

来源:互联网 发布:js复选框全选 编辑:程序博客网 时间:2024/06/06 08:37

其实让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 0
原创粉丝点击