0
votes

I need to draw polygon filled with color, but i don't know how to create my texture right, for batch to draw it Here is my code

public class VisualComponent implements Component, Pool.Poolable {
       public float[] vertices = new float[1];
       public short[] triangles = new short[1];

       @Override
       public void reset() {
           vertices = new float[1];
           triangles = new short[1];
       }
}

Here i am creating the entity which i want to be drawn

    Entity source = engine.createEntity();
    TransformComponent transformComponent = engine.createComponent(TransformComponent.class);
    VisualComponent visualComponent = engine.createComponent(VisualComponent.class);
    float redColorBits = Color.RED.toFloatBits();
    visualComponent.vertices = new float[]{
            0, 0, redColorBits,
            10, 0, redColorBits,
            10, 10, redColorBits,
            0, 10, redColorBits
    };

    visualComponent.triangles = new short[]{
            0, 1, 2,
            0, 2, 3
    };

    source.add(transformComponent);
    source.add(visualComponent);
    engine.addEntity(source);

And here is the rendering system's processEntity method

@Override
protected void processEntity(Entity entity, float deltaTime) {
    TransformComponent transformComponent = tcm.get(entity);
    VisualComponent visualComponent = vcm.get(entity);

    batch.draw(new Texture(1, 1, Pixmap.Format.RGBA8888),
            visualComponent.vertices, 0, visualComponent.vertices.length,
            visualComponent.triangles, 0, visualComponent.triangles.length);

}

Also I need a tip on how to correctly reuse my texture for not always recreating it. Thx

1

1 Answers

0
votes

Okay I did it by inheriting from PolygonSpriteBatch and adding a 1x1 white pixel texture for draw function, here is the code which worked fine.

public class TexturedPolygonSpriteBatch extends PolygonSpriteBatch {
private Texture texture;

    public TexturedPolygonSpriteBatch() {
        super();
        initTexture();
    }

    public void draw(float[] polygonVertices, int verticesOffset, int verticesCount, 
short[] polygonTriangles,
                     int trianglesOffset, int trianglesCount) {
        super.draw(texture, polygonVertices, verticesOffset, verticesCount, 
polygonTriangles, trianglesOffset, trianglesCount);
    }

    @Override
    public void dispose() {
        texture.dispose();
        super.dispose();
    }

    private void initTexture() {
        Logger.log(getClass(), "Initializing 1x1 white texture");
        Pixmap pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
        pixmap.setColor(Color.WHITE);
        pixmap.fill();
        texture = new Texture(pixmap);
        pixmap.dispose();
    }
}

Actually it was important to give a white color to the texture, because white color contains all the colors, if I set red color, only red color could be drawn there, think its some kind-a strange, but the woodoo worked.)