WIP: gl_area params allocated

This commit is contained in:
Adrien Bourmault 2023-02-26 14:57:50 +01:00
parent b2e25470ed
commit abea32cd88
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
3 changed files with 208 additions and 186 deletions

View File

@ -32,7 +32,7 @@
* of Gem-graph client current gl_areas * of Gem-graph client current gl_areas
*/ */
struct gl_area_entry { struct gl_area_entry {
void *ptr; // ptr to GtkGLArea char name[16];
GLuint vao; GLuint vao;
GLuint position_buffer; GLuint position_buffer;
GLuint color_buffer; GLuint color_buffer;
@ -50,7 +50,7 @@ struct gl_area_entry {
* *
* @return true if initialized * @return true if initialized
*/ */
bool graphics_init(void *gl_area); bool graphics_init(const char *gl_area);
/* /*
* Draws the current buffer to a gl_area * Draws the current buffer to a gl_area
@ -59,7 +59,7 @@ bool graphics_init(void *gl_area);
* *
* @return void * @return void
*/ */
void graphics_draw(void *gl_area); void graphics_draw(const void *gl_area);
/* /*
* Shutdowns a gl_area * Shutdowns a gl_area
@ -68,4 +68,25 @@ void graphics_draw(void *gl_area);
* *
* @return true if success * @return true if success
*/ */
bool graphics_shutdown(void *gl_area); bool graphics_shutdown(const void *gl_area);
/*
* Initializes the shaders of a gl_area and link them to a program
*
* @param gl_area, ptr to the gl_area widget
*
*
* @return true if initialized
*/
bool graphics_init_shaders(const void *gl_area);
/*
* Initializes the buffer of a gl_area
*
* @param gl_area, ptr to the gl_area widget
*
* Note : indices[] is defined in graphics_cube.h
*
* @return void
*/
void graphics_init_buffers(const void *gl_area);

View File

@ -24,6 +24,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <string.h>
#include <stdio.h> #include <stdio.h>
#include <fcntl.h> #include <fcntl.h>
#include <epoxy/gl.h> #include <epoxy/gl.h>
@ -43,13 +44,6 @@
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/*
* Dynamic array of ptr to dynamically allocated gl_area_entry
*/
struct gl_area_entry **gl_area_array = NULL;
/* -------------------------------------------------------------------------- */
/* /*
* Prints verbose human-readable debug messages * Prints verbose human-readable debug messages
* *
@ -166,6 +160,179 @@ static void graphics_debug_callback(GLenum source, GLenum type, GLuint id,
errseverity, string, errtype, errsource, msg); errseverity, string, errtype, errsource, msg);
} }
/* -------------------------------------------------------------------------- */
/*
* Dynamic array of ptrs to dynamically allocated gl_area_entry
*/
struct gl_area_entry **gl_area_array = NULL;
/*
* Find gl_area_entry from a gl_area ptr
*
* @param gl_area, ptr to the gl_area widget
*
*
* @return entry ptr
*/
struct gl_area_entry *find_entry_from_ptr(const char *name)
{
struct gl_area_entry **cur;
// Check uninitialized and quit
if (gl_area_array == NULL) return NULL;
// Seek in array
cur = gl_area_array;
while(*cur) {
if (strncmp((*cur)->name, name, sizeof((*cur)->name)) == 0) return *cur;
cur++;
}
// Nothing found
return NULL;
}
/*
* Find the gl_area_entry size
*
* @param void
*
* @return size of array
*/
static inline short gl_area_size(void)
{
struct gl_area_entry **cur;
short size;
// Check uninitialized and quit
if (gl_area_array == NULL) return 0;
cur = gl_area_array;
while(*cur) {
cur++;
size++;
}
return size;
}
/*
* Initializes a gl_area
*
* @param gl_area, ptr to the gl_area widget
*
* @return true if initialized
*/
bool graphics_init(const char *gl_area)
{
short array_size;
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
glEnable(GL_MULTISAMPLE);
// Check if not already initialized
if (find_entry_from_ptr(gl_area)) {
errno = EEXIST;
perror("gl_area_array already exists");
return false;
}
// Get actual array size
array_size = gl_area_size();
// If it does not exist, allocs it
if (gl_area_array == NULL) {
gl_area_array = calloc(2, sizeof(struct gl_area_entry *));
// check if an error occured during allocation
if (gl_area_array == NULL) {
printf("gl_area_array : %p", gl_area_array);
perror("Not enough memory to allocate gl_area_array");
return false;
}
// If it does exist, reallocs it
} else {
gl_area_array = realloc(gl_area_array,
(array_size + 1)
* sizeof(struct gl_area_entry *));
if (gl_area_array == NULL) {
perror("Not enough memory to allocate gl_area_array");
return false;
}
explicit_bzero(gl_area_array + array_size * sizeof(struct gl_area_entry *),
sizeof(struct gl_area_entry *));
}
// Alloc new entry
gl_area_array[array_size] = calloc(1, sizeof(struct gl_area_entry));
if (gl_area_array[array_size] == NULL) {
perror("Not enough memory to allocate gl_area_array entry");
return false;
}
strcpy(gl_area_array[array_size]->name, gl_area);
if (!graphics_init_shaders(gl_area)) return false;
graphics_init_buffers(gl_area);
glDebugMessageCallback(graphics_debug_callback, NULL);
return true;
}
/*
* Shutdowns a gl_area
*
* @param gl_area, ptr to the gl_area widget
*
* @return true if success
*/
bool graphics_shutdown(const void *gl_area)
{
struct gl_area_entry *entry;
entry = find_entry_from_ptr(gl_area);
if (entry == NULL) {
errno = EFAULT;
perror("gl_area_array entry not found");
return false;
}
glDeleteBuffers(1, &entry->position_buffer);
glDeleteBuffers(1, &entry->color_buffer);
glDeleteProgram(entry->program);
// Liberate
free(entry);
if (errno) {
perror("Impossible to free the gl_area_array entry");
return false;
}
entry = NULL;
gl_area_array = realloc(gl_area_array,
(gl_area_size())
* sizeof(struct gl_area_entry *));
if (errno) {
perror("Impossible to shrink gl_area_array");
return false;
}
return true;
}
/* -------------------------------------------------------------------------- */
/* /*
* Initializes an identity matrix * Initializes an identity matrix
* *
@ -268,55 +435,7 @@ static inline GLuint create_shader(int type, const char *src)
return shader; return shader;
} }
/* /* -------------------------------------------------------------------------- */
* Find gl_area_entry from a gl_area ptr
*
* @param gl_area, ptr to the gl_area widget
*
*
* @return entry ptr
*/
static inline struct gl_area_entry *find_entry_from_ptr(void *ptr)
{
struct gl_area_entry **cur;
// Check uninitialized and quit
if (gl_area_array == NULL) return NULL;
// Seek in array
cur = gl_area_array;
while(*cur) {
if ((*cur)->ptr == ptr) return *cur;
cur++;
}
// Nothing found
return NULL;
}
/*
* Find the gl_area_entry size
*
* @param void
*
* @return size of array
*/
static inline short gl_area_size(void)
{
struct gl_area_entry **cur;
short size;
// Check uninitialized and quit
if (gl_area_array == NULL) return 0;
cur = gl_area_array;
while(*cur) {
cur++;
size++;
}
return size;
}
/* /*
* Initializes the shaders of a gl_area and link them to a program * Initializes the shaders of a gl_area and link them to a program
@ -326,7 +445,7 @@ static inline short gl_area_size(void)
* *
* @return true if initialized * @return true if initialized
*/ */
static bool graphics_init_shaders(void *gl_area) bool graphics_init_shaders(const void *gl_area)
{ {
char *vertex_shader; char *vertex_shader;
char *fragment_shader; char *fragment_shader;
@ -437,7 +556,7 @@ static bool graphics_init_shaders(void *gl_area)
* *
* @return void * @return void
*/ */
static void graphics_init_buffers(void *gl_area) void graphics_init_buffers(const void *gl_area)
{ {
GLuint vao, vertex_buffer, color_buffer; GLuint vao, vertex_buffer, color_buffer;
struct gl_area_entry *entry; struct gl_area_entry *entry;
@ -475,77 +594,6 @@ static void graphics_init_buffers(void *gl_area)
} }
} }
/* -------------------------------------------------------------------------- */
/*
* Initializes a gl_area
*
* @param gl_area, ptr to the gl_area widget
*
* @return true if initialized
*/
bool graphics_init(void *gl_area)
{
short array_size;
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
glEnable(GL_MULTISAMPLE);
// Check if not already initialized
if (find_entry_from_ptr(gl_area)) {
errno = EEXIST;
perror("gl_area_array already exists");
return false;
}
// Get actual array size
array_size = gl_area_size();
// If it does not exist, allocs it
if (gl_area_array == NULL) {
gl_area_array = calloc(2, sizeof(struct gl_area_entry *));
// check if an error occured during allocation
if (gl_area_array == NULL) {
printf("gl_area_array : %p", gl_area_array);
perror("Not enough memory to allocate gl_area_array");
return false;
}
// If it does exist, reallocs it
} else {
gl_area_array = realloc(gl_area_array,
(array_size + 1)
* sizeof(struct gl_area_entry *));
if (gl_area_array == NULL) {
perror("Not enough memory to allocate gl_area_array");
return false;
}
explicit_bzero(gl_area_array + array_size * sizeof(struct gl_area_entry *),
sizeof(struct gl_area_entry *));
}
// Alloc new entry
gl_area_array[array_size] = calloc(1, sizeof(struct gl_area_entry));
if (gl_area_array[array_size] == NULL) {
perror("Not enough memory to allocate gl_area_array entry");
return false;
}
gl_area_array[array_size]->ptr = gl_area;
if (!graphics_init_shaders(gl_area)) return false;
graphics_init_buffers(gl_area);
glDebugMessageCallback(graphics_debug_callback, NULL);
return true;
}
/* /*
* Draws the current buffer to a gl_area * Draws the current buffer to a gl_area
* *
@ -553,7 +601,7 @@ bool graphics_init(void *gl_area)
* *
* @return void * @return void
*/ */
void graphics_draw(void *gl_area) void graphics_draw(const void *gl_area)
{ {
float m[16]; float m[16];
float v[16]; float v[16];
@ -612,50 +660,3 @@ void graphics_draw(void *gl_area)
glFlush(); glFlush();
} }
/*
* Shutdowns a gl_area
*
* @param gl_area, ptr to the gl_area widget
*
* @return true if success
*/
bool graphics_shutdown(void *gl_area)
{
struct gl_area_entry *entry;
entry = find_entry_from_ptr(gl_area);
if (entry == NULL) {
errno = EFAULT;
perror("gl_area_array entry not found");
return false;
}
glDeleteBuffers(1, &entry->position_buffer);
glDeleteBuffers(1, &entry->color_buffer);
glDeleteProgram(entry->program);
// Liberate
free(entry);
if (errno) {
perror("Impossible to free the gl_area_array entry");
return false;
}
entry = NULL;
gl_area_array = realloc(gl_area_array,
(gl_area_size())
* sizeof(struct gl_area_entry *));
if (errno) {
perror("Impossible to shrink gl_area_array");
return false;
}
return true;
}
/* -------------------------------------------------------------------------- */

View File

@ -202,10 +202,10 @@ void on_axis_value_change(GtkAdjustment *adjustment, gpointer data)
g_assert(axis >= 0 && axis < N_AXIS); g_assert(axis >= 0 && axis < N_AXIS);
/* Update the rotation angle */ /* Update the rotation angle */
rotation_angles[axis] = gtk_adjustment_get_value(adjustment); rotation_angles[axis] = gtk_adjustment_get_value(adjustment); //XXX selon widget
/* Update the contents of the GL drawing area */ /* Update the contents of the GL drawing area */
gtk_widget_queue_draw(gl_area); // XXX selon l'id de la GLArea gtk_widget_queue_draw(gl_area);
} }
gboolean on_render(GtkGLArea *area, GdkGLContext *context) gboolean on_render(GtkGLArea *area, GdkGLContext *context)
@ -213,7 +213,7 @@ gboolean on_render(GtkGLArea *area, GdkGLContext *context)
if(gtk_gl_area_get_error(area) != NULL) if(gtk_gl_area_get_error(area) != NULL)
return false; return false;
graphics_draw((void*)&area); graphics_draw(gtk_widget_get_name(GTK_WIDGET(area)));
return true; return true;
} }
@ -227,7 +227,7 @@ void on_realize(GtkWidget *widget)
if(gtk_gl_area_get_error(GTK_GL_AREA(widget)) != NULL) if(gtk_gl_area_get_error(GTK_GL_AREA(widget)) != NULL)
return; return;
if (graphics_init((void*)&widget) == false) { if (graphics_init(gtk_widget_get_name(widget)) == false) {
ui_send_internal_notification("Failed to allocate GLArea !"); ui_send_internal_notification("Failed to allocate GLArea !");
} }
} }
@ -240,7 +240,7 @@ void on_unrealize(GtkWidget *widget)
if(gtk_gl_area_get_error(GTK_GL_AREA(widget)) != NULL) if(gtk_gl_area_get_error(GTK_GL_AREA(widget)) != NULL)
return; return;
if (graphics_shutdown((void*)&widget)) { if (graphics_shutdown(gtk_widget_get_name(widget))) {
ui_send_internal_notification("Failed to allocate GLArea !"); ui_send_internal_notification("Failed to allocate GLArea !");
} }
} }