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
3 registered members (vicknick, dr_panther, VoroneTZ), 1,255 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Autosizing string #413245
12/10/12 19:25
12/10/12 19:25
Joined: Apr 2011
Posts: 75
Malta
E
Ercoles Offline OP
Junior Member
Ercoles  Offline OP
Junior Member
E

Joined: Apr 2011
Posts: 75
Malta
Can someone suggest a routine where if a string is greater than a certain length it is made to become multiline by inserting \n at certain instances of the string.

This way a string would never get out of the allocated text view but would be truncated on to the next line if it is longer that the text view.

Last edited by Ercoles; 12/10/12 19:26.
Re: Autosizing string [Re: Ercoles] #413299
12/11/12 17:19
12/11/12 17:19
Joined: Apr 2011
Posts: 75
Malta
E
Ercoles Offline OP
Junior Member
Ercoles  Offline OP
Junior Member
E

Joined: Apr 2011
Posts: 75
Malta
I am trying to do this but I stumbled with a strange bug can anyone shed light?

#include <acknex.h>
#include <default.c>
#include <strio.c>

STRING* tester="This is a test to check something.";


function getword(STRING* kelma)
{ int i;
int ii;
i=str_len(kelma);
ii=str_stri(kelma," ");
if(ii==0){return(kelma);}
str_trunc(kelma,i-ii);
return(kelma);
}
function removeword(STRING* kelmab)
{
int i;
i=str_stri(kelmab," ");
if(i==0){str_cpy(kelmab," ");return(kelmab);}
str_clip(kelmab,i);
return(kelmab);
}
function main()
{
STRING* kelma2="";
STRING* kelma3="";
STRING* kelma4="";
STRING* kelma5="";
str_cpy(kelma2,tester);
kelma4=getword(kelma2);
str_cat(kelma4," ");
str_cpy(kelma2,tester);
kelma5=removeword(kelma2);
error(kelma5); //<----------This should give same result
str_cpy(kelma2,kelma5);
error(kelma5); //<-----------As this why contents dissappear here?
while(str_len(kelma2)>0)
{
kelma3=getword(kelma2);
str_cpy(kelma2,kelma5);
kelma5=removeword(kelma2);
str_cpy(kelma2,kelma5);
str_cat(kelma4,kelma3);
str_cat(kelma4," ");
}
error(kelma4);
}

Last edited by Ercoles; 12/11/12 17:21.
Re: Autosizing string [Re: Ercoles] #413315
12/12/12 01:25
12/12/12 01:25
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
I did not check your code but are you aware of the feature/ text flag WWRAP?


"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends
Re: Autosizing string [Re: Superku] #413318
12/12/12 08:01
12/12/12 08:01
Joined: Nov 2012
Posts: 62
Istanbul
T
Talemon Offline
Junior Member
Talemon  Offline
Junior Member
T

Joined: Nov 2012
Posts: 62
Istanbul
Just yesterday, I wrote some code to manually word-wrap my strings so I'll just paste it here. Maybe it'll help you or someone. If it's not clear, just tell me.

P.S: This is written for unicode strings so you may need to change here and there.

First function finds where to split, it's called by the second one. UnT_WordWrap takes a string, splits it where it should be wrapped and fills a pre-allocated string array which it takes as the first argument.

Code:
short    const_null_uchar 			= 0x0;

int UnT_WrapSplit(STRING* arg_str, FONT* arg_font, var arg_width)
{
	if(arg_str == NULL || arg_font == NULL)
	{
		diag("\nUnT_WrapSplit: bad argument");
		return -1;
	}
	if(str_len(arg_str) < 1)
	{
		diag("\nUnT_WrapSplit: str_len(arg_str) < 1");
		return -1;
	}
	
	if(str_width(arg_str, arg_font) < arg_width) return str_len(arg_str);
	var chr = str_chr(arg_str, -str_len(arg_str) + 1, 0x20);
	if(chr == 0) 
	{
		STRING* temp_s = str_createw(&const_null_uchar);
		var temp_w = 0;
		chr = str_len(arg_str) - 1;
		do
		{
			str_cut(temp_s, arg_str, 0, chr);
			temp_w = str_width(temp_s, arg_font);
			chr -= 1;
		}while(temp_w > arg_width);
		ptr_remove(temp_s);
		return chr;
	}
	STRING* temp_s = str_cut(NULL, arg_str, 0, chr);
	var temp_v = UnT_WrapSplit(temp_s, arg_font, arg_width);
	ptr_remove(temp_s);
	return temp_v;
}

