首页 技术 正文
技术 2022年11月15日
0 收藏 761 点赞 2,222 浏览 3032 个字
#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 32const SDL_Color RGB_Black = { , , };
const SDL_Color RGB_Red = { , , };
const SDL_Color RGB_White = { , , };
const SDL_Color RGB_Yellow = { , , };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,,src,-,NULL,);
unicode_buf = (wchar_t*)malloc((nRetLen+)*sizeof(wchar_t));
MultiByteToWideChar(CP_ACP,,src,-,unicode_buf,nRetLen);
nRetLen = WideCharToMultiByte(CP_UTF8,,unicode_buf,-,NULL,,NULL,NULL);
buf = (char*)malloc(nRetLen+);
WideCharToMultiByte(CP_UTF8,,unicode_buf,-,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) == -)
return ; /* 初始化窗口 */
pScreen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE | SDL_HWSURFACE);
if (NULL == pScreen)//检测是否初始化成功
return ; /* 初始化字体库 */
if (TTF_Init() == - )
return ; /* 设置窗口名字和图标 */
SDL_WM_SetCaption(localeToUTF8("测试SDL显示文字"), NULL); /* 打开simfang.ttf 字库,设字体为20号 */
font = TTF_OpenFont("C:\\Windows\\Fonts\\simhei.ttf", );
if (font == NULL)
{
return ;
} /* 显示背景 */
pBackground = SDL_LoadBMP(".\\22.bmp");
if (NULL != pBackground)
{
ApplySurface(, , 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(, , pText, pScreen);
SDL_FreeSurface(pText);
} pText = TTF_RenderText_Shaded(font,szEnglish,RGB_Red, RGB_White);
if (NULL != pText)
{
ApplySurface(, , pText, pScreen);
SDL_FreeSurface(pText);
} pText = TTF_RenderText_Blended(font,szEnglish,RGB_Red);
if (NULL != pText)
{
ApplySurface(, , pText, pScreen);
SDL_FreeSurface(pText);
} /* 显示中文 */
pText = TTF_RenderUNICODE_Solid(font, (const Uint16 *)wszChinese, RGB_Red);
if (NULL != pText)
{
ApplySurface(, , pText, pScreen);
SDL_FreeSurface(pText);
} pText = TTF_RenderUNICODE_Shaded(font, (const Uint16 *)wszChinese, RGB_Red, RGB_White);
if (NULL != pText)
{
ApplySurface(, , pText, pScreen);
SDL_FreeSurface(pText);
} pText = TTF_RenderUNICODE_Blended(font, (const Uint16 *)wszChinese, RGB_Red);
if (NULL != pText)
{
ApplySurface(, , pText, pScreen);
SDL_FreeSurface(pText);
} /* 将缓冲在界面显示出来 */
SDL_Flip(pScreen); /* 事件处理 */
int quit = ;
while (!quit)
{
if (SDL_PollEvent(&myEvent))
{
if (SDL_QUIT == myEvent.type)
{
quit = ;
}
}
} return ;
}

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
SDL  显示汉字

    					
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905