package gameEngine.views; import org.joml.Vector3f; import static org.lwjgl.opengl.GL11.*; public class Sprite extends Renderer implements IUpdatable { private Texture texture; private int spriteWidth = 1; private int spriteHeight = 1; public Sprite(String texturePath, Vector3f pos, Vector3f scale) { this.texture = new Texture(texturePath); // テクスチャの読み込み this.position = pos; this.rotation = new Vector3f(0, 0, 0); this.scale = scale; updateSpriteDimensions(); } public Sprite(String texturePath, float posX, float posY, float scaleX, float scaleY) { this(texturePath, new Vector3f(posX, posY, 0), new Vector3f(scaleX, scaleY , 1.0f)); updateSpriteDimensions(); } public Sprite(String texturePath) { this(texturePath, new Vector3f(0, 0, 0), new Vector3f(1.0f, 1.0f, 1.0f)); } public void update(){ Integer id = texture.getId(); if(id == null) { texture.init(); } render(); } public void setColor(Color color) { this.color = color; } public void setTexturePath(String texturePath) { this.texture = new Texture(texturePath); updateSpriteDimensions(); } public void setSize(float width, float height) { float scaleX = width / spriteWidth; float scaleY = height / spriteHeight; setScale(scaleX, scaleY); } public float getDisplayedWidth() { return spriteWidth * scale.x; } public float getDisplayedHeight() { return spriteHeight * scale.y; } public void updateSpriteDimensions() { if (texture != null) { this.spriteWidth = texture.getWidth(); this.spriteHeight = texture.getHeight(); } } public boolean isMouseOver(float mouseX, float mouseY) { float width = getDisplayedWidth(); float height = getDisplayedHeight(); return mouseX >= position.x && mouseX <= position.x + width && mouseY >= position.y && mouseY <= position.y + height; } @Override public void render() { prepareRendering(); glBindTexture(GL_TEXTURE_2D, texture.getId()); glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex3f(0, 0, 0); // 左上の頂点 glTexCoord2f(1, 0); glVertex3f(spriteWidth, 0, 0); // 右上の頂点 glTexCoord2f(1, 1); glVertex3f(spriteWidth, spriteHeight, 0); // 右下の頂点 glTexCoord2f(0, 1); glVertex3f(0, spriteHeight, 0); // 左下の頂点 glEnd(); endRendering(); } }