Author Topic: DrawText() auto-scaling  (Read 1306 times)

0 Members and 2 Guests are viewing this topic.

Offline CAATINGA

  • Major(1)
  • Posts: 5
  • www.soldat.com.br
    • SOLDAT BRASIL
DrawText() auto-scaling
« on: June 08, 2007, 02:40:22 pm »
Well, I just started scripting for Soldat's Dedicated Server 2.6.1 a few days ago. I am not a Pascal programmer, so I am still getting used to it as I learn how things are done on Dedicated Server scripts.

This topic is about the second script I've made and the goal is to do a procedure that receives a string and draws it on the screen with DrawText() fitting "perfectly" just as the default messages from Soldat as "You killed player" or "Killed by player". In this procedure, differently from DrawText() you don't need to specify any coordinates or scale. Just give it the string, and my procedure should do the rest.

At the time, I think it is working a bit fine for sentences with non-capital letters. I will try to explain my code, and I expect you to help me improving it, making it possible to draw sentences with numbers and capital-letters too.

Code: [Select]
{This is where I call my procedure for testing purposes...}

procedure OnPlayerSpeak(ID: byte; Text: string);
begin

DrawOnPlayersScreen(ID,Text);

end;

Code: [Select]
{and here is the code of the procedure I am talking about...}
var
scale: single;
patternLength: integer;
argumentLength: integer;

procedure DrawOnPlayersScreen(ID: byte; Text: string);

begin
patternLength := Length('caatinga');
argumentLength := Length(Text);
scale := patternLength / argumentLength;
scale := scale * 0.4;
//0.4 is the scale that works (fits perfectly) for the string caatinga

DrawText(ID,Text,330,RGB(255,0,0),scale,40,380);


end;

OK, here's my idea:
Firstly I defined the coordinates to be static (40,380) on a position close to the one where "You killed someone" is drawn (it doesn't works too well with small words - it gets too bigger and stays out of the screen, but it works fine with medium length sentences. Later I'll try to make the Y coordinate dynamic too).

Then I've took a medium word with non-capital letters (caatinga) and tested with the DrawText() procedure until I found wich was the scale that fits fine on the coordinates I've specified. I've found 0.4 .

Now, based on my pattern word (caatinga) and the scale 0.4, I can find an aproximate scale for any other known word or sentence just using proportions concepts.
http://en.wikipedia.org/wiki/Proportionality_(mathematics)

The bigger the length, smaller the scale. So, Scale and Length of the word are inversely proportional.

Scale = K * (1/Length)
Scale * Length = K (constant)

What means that the product of Scale and Length should be always the same if it fits correctly.

So, patternScale * patternLength = argumentScale * argumentLength
the only value I don't have is the argumentScale so I'm going to isolate it.

patternScale * patternLength / argumentLength = argumentScale

So this is what I do on my code.
patternScale is 0.4 (got by testing the word caatinga on DrawText()),
patternLength is the Length of the word caatinga,
and argumentLength is the Length of the word I am trying to find the scale...

Finally I draw the sentence:

DrawText(ID,Text,330,RGB(255,0,0),argumentScale,40,380);

Well I think it I've written a lot for such a small code, I hope that you understood and thanks a lot for who read it all... thanks for your attention too.

I'm waiting anxiously for your replies =)


Grateful,
CAATINGA
« Last Edit: June 08, 2007, 03:28:13 pm by CAATINGA »

UN| CAATINGA |UN
Bando de gringo quica all f12

Offline cynicle

  • Major(1)
  • Posts: 37
Re: DrawText() auto-scaling
« Reply #1 on: June 09, 2007, 09:08:08 pm »
Impressive 2nd script mate...
Join the Army. Visit strange and exotic places. Meet fascinating people. And kill them.

Aus Soldat League 203.208.70.216:23073

Offline spkka

  • Camper
  • ***
  • Posts: 469
Re: DrawText() auto-scaling
« Reply #2 on: June 09, 2007, 09:24:28 pm »
didnĀ“t test it but you explained it all nicely i think. Clear instruction ;)

Offline deguix

  • Major
  • *
  • Posts: 79
Re: DrawText() auto-scaling
« Reply #3 on: June 11, 2007, 01:42:14 am »
This is surely like asking for help/discussion... or this is rather a complete script with descriptions fitting in the "Scripting Releases" forum, without being in the release template.