--- a/src/glcontext.c Sun May 04 21:50:13 2025 +0200 +++ b/src/glcontext.c Mon May 05 19:47:25 2025 +0200 @@ -29,6 +29,7 @@ #include "ascension/error.h" #include <GL/glew.h> +#include <cx/array_list.h> static void asc_gl_debug_callback( GLenum source, GLenum type, GLuint id, GLenum severity, @@ -96,6 +97,31 @@ asc_texture_destroy(ctx->textures_2d, ASC_TEXTURE_2D_COUNT); } +struct asc_gl_context_cleanup_data { + int type; + union { + void(*f1)(void); + void(*f2)(void*); + void(*f3)(void*, void*); + }; + void *memory; + void *additional_data; +}; + +static void asc_gl_context_cleanup(struct asc_gl_context_cleanup_data *data) { + switch (data->type) { + case 1: + data->f1(); + break; + case 2: + data->f2(data->memory); + break; + default: + data->f3(data->memory, data->additional_data); + break; + } +} + bool asc_gl_context_initialize( AscGLContext *ctx, SDL_Window *window, @@ -136,7 +162,8 @@ return false; } - ctx->mpool = cxMempoolCreateSimple(128); + ctx->cleanup_funcs = cxArrayListCreateSimple(sizeof(struct asc_gl_context_cleanup_data), 8); + cxDefineDestructor(ctx->cleanup_funcs, asc_gl_context_cleanup); return true; } else { @@ -150,8 +177,7 @@ if (ctx->glctx == NULL) return; SDL_GL_MakeCurrent(ctx->window, ctx->glctx); - cxMempoolFree(ctx->mpool); - ctx->mpool = NULL; + cxListFree(ctx->cleanup_funcs); asc_texture_destroy_predefined(ctx); asc_shader_destroy_predefined(ctx); asc_primitives_destroy(ctx); @@ -160,3 +186,24 @@ SDL_GL_DeleteContext(ctx->glctx); ctx->glctx = NULL; } + +void asc_gl_context_add_cleanup_func(AscGLContext *ctx, void(*func)(void)) { + struct asc_gl_context_cleanup_data *data = cxListEmplace(ctx->cleanup_funcs); + data->type = 1; + data->f1 = func; +} + +void asc_gl_context_add_cleanup_func2(AscGLContext *ctx, void(*func)(void*), void *memory) { + struct asc_gl_context_cleanup_data *data = cxListEmplace(ctx->cleanup_funcs); + data->type = 2; + data->f2 = func; + data->memory = memory; +} + +void asc_gl_context_add_cleanup_func3(AscGLContext *ctx, void(*func)(void*,void*), void *memory, void *additional_data) { + struct asc_gl_context_cleanup_data *data = cxListEmplace(ctx->cleanup_funcs); + data->type = 3; + data->f3 = func; + data->memory = memory; + data->additional_data = additional_data; +}