There are many extensions to the SDL library, which is convenient. But each expansion library is bloated, typically dragging an additional two or three open source libraries, and, even more, the size of the extension library is much larger than the SDL library itself. But there is a self-bringing, very useful library that is easily overlooked by everyone. It is the Sdl_test library that this article is going to talk about. This library can draw strings on a window when the TTF library is not loaded.
The function is called sdltest_drawstring, and the following is an introduction to the function of E and how to use it:
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.
In short, a renderer, coordinate, and C-style string are passed in to draw. Very simple, no more introduction, here directly to the sample source code:
1#include <SDL2/SDL.h>2#include <SDL2/SDL_test.h>3 4Sdl_renderer *windowren=NULL;5Sdl_window *windows=NULL;6 7 intMainintargcChar*argv[]) {8 Sdl_init (sdl_init_audio);9Windows=sdl_createwindow ("teststring", -, -,650,650, Sdl_window_shown);TenWindowren=sdl_createrenderer (windows,-1,0); OneSdl_setrenderdrawcolor (Windowren,255,0,0,255);//Set The color of the string ASdltest_drawstring (Windowren, -, -,"helloworld!");//Draw A string on (100,100) - sdl_renderpresent (windowren); - while(1){ the sdl_pumpevents (); - sdl_renderpresent (windowren); - } - return 0; +}
It is also important to note that when connecting, the Sdl2_test library is placed behind the SDL2 library because the former relies on the function that the latter leads to.
Sdl_test Library (1)--sdl draw text without TTF library