add memory pool to gl context default tip

Wed, 23 Apr 2025 23:59:34 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 23 Apr 2025 23:59:34 +0200
changeset 93
52611a99e574
parent 92
78ce93fb46e5

add memory pool to gl context

src/ascension/glcontext.h file | annotate | diff | comparison | revisions
src/glcontext.c file | annotate | diff | comparison | revisions
test/snake/snake.c file | annotate | diff | comparison | revisions
--- a/src/ascension/glcontext.h	Wed Apr 23 23:43:45 2025 +0200
+++ b/src/ascension/glcontext.h	Wed Apr 23 23:59:34 2025 +0200
@@ -34,6 +34,8 @@
 #include "shader.h"
 #include "texture.h"
 
+#include <cx/mempool.h>
+
 typedef struct AscGLContextSettings {
     int gl_major_version;
     int gl_minor_version;
@@ -58,6 +60,7 @@
 
 typedef struct AscGLContext {
     SDL_Window *window;
+    CxMempool *mpool;
     SDL_GLContext glctx;
     AscMesh primitives[ASC_PRIMITIVE_COUNT];
     AscTexture textures_2d[ASC_TEXTURE_2D_COUNT];
@@ -68,6 +71,7 @@
 } AscGLContext;
 
 #define asc_active_glctx (&asc_active_window->glctx)
+#define asc_active_glctx_mpool asc_active_window->glctx.mpool
 #define ASC_PRIMITIVE_PLANE (&asc_active_glctx->primitives[ASC_PRIMITIVE_PLANE_IDX])
 #define ASC_TEXTURE_2D_EMPTY_1X1 (&asc_active_glctx->textures_2d[ASC_TEXTURE_2D_EMPTY_1X1_IDX])
 #define ASC_TEXTURE_RECT_EMPTY_1X1 (&asc_active_glctx->textures_rect[ASC_TEXTURE_RECT_EMPTY_1X1_IDX])
--- a/src/glcontext.c	Wed Apr 23 23:43:45 2025 +0200
+++ b/src/glcontext.c	Wed Apr 23 23:59:34 2025 +0200
@@ -138,6 +138,8 @@
             return false;
         }
 
+        ctx->mpool = cxMempoolCreateSimple(128);
+
         return true;
     } else {
         asc_error("glewInit failed: %s", glewGetErrorString(err));
@@ -150,6 +152,8 @@
     if (ctx->glctx == NULL) return;
     SDL_GL_MakeCurrent(ctx->window, ctx->glctx);
 
+    cxMempoolFree(ctx->mpool);
+    ctx->mpool = NULL;
     asc_texture_destroy_predefined(ctx);
     asc_shader_destroy_predefined(ctx);
     asc_primitives_destroy(ctx);
--- a/test/snake/snake.c	Wed Apr 23 23:43:45 2025 +0200
+++ b/test/snake/snake.c	Wed Apr 23 23:59:34 2025 +0200
@@ -37,13 +37,14 @@
 static AscTexture tex2d[TEX2D_COUNT];
 #define TEXTURE_SHIP &tex2d[TEX_SHIP]
 
+static void destroy_textures(void *dummy) {
+    asc_texture_destroy(tex2d, TEX2D_COUNT);
+}
+
 static void init_textures(void) {
     asc_texture_init_2d(tex2d, TEX2D_COUNT);
     asc_texture_from_file(TEXTURE_SHIP, "ship.png");
-}
-
-static void destroy_textures(void) {
-    asc_texture_destroy(tex2d, TEX2D_COUNT);
+    cxMempoolRegister(asc_active_glctx_mpool, tex2d, destroy_textures);
 }
 
 static void update_fps_counter(AscSceneNode *node) {
@@ -155,7 +156,6 @@
     } while (asc_loop_next());
 
     // cleanup
-    destroy_textures();
     asc_context_destroy();
     return 0;
 }

mercurial