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,76 +288,105 @@ 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 (gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress))
{
// Check if the platform supports OpenGL 3.2
if (GLAD_GL_VERSION_3_2)
{
//glEnable(GL_DEBUG_OUTPUT);
//glDebugMessageCallback(MessageCallback, 0);
if (SDL_GL_MakeCurrent(window, context) < 0) glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
return NULL; glClear(GL_COLOR_BUFFER_BIT);
if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress)) // Set up Vertex Array Object
return NULL; glGenVertexArrays(1, &vertex_array_id);
glBindVertexArray(vertex_array_id);
// Check if the platform supports OpenGL 3.2 // Set up Vertex Buffer Object
if (!GLAD_GL_VERSION_3_2) glGenBuffers(1, &vertex_buffer_id);
return NULL; glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_id);
glBufferData(GL_ARRAY_BUFFER, 1 * sizeof(VertexBufferSlot), NULL, GL_STREAM_DRAW);
// glEnable(GL_DEBUG_OUTPUT); // Set up the vertex attributes
// glDebugMessageCallback(MessageCallback, 0); glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, vertex_coordinate));
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, texture_coordinate));
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set up our shaders
glClear(GL_COLOR_BUFFER_BIT); program_texture = CompileShader(vertex_shader_texture, fragment_shader_texture);
program_texture_colour_key = CompileShader(vertex_shader_texture, fragment_shader_texture_colour_key);
program_colour_fill = CompileShader(vertex_shader_plain, fragment_shader_colour_fill);
program_glyph_normal = CompileShader(vertex_shader_texture, fragment_shader_glyph_normal);
program_glyph_subpixel_part1 = CompileShader(vertex_shader_texture, fragment_shader_glyph_subpixel_part1);
program_glyph_subpixel_part2 = CompileShader(vertex_shader_texture, fragment_shader_glyph_subpixel_part2);
// Set up Vertex Array Object 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)
glGenVertexArrays(1, &vertex_array_id); {
glBindVertexArray(vertex_array_id); printf("Failed to compile shaders\n");
}
else
{
// Get shader uniforms
program_colour_fill_uniform_colour = glGetUniformLocation(program_colour_fill, "colour");
program_glyph_normal_uniform_colour = glGetUniformLocation(program_glyph_normal, "colour");
program_glyph_subpixel_part2_uniform_colour = glGetUniformLocation(program_glyph_subpixel_part2, "colour");
// Set up Vertex Buffer Object // Set up framebuffer (used for surface-to-surface blitting)
glGenBuffers(1, &vertex_buffer_id); glGenFramebuffers(1, &framebuffer_id);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_id); glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id);
glBufferData(GL_ARRAY_BUFFER, 1 * sizeof(VertexBufferSlot), NULL, GL_STREAM_DRAW);
// Set up the vertex attributes // Set up framebuffer screen texture (used for screen-to-surface blitting)
glEnableVertexAttribArray(1); glGenTextures(1, &framebuffer.texture_id);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, vertex_coordinate)); glBindTexture(GL_TEXTURE_2D, framebuffer.texture_id);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, texture_coordinate)); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, window_width, window_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
// Set up our shaders framebuffer.width = window_width;
program_texture = CompileShader(vertex_shader_texture, fragment_shader_texture); framebuffer.height = window_height;
program_texture_colour_key = CompileShader(vertex_shader_texture, fragment_shader_texture_colour_key);
program_colour_fill = CompileShader(vertex_shader_plain, fragment_shader_colour_fill);
program_glyph_normal = CompileShader(vertex_shader_texture, fragment_shader_glyph_normal);
program_glyph_subpixel_part1 = CompileShader(vertex_shader_texture, fragment_shader_glyph_subpixel_part1);
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) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, framebuffer.texture_id, 0);
printf("Failed to compile shaders\n"); glViewport(0, 0, framebuffer.width, framebuffer.height);
// Get shader uniforms return &framebuffer;
program_colour_fill_uniform_colour = glGetUniformLocation(program_colour_fill, "colour"); }
program_glyph_normal_uniform_colour = glGetUniformLocation(program_glyph_normal, "colour");
program_glyph_subpixel_part2_uniform_colour = glGetUniformLocation(program_glyph_subpixel_part2, "colour");
// Set up framebuffer (used for surface-to-surface blitting) if (program_glyph_subpixel_part2 != 0)
glGenFramebuffers(1, &framebuffer_id); glDeleteProgram(program_glyph_subpixel_part2);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id);
// Set up framebuffer screen texture (used for screen-to-surface blitting) if (program_glyph_subpixel_part1 != 0)
glGenTextures(1, &framebuffer.texture_id); glDeleteProgram(program_glyph_subpixel_part1);
glBindTexture(GL_TEXTURE_2D, framebuffer.texture_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, window_width, window_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
framebuffer.width = window_width; if (program_glyph_normal != 0)
framebuffer.height = window_height; glDeleteProgram(program_glyph_normal);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, framebuffer.texture_id, 0); if (program_texture_colour_key != 0)
glViewport(0, 0, framebuffer.width, framebuffer.height); glDeleteProgram(program_texture_colour_key);
return &framebuffer; 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)