package speiger.src.coreengine.rendering.textures.base;

import speiger.src.coreengine.rendering.utils.GLFunctions;
import speiger.src.coreengine.rendering.utils.GLStateTracker;
import speiger.src.coreengine.rendering.utils.values.IGLValue;
import speiger.src.coreengine.rendering.utils.values.textures.GLTextureType;

public abstract class BaseTexture implements ITexture {
	protected int id;
	
	protected BaseTexture() {}
	
	protected BaseTexture(int id) {
		this.id = id;
		track();
	}
	
	protected void createTexture() {
		this.id = GLFunctions.createTexture(textureType());
	}
	
	protected IGLValue textureType() {
		return GLTextureType.TEXTURE_2D;
	}
	
	protected void removeTexture() {
		if(id == 0) return;
		GLFunctions.deleteTextures(id);
		id = 0;
	}
	
	protected void track() {
		GLStateTracker.TEXTURE_TRACKER.registerTexture(this);
	}
	
	@Override
	public int id() {
		return id;
	}
	
	@Override
	public void bind(int unit) {
		GLStateTracker.instance().textures.bind(unit, id);
	}
	
	@Override
	public void delete(boolean untrack) {
		removeTexture();
		if(untrack) {
			GLStateTracker.TEXTURE_TRACKER.deleteTexture(this);
		}
	}
}
