src/glcontext.c

changeset 88
6234b7ea48f3
parent 81
84a546e282b7
child 89
e1f682a8a145
--- a/src/glcontext.c	Sat Apr 19 19:30:46 2025 +0200
+++ b/src/glcontext.c	Sun Apr 20 15:41:16 2025 +0200
@@ -74,6 +74,34 @@
     asc_shader_program_destroy(&ctx->shader.sprite.program);
 }
 
+static int asc_texture_initialize_predefined(AscGLContext *ctx) {
+    asc_texture_init_rectangle(&ctx->textures.empty_1x1_rect);
+    asc_texture_init_2d(&ctx->textures.empty_1x1_2d);
+
+    // Create a 1x1 surface with 32-bit RGBA format
+    SDL_Surface* white1x1 = SDL_CreateRGBSurface(
+        0, 1, 1, 32,
+        0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
+    if (white1x1 == NULL) {
+        // TODO: merge error messages once asc_error allows formatting
+        asc_error("Failed to create surface");
+        asc_error(SDL_GetError());
+        return 1;
+    }
+    SDL_FillRect(white1x1, NULL, 0xFFFFFFFF);
+
+    // Initialize the empty textures with the white surface
+    asc_texture_from_surface(&ctx->textures.empty_1x1_2d, white1x1);
+    asc_texture_from_surface(&ctx->textures.empty_1x1_rect, white1x1);
+
+    return asc_error_catch_all_gl();
+}
+
+static void asc_texture_destroy_predefined(AscGLContext *ctx) {
+    asc_texture_destroy(&ctx->textures.empty_1x1_2d);
+    asc_texture_destroy(&ctx->textures.empty_1x1_rect);
+}
+
 bool asc_gl_context_initialize(
         AscGLContext *ctx,
         SDL_Window *window,
@@ -109,6 +137,11 @@
             return false;
         }
 
+        if (asc_texture_initialize_predefined(ctx)) {
+            asc_error("Initializing predefined textures failed");
+            return false;
+        }
+
         return true;
     } else {
         asc_error(glewGetErrorString(err));
@@ -121,6 +154,7 @@
     if (ctx->glctx == NULL) return;
     SDL_GL_MakeCurrent(ctx->window, ctx->glctx);
 
+    asc_texture_destroy_predefined(ctx);
     asc_shader_destroy_predefined(ctx);
     asc_primitives_destroy(ctx);
 

mercurial