SDL显示图像和文字

这两天在做一个视频分析软件需要用到SDL显示图像及文字,将阶段成果做一下总结:

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

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

闲话少说,请看代码(工程环境:VS2005):

#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;
}

我将要点写成了一个DEMO程序:http://download.csdn.net/detail/skywalker_leo/8368025

参考链接:

http://blog.csdn.net/leixiaohua1020/article/details/8652605

http://blog.163.com/niuxiangshan@126/blog/static/17059659520112711935975/

http://www.cnblogs.com/landmark/archive/2012/06/01/2526140.html


你可能感兴趣的:(图像,文字,sdl)