Newer
Older
JumpingGame / src / main / java / views / Image2D.java
package views;

import entities.Pair;
import entities.config.GLConfigVariable;

import static org.lwjgl.opengl.GL11.*;

public class Image2D {

    private int id;
    private Pair<Double> spriteSize;
    private Pair<Double> position;
    private Color color;
    private double rotation;
    private double scale;
    private double alpha;

    public Image2D(Texture tex) {
        if (tex != null)
            this.id = tex.getId();
        else
            this.id = 0;

        this.position = new Pair<>(0.0, 0.0);
        this.color = new Color(1, 1, 1, 1);

        if (tex != null)
            spriteSize = new Pair<>((double) tex.getWidth(), (double) tex.getHeight());
        else
            spriteSize = new Pair<>(32d, 20d);
        rotation = 0.0;
        scale = 1.0;
        alpha = 1.0;
    }

    public void setPosition(Pair<Double> position) {
        this.position = position;
    }

    public void setScale(double scale) {
        this.scale = scale;
    }

    public void draw() {
        glBindTexture(GL_TEXTURE_2D, id);
        glPushMatrix();
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity(); // 単位行列化
        glTranslated(position.getLeft(), position.getRight(), 0); // 移動
        glRotated(rotation, 0, 0, 1); // 回転
        glScaled(scale, scale, 1); // 拡縮
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, GLConfigVariable.WIDTH, 0, GLConfigVariable.HEIGHT, -GLConfigVariable.DEPTH, GLConfigVariable.DEPTH); // 正射影投影

        glViewport(0, 0, GLConfigVariable.WIDTH, GLConfigVariable.HEIGHT);

        glColor4d(color.getR(), color.getG(), color.getB(), alpha);
        glBegin(GL_TRIANGLE_STRIP);
        glTexCoord2d(0, 0);
        glVertex2d(-spriteSize.getLeft() / 2, spriteSize.getRight() / 2);
        glTexCoord2d(0, 1);
        glVertex2d(-spriteSize.getLeft() / 2, -spriteSize.getRight() / 2);
        glTexCoord2d(1, 0);
        glVertex2d(spriteSize.getLeft() / 2, spriteSize.getRight() / 2);
        glTexCoord2d(1, 1);
        glVertex2d(spriteSize.getLeft() / 2, -spriteSize.getRight() / 2);
        glEnd();

        glPopMatrix();
        glBindTexture(GL_TEXTURE_2D, 0);
    }

    //---------------------------------------------------------------
}