Game Development Community

Text Object is back

by Pedro Vicente · in Torque 2D Beginner · 12/05/2013 (7:38 pm) · 7 replies

I created a git branch, forked from the development branch, that includes a scene object named TextObject, a copy of the old object t2dTextObject

github.com/pedro-vicente/Torque2D/tree/text_object

The render part is done in this function

void TextObject::sceneRender( const SceneRenderState* pSceneRenderState, const SceneRenderRequest* pSceneRenderRequest, BatchRender* pBatchRenderer )
{
  renderObject();
}

I think that the vertical coordinates of the current Torque 2D engine are reversed in comparison with the old engine.

So, to make this "work", I made this call

//Invert texture 
glScalef(1.0f, -1.0f, 1.0f);

because the texture (the representation of one character) was showing upside down.

The repo has a demo project. I only added the makefile changes (Visual Studio 2010) for Windows.

To run, start

<your repo root>project_game/main.cs

This script code shows how to add an object

function TextScene::OnAdd(%this)
{
  %obj = games.MakeTextObject(%this,40,"Torque rules");
  %obj.SetPosition( 0, 100 );
  
  %obj = games.createRectangle(%this,30,30);
  %obj.SetPosition( 0, 100 );
  
}

that produces this image

www.space-research.org/games/garage_games/text_object_135756_1.png

As can be seen the coordinates of the text object are reversed. To use just invert the vertical axis, like in for this case

%obj.SetPosition( 0, -100 );

that would locate the rectangle and the text in the same location.

It would be great if GG could fix this and make it an "official" feature.







#1
12/05/2013 (8:00 pm)
Remember that GG is not working on the engine anymore; if you want this feature to make it into the official repo, you will have to submit a pull request.
#2
12/06/2013 (5:21 am)
Great work Pedro! This will be highly useful. One thing, though. Your pull request was made against the wrong branch in the repository. You need to submit against the development branch, which is where code is tested before going into master.
#3
12/06/2013 (11:53 am)
Aww, do I have to play the bad cop to Mich's good cop?! Not fair!

Few things to nitpick before a pull request against the dev branch can realistically be merged:

1. Did you manage to fix the position issue described in the first post? Can't let that slip in, would cause too much confusion having an object that does not respect the box2d Y axis convention.

2. I didn't see the necessary write functions defined for this to export properly with TAML.

3. Most of the scene objects that render something go through the batch render. Would be good to change this class to support batching like ImageFont does.

4. Speaking of ImageFont, my personal preference would be to rename TextObject to VectorFont. For someone new to the engine it would not be clear from the class names as to what the difference is between TextObject and ImageFont - without reading the docs.

5. Would be good to break all the ConsoleMethods out into a ScriptBinding.h file like the rest of the scene objects, and use the new doxygen documentation format.

6. The demo module can't be included in the pull request, sorry.

I'll try to help where I can so we can get these fixes in place.

Edit - one more thing I forgot. As a community we need to make sure the project files for VS2012, Xcode, and now Eclipse for Android get updated as well. Very few can update all project files on their own, this needs to be a group effort. Thanks!
#4
12/07/2013 (4:02 pm)
@Mike

Quote:
1. Did you manage to fix the position issue described in the first post? Can't let that slip in, would cause too much confusion having an object that does not respect the box2d Y axis convention.

Yes, you are right about this. I pushed some changes that fix this in the engine by re-defining setPosition().

So, this code

function TextScene::OnAdd(%this)  
{  
  %obj = games.MakeTextObject(%this,40,"Torque rules");  
  %obj.SetPosition( 0, 100 );  
        
  %obj = games.createRectangle(%this,30,30);  
  %obj.SetPosition( 0, 100 );        
}

now produces what is to be expected.


www.space-research.org/games/garage_games/text_object_135756_2.png
#5
12/07/2013 (4:14 pm)
Quote:
2. I didn't see the necessary write functions defined for this to export properly with TAML.

Ok, I will add them.

Quote:
3. Most of the scene objects that render something go through the batch render. Would be good to change this class to support batching like ImageFont does.

Yes, exactly, that was my first attempt. I used as template the ImageFont class render method. But since I could not make it to work with the current batch render, I just called the old render code in renderObject(), that makes the low level openGL calls.


Quote:
4. Speaking of ImageFont, my personal preference would be to rename TextObject to VectorFont. For someone new to the engine it would not be clear from the class names as to what the difference is between TextObject and ImageFont - without reading the docs.

I can name the class to anything you want. TextObject was just the choice because the class is basically a verbatim copy of t2dTextObject.
But the name TextObject causes confusion with ImageFont, yes.

Quote:
5. Would be good to break all the ConsoleMethods out into a ScriptBinding.h file like the rest of the scene objects, and use the new doxygen documentation format.

ok

#6
12/07/2013 (4:18 pm)
Quote:
6. The demo module can't be included in the pull request, sorry.

Ok, understood.

I reproduce the script part here, in case anyone wants to use it. These are basically utility functions that I use on my projects.

function games::createRectangle( %this, %scene_id, %x_size, %y_size)
{	
  %obj = new ShapeVector() 
  { 
    Scene = %scene_id; 
  };	
  %obj.setPolyCustom( 4, "-1 -1 1 -1 1 1 -1 1" );
  %obj.setSize( %x_size, %y_size );
  %obj.setLineColor( "0 0 0 1" );
  %obj.setFillMode( true );
  %obj.setFillColor( "1 0 0" );
  %obj.setFillAlpha( 0.6 );
  %obj.setSceneLayer( 0 );
  return %obj;
}

function games::MakeTextObject( %this, %scene_ID, %size, %text )
{
  %obj = new TextObject()
  { 
    Scene                   = %scene_ID; 
    textAlign               = "Center";
    font                    = "Times New Roman Bold";
    hideOverflow            = false;
    autoSize                = true;
    Visible                 = true;
    blendIgnoreTextureAlpha = "0";
    wordWrap                = "0";
    hideOverflow            = "0";
    aspectRatio             = "1";
    lineSpacing             = "0";
    characterSpacing        = "0";
    autoSize                = "1";
    filter                  = "1";
    integerPrecision        = "1";
    noUnicode               = "0";
    hideOverlap             = "0";        
  };

  %obj.removeAllFontSizes();
  %fontsize = 48;    
  %obj.addFontSize( %fontsize );
  %obj.LineHeight = %fontsize + 10; 
  //call  setSize after LineHeight
  %obj.setSize( %size );
  %obj.setBlendColor( 0.0, 0.0, 0.0, 1.0 );    
  %obj.setSceneLayer( 5 );
  %obj.setText(%text);

  //add to scene
  %obj.addToScene(%scene_ID);
  return %obj;
}

#7
12/12/2013 (9:11 pm)
Quote:
6. The demo module can't be included in the pull request.

I removed the demo module from the branch. Now it does not have anything that should not be merged into the development branch, but still is missing the other items. I would like to create a wiki page, but before that, the name of the class must be chosen. "VectorFont", is that the choice from the T2D Steering committee?