Game Development Community

Simple T2D-based text writer

by Fenrir Wolf · in Torque Game Builder · 03/09/2005 (1:13 pm) · 10 replies

Well, I knew this moment would come eventually, but I had need for displaying text within the T2D scenegraph itself. While I think this would be better handled with a custom T2D class, I was able to get a script-based version up and running in less than 15 minutes. So, in case you have need of this, I've decided to post it.

This requires the "font1.png" file that is under the T2D/client directory. I haven't tested it, but font2 would probably work just as well.

You will need to make one modification to the engine to get this work: Under engine\console\consoleFunctions.cc, add anywhere (I put mine at the end):

//----------------------------------------------------------------

ConsoleFunctionGroupBegin( DavidStuff, "Functions that David Grace bolted onto here!");
ConsoleFunction(ord, S32, 2, 2, "ord(chartoconvert)") { return argv[1][0] ; }
ConsoleFunctionGroupEnd( DavidStuff )
Why? Because as far as I can, you can't get the ASCII value of a character from within Torquescript itself. So I had to add a simple function that does so.

The script function will return the string object, so you can apply physics to it. It will delete itself after %time milliseconds automatically. Right now, it only handles alphanumerics, but it would be easy to hunt down the values for punctuation and add them.

function createstringobj (%pos, %size, %color, %time, %str)
{
	%strobj = new fxStaticSprite2D() { scenegraph = cbSceneGraph2D; };
	%strobj.setsize (10) ;
	%strobj.setcollisionsuppress (true) ;
	%strobj.setposition (%pos) ;

	// font is 15x15 across
	for (%i = 0; %i < strlen (%str); %i++)
	{
		%let = new fxStaticSprite2D() { scenegraph = [b]ReplaceWithYourSceneGraph2DObjectHere[/b]; };
		%let.setsize (%size) ;
		%chr = ord (getsubstr(%str, %i, 1)) ;
		%val = 0 ;
		if (%chr >= 65 && %chr <= 90)
		  %val = %chr - 65 + 33 ;
		if (%chr >= 97 && %chr <= 122)
		  %val = %chr - 97 + 65 ;
		if (%chr >= 48 && %chr <= 57)
		  %val = %chr - 48 + 16 ;
		%let.setimagemap (font1ImageMap, %val) ;
		%let.mount (%strobj, %i * (%size * 0.11) SPC "0", 0, true, true, true, true) ;
		%let.setcollisionsuppress (true) ;
		%let.setblendcolour (%color) ;
		%let.setblending (true) ;
		%let.setlayer (0) ;
	}
	%strobj.schedule (%time, "safedelete") ;
	%strobj.setlayer (0) ;
	return (%strobj) ;
}

#1
03/09/2005 (1:56 pm)
Excellent.

You could possibly use the console-functions "strstr" or "strpos" to search against a table-string like "012345689ABCDEFGHIJKLMNOPQRSTUVWXYZ" or a complete ASCII string?

- Melv.
#2
03/09/2005 (1:59 pm)
As a suppliment to this resource I used the following aplication to create font-sheets although I believe it only runs under Windows. Any other useful utilities could be posted here as well.

Font Builder.

Also...

Bitmap Fonts


- Melv.
#3
03/14/2005 (8:57 am)
It looks like font forge can do that for linux, although you'd want to use a script. From the menu you can save out a single glyph as an image file so with a script it should be possible to generate a set of files and then (using pnm tools) assemble into a single file.
#4
03/14/2005 (9:05 am)
@David

I did this a week or so ago when I posted that "Am I going mad" thread. I did pretty much what you have done in script but, using the loookup Melv suggested to index into my texture and my string.

Works ok except it would be much better to have this as a custom class. Once I understand T2D better I may write a simple class to handle this. Just give it a texture (imagemap) some offsets for each character and voila.
#5
04/06/2005 (2:02 pm)
Screenshots? I wanna see pics!
#6
04/06/2005 (7:55 pm)
I did, but as Peter Dwyer and Melv mentions above -- you can use something like strstr to get the index of the character, instead of resorting to engine changes.

But I shall leave that as an exercise to the reader for implementation. Or you could use the resource that someone posted (sorry, no have the link on me but it's pretty recent) which added the text display as an actual fxSceneGraph2D object. So it's more efficient.

John: Check Cloudburst, as it's used for the multiplier display.
#7
04/07/2005 (11:27 am)
What would his funtion look like if it used strstr?
#8
04/07/2005 (6:42 pm)
At a quick glance you'd remove this code:

%val = 0 ;
      if (%chr >= 65 && %chr <= 90)
        %val = %chr - 65 + 33 ;
      if (%chr >= 97 && %chr <= 122)
        %val = %chr - 97 + 65 ;
      if (%chr >= 48 && %chr <= 57)
        %val = %chr - 48 + 16 ;

And replace it with code to determine "val" from a strpos instead. Create a string matching the characters in the font image, making sure they match up exactly. Then do something like:

$testStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

%val = strpos($testStr, %chr);
#9
04/07/2005 (6:43 pm)
Thats off the top of my head and done real quick, but the concept is pretty simple. You have a string that matches character for character the bitmap image you have of letters. Then you simply determine what position in your string the letter is in then use that as your frame number in the bitmap.
#10
04/08/2005 (5:20 pm)
Thanks John!

Using your modification means I no longer have to hack the engine?