Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (Ayumi), 662 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
str_width improvements #241666
12/18/08 01:12
12/18/08 01:12
Joined: Mar 2002
Posts: 1,774
Magdeburg
F
FlorianP Offline OP
Serious User
FlorianP  Offline OP
Serious User
F

Joined: Mar 2002
Posts: 1,774
Magdeburg
It would be really helpful to have the following functions in addition to str_width

foo(string, font, len)
Gives the width of the first [len] chars of string

and

foo(string, font, width)
Gives the number of chars that fit in [width] pixels

I know that this can be done quite easy with str_width but i think an intern solution would be much faster


I <3 LINQ
Re: str_width improvements [Re: FlorianP] #241720
12/18/08 12:22
12/18/08 12:22
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
Code:
int str_widthpart( STRING* str, FONT* font, int len ) {
	STRING* temp="";
	str_cpy(temp, str);
	str_trunc( temp, str_len(str) - len );
	return str_width( temp, font );
}

int str_fit( STRING* str, FONT* font, int width ) {
	STRING* temp="";
	str_cpy(temp, str);
	
	var length=str_width(st, font);
	while( length > width && str_len( temp ) > 0) {
		str_trunc (temp, 1);
		length = str_width( temp, font );
	}
	
	return str_len( temp );
}

I don't have the option to use str_width, so I can't test. But that should work.

Last edited by Joozey; 12/18/08 12:23.

Click and join the 3dgs irc community!
Room: #3dgs
Re: str_width improvements [Re: Joozey] #241735
12/18/08 13:50
12/18/08 13:50
Joined: Mar 2002
Posts: 1,774
Magdeburg
F
FlorianP Offline OP
Serious User
FlorianP  Offline OP
Serious User
F

Joined: Mar 2002
Posts: 1,774
Magdeburg
Thats not the point.
These functions are terribly slow and both should need less calculations than one str_width command - So it is like going from NY to LA with a stop in Moscow


I <3 LINQ
Re: str_width improvements [Re: FlorianP] #241737
12/18/08 14:04
12/18/08 14:04
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
Well, Joozey posted the functions you wanted. I don't see why you think ours whould be faster.

Re: str_width improvements [Re: jcl] #241781
12/18/08 20:26
12/18/08 20:26
Joined: Mar 2002
Posts: 1,774
Magdeburg
F
FlorianP Offline OP
Serious User
FlorianP  Offline OP
Serious User
F

Joined: Mar 2002
Posts: 1,774
Magdeburg
The way i think str_width works (simplified):

function str_width(string, font)
{
while (i < str_len(string) )
{
width += Char_Size[ string[i] ] //Adding the width of each char of the string
i++
}
return width
}

so an internal solution could do the following

Give the number of chars that fit in [max_width]:
function str_widthanything (string, font, max_width)
{
while (width < max_width && i < str_len(string) )
{
width += Char_Size[ string[i] ] //Adding the width of each char of the string
i++
}
return i - 1
}

Adds the sizes of the first [char_num] chars:
function str_widthanything (string, font, char_num)
{
while ( i < char_num )
{
width += Char_Size[ string[i] ] //Adding the width of each char of the string
i++
}
return width
}


So, if str_width works this way - don't u think it would be senseless calling str_width that many times instead of adding two modified versions of it?
Tell me if im wrong...

Last edited by FlorianP; 12/18/08 20:28.

I <3 LINQ
Re: str_width improvements [Re: FlorianP] #241788
12/18/08 21:22
12/18/08 21:22
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
What you can do to maximize speed; Store the width's of all the characters (e.g. via their ascii value) once at game start, and read the values out when nessecary.


Click and join the 3dgs irc community!
Room: #3dgs
Re: str_width improvements [Re: Joozey] #241790
12/18/08 21:37
12/18/08 21:37
Joined: Mar 2002
Posts: 1,774
Magdeburg
F
FlorianP Offline OP
Serious User
FlorianP  Offline OP
Serious User
F

Joined: Mar 2002
Posts: 1,774
Magdeburg
Originally Posted By: Joozey
What you can do to maximize speed; Store the width's of all the characters (e.g. via their ascii value) once at game start, and read the values out when nessecary.


Of course - i can also write my own engine. I just thought it to be a useful feature and didnt ask for workarounds.
Please dont get me wrong, i appreciate that you want to help me, but it was just a suggestion to 'improve' a7


I <3 LINQ
Re: str_width improvements [Re: FlorianP] #241827
12/19/08 07:33
12/19/08 07:33
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
There is no defined "character width". The space of a character depends on its environment in a text and involves several parameters and look up tables.

For str_width we're just calling an OS function. We had to use it for any related functions just in the way Joozey suggested.


Moderated by  aztec, Spirit 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1