Search code examples
c++user-interfacetextboxsdlfreetype

Correct way to render cursor in custom text area?


I am using SDL and libfreetype and constructing a very basic GUI, I'm implementing a textBox, but I haven't been able to figure out how I'm supposed to generate the standard blinking cursor part. It doesn't seem to be exactly the same as a | character. And moreover if I draw it as a | character that changes the text width.

What's the canonically correct way to render text in a textbox with a cursor?


Solution

  • The easiest way is to just draw a line primitive, this gives you a lot more control over the spacing, length and width of the caret.

    And if you want to keep it as a text character in your font system, you can do a render-to-texture and copy it out, or do a simple memory blit onto your font atlas (so you can can keep pipe character separate, an use a control char like 0x01 for the caret).