Game Development Community

Drawing Basic Shapes in TorqueScript?

by Charlie Patterson · in Torque Game Builder · 05/14/2011 (9:40 pm) · 9 replies

There are times when I want to draw basic shapes in my planned game, especially the HUD. For instance:

* fill in a progress bar with a growing rectangle
* fill in a pie with x%
* draw a graph

Any way to do this? How are others doing this?

Thanks!

#1
05/15/2011 (10:25 am)
TGB has sprite-based graphics, as far as I know there is no way to draw shapes directly. I believe there should be progress bar GUI control, even though I'm not sure.

Pies and graps on the other hand are not basic shapes, and they aren't used alot in games, so one shouldn't expect them to be implemented.

The most straight and proper way would be to add GUI controls for these to the engine, but I also can imagine someone using some trickery and sprites to imitate pies and simple graphs.
#2
05/15/2011 (10:56 am)
Thanks, Rpahut. But ugh! Anyone else know differently?

I read a bit about the GUI but felt I would be doing 90% of the figuring out on my own. This didn't give me a comfortable feeling that the GUI objects where "first class" engine objects. Maybe they are in flux or something.
#3
05/15/2011 (4:22 pm)
You cannot draw shapes (afaik), but you can do something like:
function MakeShape(%type)
{
   switch$ %type
   {
      case "circle":
         %bitmap = "~/data/images/shapes/circle.png"
      case "square":
         %bitmap = "~/data/images/shapes/square.png"
      case "triangle":
         %bitmap = "~/data/images/shapes/triangle.png"
      default:
         %bitmap = "~/data/images/shapes/circle.png"
   }

  %shape = new GuiBitmapCtrl() {
      Position = "0 0";
      Extent = "10 10";
      MinExtent = "8 8";
      Visible = "1";
      wrap = "0";
      bitmap = %bitmap;
   };
   
   return %shape; 
}
Then, from script code as well, you can alter the properties of that "shape" dynamically (ie, position, size, etc). You just need some PNGs graphics to play with.

For certain jobs, like a progress bar, you better make use of the stock controls, easier job.
#4
05/15/2011 (7:18 pm)
T2dShapeVector to the rescue !

This took me a while to find too. This is something that could use a bit of improvement in the documentation. The reference has an extensive API list, but only with function prototypes. It would be nice to have a couple of sentences describing *what* do the functions accomplish (sample code, and images would be super :) )

Here's some sample code

function iTGBTutorial01::CreateRectangle( %this )
{	
	%obj = new T2dShapeVector() 
	{ 
		scenegraph = %this; 
	};	
	%obj.setPolyPrimitive( 4 );
	%obj.setPolyCustom( 4, "0 0 0 1 1 1 1 0" );
	%obj.setSize( 500, 300 );
	%obj.setLineColor( "0 0 0 1" );
	%obj.setFillMode( true );
	%obj.setFillColor( "1 0 0" );
	%obj.setFillAlpha( 0.6 );
	%obj.setLayer( 1 );
	return %obj;
}

This is kind of a low level primitive, so it's certainly possible to render what you need. And it would be also a nice addition to the library if done.
#6
05/15/2011 (7:46 pm)
Hey Pedro, thats cool!

Edit: whats does this line do?
%obj.setPolyCustom( 4, "0 0 0 1 1 1 1 0" );
#7
05/15/2011 (8:05 pm)
Hi Novack

That is the polygon point list: 4 points, then the x,y coordinate of each one. Reference is here

docs.garagegames.com/it2d/official/content/documentation/Reference/TGB%20Referen...

I got a more detailed explanation of what the function does here:

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

The interesting thing is that t2dShapeVector inherits from t2dSceneObject, so one can apply all the usual cool things, rotation, movement, collision... moving pie charts for example :-)
#8
05/16/2011 (12:29 pm)
Wow Pedro! Great find! I'll dig deeper into this shapeVector.

Quote:
The interesting thing is that t2dShapeVector inherits from t2dSceneObject, so one can apply all the usual cool things, rotation, movement, collision... moving pie charts for example :-)

Heh. Columnar reports with mounted particle effects, here I come! (or not)
#9
05/16/2011 (12:35 pm)
Now that Pedro has pointed this out, I just noticed that the editor does have shapeVectors: see Create tab > Other section > Polygon. Here I can give my Polygon line and fill colors, etc. It has to be convex. And it isn't obvious if I can draw circles, etc. with script. Still playing...