SDL_Test库(1)——SDL不用TTF库绘制文字

  SDL库有很多的扩展,这很方便。但是每个扩展库都很臃肿,一般都会拖上额外的两三个开源库,更有甚者,扩展库的大小比SDL库本身还大得多。但有一个自带的、很有用的库很容易被大家忽视。它就是本文要讲的SDL_Test库。本库可以在不加载TTF库时在窗口上绘制字符串。

  函数名叫SDLTest_DrawString,下面是E文的函数介绍和使用方法:

Draw a string in the currently set font.

* param renderer The renderer to draw on.
* param x The X coordinate of the upper left corner of the string.
* param y The Y coordinate of the upper left corner of the string.
* param s The string to draw.

* returns Returns 0 on success, -1 on failure.

  简而言之,就是传入一个Renderer,坐标与C风格字符串,就可以完成绘制。很简单,无须再多的介绍,这里直接给上样例源码:

 #include <SDL2/SDL.h>
#include <SDL2/SDL_test.h> SDL_Renderer *WindowRen=NULL;
SDL_Window *Windows=NULL; int main(int argc,char* argv[]){
SDL_Init(SDL_INIT_AUDIO);
Windows=SDL_CreateWindow("TestString",,,,,SDL_WINDOW_SHOWN);
WindowRen=SDL_CreateRenderer(Windows,-,);
SDL_SetRenderDrawColor(WindowRen,,,,);//Set the color of the string
SDLTest_DrawString(WindowRen,,,"HelloWorld!");//Draw a string on (100,100)
SDL_RenderPresent(WindowRen);
while (){
SDL_PumpEvents();
SDL_RenderPresent(WindowRen);
}
return ;
}

  另外有一点要注意的是,连接时,SDL2_test库要放在SDL2库的后面,原因是前者依赖后者引出的函数。

另: 本函数在Android(4.2.2)平台上不支持中文绘制,Windows与Linux下待测试。(2015-4-27)

又另:本函数在Mingw4.8.2的PC平台(Win7 SP1)上已测试,不支持中文,ubuntu14.04LTS也一样。

上一篇:LinQ—Lambda表达式


下一篇:c++ 对象内存分配和虚函数