WIP: solved segfault but need to improve gl_area_array

This commit is contained in:
Adrien Bourmault 2023-02-25 21:23:37 +01:00
parent 0396f8039b
commit b2e25470ed
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
4 changed files with 41 additions and 20 deletions

View File

@ -126,6 +126,11 @@ debug: build_system
@gdb $(BINDIR)/gem-graph-client @gdb $(BINDIR)/gem-graph-client
@echo -e ${CL2}[$@] ${CL}done.${CL3} @echo -e ${CL2}[$@] ${CL}done.${CL3}
valgrind: build_system
@echo -e ${CL2}[$@] ${CL}executing...${CL3}
@valgrind $(BINDIR)/gem-graph-client
@echo -e ${CL2}[$@] ${CL}done.${CL3}
all: build_system all: build_system
@echo -e ${CL2}[$@] ${CL}done.${CL3} @echo -e ${CL2}[$@] ${CL}done.${CL3}

View File

@ -47,31 +47,32 @@ enum
* *
* @return void * @return void
*/ */
static inline bool read_file(char *filename, char **contents) static inline char *read_file(char *filename)
{ {
int fd; int fd;
int filesize; int filesize;
char *contents;
fd = open(filename, O_RDONLY); fd = open(filename, O_RDONLY);
if(fd < 0) { if(fd < 0) {
printf("Couldn't read file: %s\n",filename); printf("Couldn't read file: %s\n",filename);
return false; return NULL;
} }
filesize = lseek(fd, 0, SEEK_END) +1 ; filesize = lseek(fd, 0, SEEK_END) + 1 ;
*contents = calloc(filesize, sizeof(char)); contents = malloc(filesize * sizeof(char));
if (contents == NULL) { if (contents == NULL) {
perror("Not enough memory to allocate file"); perror("Not enough memory to allocate file");
return false; return NULL;
} }
lseek(fd, 0, SEEK_SET); lseek(fd, 0, SEEK_SET);
read(fd,*contents,filesize); read(fd,contents,filesize);
*(contents+filesize-1)='\0'; contents[filesize-1] = '\0';
close(fd); close(fd);
return true; return contents;
} }

View File

@ -9,6 +9,7 @@
"coreutils" "coreutils"
"gcc-toolchain" "gcc-toolchain"
"pkg-config" "pkg-config"
"valgrind"
"findutils" "findutils"
"gdb" "gdb"
"make" "make"

View File

@ -251,14 +251,14 @@ static inline GLuint create_shader(int type, const char *src)
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_len); glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_len);
buffer = g_malloc(log_len + 1); buffer = malloc(log_len + 1);
glGetShaderInfoLog(shader, log_len, NULL, buffer); glGetShaderInfoLog(shader, log_len, NULL, buffer);
g_warning("Compile failure in %s shader:\n%s", g_warning("Compile failure in %s shader:\n%s",
type == GL_VERTEX_SHADER ? "vertex" : "fragment", type == GL_VERTEX_SHADER ? "vertex" : "fragment",
buffer); buffer);
g_free(buffer); free(buffer);
glDeleteShader(shader); glDeleteShader(shader);
@ -328,7 +328,8 @@ static inline short gl_area_size(void)
*/ */
static bool graphics_init_shaders(void *gl_area) static bool graphics_init_shaders(void *gl_area)
{ {
char *file_content; char *vertex_shader;
char *fragment_shader;
struct gl_area_entry *entry; struct gl_area_entry *entry;
int status; int status;
GLuint vertex, fragment; GLuint vertex, fragment;
@ -346,25 +347,28 @@ static bool graphics_init_shaders(void *gl_area)
} }
// Load vertex shader file // Load vertex shader file
if (read_file(VERTEX_SHADER_FILE, &file_content) == false) vertex_shader = read_file(VERTEX_SHADER_FILE);
if (vertex_shader == NULL)
return false; return false;
vertex = create_shader(GL_VERTEX_SHADER, file_content); vertex = create_shader(GL_VERTEX_SHADER, vertex_shader);
free(file_content);
if(vertex == 0) { if(vertex == 0) {
entry->program = 0; entry->program = 0;
free(vertex_shader);
return false; return false;
} }
// Load fragment shader file // Load fragment shader file
if (read_file(VERTEX_SHADER_FILE, &file_content) == false) fragment_shader = read_file(FRAG_SHADER_FILE);
if (fragment_shader == NULL)
return false; return false;
fragment = create_shader(GL_FRAGMENT_SHADER, file_content); fragment = create_shader(GL_FRAGMENT_SHADER, fragment_shader);
free(file_content);
if(fragment == 0) { if(fragment == 0) {
glDeleteShader(vertex); glDeleteShader(vertex);
entry->program = 0; entry->program = 0;
free(vertex_shader);
free(fragment_shader);
return false; return false;
} }
@ -393,7 +397,13 @@ static bool graphics_init_shaders(void *gl_area)
glDeleteProgram(program); glDeleteProgram(program);
program = 0; program = 0;
goto out; glDeleteShader(vertex);
glDeleteShader(fragment);
free(vertex_shader);
free(fragment_shader);
return false;
} }
/* Get the location of the "mvp" uniform */ /* Get the location of the "mvp" uniform */
@ -404,7 +414,6 @@ static bool graphics_init_shaders(void *gl_area)
glDetachShader(program, vertex); glDetachShader(program, vertex);
glDetachShader(program, fragment); glDetachShader(program, fragment);
out:
glDeleteShader(vertex); glDeleteShader(vertex);
glDeleteShader(fragment); glDeleteShader(fragment);
@ -413,6 +422,9 @@ out:
entry->v = v; entry->v = v;
entry->p = p; entry->p = p;
free(vertex_shader);
free(fragment_shader);
return true; return true;
} }
@ -525,8 +537,10 @@ bool graphics_init(void *gl_area)
gl_area_array[array_size]->ptr = gl_area; gl_area_array[array_size]->ptr = gl_area;
if (!graphics_init_shaders(gl_area)) return false;
graphics_init_buffers(gl_area); graphics_init_buffers(gl_area);
graphics_init_shaders(gl_area);
glDebugMessageCallback(graphics_debug_callback, NULL); glDebugMessageCallback(graphics_debug_callback, NULL);
return true; return true;