Perform cleanup when OpenGL's Backend_Init fails

This commit is contained in:
Clownacy 2020-01-17 10:54:49 +00:00
parent 0c9c0115cf
commit a40c3d7b1b

View file

@ -288,21 +288,17 @@ Backend_Surface* Backend_Init(SDL_Window *p_window)
context = SDL_GL_CreateContext(window); context = SDL_GL_CreateContext(window);
if (context == NULL) if (context != NULL)
return NULL; {
if (SDL_GL_MakeCurrent(window, context) == 0)
if (SDL_GL_MakeCurrent(window, context) < 0) {
return NULL; if (gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress))
{
if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress))
return NULL;
// Check if the platform supports OpenGL 3.2 // Check if the platform supports OpenGL 3.2
if (!GLAD_GL_VERSION_3_2) if (GLAD_GL_VERSION_3_2)
return NULL; {
//glEnable(GL_DEBUG_OUTPUT);
// glEnable(GL_DEBUG_OUTPUT); //glDebugMessageCallback(MessageCallback, 0);
// glDebugMessageCallback(MessageCallback, 0);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
@ -330,8 +326,11 @@ Backend_Surface* Backend_Init(SDL_Window *p_window)
program_glyph_subpixel_part2 = CompileShader(vertex_shader_texture, fragment_shader_glyph_subpixel_part2); program_glyph_subpixel_part2 = CompileShader(vertex_shader_texture, fragment_shader_glyph_subpixel_part2);
if (program_texture == 0 || program_texture_colour_key == 0 || program_colour_fill == 0 || program_glyph_normal == 0 || program_glyph_subpixel_part1 == 0 || program_glyph_subpixel_part2 == 0) if (program_texture == 0 || program_texture_colour_key == 0 || program_colour_fill == 0 || program_glyph_normal == 0 || program_glyph_subpixel_part1 == 0 || program_glyph_subpixel_part2 == 0)
{
printf("Failed to compile shaders\n"); printf("Failed to compile shaders\n");
}
else
{
// Get shader uniforms // Get shader uniforms
program_colour_fill_uniform_colour = glGetUniformLocation(program_colour_fill, "colour"); program_colour_fill_uniform_colour = glGetUniformLocation(program_colour_fill, "colour");
program_glyph_normal_uniform_colour = glGetUniformLocation(program_glyph_normal, "colour"); program_glyph_normal_uniform_colour = glGetUniformLocation(program_glyph_normal, "colour");
@ -358,6 +357,36 @@ Backend_Surface* Backend_Init(SDL_Window *p_window)
glViewport(0, 0, framebuffer.width, framebuffer.height); glViewport(0, 0, framebuffer.width, framebuffer.height);
return &framebuffer; return &framebuffer;
}
if (program_glyph_subpixel_part2 != 0)
glDeleteProgram(program_glyph_subpixel_part2);
if (program_glyph_subpixel_part1 != 0)
glDeleteProgram(program_glyph_subpixel_part1);
if (program_glyph_normal != 0)
glDeleteProgram(program_glyph_normal);
if (program_texture_colour_key != 0)
glDeleteProgram(program_texture_colour_key);
if (program_texture_colour_key != 0)
glDeleteProgram(program_texture_colour_key);
if (program_texture != 0)
glDeleteProgram(program_texture);
glDeleteBuffers(1, &vertex_buffer_id);
glDeleteVertexArrays(1, &vertex_array_id);
}
}
}
SDL_GL_DeleteContext(context);
}
return NULL;
} }
void Backend_Deinit(void) void Backend_Deinit(void)