1

I'm trying to draw/highlight text in SDL2, there is the simple SDL_Surface *surf = TTF_RenderText_Blended(font, text, size); and then SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);, and then query the texture, and then draw the text, but this text adapts to kerning and is not all the same size (widths and heights). In doing so, I can't figure out how to do highlighting due to the changing size in characters. How can I draw/render text in monospace such that each character is the same width (for example, default codeblocks text editor spaces text in monospace)?

404 TNF
  • 19
  • 6
  • The easiest option is to use a monospace font. – HolyBlackCat Nov 07 '20 at 08:38
  • Yes, I just mean something like arial.ttf for example – 404 TNF Nov 07 '20 at 08:40
  • Then, render individual glyphs (letters) and space them as you want. – HolyBlackCat Nov 07 '20 at 08:41
  • I tried this, but to draw a bunch of text I have to iterate through the lengths of text and it runs far slower. Is there no way to use a font like ```arial.ttf``` and create a monospace texture of text? – 404 TNF Nov 07 '20 at 08:43
  • I don't get it, why iterate through text lengths? Render all the glyphs you need to surfaces, then draw the surfaces with the spacing you want. This doesn't require querying text length. Or, if you want to, draw glyphs from those surfaces to a single surface first. – HolyBlackCat Nov 07 '20 at 08:58
  • If im doing them by individual letters, when I call SDL_RenderCopy(), i need to draw a character. So by drawing a word, say "Text", that word will now require 4 iterations of drawing, rather than 1 if it were a texture – 404 TNF Nov 07 '20 at 09:56
  • Yep. If that's too slow, draw them to a single large surface first, then draw that surface – HolyBlackCat Nov 07 '20 at 10:06
  • ohhhh. You can do that?? How? – 404 TNF Nov 07 '20 at 10:09
  • Use https://wiki.libsdl.org/SDL_BlitSurface – HolyBlackCat Nov 07 '20 at 10:11

0 Answers0