Is there a StrWidth function?

Started by Kweepa, Thu 16/10/2003 00:38:43

Previous topic - Next topic

Kweepa

For example:
int StrWidth(string str, int font_num)
or
int StrWidth(string str)
(which assumes the current display font)

I'm trying to animate a hand that's writing text, but the nib drifts away from the last letter.
Also, if I use AA fonts, the text gets heavier and heavier. To get around that I use RawScreenSave() and RawScreenRestore() every frame, but that's probably quite slow.

I'm contemplating using an array of character widths that I have to manually measure, but I'd rather avoid that - does the game kern TT fonts?

Thanks,
Steve
Still waiting for Purity of the Surf II

Scorpiorus

Quoteint StrWidth(string str, int font_num)
You mean it to return the length in pixels/room-scaled pixels? Then I think there is no one. But what do you need it for (guessing for that scene but what way)? :P

Kweepa

Yes, in pixels.

I want to move the nib of the pen to the currently printed letter.
It would also be useful for formatting the text so I can automatically print a paragraph, and make it easy to edit the text later.

Thanks,
Steve
Still waiting for Purity of the Surf II

Scorpiorus

Quote...but the nib drifts away
Ah, I see. Actually, there is a GetTextExtent() function, but it's a part of AGS plugin API, unfortunately, so you will bind your game to Windows only then. Check it out, anyway (just for reference): AGS plugin API

btw, how do you draw a text? Via RawPrint()?

Kweepa

Yeah, basically it looks like this:

I think I'll nag Chris for the function. Seems a bit crap to lose cross platform compatibility...

function scribble_line(string line, int texty) {
 string part;
 StrCopy(part, line);
 int i = 0;
 while (i < StrLen(part)) {
   StrSetCharAt(part, i, ' ');
   i++;
 }
 // object 0 is the pen, object 1 is the shadow
 int y0 = GetObjectY(0);
 int y1 = GetObjectY(1);
 int ry = 0;
 i = 0;
 RawSaveScreen();
 while (i < StrLen(line)) {
   StrSetCharAt(part, i, StrGetCharAt(line, i));
   SetNormalFont(3);
   RawRestoreScreen();
   RawPrint(120, texty, part);
   SetNormalFont(0);
   int j = 0;
   while (j < 2) {
     ry = ry + Random(4) - 2;
     if (ry > 6) ry = 6;
     if (ry < -3) ry = -3;
     SetObjectPosition(0, GetObjectX(0)+3, y0+ry);
     SetObjectPosition(1, GetObjectX(1)+3, y1+ry);
     Wait(1);
     j++;
   }
   i++;
 }
 SetObjectPosition(0, GetObjectX(0), y0);
 SetObjectPosition(1, GetObjectX(1), y1);
}
Still waiting for Purity of the Surf II

Kweepa

Thanks a lot Scorpy!
I made a DLL with GetTextExtentX() and GetTextExtentY() in it and it works fine.
Cheers!
Steve
Still waiting for Purity of the Surf II

Scorpiorus

#6
Quote from: SteveMcCrea on Thu 16/10/2003 21:02:57I think I'll nag Chris for the function. Seems a bit crap to lose cross platform compatibility...
Yep, besides, the function is already written. so it shouldn't be much work to add it as a script command. :P

Quotefunction scribble_line(string line, int texty)...
Very nice one. :)
As about the "text gets heavier and heavier" problem it's due to how AA works: AGS interpolates the font color with the background one and since there is an old string already drawn (RawDraw is permanent) each loop it processes different colors.

You could try to use an overlay instead:

function scribble_line(string line, int texty) {
int overlay = CreateTextOverlay(0,0, 1, 0 , 0, "");
string part;
StrCopy(part, line);
...
...
while (i < StrLen(line)) {
StrSetCharAt(part, i, StrGetCharAt(line, i));
SetNormalFont(3);
//RawRestoreScreen();
SetTextOverlay(overlay, 120, texty, 200, 3, 14, part);

Does AA work with the text overlays? :P

EDIT:
Quote from: SteveMcCrea on Thu 16/10/2003 22:01:42
Thanks a lot Scorpy!
I made a DLL with GetTextExtentX() and GetTextExtentY() in it and it works fine.
Cheers!
Steve
Not at all, Steve. Glad you've implemented it successfully.

~Cheers

SMF spam blocked by CleanTalk