I use Artemis and LibGDX.
I have following components two manipulate an 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, but before it check for components that maybe have for example a rotation component.
@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);
}
Maintenance I can create 5000 Entities with textures and an small rotation animation.
I know its very bad, that I create in method a lot of new instances, but this is only for the development and for a better "layout".
Later I will optimize this.
Now my question, have anyone a good idea how can get a better performance, how I can avoid to check for every component that the entity maybe has?
I hope someone know a good idea.
Thanks for reading :D