|
3 registered members (TipmyPip, alx, Martin_HH),
6,129
guests, and 2
spiders. |
|
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: Request: str_height
[Re: Talemon]
#413230
12/10/12 16:06
12/10/12 16:06
|
Joined: Nov 2012
Posts: 62 Istanbul
Talemon
OP
Junior Member
|
OP
Junior Member
Joined: Nov 2012
Posts: 62
Istanbul
|
Well, I realized that my problem was dealing with word-wrapped TEXT strings so I wrote a function to calculate how many lines would a string take given a fixed width. In case anyone needs it, here it is:
int CountLines(STRING* str, FONT* font, var width)
{
if(str == NULL) return 0;
if(str_width(str, font) < width)return 1;
var chr = str_chr(str, -str_len(str), 0x20);
if(chr == 0)
{
chr = str_len(str) - 1;
while(str_width(str_cut(NULL, str, 0, chr), font) > width)
chr -= 1;
}
STRING* rest = str_cut(NULL, str, chr + 1, 0);
return 1 + CountLines(rest, font, width);
}
|
|
|
Re: Request: str_height
[Re: Kartoffel]
#413267
12/11/12 07:33
12/11/12 07:33
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
You might want to convert this into a loop, this doesn't have to be recursive at all.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: Request: str_height
[Re: krial057]
#413277
12/11/12 14:44
12/11/12 14:44
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
Expert
|
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
Just a question: would
#define str_height(str,fnt) fnt->dy * (int)*str->pad
be faster than using it as a seperate function?
Last edited by Kartoffel; 12/11/12 15:41.
POTATO-MAN saves the day! - Random
|
|
|
Re: Request: str_height
[Re: Kartoffel]
#413377
12/12/12 21:43
12/12/12 21:43
|
Joined: Sep 2007
Posts: 101 Luxembourg
krial057
Member
|
Member
Joined: Sep 2007
Posts: 101
Luxembourg
|
Yes, that would be faster. However you have to put everything in Parentheses:
#define str_height(str,fnt) (fnt->dy * (int)*str->pad)
Otherwise code like this would fail, because in this case / is evaluated before *:
printf("%i", 40/str_height(test, arial));
if str_height returns 40, your old macro would return 2 instead of 1. Further more, you get strange compiler errors if something goes wrong in or near the macro and you can't get a function-pointer(but I don't think you will ever need to have to point to the str_height function  ) If you can live with that, I don't see why not to use your macro(with parentheses of course  ) and it should be faster
|
|
|
|