com.dbi.games.fortress.engine.graphics.Sprite.java Source code

Java tutorial

Introduction

Here is the source code for com.dbi.games.fortress.engine.graphics.Sprite.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.dbi.games.fortress.engine.graphics;

import com.dbi.games.fortress.engine.MainEngine;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;

/**
 *
 * @author Falken
 */
public class Sprite {
    private Texture tex;

    public Sprite(String textureName) {
        tex = MainEngine.get().graphicsEngine().getTexture(textureName);
    }

    public void draw(int x, int y) {
        if (tex == null)
            return;
        GL11.glPushMatrix();

        tex.bind();
        GL11.glTranslatef(x, y, 0);

        GL11.glBegin(GL11.GL_QUADS);
        {
            GL11.glTexCoord2f(0, 0);
            GL11.glVertex2f(0, 0);

            GL11.glTexCoord2f(0, 1f);
            GL11.glVertex2f(0, getHeight());

            GL11.glTexCoord2f(1f, 1f);
            GL11.glVertex2f(getWidth(), getHeight());

            GL11.glTexCoord2f(1f, 0);
            GL11.glVertex2f(getWidth(), 0);

        }
        GL11.glEnd();

        GL11.glPopMatrix();
    }

    public int getWidth() {
        return tex.getTextureWidth();
    }

    public int getHeight() {
        return tex.getTextureHeight();
    }
}