Print text on the screen with SDL2

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.