grimfang4 / SDL_FontCache

A generic font caching C library with loading and rendering support for SDL.
MIT License
305 stars 66 forks source link

Get Size of Text #20

Open EyesightTechnology opened 6 years ago

EyesightTechnology commented 6 years ago

Before I started using this library, I was using SDL_QueryTexture() to get the size of the text I was loading. This way, I could adapt my other positions to different sizes of text. For example, Load TextB 100 pixels away from the bottom of TextA, rather than having a fixed position for loading.

Is there any way of getting the size of some text with this library, so I can shift my screen around accordingly??? Thanks!

P.S. Is there any way of wrapping text once it gets to a certain length?

grimfang4 commented 6 years ago

Yep! Use FC_GetWidth() to figure out the width of some text. Use FC_DrawBox() or FC_DrawColumn() to wrap text. There are some examples of this stuff in the test program: https://github.com/grimfang4/SDL_FontCache/blob/master/test/main.c

EyesightTechnology commented 6 years ago

The get_width function works great! Thanks! I'm still quite confused about the DrawBox function. (And using it for line wrapping) Can you please give me an example of how I would line wrap text with this function? Thanks!

grimfang4 commented 6 years ago

FC_DrawColumn() does wrapping to a given width: FC_DrawColumn(font, screen, x, y, max_width, "Some wrapped text");

FC_DrawBox() wraps the text and clips it if it goes outside of the given rect (e.g. by being too many lines tall):

SDL_Rect box = {x, y, max_width, max_height};
FC_DrawBox(font, screen, box, "Some wrapped text");
EyesightTechnology commented 6 years ago

I tried using the column method, and got some very odd results. I will get back to you on that if it remains an issue. As for the box method, when the text hits the end of the box it is not wrapped, simply cut off. I want it to wrap, as in start a new line. How do I make it do this?

EyesightTechnology commented 6 years ago

I just checked, and have confirmed there is no other function for wrapping. So how do I use the drawbox function to wrap, not clip?

grimfang4 commented 6 years ago

Those functions (naively) assume spaces in the text so it can wrap on word boundaries. It won't wrap otherwise. Is that what you're seeing? If it isn't wrapping text that overflows horizontally and you have spaces in your text, then that's a bug.

EyesightTechnology commented 6 years ago

The text goes out to a certain point, then it is stopped. It does not wrap to a new line- it just stops. I noticed something VERY strange as well. Some of (most of) my text actually does wrap if it is extended far enough. However, the text that I actually need to wrap does not wrap. The only difference between the not-wrapping text and the wrapping text is that the non-wrapping text is a bold font. Could this be the issue???