////////////////////////////////
// NOTE(allen): OpenGL Helper Functions

function OGL_Shader
ogl_make_shader(Arena *arena, char *src, GLenum kind){
  OGL_Shader result = {0};
  
  // api check
  if (!ogl_is_inited()){
    er_emit(str8_lit("ogl api is not initialized"));
  }
  
  // main path
  else{
    // create shader
    GLuint shader = glCreateShader(kind);
    
    GLenum error = glGetError();
    
    // set source
    glShaderSource(shader, 1, (GLchar const*const*)&src, 0);
    
    // compile
    glCompileShader(shader);
    
    // read log
    String8 log = {0};
    GLint info_log_length = 0;
    glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_log_length);
    if (info_log_length > 0){
      char *buffer = push_array(arena, char, info_log_length + 1);
      GLint length = 0;
      glGetShaderInfoLog(shader, info_log_length + 1, &length, buffer);
      log = str8((U8*)buffer, length);
    }
    
    // check status
    GLint compile_status = 0;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);
    
    // handle error
    if (compile_status == 0){
      glDeleteShader(shader);
      shader = 0;
    }
    
    // fill result
    result.handle = shader;
    result.log = log;
  }
  
  return(result);
}

function OGL_Shader
ogl_make_program(Arena *arena, GLuint *shaders, U32 count){
  OGL_Shader result = {0};
  
  // api check
  if (!ogl_is_inited()){
    er_emit(str8_lit("ogl api is not initialized"));
  }
  
  // main path
  else{
    // link program
    GLuint program = glCreateProgram();
    for (U32 i = 0; i < count; i += 1){
      glAttachShader(program, shaders[i]);
    }
    glLinkProgram(program);
    
    // read log
    String8 log = {0};
    GLint info_log_length = 0;
    glGetProgramiv(program, GL_INFO_LOG_LENGTH, &info_log_length);
    if (info_log_length > 0){
      char *buffer = push_array(arena, char, info_log_length + 1);
      GLint length = 0;
      glGetProgramInfoLog(program, info_log_length + 1, &length, buffer);
      log = str8((U8*)buffer, length);
    }
    
    // check status
    GLint link_status = 0;
    glGetProgramiv(program, GL_LINK_STATUS, &link_status);
    
    // handle error
    if (link_status == 0){
      glDeleteProgram(program);
      program = 0;
    }
    
    // fill result
    result.handle = program;
    result.log = log;
  }
  
  return(result);
}
