3. Textures and Sprites - Tutorial

This is most important tutorial because sprites are core philosophy of Vortex2D.NET. In this sample we need to load textures, create sprites from textures and draw background and dynamic objects.


Loading Textures

Canvas2D.API uses Texture conception. Textures are represented with Vortex.Drawing.Texture class. Each texture is just image surface. Created textures should be disposed at the end of their usage (commonly in Unload() method of game class). Easiest way to get texture - create it specifying file name as a constructor argument.

Before loading texture we need to define reference variables as game class member:

protected override void Load () {
    //declaration of textures
    Texture _SpaceTexture;
    Texture _SpaceshipTexture;
    Texture _SpaceStationTexture;
Textures loaded from JPEG files automatically created with PixelFormat.Default (PixelFormat.X8R8G8B8). PNG images could be loaded into texture with alpha channel or without it depending on file content.

_SpaceTexture = new Texture("graphics\\space.jpg");
_SpaceshipTexture = new Texture("graphics\\spaceship.png");
_SpaceStationTexture = new Texture("graphics\\spacestation.png", PixelFormat.A4R4G4B4);
Engine will automatically choose best pixel format. Anyway you can override it with adding one more parameter in constructor

Alternative source for texture is object of type System.Drawing.Bitmap. This method could be useful to prepare some procedural images with .NET graphics library. As example Pacman animation sprites are prepared in such way. This method will be covered in next tutorials.

Don't forget free textures in Unload() method


Creating Sprites

Sprites also can be prepared in Load() section to avoid dynamic creation overhead. That overhead is small but can be noticeable in heavy loops. Sprites doesn't require freeing. Even if texture is disposed engine will no crush, drawing some texture stub instead. There are two equivalent ways to create sprites. First, using sprite constructor new Sprite(...), second, asking texture to generate sprite using Texture.ToSprite(...) methods.

_SpaceShip = new Sprite(_SpaceshipTexture);
_SpaceStation = _SpaceStationTexture.ToSprite(0, 0, 256, 256); //or _SpaceStationTexture.ToSprite();

Since verision 0.8 of Vortex2D you can adjust sprite origin point. It could be very useful. Origin point is used for calculation of drawing region for drawing sprite at specified point. Also this point will be used for local sprite rotation. In tutorial I changed origin for space station. Small red circle exactly points on drawing point.

//set origin point of sprite in texture coordinate space
_SpaceStation.Origin = new Vector2(128, 80);

Drawing Sprites

Going down to Render() method... Lets draw background first. It is unnecessary to clear canvas buffer because opaque background will override previous frame content. Also remember easy way to get canvas target size.

// canvas.Region is equivalent to new Rect(0, 0, canvas.Width, canvas.Height)
canvas.DrawSprite(canvas.Region, _SpaceTexture.ToSprite(), ColorU.White); 

It is good idea to move some sprites. Lets calculate space ship position based on total application execution time.

protected override void Update (GameTime time) {
    _SpaceShipPosition = new Vector(700 - (time.TotalGameTime * 10) % 800, 400);
Having background we can draw space ship and space station over.

//drawing spaciship targeting its center point and sprite size
canvas.DrawSprite(_SpaceShipPosition, new Vector(128, 64), _SpaceShip, ColorU.White);

//sprite angular rotation around center is used for drawing spacestation 
canvas.DrawSprite(400, 150, Game.Time.TotalGameTime * 0.1f, _SpaceStation, ColorU.White);  

There are a lot of Canvas2D.DrawSprite overloads. It is recommended to known all of them and use in proper situations.

Last edited Dec 20, 2010 at 6:06 PM by AlexKhomich, version 9


No comments yet.