SDL 显示汉字

#include "stdafx.h"

#pragma comment(lib,"SDL.lib")
#pragma comment(lib,"SDL2_image.lib")
#pragma comment(lib,"SDL_ttf.lib")

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <Windows.h>

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

/* 屏幕分辩率 */
#define  SCREEN_WIDTH   720
#define  SCREEN_HEIGHT  480
#define  SCREEN_BPP     32

const SDL_Color RGB_Black   = { 0, 0, 0 };
const SDL_Color RGB_Red     = { 255, 0, 0 };
const SDL_Color RGB_White   = { 255, 255, 255 };
const SDL_Color RGB_Yellow  = { 255, 255, 0 };

void ApplySurface(int x, int y, SDL_Surface* pSrc, SDL_Surface* pDest)
{
    SDL_Rect rect;

    rect.x = x;
    rect.y = y;
    rect.w = pSrc->w;
    rect.h = pSrc->h;

    SDL_BlitSurface(pSrc, NULL, pDest, &rect);
}

char *localeToUTF8(char *src)
{
    static char *buf = NULL;
    wchar_t *unicode_buf;
    int nRetLen;

    if(buf){
        free(buf);
        buf = NULL;
    }
    nRetLen = MultiByteToWideChar(CP_ACP,0,src,-1,NULL,0);
    unicode_buf = (wchar_t*)malloc((nRetLen+1)*sizeof(wchar_t));
    MultiByteToWideChar(CP_ACP,0,src,-1,unicode_buf,nRetLen);
    nRetLen = WideCharToMultiByte(CP_UTF8,0,unicode_buf,-1,NULL,0,NULL,NULL);
    buf = (char*)malloc(nRetLen+1);
    WideCharToMultiByte(CP_UTF8,0,unicode_buf,-1,buf,nRetLen,NULL,NULL);
    free(unicode_buf);
    return buf;
}

int main(int argc,char * argv[])
{
    SDL_Surface     *pScreen;
    SDL_Surface     *pBackground;
    SDL_Surface     *pText;

    SDL_Event       myEvent;

    TTF_Font *font;

    char szEnglish[]        = "Hello World!";
    wchar_t wszChinese[]    = L"世界,你好!";

    /* 初始化 SDL */
    if (SDL_Init(SDL_INIT_VIDEO) == -1)
        return 0;

    /* 初始化窗口 */
    pScreen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE | SDL_HWSURFACE);
    if (NULL == pScreen)//检测是否初始化成功
        return 0;

    /* 初始化字体库 */
    if (TTF_Init() == -1 )
        return 0;

    /* 设置窗口名字和图标 */
    SDL_WM_SetCaption(localeToUTF8("测试SDL显示文字"), NULL);

    /* 打开simfang.ttf 字库,设字体为20号 */
    font  = TTF_OpenFont("C:\\Windows\\Fonts\\simhei.ttf", 20);
    if (font == NULL)
    {
        return 0;
    }  

    /* 显示背景 */
    pBackground =  SDL_LoadBMP(".\\22.bmp");
    if (NULL != pBackground)
    {
        ApplySurface(0, 0, pBackground, pScreen);
        SDL_FreeSurface(pBackground);
    }

    /* 设置字体样式(加粗|斜体)*/
    TTF_SetFontStyle(font, TTF_STYLE_BOLD |  TTF_STYLE_ITALIC);

    /* 显示英文 */
    pText = TTF_RenderText_Solid(font, szEnglish, RGB_Red);
    if (NULL != pText)
    {
        ApplySurface(80, 120, pText, pScreen);
        SDL_FreeSurface(pText);
    }

    pText = TTF_RenderText_Shaded(font,szEnglish,RGB_Red, RGB_White);
    if (NULL != pText)
    {
        ApplySurface(80, 150, pText, pScreen);
        SDL_FreeSurface(pText);
    }

    pText = TTF_RenderText_Blended(font,szEnglish,RGB_Red);
    if (NULL != pText)
    {
        ApplySurface(80, 180, pText, pScreen);
        SDL_FreeSurface(pText);
    }

    /* 显示中文 */
    pText = TTF_RenderUNICODE_Solid(font, (const Uint16 *)wszChinese, RGB_Red);
    if (NULL != pText)
    {
        ApplySurface(280, 120, pText, pScreen);
        SDL_FreeSurface(pText);
    }

    pText = TTF_RenderUNICODE_Shaded(font, (const Uint16 *)wszChinese, RGB_Red, RGB_White);
    if (NULL != pText)
    {
        ApplySurface(280, 150, pText, pScreen);
        SDL_FreeSurface(pText);
    }

    pText = TTF_RenderUNICODE_Blended(font, (const Uint16 *)wszChinese, RGB_Red);
    if (NULL != pText)
    {
        ApplySurface(280, 180, pText, pScreen);
        SDL_FreeSurface(pText);
    }
    
    /* 将缓冲在界面显示出来 */
    SDL_Flip(pScreen);

    /* 事件处理 */
    int quit = 0;
    while (!quit)
    {
        if (SDL_PollEvent(&myEvent))
        {
            if (SDL_QUIT == myEvent.type)
            {
                quit = 1;
            }
        }       
    }   

    return 0;
}

SDL本身并没有实际文字的功能,需要用到其扩展库SDL_ttf,下载地址:

http://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html

 

注意1:

TTF_OpenFont打开字体时,注意该字体是否支持汉字。
如果需要加载微软雅黑,其文件名是:msyhbd.ttf。这是因为C:\\Windows\\Fonts\\这个目录下我们看到的是“微软雅黑.ttf”这个文件。拷贝到桌面出来却是msyhbd.ttf跟msyh.ttf

注意2:
SDL_BlitSurface 不要使用,在以下配置环境中运行需要120ms



 

posted @ 2016-01-27 13:59  QQ76211822  阅读(674)  评论(0编辑  收藏  举报