Game Development Community

font scene objects in Torque 2D ( similar to t2dTextObject)

by Pedro Vicente · in Torque 2D Beginner · 11/04/2013 (4:28 pm) · 5 replies

In iTorque 1.5 there was the class t2dTextObject, that allowed to generate fonf objects rather easily, with code like this

function WordySceneGraph::MakeTextObject( %this, %code )  
{  
    %obj = new t2dTextObject()   
    {   
        scenegraph      = %this;  
        text            = "";  
        textAlign       = "Center";  
        font            = "Times New Roman Bold";  
        hideOverflow    = false;  
        autoSize        = true;  
        Visible         = true;  
    };  
    %fontsize = 48;  
    %obj.removeAllFontSizes();  
    %obj.addFontSize( %fontsize );  
    %obj.LineHeight = %fontsize;    
    %obj.setBlendColor( 0.0, 0.0, 0.0, 1.0 );  
    %obj.setSize( 40 );  
    %obj.setLayer( 5 );  
    return %obj;  
}

but this class is no longer available

Now we have the class ImageFont, explained here

github.com/GarageGames/Torque2D/wiki/ImageFont-Guide

but this class seems to require that we generate a bitmap with fonts

Quote:
As mentioned in the introduction, to create a valid ImageFont you need to use an ImageAsset that contains at least 96 frames

Is this the case? Is there a way to avoid this bitmap generation?

The old class t2dTextObject was convenient because we could just do for example

%obj.font = "Times New Roman Bold";

Is there any chance to put this class back in the engine?

This thread shows some usage of the class

www.garagegames.com/community/forums/viewthread/127035

www.space-research.org/games/garage_games/127035_2.png

#1
11/04/2013 (5:12 pm)
also, the class t2dTileLayer is gone... this class allowed to do the grid seen in the image above.

Is there any replacement to this class?
#2
11/05/2013 (12:39 am)
The closest thing to t2dTileLayers in T2D MIT are CompositeSprites.

You can find a basic guide here

Bear in mind that if you want the top-left coordinate of your 'tile layer' to be 0,0, you will need to use the Custom layout using the function

setBatchLayout("custom");
#3
11/05/2013 (7:52 pm)
Thanks, this class seems more powerful than the previous t2dTileLayer.

Based on the examples, I did this code, that generates a 3x2 grid.

function WordyScene::CreateModelTileLayer(%this)
{
  %range_x = 3;
  %range_y = 2;
  %composite = new CompositeSprite();
  // Set the default sprite stride.
  // This is used in rectilinear layout mode to scale the specified logical position arguments.	
  %composite.setDefaultSpriteStride( 64, 64 );
  // Set the default sprite size used to a little less than the stride so we get a "gap"
  // in between the sprites.
  %composite.setDefaultSpriteSize( 64, 64 );
  // Set the batch layout mode.  We must do this before we add any sprites.
  %composite.SetBatchLayout( "rect" ); 
  // Add some sprites.
  for ( %y = 0; %y < %range_y; %y++ )
	{
	  for ( %x = 0; %x < %range_x; %x++ )
	  {
	    // Add a sprite with the specified logical position.
      // In rectilinear layout this two-part position is scaled by the default sprite-stride.
      %composite.addSprite( %x SPC %y );
      // Set the sprite image with a random frame.     
      // image is 512x512, with 8 columns and 7 rows filled  (56 frames)        
      %composite.setSpriteImage( "games:blocks", getRandom(0,55) );     
    }
  }
	
  // Add to the scene.
	%this.add( %composite );
}

But it seems that in "rect" mode, the origin is at logical position (0,0), center of screen.

www.space-research.org/games/garage_games/135515_01.png
When I change to

%composite.SetBatchLayout( "custom" );

I only get 1 sprite displayed.

How can I have the same grid, starting at some predefined point in the scene?






#4
11/05/2013 (8:55 pm)
The issue you're getting is not that it draws only one tile, it's that the engine draws every tile at the same position, so you only see the last one that was rendered.

I forgot to mention, In order to use the custom layout, you need to handle the actual positioning of the tiles yourself. Check out the Sandbox CompositeSprite demo's custom sprite layout for a clear example.

Here's a short version nonetheless :

In TorqueScript, when creating the CompositeSprite, assign it a valid name.

%composite = new CompositeSprite(MyC_Sprite);

Then, define a new callback function like so
function MyC_Sprite::onCustomLayout(%logical_position)
{
...

%LocalPosition = %x SPC %y
return(%LocalPosition);
}

Whenever you add a "tile" to the CompositeSprite, the engine will call the above function to determine where the tiles are supposed to go.

In the above example, %logical_position would be "0 0". In the function, you have to determine how this translates to a Local Position, considering that Local Position 0,0 is in the center of the sprite.

It does get confusing but studying the example in the Sandbox should help. If I have time, I'll try to reply with the algorithm to actually draw tiles where 0,0 is in the upper-left corner.
#5
11/05/2013 (9:23 pm)
Thanks, Simon, I really appreciate your help on this...

I am not sure if I need the custom layout. What I want happens to be a rectangular grid, so the rect layout seems just perfect.

Since I can set the position with

%composite.setPosition(%this.x_grid, %this.y_grid);

I can position the composite anywhere.

For example, hardcoded values for iPhone (I set the camera to this size, 320x480), so my screen goes from

%this.x_min = -320 / 2;
%this.x_max =  320 / 2;
%this.y_min = -480 / 2;
%this.y_max =  480 / 2;

//dependent on image frame size
%this.x_grid = %this.x_min + 64/2;
%this.y_grid = %this.y_min + 64/2;

This positions the composite at the lower left corner.