Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Only draw N characters of a string?

This thread is locked; no one can reply to it. rss feed Print
Only draw N characters of a string?
jmasterx
Member #11,410
October 2009

I was wondering if A5 had a way to draw only N chars of a string rather than keep going until it hits NULL?

Thanks

SiegeLord
Member #7,827
October 2006
avatar

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

type568
Member #8,381
March 2007
avatar

jmasterx said:

I was wondering if A5 had a way to draw only N chars of a string rather than keep going until it hits NULL?

You could just warp the allegro write routine in to something like this:

//The function assumes valid input.
void myWrite(char* str,int n){
    char ch=str[n+1];

    str[n+1]='\0';
    /*Allegro write routine here*/
    str[n+1]=ch;
}

jmasterx
Member #11,410
October 2009

type568 said:

You could just warp the allegro write routine in to something like this:

I thought of that, but I was hoping to avoid doing memory ops, and also for Unicode, I was hoping to avoid doing a BringToNextUTF8() function,

I'm working on adding multi color to my text box and unfortunately,this means I must draw character by character: like this:

#SelectExpand
1 int curLen = 0; 2 int curWidth = 0; 3 int totalWidth = 0; 4 size_t uniPos = 0; 5 int bytesSkipped = 0; 6 int atChar = 0; 7 for(int i = linesSkipped; i <= maxitems + linesSkipped; ++i) 8 { 9 if(i >= (int)textRows.size()) 10 { 11 break; 12 } 13 atChar = 0; 14 uniPos = 0; 15 bytesSkipped = 0; 16 totalWidth = 0; 17 18 19 curLen = _unicodeFunctions.bringToNextUnichar(uniPos,textRows[i]); 20 while(curLen > 0) 21 { 22 curStr = textRows[i].substr(bytesSkipped,curLen); 23 curWidth = getFont().getTextWidth(curStr); 24 bytesSkipped += curLen; 25 atChar++; 26 27 paintargs.graphics()->drawText(AguiPoint(textX + totalWidth, 28 textY + (i * getLineHeight())), 29 curStr.c_str(),AguiColor((curLen + curWidth) * 10,(atChar + curLen) * 5,i * 5),getFont()); 30 31 totalWidth += curWidth; 32 33 curLen = _unicodeFunctions.bringToNextUnichar(uniPos,textRows[i]); 34 } 35 36 }

Which ends up using about 7% more cpu than drawing text line by line :(

SiegeLord
Member #7,827
October 2006
avatar

type568 said:

You could just warp the allegro write routine in to something like this

There's no point, since internally allegro does what I said in my post.

void al_draw_text(const ALLEGRO_FONT *font,
   ALLEGRO_COLOR color, float x, float y, int flags,
   char const *text) 
{
   ALLEGRO_USTR_INFO info;
   ASSERT(text);
   al_draw_ustr(font, color, x, y, flags, al_ref_cstr(&info, text));
}

You'll make that str array using n and then allegro will use strlen to figure out what n must have been. You can just avoid all that by using al_ref_buffer and al_draw_ustr directly.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

jmasterx
Member #11,410
October 2009

SiegeLord said:

You'll make that str array using n and then allegro will use strlen to figure out what n must have been. You can just avoid all that by using al_ref_buffer and al_draw_ustr directly.

The complication is, I cannot use al functions because Allegro is wrapped into a backend of my GUI API. But under normal circumstances I would do as you said.

Peter Wang
Member #23
April 2000

jmasterx said:

I'm working on adding multi color to my text box and unfortunately,this means I must draw character by character: like this:

In most cases you will have long spans of the same colour, so you don't need to draw character by character.

jmasterx said:

I cannot use al functions because Allegro is wrapped into a backend of my GUI API

I don't get it.

jmasterx
Member #11,410
October 2009

I don't get it.

The point of creating a backend independent system is so the paint events of the widgets do not need rewriting when one switches from ex, al5 to sdl, to gl etc. In the same way using HeapAlloc() in an Allegro program would be silly and break cross platform.

also, what does one do in the event that every character has a different color (worst case)

cgman24
Member #12,536
February 2011
avatar

strncpy?

Thomas Fjellstrom
Member #476
June 2000
avatar

So use those al_ functions in your backend. Like you're doing with all the rest of the al_ functions.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

LennyLen
Member #5,313
December 2004
avatar

jmasterx said:

I was wondering if A5 had a way to draw only N chars of a string rather than keep going until it hits NULL?

The complication is, I cannot use al functions

So you want to know if A5 has a way of doing something, but you don't want to use A5 functions?

type568
Member #8,381
March 2007
avatar

jmasterx said:

Which ends up using about 7% more cpu than drawing text line by line

You care? :)

However, in my writing routines I used a putpixel :-/

Append:
Unless too much output it isn't an issue, and it adds great flexibility.
Although probably it was just cos' I'm not that familiar with the library itself, and because I love the basics overall..

Go to: