import dlib.aliases;
import dlib.util;
import dlib.alloc;

static if(NativeTarget)
{
	       import dlibincludes;
	public import opengl_native;
}
else
{
	public import opengl_wasm;
}

public import glenum;

u32
GLCreateShader(GLEnum shader_type, string[] shader_src)
{
	i32[32]              src_lengths;
	immutable(char)*[32] src_ptrs;
	for(usize i = 0; i < shader_src.length; i += 1)
	{
		src_ptrs[i]    = shader_src[i].ptr;
		src_lengths[i] = cast(i32)shader_src[i].length;
	}

	u32 shader = glCreateShader(shader_type);
	glShaderSource(shader, cast(u32)shader_src.length, src_ptrs.ptr, src_lengths.ptr);

	i32 success;
	glGetShaderiv(shader, GLE.COMPILE_STATUS, &success);
	if(!success)
	{
		char[512] info_log;
		i32       log_length;
		glGetShaderInfoLog(shader, cast(u32)info_log.length, &log_length, info_log.ptr);

		Errf("Error compiling shader: %s", Str(info_log[0 .. log_length]));
		shader = 0;
	}

	return shader;
}

u32
GLCreateProgramFromSources(string[] vertex_src, string[] fragment_src)
{
	u32 program;

	u32 vertex_shader   = GLCreateShader(GLE.VERTEX_SHADER,   vertex_src);
	u32 fragment_shader = GLCreateShader(GLE.FRAGMENT_SHADER, fragment_src);
	if(vertex_shader && fragment_shader)
	{
		program = glCreateProgram();
		glAttachShader(program, vertex_shader);
		glAttachShader(program, fragment_shader);
		glLinkProgram(program);

		i32 success;
		glGetProgramiv(program, GLE.LINK_STATUS, &success);
		if(!success)
		{
			char[512] info_log;
			i32       log_length;
			glGetProgramInfoLog(program, cast(u32)info_log.length, &log_length, info_log.ptr);

			Errf("Error compiling program: %s", Str(info_log[0 .. log_length]));

			program = 0;
		}
	}

	if(!program)
	{
		Errf("Failed to create program!");
	}

	return program;	
}

