#include <iostream>
#include <cstring>
#include "ShaderProgram.h"
#include "Shader.h"
#include "../utils.h"

ShaderProgram::ShaderProgram(const std::string& vertexShaderPath, const std::string& fragmentShaderPath) {
	Shader vShader(GL_VERTEX_SHADER, vertexShaderPath);
	Shader fShader(GL_FRAGMENT_SHADER, fragmentShaderPath);

	vShader.compile();
	fShader.compile();

	_program = glCreateProgram();
	if(_program == 0) {
		throw std::runtime_error("error creating shader program");
	}

	glAttachShader(_program, vShader);
	utils::throwIfGLError("error attaching vertex shader to program");
	glAttachShader(_program, fShader);
	utils::throwIfGLError("error attaching fragment shader to program");
	glLinkProgram(_program);
	utils::throwIfGLError("error linking program");
}

void ShaderProgram::use() const {
	glUseProgram(_program);
}

ShaderProgram::~ShaderProgram() {
	glDeleteProgram(_program);
}

ShaderProgram::operator GLuint() const {
	return _program;
}
