Game Development Community

Anyone know how to create a T2DStaticSprite without using TXB?

by Brian · in Torque X 2D · 07/19/2009 (7:15 pm) · 13 replies

Hey all,
What is the bare minimum code for creating a usable T2DStaticSprite without using TXB2D? All tutorials always rely on it and I need to keep the materials I'm using from filling up the editor materials list.

Brian

#1
07/19/2009 (8:09 pm)
This ought to be what you want. Drop this into your BeginRun():

SimpleMaterial mat = new SimpleMaterial();
mat.TextureFilename = @"data\images\ball.png";
GenericTextureDivider gtd = new GenericTextureDivider();
mat.TextureDivider = gtd;

T2DStaticSprite ss1 = new T2DStaticSprite();
ss1.Material = mat;
ss1.Name = "ball";
ss1.Size = new Vector2(1, 1);
ss1.Rotation = 0;
ss1.Position = new Vector2(3, 0);
ss1.Physics.Velocity = new Vector2(250, 0);
ss1.Collision.ResolveCollision = T2DPhysicsComponent.BounceCollision;
ss1.Collision.CollisionMaterial = new T2DCollisionMaterial(1, 0, 1);
ss1.CollisionsEnabled = true;

T2DWorldLimitComponent wlc = new T2DWorldLimitComponent();
wlc.MoveLimitMin = new Vector2(-50f, -37.5f);
wlc.MoveLimitMax = new Vector2(50f, 37.5f);
wlc.WorldLimitResolveCollision = T2DPhysicsComponent.BounceCollision;
ss1.Components.AddComponent(wlc);

TorqueObjectDatabase.Instance.Register(ss1);

A couple of quick things: This does more than you may need, but I figured you wouldn't mind. I added collision and world limits. The "Name" of the StaticSprite is independent of the name of the resource it's rendering. So, don't feel compelled to name you object "ball" just because the material is named "ball."

The last step to register the object is when it enters the engine.
#2
07/19/2009 (11:03 pm)
Awesome! Thanks a lot Jason. Should do the trick. I wish there was a better way built in to organize materials in TXB2D, but what can you do without the source to TXB2D right?

Brian
#3
07/19/2009 (11:43 pm)
Sorry - I'm not sure I understand what you mean? I don't use TXB except to experiment with their file formats. TXB can teach you everything you'd ever want to know about how their objects and engine works, just by examining the .txscene file. SceneLoad is basically just newing up objects and setting the properties in the XML verbatim.

Once that lightbulb when on in my head, I was able to largely kick TXB to the curb (except for the occassional experiment with things like particles, where TXB is invaluable as a playground, and just do everything is my game's code.

You don't need engine sources unless you want to deeply understand how something works or unless you want to extend the engine.
#4
07/19/2009 (11:52 pm)
Oh, I meant the Source to the builder... you can't really enhance it to add things like using other folders for materials or a nice text editor with custom markup for say a RPG that could be associated with player objects in the editor. Basically, if you want an editor that can do more than what TXB2D currently does, you have to build one from scratch.

Brian
#5
07/20/2009 (10:13 am)
Yes. TXB is not build in TorqueX, either. It is based on TGB, so the source is COMPLETELY different (it's C++ and TorqueScript). So, for all intents and purposes, you should plan on building your own editors if you have specific needs like you list above.
#6
11/05/2009 (9:10 pm)
@Jason

Thanks. This was just what I needed. I've been trying for about a day or two to try and display a image on the screen manually. I just realized that I was not configuring the 'size'.

The in game projects sources are great but I think they rely too much on the building your scene with the builder. I like learning the manual way first. My two cents.

-Kareem
#7
12/10/2009 (9:03 pm)
im trying to automate the animationstrip setup in code, so I don't have to do it in txb, when you have like 50 animation strip pngs it's brutal doing it manually in txb

good post

does this mean it doesn't go thru the xna content pipeline?
(png's have to be in the folder but not part of the project?)

#8
12/10/2009 (9:24 pm)
BTW, why does my Game.txproj have this:

<t2dAnimationDatablock name="spiderIdleNAnimation">
<defaultConfig></defaultConfig>
<imageMap>spiderIdleNMaterial</imageMap>
<animationFrames>0 1 2 3 4 5</animationFrames>
<animationTime>6</animationTime>
<animationCycle>true</animationCycle>
<randomStart>true</randomStart>
<startFrame>0</startFrame>
<DynamicFieldCount>0</DynamicFieldCount>
</t2dAnimationDatablock>


while my leveldata.tscene file has this:

<AnimationData name="spiderIdleNAnimation">
<Material nameRef="spiderIdleNMaterial" />
<AnimationFrames>0 1 2 3 4 5</AnimationFrames>
<AnimationDuration>6</AnimationDuration>
<AnimationCycle>true</AnimationCycle>
</AnimationData>


what's with the redundancy?
#9
12/11/2009 (11:42 am)
The txproj file seems to have properties included that are left over from porting the TXB editor from TGB - I'm guessing the editor still reads these properties so they have been left in the project file. But the scenes don't need those properties so they are left out of scene files.

You only need to worry about scene files in your game code (only the editor reads the proj file).
#10
12/11/2009 (1:59 pm)
I wish that were so...
The txscene file breaks without the txproj datablocks

if you wanted to add assets to txb by amending the txscene file, it won't work unless there's a datablock in the txproj file as well
#11
12/11/2009 (3:25 pm)
If you want to add things that the scene editor will be able to use, then yes, you should be modifiying the txproj file as that is where the editor looks for those things (as described in the previous post). You can write a utility to help you automate modifying the txproj file.
#12
12/11/2009 (4:22 pm)
Duncan, what would the utility look like?
(thanks!)
#13
12/11/2009 (4:43 pm)
The txproj is an xml file so you'd need to write a utility that adds the correct xml to the file (or possibly generates the animation data xml and you paste it into the txproj by hand, rather than the utility editing the file itself).

Other than that it's hard to say what the utility would look like as it will be dependent on your particular needs.

Whether writing a utility like that will save you any time over adding via txb as you usually would, well that depends on how quickly you would write such a utility vs how long it would take in txb to do by hand.