TorqueX/CSharp

From TDN

This page is for people working with TorqueX directly from C#, without using the TorqueXBuilder (TXB). Dont add TXB tutorials here!


Basic "hello world"

This is the equivlant of hello world.

Here are the things it accomplishes:

  • displays a bunch of sprites lined up as if they were tiles (note, it would be better to use a T2dTileLayer for tiles)
  • sets up a custom (physics) camera
  • binds to user input (arrow keys to move the camera)

Steps

  • Create a new "StarterGame"
  • Build it and run it (to make sure it works as-is)
  • add a texture, located and named as "data/images/tile1.jpg"
  • paste the following code at the bottom of Game.cs:BeginRun()
float tileSize = 16f;

//setup template
GarageGames.Torque.Materials.DXEffects.DefaultEffect material = new GarageGames.Torque.Materials.DXEffects.DefaultEffect();
material.Filename = "DefaultEffect";
material.BaseTex = "data/images/tile1.jpg";

GarageGames.Torque.T2D.T2DStaticSprite template = new GarageGames.Torque.T2D.T2DStaticSprite();
template.Name = "tile1";
template.Material = material;
template.Size = new Microsoft.Xna.Framework.Vector2(tileSize, tileSize);
template.Visible = true;
template.IsTemplate = true;


//draw sprites as tiles
GarageGames.Torque.T2D.T2DStaticSprite sprite;

for (int i = 0; i < 8; i++)
{
    for (int j = 0; j < 8; j++)
    {
        sprite = template.Clone() as GarageGames.Torque.T2D.T2DStaticSprite;
        sprite.IsTemplate = false;
        sprite.Position = new Microsoft.Xna.Framework.Vector2(tileSize * i, tileSize * j);
        GarageGames.Torque.Core.TorqueObjectDatabase.Instance.Register(sprite);
    }
}

//create a camera with physics
camera = new T2DSceneCamera();
camera.CreateWithPhysics = true;
GarageGames.Torque.Core.TorqueObjectDatabase.Instance.Register(camera);
camera.Extent = new Microsoft.Xna.Framework.Vector2(80, 60);
camera.Position = new Microsoft.Xna.Framework.Vector2(0, 0);
GarageGames.Torque.T2D.T2DSceneGraph.Instance.Camera = camera;

//setup camera movement.
GarageGames.Torque.Sim.InputMap.Global.BindAction("keyboard", "Right", MoveRight);
GarageGames.Torque.Sim.InputMap.Global.BindAction("keyboard", "Down", MoveDown);
GarageGames.Torque.Sim.InputMap.Global.BindAction("keyboard", "Left", MoveLeft);
GarageGames.Torque.Sim.InputMap.Global.BindAction("keyboard", "Up", MoveUp);
  • after the BeginRun() method paste the following:
//new camera
GarageGames.Torque.T2D.T2DSceneCamera camera;

//helper methods
private void MoveRight(float val)
{
    camera.Physics.VelocityX = val * 100;
}
private void MoveDown(float val)
{
    camera.Physics.VelocityY = val * 100;
}
private void MoveLeft(float val)
{
    camera.Physics.VelocityX = -val * 100;
}
private void MoveUp(float val)
{
    camera.Physics.VelocityY = -val * 100;
}
  • start the game, use arrow keys to scroll

NOTE: Remember that you shouldnt use sprites for tiles, use a T2dTileLayer instead!

Hope this helps, please only modify this with bug fixes. if you want to extend the tutorial, copy the parts you like and add a new tutorial!

--Jason Swearingen 00:34, 27 June 2007 (PST)