int UnT_WordWrap(STRING** out_str_array, int arg_arrayCapacity, STRING* arg_in_str, FONT* arg_font, var arg_width)
{
	if(out_str_array == NULL || arg_in_str == NULL || arg_font == NULL)
	{
		diag("\nUnT_WordWrap: bad argument");
		return -1;
	}
	
	var loc_str_len = str_len(arg_in_str);
	int loc_idx = 0;
	STRING* loc_temp_str = str_createw(&const_null_uchar);
	STRING* loc_temp_str_rest = str_createw(&const_null_uchar);
	str_cpy(loc_temp_str, arg_in_str);
	
	while(loc_idx < arg_arrayCapacity)
	{
		var end = UnT_WrapSplit(loc_temp_str, arg_font, arg_width);
		if(end < 1) break;
		str_cut(out_str_array[loc_idx], loc_temp_str, 0, end);
		str_trim(out_str_array[loc_idx]);
		str_cut(loc_temp_str_rest, loc_temp_str, end, 0);
		ptr_remove(loc_temp_str);
		loc_temp_str = loc_temp_str_rest;
		loc_idx++;
	}
	return loc_idx;
}


Last edited by Talemon; 12/12/12 08:03.
Re: Autosizing string [Re: Talemon] #413360
12/12/12 17:15
12/12/12 17:15
Joined: Apr 2011
Posts: 75
Malta
E
Ercoles Offline OP
Junior Member
Ercoles  Offline OP
Junior Member
E

Joined: Apr 2011
Posts: 75
Malta
Thanks all of you. In the meantime I had made this routine:

function Stringer(STRING* kelma)
{ var i; var ii; var iii;
STRING* kelma2="";
STRING* kelma3="";
STRING* kelma4="";
iii=str_len(kelma);
kelma2=str_parse(NULL,kelma,1);
str_cat(kelma3,kelma2);
str_cat(kelma3," ");
ii=str_len(kelma3);
while(ii<iii)
{
str_parse(kelma2,kelma,0);
str_cat(kelma3,kelma2);
ii +=(str_len(kelma2)+1);
str_cat(kelma3," ");
i=str_len(kelma3);
if(i>65){str_cat(kelma3,"\n");str_cat(kelma4,kelma3);str_cpy(kelma3,"");}
}
str_cat(kelma4,kelma3);
return(kelma4);
}

I do not need it because the WWRAP method solved my problem but can anyone tell me why the function I wrote above works well only when called the first time and when called the second time it leaves fragments from the previous call string?

Re: Autosizing string [Re: Ercoles] #413361
12/12/12 17:25
12/12/12 17:25
Joined: Oct 2011
Posts: 1,082
Germany
C
Ch40zzC0d3r Offline
Serious User
Ch40zzC0d3r  Offline
Serious User
C

Joined: Oct 2011
Posts: 1,082
Germany
I think STRING* anyname = "" overwrites the old strings in memory.
I had problems with it smirk
str_cpy same problem. I just used str_create every time, and now it works just fine laugh

Re: Autosizing string [Re: Ch40zzC0d3r] #413364
12/12/12 18:33
12/12/12 18:33
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
Code:
STRING* kelma2="";


That's wrong. "" is a char array or a pointer to a char but never a STRING. You can define strings globally this way but not local.


Always learn from history, to be sure you make the same mistakes again...
Re: Autosizing string [Re: Uhrwerk] #413365
12/12/12 19:29
12/12/12 19:29
Joined: Apr 2011
Posts: 75
Malta
E
Ercoles Offline OP
Junior Member
Ercoles  Offline OP
Junior Member
E

Joined: Apr 2011
Posts: 75
Malta
So how should they be defined locally?

Last edited by Ercoles; 12/12/12 19:31.
Re: Autosizing string [Re: Ercoles] #413370
12/12/12 20:30
12/12/12 20:30
Joined: Oct 2011
Posts: 1,082
Germany
C
Ch40zzC0d3r Offline
Serious User
Ch40zzC0d3r  Offline
Serious User
C

Joined: Oct 2011
Posts: 1,082
Germany
As I said via str_create ......

Re: Autosizing string [Re: Ercoles] #413376
12/12/12 21:26
12/12/12 21:26
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
Originally Posted By: Ercoles
So how should they be defined locally?

Not at all. A STRING* is an engine object. Allocation is costly. You should use global or static strings whereever possible. If you have good reason to create a local string with str_create don't forget to free them afterwards, or you'll create a memory leak.


Always learn from history, to be sure you make the same mistakes again...
Page 1 of 2 1 2

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