After printing the picture, it's time to print the text. This refers to the SDL Font extension library, sdl2_ttf.lib, which needs to include the corresponding header file.
Environment: SDL2 + vc++2015
The following code prints a paragraph of text in the window and responds to the corresponding action.
This time, the error has been streamlined and the code looks fresher.
1#include <iostream>2#include"SDL.h"3#include"sdl_ttf.h"4 5 intMainintargcChar**argv)6 {7 //Screen Width8 Const intScreen_width =680;9 Const intScreen_height = -;Ten //Initialize SDL One Sdl_init (sdl_init_everything); A //Initializing font Libraries - ttf_init (); - //Create Window theSdl_window *window = Sdl_createwindow ("Font Test", - sdl_windowpos_centered, sdl_windowpos_centered, - screen_width, Screen_height, sdl_window_shown); - //Create renderer +Sdl_renderer *renderer = sdl_createrenderer (window,-1, -sdl_renderer_accelerated |sdl_renderer_presentvsync); + A //Open Font atTtf_font *font = Ttf_openfont ("XHEI.TTC", -); - //Text Turn surface -Sdl_color Color = {255,255,255 }; -Sdl_surface *surf = ttf_rendertext_blended (font,"Cool, Cooler, coolest!", color); - //Surface Turn Texture -Sdl_texture *text =Sdl_createtexturefromsurface (renderer, surf); in //Freeing Resources - sdl_freesurface (SURF); to Ttf_closefont (font); + - BOOLQuit =false; the sdl_event E; * //Main Loop $ while(!quit)Panax Notoginseng { - //Event Stack Polling the while(Sdl_pollevent (&e)) + { A //Press the X in the upper-right corner or click the mouse to exit the if(E.type = = Sdl_quit | | e.type = = sdl_mousebuttondown) QUIT =true; + //Empty renderer - sdl_renderclear (renderer); $ //Rendering Fonts $ sdl_rendercopy (renderer, text, NULL, NULL); - //Render Renderer - sdl_renderpresent (renderer); the } - }Wuyi the //Freeing Resources - sdl_destroytexture (text); Wu sdl_destroyrenderer (renderer); - Sdl_destroywindow (window); About $ sdl_quit (); - - return 0; -}
Print text on the screen with SDL2