src/graphics/graphics.c: corrected segfault caused by incorrect size for elements

This commit is contained in:
Adrien Bourmault 2023-09-06 22:19:36 +02:00
parent 8130b663d8
commit 234d78542f
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
3 changed files with 11 additions and 2 deletions

View File

@ -68,6 +68,7 @@ static inline char *read_file(char *filename)
filesize = lseek(fd, 0, SEEK_END) + 1 ;
contents = g_malloc(filesize * sizeof(char));
assert (contents);
lseek(fd, 0, SEEK_SET);
read(fd,contents,filesize);

View File

@ -24,6 +24,7 @@
*/
#pragma once
#include "base.h"
#include <unistd.h>
#include <stdbool.h>
#include <epoxy/gl.h>
@ -223,6 +224,7 @@ static inline GLuint create_shader(int type, const char *src)
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_len);
buffer = g_malloc(log_len + 1);
assert (buffer);
glGetShaderInfoLog(shader, log_len, NULL, buffer);
g_warning("Compile failure in %s shader:\n%s",

View File

@ -222,7 +222,7 @@ void graphics_write_vertex (GLfloat x, GLfloat y, GLfloat z)
buffer_vertex_size += 3;
printf("Created vertex (%f,%f,%f), size is %d\n", x,y,z, buffer_vertex_size);
//printf("Created vertex (%f,%f,%f), size is %d\n", x,y,z, buffer_vertex_size);
}
/*
@ -293,7 +293,7 @@ void graphics_init_buffers(const void *gl_area)
// colors
glGenBuffers(1, &color_buffer);
glBindBuffer(GL_ARRAY_BUFFER, color_buffer);
glBufferData(GL_ARRAY_BUFFER, buffer_colors_size * sizeof(buffer_colors_origin), buffer_colors_origin, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, buffer_colors_size * sizeof(buffer_colors_origin[0]), buffer_colors_origin, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// We only use one VAO, so we always keep it bound
@ -377,6 +377,7 @@ bool graphics_init_shaders(const void *gl_area)
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_len);
buffer = g_malloc(log_len + 1);
assert(buffer);
glGetProgramInfoLog(program, log_len, NULL, buffer);
g_warning("Linking failure:\n%s", buffer);
@ -573,12 +574,14 @@ bool graphics_init(const char *gl_area)
// If it does not exist, allocs it
if (gl_area_array == NULL) {
gl_area_array = g_malloc0(sizeof(struct gl_area_entry *) * 2);
assert(gl_area_array);
// If it does exist, g_reallocs it
} else {
gl_area_array = g_realloc(gl_area_array,
(array_size + 1)
* sizeof(struct gl_area_entry *));
assert (gl_area_array);
if (gl_area_array == NULL) {
perror("Not enough memory to allocate gl_area_array");
return false;
@ -590,6 +593,7 @@ bool graphics_init(const char *gl_area)
// Alloc new entry
gl_area_array[array_size] = g_malloc0(sizeof(struct gl_area_entry));
assert(gl_area_array[array_size]);
strcpy(gl_area_array[array_size]->name, gl_area);
@ -633,6 +637,7 @@ bool graphics_shutdown(const void *gl_area)
gl_area_array = g_realloc(gl_area_array,
(gl_area_size())
* sizeof(struct gl_area_entry *));
assert (gl_area_array || !gl_area_size());
g_free(buffer_vertex_origin);
g_free(buffer_colors_origin);
@ -716,6 +721,7 @@ void main_test_graphics (void)
arrows, model_arrows_nb,
pref_show_grid);
printf("main_test_graphics [ok]\n");
}