Computer Science Canada

[Tutorial]Font.Drawing strings,variables w/wo image

Author:  .hack [ Sun Jun 06, 2004 5:48 pm ]
Post subject:  [Tutorial]Font.Drawing strings,variables w/wo image

I am making an RPG game for my ISU and I found this a little sneaky but very useful. While this tutorial isn't amazing, huge or advanced it is something is useful, so I find posting this useful. Alot of people use font.draw to draw text because it looks sexy. However in games such as RPG games where character / Enemy names are quite frequent (If you make one with text and fancy battles etc) YOu will probably want the font.draw to draw Variables, but it won't, because Turing sucks.

So this tutorial covers how to Font.Draw a variable with the characters image if you want to import one from GUI and If you don't want to show an image then I show you how to font.draw a variable and text without one. I'm a pretty bad teacher and usually type more the I need, try to filter out whatever doesn't seem to make sense and read it over a bit Razz.



There is a way around it however, allow me to enlighten. ALl these examples are extracted from my game, so the text that it is to typoe may seem odd Razz.

First, when your font.drawing its generally.


var font1 := Font.New ("Times:12")
%Declares the Font as Times New Roman, Size12

Font.Draw ("War Cry",230, 390, font1, red)

%Font.Draw calling the procedure, "War Cry" being the text to output, 230 being the x coord, and 390 being the y coord to locate. Font1 being the font you specified when you declare it, and Red being the output color.


Now, say you have a character name being "leet", assigned to the variable charname.

One would think you could simply go:

Font.Draw ("Hellp ",charname,239,390,font1,red)

Unfortunately the syntax is messed up, notice how charname fills up a spot reserved for the y coord, and there is now to many parameters. Here is how we get around this.

Lets pretenmd we have nothing Declared so I can simply go through the code for a better unsterstanding

First we must declare our font.

var Font1 : Font.New ("Times : 12" )
%Declares Times New Roman, Size 12 as a usable system font to import.

next we define the character name.

var charname : string
charname := "leet"

%Sets charname as leet

Now is the tricky part, this the meat and podadoes.
Now that we have our font and variable set, we have to work around the too many parametees error.


First we create an array, that will hold all of the strings of text that you want to font.draw. Here is a small batch from my game:

var textstring : array 1 .. 20 of string
textstring (1) := "I'm " + charname + " are you hurt?"
textstring (2) := "I'm " + charname + " who are you?"
textstring (3) := "My name is " + charname + " how may I serve you?"
textstring (4) := "Sir, I am " + charname + " you had called for me?"
textstring (5) := charname + ":"


I named my array textstring 1, because we are dealing witht he strings of text we want to font.draw. Lets look at textstring(5), it being the most basic. In most RPGs when a character is speaking it will show the characters name, and a colon and after that is what he is saying, the same deal applies here. Textstring (5) will output, "leet:". looking at textstring (1) it will output "I'm leet are you hurt?. Etc.

Notice the Plus(+) sign inbetween the strings of text, unlike the normal put statements that use commas(,), these use +. So lets examine textstring(1) again. This line is basically saying that textstring (1) is equal to "I'm" + charname(leet) + " how are you". Understand, if we were to type put textstring(1) in our compilers and run it, it would simply out put "I'm leet are you hurt?"

SO after you have setup the textstrings you wish to use, we can move onto drawing it with font.draw. Lets continue.

