![]() |
|
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 Agui GUI API -> https://github.com/jmasterx/Agui |
SiegeLord
Member #7,827
October 2006
![]() |
al_ref_buffer + al_draw_ustr perhaps. "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
type568
Member #8,381
March 2007
![]() |
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: 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 Agui GUI API -> https://github.com/jmasterx/Agui |
SiegeLord
Member #7,827
October 2006
![]() |
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 |
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. Agui GUI API -> https://github.com/jmasterx/Agui |
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
|
Peter Wang said: 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) Agui GUI API -> https://github.com/jmasterx/Agui |
cgman24
Member #12,536
February 2011
![]() |
strncpy?
|
Thomas Fjellstrom
Member #476
June 2000
![]() |
So use those al_ functions in your backend. Like you're doing with all the rest of the al_ functions. -- |
LennyLen
Member #5,313
December 2004
![]() |
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
![]() |
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:
|
|