SDL2.0 写汉字

SDL_ttf 是通过调用 TTF_Init()完成初始化的。如果期间出现错误,该函数会返回 -1。

TTF_Init() 必须在使用任何其他 SDL_ttf 的函数前被调用。

  
  
  1. bool load_files()
  2. {
  3. //加载背景图片
  4. background = load_image( "background.png" );
  5. //打开字体
  6. font = TTF_OpenFont( "lazy.ttf", 28 );
  7. //如果背景加载出错
  8. if( background == NULL )
  9. {
  10. return false;
  11. }
  12. //如果字体加载出错
  13. if( font == NULL )
  14. {
  15. return false;
  16. }
  17. //如果加载正常
  18. return true;
  19. }
这是文件加载函数。要加载 *.ttf 字体,必须使用 TTF_OpenFont()  函数。

该函数的第一个参数是你想要打开的字体文件的文件名,第二个参数是你在打开字体文件时想要设定的字体大小。

如果加载字体时出现错误,TTF_OpenFont() 会返回NULL。


TTF_Init();
		TTF_Font *m_font = TTF_OpenFont("F:\\Project\\SDL_Lesson1\\Debug\\STKAITI.ttf", 200);
		if (!m_font){
			return 0;
		}

		SDL_Color textColor = { 255, 255, 255 };
		wchar_t wcText[1024] = { L"计算机" };
		SDL_Surface *surface_font = TTF_RenderUNICODE_Solid(m_font, (Uint16*)wcText, textColor);


		SDL_Init(SDL_INIT_EVERYTHING);//SDL初始化
		SDL_Window *Screen = SDL_CreateWindow("Title", 100, 100, 640, 480, SDL_WINDOW_RESIZABLE);//创建窗口
		SDL_Renderer *render = SDL_CreateRenderer(Screen, -1, 0);//创建渲染器
		SDL_Surface *bk = IMG_Load("F:\\Project\\SDL_Lesson1\\Debug\\bk.png");//SDL IMAGE扩展库读取tga图片
		SDL_Surface *foo = IMG_Load("F:\\Project\\SDL_Lesson1\\Debug\\foo.png");//SDL IMAGE扩展库读取tga图片

		//Use this function to map an RGB triple to an opaque pixel value for a given pixel format
		//format:an SDL_PixelFormat structure describing the format of the pixel
		Uint32 colorkey = SDL_MapRGB(foo->format, 0x00, 0xff, 0xff);//用画图工具提取foo.png的背景颜色,发现foo.png的背景色是0x00ffff


		//surface:the SDL_Surface structure to update
		//flag:SDL_TRUE to enable color key, SDL_FALSE to disable color key
		//key:the transparent pixel
		//Returns 0 on success or a negative error code on failure; call SDL_GetError() for more information.
		SDL_SetColorKey(foo, 1, colorkey);//Use this function to set the color key (transparent pixel) in a surface.

		SDL_Texture *texture = SDL_CreateTextureFromSurface(render, bk);//创建纹理
		SDL_Texture *texture1 = SDL_CreateTextureFromSurface(render, foo);//创建纹理
		SDL_Texture *texture_font = SDL_CreateTextureFromSurface(render, surface_font);

		SDL_RenderClear(render);
		SDL_RenderCopy(render, texture, NULL, NULL);//拷贝背景图

		SDL_Rect rect;//小人在哪里显示
		rect.x = 50;
		rect.y = 125;
		rect.w = foo->w;
		rect.h = foo->h;
		SDL_RenderCopy(render, texture1, NULL, &rect);//拷贝小人图片

		SDL_Rect rect_Font;//字体在哪里输出
		rect_Font.x = 200;
		rect_Font.y = 100;
		rect_Font.w = 100;
		rect_Font.h = 100;
		SDL_RenderCopy(render, texture_font, NULL, &rect_Font);//拷贝字体数据

		SDL_RenderPresent(render);

		/*SDL_Event event;
		while (1){
			SDL_PollEvent(&event);
			if (event.type == SDL_QUIT){
				break;
			}
		}*/

		
		SDL_FreeSurface(bk);//是否图片资源
		SDL_FreeSurface(foo);//是否图片资源
		SDL_FreeSurface(surface_font);//释放字体
		SDL_DestroyTexture(texture);//释放纹理
		SDL_DestroyTexture(texture1);//释放纹理
		SDL_DestroyTexture(texture_font);//释放纹理
		SDL_DestroyRenderer(render);//释放渲染器
		SDL_DestroyWindow(Screen);//销毁窗口
		TTF_CloseFont(m_font);//释放字体资源
		TTF_Quit();
		SDL_Quit();//退出



demo:http://download.csdn.net/detail/sz76211822/9878392

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值