About 2D development

I want to create some 2D stuff, but I’m wondering if there is already some library to use.

I’m used to Lemur and a little about Nifty (never used t0neg0dUI), but for me those libs/framework seems to be focused on UI/HUD design, not for 2D development.

I read about The Sprite Project, but it doesn’t seems to be a framework for 2D development.

I’m asking it just to check if someone else didn’t a framework for 2D already, if not, I’ll try to use a mix of those and create a 2D game.

Might be worth summoning @ndebruyn as I believe he’s been working on a 2d framework on top of JME for his mobile games.

Hi guys,
Well you are right @DannyJo, I have a framework I build for all my game development and it by chance also contains a 2D framework with physics available to use.
It is currently tightly merged into the framework and not so loosely coupled.
So for anyone to use it they will need to extends my classes and use my UI lib.
It is a very simple framework, but powerful.

So you build your own using just quads? I just need some quick tips to start, if you don’t mind.

Well yes, I made my own version of the quad mesh where you can split an image up into a grid or sprite sheet.

Here is my code:

public class SpriteQuad extends Mesh {
    
    private float width;
    private float height;
    private int rows = 1;
    private int columns = 1;
    private int colPosition = 0;
    private int rowPosition = 0;
    private boolean flipCoords = false;
    private float uvSpacing = 0.002f;
    
    /**
     * Serialization only. Do not use.
     */
    public SpriteQuad(){
    }
    
    public SpriteQuad(float width, float height, int columns, int rows, int colPosition, int rowPosition) {
        
        this.width = width;
        this.height = height;
        this.columns = columns;
        this.rows = rows;
        this.colPosition = colPosition;
        this.rowPosition = rowPosition;

        initializeMesh();

    }
    
    private void initializeMesh() {
        // Vertex positions in space
        Vector3f [] vertices = new Vector3f[4];
        vertices[0] = new Vector3f(-width*0.5f, -height*0.5f, 0f);
        vertices[1] = new Vector3f(width*0.5f, -height*0.5f, 0f);
        vertices[2] = new Vector3f(-width*0.5f, height*0.5f, 0f);
        vertices[3] = new Vector3f(width*0.5f,height*0.5f, 0f);
        
        float colSize = 1f/(float)columns;
        float rowSize = 1f/(float)rows;

        // Texture coordinates
        Vector2f [] texCoord = new Vector2f[4];
        if (flipCoords) {
            texCoord[0] = new Vector2f(colSize*colPosition+colSize - uvSpacing, rowSize*rowPosition + uvSpacing);
            texCoord[1] = new Vector2f(colSize*colPosition + uvSpacing, rowSize*rowPosition + uvSpacing);
            texCoord[2] = new Vector2f(colSize*colPosition+colSize - uvSpacing, rowSize*rowPosition+rowSize - uvSpacing);
            texCoord[3] = new Vector2f(colSize*colPosition + uvSpacing, rowSize*rowPosition+rowSize - uvSpacing);
            
        } else {
            texCoord[0] = new Vector2f(colSize*colPosition + uvSpacing, rowSize*rowPosition+rowSize - uvSpacing);
            texCoord[1] = new Vector2f(colSize*colPosition+colSize - uvSpacing, rowSize*rowPosition+rowSize - uvSpacing);
            texCoord[2] = new Vector2f(colSize*colPosition + uvSpacing, rowSize*rowPosition + uvSpacing);
            texCoord[3] = new Vector2f(colSize*colPosition+colSize - uvSpacing, rowSize*rowPosition + uvSpacing);
        }


        // Indexes. We define the order in which mesh should be constructed
        short[] indexes = {2, 0, 1, 1, 3, 2};

        // Setting buffers
        setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
        setBuffer(VertexBuffer.Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));
        setBuffer(VertexBuffer.Type.Normal, 3, new float[]{0, 0, 1,
                                              0, 0, 1,
                                              0, 0, 1,
                                              0, 0, 1});
        setBuffer(VertexBuffer.Type.Index, 1, BufferUtils.createShortBuffer(indexes));
        updateBound();

    }
    
    /**
     * This method can be called if the user wants to flip the coordinates.
     * @param flip 
     */
    public void flipCoords(boolean flip) {
        this.flipCoords = flip;
        updateTextureCoords(colPosition, rowPosition);
    }
    
    public void updateTextureCoords(int colPosition, int rowPosition) {
        this.colPosition = colPosition;
        this.rowPosition = rowPosition;
        
        float colSize = 1f/(float)columns;
        float rowSize = 1f/(float)rows;

        // Texture coordinates
        Vector2f [] texCoord = new Vector2f[4];
        
        if (flipCoords) {
            texCoord[0] = new Vector2f(colSize*colPosition+colSize - uvSpacing, rowSize*rowPosition + uvSpacing);
            texCoord[1] = new Vector2f(colSize*colPosition + uvSpacing, rowSize*rowPosition + uvSpacing);
            texCoord[2] = new Vector2f(colSize*colPosition+colSize - uvSpacing, rowSize*rowPosition+rowSize - uvSpacing);
            texCoord[3] = new Vector2f(colSize*colPosition + uvSpacing, rowSize*rowPosition+rowSize - uvSpacing);
            
        } else {
            texCoord[0] = new Vector2f(colSize*colPosition + uvSpacing, rowSize*rowPosition+rowSize - uvSpacing);
            texCoord[1] = new Vector2f(colSize*colPosition+colSize - uvSpacing, rowSize*rowPosition+rowSize - uvSpacing);
            texCoord[2] = new Vector2f(colSize*colPosition + uvSpacing, rowSize*rowPosition + uvSpacing);
            texCoord[3] = new Vector2f(colSize*colPosition+colSize - uvSpacing, rowSize*rowPosition + uvSpacing);
        }
        
        setBuffer(VertexBuffer.Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));
    }

    public boolean isFlipCoords() {
        return flipCoords;
    }

    
}

1 Like

I’m working on a spritesheet library, will eventually opensource it (once I release the actual game ;))

1 Like