SDL显示中文(一个函数而已)
来源:互联网 发布:js复选框全选 编辑:程序博客网 时间:2024/06/06 08:37
其实让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;
}
效果如下:
- SDL显示中文(一个函数而已)
- SDL入门教程(十):5、SDL完美显示中文
- SDL入门教程(十):5、SDL完美显示中文
- SDL 显示中文的方法
- SDL入门教程(十):2、直接通过Unicode让SDL显示中文
- SDL入门教程(十):2、直接通过Unicode让SDL显示中文
- 一个最简单的sdl显示图片例子sdl example sdl 例子 例程SDL贴图bmp
- SDL在ubuntu终端显示中文
- SDL游戏之路(十三)--中文显示
- SDL显示
- SDL 功能函数(一)
- SDL & Object Pascal (Delphi) [2] 显示中文字符
- 仅仅是一个测试而已!
- 用SDL显示一个图片(图片是MM哦)
- 一个SDL的例子分析(一)
- SDL事件查询函数:SDL_PeepEvents()
- SDL中文教程
- SDL GUID 中文译本
- 负载均衡技术之4算法
- 由冰激凌事件联想分布式系统
- Centos 6.5 安装 Scrapy 0.22.2成功
- leetcode做题总结,题目Remove Nth Node From End of List 2012/01/27
- 纯虚函数与抽象类
- SDL显示中文(一个函数而已)
- C语言 gets()和scanf()函数的区别
- leetcode做题总结,题目Combinations 2012/04/18
- 请用fontAwesome代替网页icon小图标
- 不要62 2089
- Linux驱动开发二:外部中断分析
- 因添加两个webservice引用时提示:{"无法加载协定为“sms.WebServiceSoap”的终结点配置部分,找到了该协定的多个终结点配置。请按名称指示首选的终结点配置部分。"}
- 不成熟的六个表现
- Microsoft SQL Server 2008 基本安装说明