c++ - How to render text in SDL2? -
i wondering how render text sdl2. found api called sdl_ttf
, tutorials, not work situation.
i'm using sdl_window
, sdl_renderer
, whereas tutorials specific sdl_surface
.
is possible use sdl_ttf
sdl_render/sdl_window
? if so, how?
yep, possible, given have renderer , window plus don't have thoughts on dabbling surfaces might want mind on creating texture, here sample code
ttf_font* sans = ttf_openfont("sans.ttf", 24); //this opens font style , sets size sdl_color white = {255, 255, 255}; // color in rgb format, maxing out give color white, , text's color sdl_surface* surfacemessage = ttf_rendertext_solid(sans, "put text here", white); // ttf_rendertext_solid used on sdl_surface have create surface first sdl_texture* message = sdl_createtexturefromsurface(renderer, surfacemessage); //now can convert texture sdl_rect message_rect; //create rect message_rect.x = 0; //controls rect's x coordinate message_rect.y = 0; // controls rect's y coordinte message_rect.w = 100; // controls width of rect message_rect.h = 100; // controls height of rect //mind (0,0) on top left of window/screen, think rect text's box, way simple understance //now since it's texture, have put rendercopy in game loop area, area whole code executes sdl_rendercopy(renderer, message, null, &message_rect); //you put renderer's name first, message, crop size(you can ignore if don't want dabble cropping), , rect size , coordinate of texture //don't forget free surface , texture
i tried explain code line line, don't see window right there since assumed knew how initialize renderer give me idea know how initialize window, need idea on how initialize texture.
minor questions here, did window open? colored black? if thoughts right, if not, can ask me , change code implement whole section consists of renderer , window.
Comments
Post a Comment