SDL显示图像和文字

转自 : http://blog.csdn.net/skywalker_leo/article/details/42776377


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

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

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

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

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include "stdafx.h"  
  2.   
  3. #pragma comment(lib,"SDL.lib")  
  4. #pragma comment(lib,"SDL2_image.lib")  
  5. #pragma comment(lib,"SDL_ttf.lib")  
  6.   
  7. #include <stdio.h>  
  8. #include <stdlib.h>  
  9. #include <string>  
  10. #include <Windows.h>  
  11.   
  12. #include "SDL.h"  
  13. #include "SDL_ttf.h"  
  14.   
  15. /* 屏幕分辩率 */  
  16. #define  SCREEN_WIDTH   720  
  17. #define  SCREEN_HEIGHT  480  
  18. #define  SCREEN_BPP     32  
  19.   
  20. const SDL_Color RGB_Black   = { 0, 0, 0 };  
  21. const SDL_Color RGB_Red     = { 255, 0, 0 };  
  22. const SDL_Color RGB_White   = { 255, 255, 255 };  
  23. const SDL_Color RGB_Yellow  = { 255, 255, 0 };  
  24.   
  25. void ApplySurface(int x, int y, SDL_Surface* pSrc, SDL_Surface* pDest)  
  26. {  
  27.     SDL_Rect rect;  
  28.   
  29.     rect.x = x;  
  30.     rect.y = y;  
  31.     rect.w = pSrc->w;  
  32.     rect.h = pSrc->h;  
  33.   
  34.     SDL_BlitSurface(pSrc, NULL, pDest, &rect);  
  35. }  
  36.   
  37. char *localeToUTF8(char *src)  
  38. {  
  39.     static char *buf = NULL;  
  40.     wchar_t *unicode_buf;  
  41.     int nRetLen;  
  42.   
  43.     if(buf){  
  44.         free(buf);  
  45.         buf = NULL;  
  46.     }  
  47.     nRetLen = MultiByteToWideChar(CP_ACP,0,src,-1,NULL,0);  
  48.     unicode_buf = (wchar_t*)malloc((nRetLen+1)*sizeof(wchar_t));  
  49.     MultiByteToWideChar(CP_ACP,0,src,-1,unicode_buf,nRetLen);  
  50.     nRetLen = WideCharToMultiByte(CP_UTF8,0,unicode_buf,-1,NULL,0,NULL,NULL);  
  51.     buf = (char*)malloc(nRetLen+1);  
  52.     WideCharToMultiByte(CP_UTF8,0,unicode_buf,-1,buf,nRetLen,NULL,NULL);  
  53.     free(unicode_buf);  
  54.     return buf;  
  55. }  
  56.   
  57. int main(int argc,char * argv[])  
  58. {  
  59.     SDL_Surface     *pScreen;  
  60.     SDL_Surface     *pBackground;  
  61.     SDL_Surface     *pText;  
  62.   
  63.     SDL_Event       myEvent;  
  64.   
  65.     TTF_Font *font;  
  66.   
  67.     char szEnglish[]        = "Hello World!";  
  68.     wchar_t wszChinese[]    = L"世界,你好!";  
  69.   
  70.     /* 初始化 SDL */  
  71.     if (SDL_Init(SDL_INIT_VIDEO) == -1)  
  72.         return 0;  
  73.   
  74.     /* 初始化窗口 */  
  75.     pScreen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE | SDL_HWSURFACE);  
  76.     if (NULL == pScreen)//检测是否初始化成功  
  77.         return 0;  
  78.   
  79.     /* 初始化字体库 */  
  80.     if (TTF_Init() == -1 )  
  81.         return 0;  
  82.   
  83.     /* 设置窗口名字和图标 */  
  84.     SDL_WM_SetCaption(localeToUTF8("测试SDL显示文字"), NULL);  
  85.   
  86.     /* 打开simfang.ttf 字库,设字体为20号 */  
  87.     font  = TTF_OpenFont("C:\\Windows\\Fonts\\simhei.ttf", 20);  
  88.     if (font == NULL)  
  89.     {  
  90.         return 0;  
  91.     }    
  92.   
  93.     /* 显示背景 */  
  94.     pBackground =  SDL_LoadBMP(".\\22.bmp");  
  95.     if (NULL != pBackground)  
  96.     {  
  97.         ApplySurface(0, 0, pBackground, pScreen);  
  98.         SDL_FreeSurface(pBackground);  
  99.     }  
  100.   
  101.     /* 设置字体样式(加粗|斜体)*/  
  102.     TTF_SetFontStyle(font, TTF_STYLE_BOLD |  TTF_STYLE_ITALIC);  
  103.   
  104.     /* 显示英文 */  
  105.     pText = TTF_RenderText_Solid(font, szEnglish, RGB_Red);  
  106.     if (NULL != pText)  
  107.     {  
  108.         ApplySurface(80, 120, pText, pScreen);  
  109.         SDL_FreeSurface(pText);  
  110.     }  
  111.   
  112.     pText = TTF_RenderText_Shaded(font,szEnglish,RGB_Red, RGB_White);  
  113.     if (NULL != pText)  
  114.     {  
  115.         ApplySurface(80, 150, pText, pScreen);  
  116.         SDL_FreeSurface(pText);  
  117.     }  
  118.   
  119.     pText = TTF_RenderText_Blended(font,szEnglish,RGB_Red);  
  120.     if (NULL != pText)  
  121.     {  
  122.         ApplySurface(80, 180, pText, pScreen);  
  123.         SDL_FreeSurface(pText);  
  124.     }  
  125.   
  126.     /* 显示中文 */  
  127.     pText = TTF_RenderUNICODE_Solid(font, (const Uint16 *)wszChinese, RGB_Red);  
  128.     if (NULL != pText)  
  129.     {  
  130.         ApplySurface(280, 120, pText, pScreen);  
  131.         SDL_FreeSurface(pText);  
  132.     }  
  133.   
  134.     pText = TTF_RenderUNICODE_Shaded(font, (const Uint16 *)wszChinese, RGB_Red, RGB_White);  
  135.     if (NULL != pText)  
  136.     {  
  137.         ApplySurface(280, 150, pText, pScreen);  
  138.         SDL_FreeSurface(pText);  
  139.     }  
  140.   
  141.     pText = TTF_RenderUNICODE_Blended(font, (const Uint16 *)wszChinese, RGB_Red);  
  142.     if (NULL != pText)  
  143.     {  
  144.         ApplySurface(280, 180, pText, pScreen);  
  145.         SDL_FreeSurface(pText);  
  146.     }  
  147.       
  148.     /* 将缓冲在界面显示出来 */  
  149.     SDL_Flip(pScreen);  
  150.   
  151.     /* 事件处理 */  
  152.     int quit = 0;  
  153.     while (!quit)  
  154.     {  
  155.         if (SDL_PollEvent(&myEvent))  
  156.         {  
  157.             if (SDL_QUIT == myEvent.type)  
  158.             {  
  159.                 quit = 1;  
  160.             }  
  161.         }         
  162.     }     
  163.   
  164.     return 0;  
  165. }  

我将要点写成了一个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 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值