package speiger.src.coreengine.rendering.shader;

import org.lwjgl.opengl.GL20;

import speiger.src.coreengine.assets.AssetLocation;
import speiger.src.coreengine.assets.base.IAssetProvider;
import speiger.src.coreengine.rendering.utils.values.ShaderType;

public abstract class SimpleShader extends ShaderProgram {
	
	public SimpleShader(IAssetProvider provider, String identifer, AssetLocation vertex, AssetLocation fragment, String...attributes) {
		setId(loadOrGenerateCache(identifer, provider.getLatestTime(vertex, fragment), id -> {
			int vId = loadShader(provider, vertex, ShaderType.VERTEX);
			if(vId == -1) return false;
			int fId = loadShader(provider, fragment, ShaderType.FRAGMENT);
			if(fId == -1) return false;
			GL20.glAttachShader(id, vId);
			GL20.glAttachShader(id, fId);
			for(int i = 0;i < attributes.length;i++) {
				GL20.glBindAttribLocation(id, i, attributes[i]);
			}
			GL20.glLinkProgram(id);
			GL20.glDetachShader(id, vId);
			GL20.glDetachShader(id, fId);
			GL20.glDeleteShader(vId);
			GL20.glDeleteShader(fId);
			return true;
		}));
	}
}
