Only draw N characters of a string?
jmasterx

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
type568
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
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
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.

jmasterx
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
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

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

strncpy?

Thomas Fjellstrom

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

LennyLen
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
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..

Thread #606814. Printed from Allegro.cc