[IF YOU WANT TO FONT.DRAW A STRING OF TEXT WITH AN IMAGE, SUCH AS A CHARACTER IMAGE READ BELOW. if you only want to font.draw a variable or textstring without an image, skip down to the Green % signs and read below that.

Second we need to create a PROCEDURE, I would prefereably name it what its going to say. If we wanted to draw "Hello" then the character name, I would name it Hellocharacter. You may need to create many of these to cover anything that may be said which includes your character name. Or you can simply create a single procedure that just types charname, and locate it to wherever it needs to be inserted. The choice is yours. Ok Here we go!

procedure Areyouhurt
Pic.Draw (Appp7, 0, tby, picMerge)
Font.Draw (textstring (1), 115, ty, font1, black)
end Areyouhurt


now let me explain, as my procedure will differ from yours. I'm using a pic.draw to import the characters face and place it. If you are not going to have an image of a characters face, then completely forget taht line is there and focus on the font.draw line below it.

As you can see now, in the procedure will call the character picture(again forget this if you are not using it), and it will call textstring(1),write it to the given coords with the proper font and color. Now, remember that my textstring (1) was "I'm" + charname + "Are You Hurt". Calling the procedure will Font.Draw that, in its proper font, so it is possibl to font.draw variables. In this case the output would be "I'm leet are you hurt?" in Times font, size 12 black. So when you want to draw the specific textstring you can call the procedure you made above. But wait, theres more. Notice how my X and Y coords are 115 and ty. This is because when I am drawing into my text boxes for my game, I want the text to be shown 115 spaces away from the left wall, that is the X coordinate. However, how does my program know where to draw that line in my text box. should it draw on the first line of my textbox, the second or third? Is it going to be on my top textbox or the textbox at the bottom of my screen? I simply declared a varaible ty as an int (ty = text Y). and before I call te procedure to type the textstring out, I set the ty value to where it fits.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
If you are not showing an image with your font.draw then simply do the following:

Font.Draw (textstring (1), 115, ty, font1, black)

You don't even need a procedure in this case, just the array for textstring.


I know this is a long tutorial for something simple, enjoy guys! I would suggest reading the whole thing, even if you only needed this last part about how to font.draw text or variables. The sections above it offer a greater insight which may allow you to understand more about how the font.draw works with the procedure, as it is not throroughly explained for you Green section guys.

Anywas, Enjoy, as I said abhove, I'm a crappy teach so if you have any qeustions feel absouletely free to e-mail or IM me.

E-mail/MSN - Ph34revolved@hotmail.com
AIM - Shadow MosesIV
[/i]

Author:  Delos [ Sun Jun 06, 2004 9:33 pm ]
Post subject: 

Dude...Turing has been known to suck...yes, we all agree...but seriously...

If you're going to bash it, at least make sure your bashings are founded!!!

Quote:

YOu will probably want the font.draw to draw Variables, but it won't, because Turing sucks.

Yes it does. Rather well.

code:

var font : int
var text : string := "Meh!"

font := Font.New ("Times New Roman:12")

Font.Draw (text, 100, 100, font, 7)


*************

Quote:

Now, say you have a character name being "leet", assigned to the variable charname.

One would think you could simply go:

Font.Draw ("Hellp ",charname,239,390,font1,red)

Unfortunately the syntax is messed up, notice how charname fills up a spot reserved for the y coord, and there is now to many parameters


Well, you don't say the 'y coord' is filled up...

code:

var font : int
var text : string := "Meh!"
var text2 : string := "MehAgain!"

font := Font.New ("Times New Roman:12")

Font.Draw (text + " " + text2, 100, 100, font, 7)


Ever heard of '+'?
Seriously...you're joking right? You DID realise that by putting commas you were switching to the next parameter in the syntax, rite? And that you needed to use '+' to extend the current parameter, if possible?

Dude...I don't mean to be overly mean or rude...but come on... Thinking .

Author:  .hack [ Sun Jun 06, 2004 10:25 pm ]
Post subject: 

Wow, good call..

Yea, so consider this thread deleted pending I can delete my own threads. I never actually thought of using a + in the font.draw line.

Doh.

Author:  this_guy [ Mon Jun 21, 2004 3:34 pm ]
Post subject: 

Delos wrote:

Ever heard of '+'?
Seriously...you're joking right? You DID realise that by putting commas you were switching to the next parameter in the syntax, rite? And that you needed to use '+' to extend the current parameter, if possible?

Dude...I don't mean to be overly mean or rude...but come on... Thinking .


whoa...dont be rude man....
took me a whole semester of learnin turing to find that out!!!
i support you tutorial person!!!

Author:  TechLacky [ Sat Jan 14, 2012 7:53 pm ]
Post subject:  Re: [Tutorial]Font.Drawing strings,variables w/wo image

Ty man! this helped me out alot!

i'm also wondering how to draw an int var tho (if you know how?)

like:

var a : int
var a := 0

a := a+1

so how do i Font.Draw the a?

Author:  evildaddy911 [ Sat Jan 14, 2012 8:18 pm ]
Post subject:  Re: [Tutorial]Font.Drawing strings,variables w/wo image

Font.Draw (intstr(integer), x, y, font, clr)
Btw delos, thanks for mentioning that, i hadnt realised that!

Author:  Heartseed [ Sun Jun 15, 2014 1:43 pm ]
Post subject:  RE:[Tutorial]Font.Drawing strings,variables w/wo image

Could I use this with numbers, im trying to get this to work in my game so far it hasnt worked.

Author:  Dreadnought [ Sun Jun 15, 2014 2:23 pm ]
Post subject:  Re: [Tutorial]Font.Drawing strings,variables w/wo image

Heartseed wrote:

Could I use this with numbers, im trying to get this to work in my game so far it hasnt worked.

Look up intstr or realstr if you need to convert integers or real numbers to strings (since Font.Draw takes a string as its argument).

If that doesn't work for you make a new thread in the help section and show us what you've tried. Smile


: