SDL顯示中文(一個函數而已),sdl顯示中文函數

來源:互聯網
上載者:User

SDL顯示中文(一個函數而已),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;
}

效果如下:


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.