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
@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
@echo -e ${CL2}[$@] ${CL}done.${CL3}

View File

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

View File

@ -9,6 +9,7 @@
"coreutils"
"gcc-toolchain"
"pkg-config"
"valgrind"
"findutils"
"gdb"
"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);
buffer = g_malloc(log_len + 1);
buffer = malloc(log_len + 1);
glGetShaderInfoLog(shader, log_len, NULL, buffer);
g_warning("Compile failure in %s shader:\n%s",
type == GL_VERTEX_SHADER ? "vertex" : "fragment",
buffer);
g_free(buffer);
free(buffer);
glDeleteShader(shader);
@ -328,7 +328,8 @@ static inline short gl_area_size(void)
*/
static bool graphics_init_shaders(void *gl_area)
{
char *file_content;
char *vertex_shader;
char *fragment_shader;
struct gl_area_entry *entry;
int status;
GLuint vertex, fragment;
@ -346,25 +347,28 @@ static bool graphics_init_shaders(void *gl_area)
}
// 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;
vertex = create_shader(GL_VERTEX_SHADER, file_content);
free(file_content);
vertex = create_shader(GL_VERTEX_SHADER, vertex_shader);
if(vertex == 0) {
entry->program = 0;
free(vertex_shader);
return false;
}
// 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;
fragment = create_shader(GL_FRAGMENT_SHADER, file_content);
free(file_content);
fragment = create_shader(GL_FRAGMENT_SHADER, fragment_shader);
if(fragment == 0) {
glDeleteShader(vertex);
entry->program = 0;
free(vertex_shader);
free(fragment_shader);
return false;
}
@ -393,7 +397,13 @@ static bool graphics_init_shaders(void *gl_area)
glDeleteProgram(program);
program = 0;
goto out;
glDeleteShader(vertex);
glDeleteShader(fragment);
free(vertex_shader);
free(fragment_shader);
return false;
}
/* Get the location of the "mvp" uniform */
@ -404,7 +414,6 @@ static bool graphics_init_shaders(void *gl_area)
glDetachShader(program, vertex);
glDetachShader(program, fragment);
out:
glDeleteShader(vertex);
glDeleteShader(fragment);
@ -413,6 +422,9 @@ out:
entry->v = v;
entry->p = p;
free(vertex_shader);
free(fragment_shader);
return true;
}
@ -525,8 +537,10 @@ bool graphics_init(void *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_shaders(gl_area);
glDebugMessageCallback(graphics_debug_callback, NULL);
return true;