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风格字符串,就可以完成绘制。很简单,无须再多的介绍,这里直接给上样例源码:

 1 #include <SDL2/SDL.h>

 2 #include <SDL2/SDL_test.h>

 3 

 4 SDL_Renderer *WindowRen=NULL;

 5 SDL_Window *Windows=NULL;

 6 

 7 int main(int argc,char* argv[]){

 8     SDL_Init(SDL_INIT_AUDIO);

 9     Windows=SDL_CreateWindow("TestString",100,100,650,650,SDL_WINDOW_SHOWN);

10     WindowRen=SDL_CreateRenderer(Windows,-1,0);

11     SDL_SetRenderDrawColor(WindowRen,255,0,0,255);//Set the color of the string

12     SDLTest_DrawString(WindowRen,100,100,"HelloWorld!");//Draw a string on (100,100)

13     SDL_RenderPresent(WindowRen);

14     while (1){

15         SDL_PumpEvents();

16         SDL_RenderPresent(WindowRen);

17     }

18     return 0;

19 }

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

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

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

你可能感兴趣的:(test)