I use Artemis and LibGDX. I have the following two components which manipulate a texture:
- ComponentTexture // Stores the texture
- ComponentSource // Define a region for the texture, that will only be shown
- ComponentSize // The size of the texture
- ComponentMirror // Stores wether you want to flup on x and y axe
- ComponentOrigin // The origin of the texture
- ComponentPosition // The position of the texture
- ComponentRotation // Rotation of the texture
- ComponentScale // The scale of the texture
Than I have a System that requires the Texture and position component(Thats the minimum to draw a texture) and draw them to screen, before I render I check if it has some possible extra components.
@Override protected void process(Entity entity)
{
final ComponentTexture componentTexture = mComponentTextureMapper.get(entity);
final ComponentSource componentSource = mComponentSourceMapper.getSafe(entity);
final ComponentSize componentSize = mComponentSizeMapper.getSafe(entity);
final ComponentMirror componentMirror = mComponentMirrorMapper.getSafe(entity);
final ComponentOrigin componentOrigin = mComponentOriginMapper.getSafe(entity);
final ComponentPosition componentPosition = mComponentPositionMapper.get(entity);
final ComponentRotation componentRotation = mComponentRotationMapper.getSafe(entity);
final ComponentScale componentScale = mComponentScaleMapper.getSafe(entity);
// Texture
final Texture texture = componentTexture.getTexture();
// Source
final Rectangle source;
if(componentSource == null)
{
source = new Rectangle(0, 0, texture.getWidth(), texture.getHeight());
}
else
{
source = componentSource.getSource();
}
// Size
final Vector2 size;
if(componentSize == null)
{
size = new Vector2(texture.getWidth(), texture.getHeight());
}
else
{
size = componentSize.getSize();
}
// Mirror
final boolean mirrorX;
final boolean mirrorY;
if(componentMirror == null)
{
mirrorX = false;
mirrorY = false;
}
else
{
mirrorX = componentMirror.isMirrorX();
mirrorY = componentMirror.isMirrorY();
}
// Origin
final Vector2 origin;
if(componentOrigin == null)
{
origin = new Vector2(0, 0);
}
else
{
origin = componentOrigin.getOrigin();
}
// Position
final Vector2 position = componentPosition.getPostion();
// Rotation
final float rotation;
if(componentRotation == null)
{
rotation = 0;
}
else
{
rotation = componentRotation.getRotation();
}
// Scale
final Vector2 scale;
if(componentScale == null)
{
scale = new Vector2(1, 1);
}
else
{
scale = componentScale.getScale();
}
mSpriteBatch.draw(texture, position.x - origin.x, position.y - origin.y, origin.x, origin.y, size.x, size.y, scale.x, scale.y, rotation, (int) source.x, (int) source.y, (int) source.width, (int) source.height, mirrorX, mirrorY);
}
I can create 5000 Entities with textures and a small rotation animation. I'm wondering about the efficiency of this method and how bad of a performance hit I will get from creating a lot of instances in the method. I plan to optimize this.
Does anyone have a good idea as to how I can get better performance from drawing textures?
How I can avoid the check for every component that the entity has?
Thanks for reading :D