Tutorial 4 – Textures

Since Genome2D is a GPU framework you need to load your assets into textures before you can draw them. Textures are basically bitmap representation in the GPU memory that once uploaded on GPU is accessed and drawn very quickly. There are special dedicated factory classes to create textures in Genome2D from various sources.

You can create textures from embedded bitmap, asset, bitmapdata, ATF bytearray or a rectangle which will create a render texture.

// Create a texture from embedded bitmap
GTextureManager.createTexture("textureId1", EmbeddedClass);

// Create a texture from bitmap data
GTextureManager.createTexture("textureId2", bitmapData);

// Create a texture from GImageAsset
GTextureManager.createTexture("textureId3", asset);

// Create a texture from ATF bytearray
GTextureManager.createTexture("textureId4", atfBytearray);

// Create a render texture
GTextureManager.createTexture("textureId5', rectangle);

First parameter is texture ID which needs to be UNIQUE within Genome2D. This way Genome2D always keeps all references to textures which are in a ID lookup table. You are always sure there are no lose references somewhere floating around. And you can always access all textures using GTextureManager.getTexture(“textureId”) any time.

Second argument is a dynamic source type which is then inspected by Genome2D itself and once its a valid source it creates a texture based on it.

You can also store texture references directly as factory methods return the reference to the newly created texture.

// This code is faster but the same
var texture:GTexture = GTextureManager.createTexture("textureId", bitmapData);

// Is the same as this one
GTextureManager.createTexture("textureId", bitmapData);
var texture:GTexture = GTextureManager.getTexture("textureId");

You can also create a texture based on other texture such as:

// This will create a texture based on other texture
GTextureManager.createTexture("textureId", texture);

It is frequently used to create subtextures where you can then set region to define region withhin the actual texture. For this use case Genome2D offers a helper factory method to create subtextures.

// This code is the same
var texture:GTexture = GTextureManager.createTexture(parentTexture.id+"_textureId", parentTexture);
texture.region = customRegion;

// As this used helper function
GTextureManager.createSubTexture("textureId", parentTexture, customRegion);

As you can see in the example when you create subtextures using the helper method it will automatically prefix the texture ID you specify with the subtexture ID for better readability. This functionality can be disabled using 5th argument of the helper method p_prefixParentId.

To create subtextures in a bulk using an XML generated by a software such as TexturePacker you can use another helper function.

// Create all subtextures defined within the xml
GTextureManager.createSubTextures(parentTexture, xml);