a-game

2D platformer written from scratch.
git clone git://git.amin.space/a-game.git
Log | Files | Refs | README | LICENSE

commit 9442e6627006ee7cf338792a5e430836e55e4647
parent d1d0786bbae5fdd2b7ecd6625769d842f0832672
Author: amin <dev@aminmesbah.com>
Date:   Wed,  3 Apr 2019 04:58:18 +0000

Free memory allocated for shader source code

FossilOrigin-Name: d8afc65b510f09ef8f4b020d721d78b92590a3295cfd73a22486e713d17d8b6a
Diffstat:
Msrc/shader.c | 83+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
1 file changed, 47 insertions(+), 36 deletions(-)

diff --git a/src/shader.c b/src/shader.c @@ -43,48 +43,59 @@ struct Shader shader_compile(GLchar *vertex_path, GLchar *fragment_path) GLint success; GLchar info_log[512]; - GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vertex_shader, 1, &vertex_shader_source, NULL); - glCompileShader(vertex_shader); - glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success); - if (!success) - { - glGetShaderInfoLog(vertex_shader, 512, NULL, info_log); - printf("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n %s\n", info_log); - // TODO: handle errors here in a better way - exit(1); - } - - GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(fragment_shader, 1, &fragment_shader_source, NULL); - glCompileShader(fragment_shader); - glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success); - if (!success) + if (!(vertex_shader_source && fragment_shader_source)) { - glGetShaderInfoLog(fragment_shader, 512, NULL, info_log); - printf("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n %s\n", info_log); - // TODO: handle errors here in a better way + printf("Error: One or more shader source files weren't loaded.\n"); exit(1); } - - struct Shader s; - s.program = glCreateProgram(); - glAttachShader(s.program, vertex_shader); - glAttachShader(s.program, fragment_shader); - glLinkProgram(s.program); - glGetProgramiv(s.program, GL_LINK_STATUS, &success); - if (!success) + else { - glGetShaderInfoLog(s.program, 512, NULL, info_log); - printf("ERROR::SHADER::LINKING_FAILED\n %s\n", info_log); - // TODO: handle errors here in a better way - exit(1); - } + GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertex_shader, 1, &vertex_shader_source, NULL); + glCompileShader(vertex_shader); + glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success); + if (!success) + { + glGetShaderInfoLog(vertex_shader, 512, NULL, info_log); + printf("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n %s\n", info_log); + // TODO: handle errors here in a better way + exit(1); + } + + GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragment_shader, 1, &fragment_shader_source, NULL); + glCompileShader(fragment_shader); + glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success); + if (!success) + { + glGetShaderInfoLog(fragment_shader, 512, NULL, info_log); + printf("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n %s\n", info_log); + // TODO: handle errors here in a better way + exit(1); + } - glDeleteShader(fragment_shader); - glDeleteShader(vertex_shader); + struct Shader s; + s.program = glCreateProgram(); + glAttachShader(s.program, vertex_shader); + glAttachShader(s.program, fragment_shader); + glLinkProgram(s.program); + glGetProgramiv(s.program, GL_LINK_STATUS, &success); + if (!success) + { + glGetShaderInfoLog(s.program, 512, NULL, info_log); + printf("ERROR::SHADER::LINKING_FAILED\n %s\n", info_log); + // TODO: handle errors here in a better way + exit(1); + } + + glDeleteShader(fragment_shader); + glDeleteShader(vertex_shader); - return s; + free((GLchar*)fragment_shader_source); + free((GLchar*)vertex_shader_source); + + return s; + } }