some minor improvements

Fri, 18 Apr 2025 19:34:31 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 18 Apr 2025 19:34:31 +0200
changeset 80
9f7bfc0a1dc3
parent 79
ed46a265b679
child 81
84a546e282b7

some minor improvements

src/ascension/context.h file | annotate | diff | comparison | revisions
src/ascension/shader.h file | annotate | diff | comparison | revisions
src/glcontext.c file | annotate | diff | comparison | revisions
src/shader.c file | annotate | diff | comparison | revisions
src/text.c file | annotate | diff | comparison | revisions
test/snake.c file | annotate | diff | comparison | revisions
--- a/src/ascension/context.h	Thu Mar 20 20:36:09 2025 +0100
+++ b/src/ascension/context.h	Fri Apr 18 19:34:31 2025 +0200
@@ -38,10 +38,10 @@
 /** The flag for the overall initialized state. */
 #define ASC_FLAG_INITILIZED  0x01u
 
-/** Flag is set, when error buffer contains new error information. */
+/** Flag is set when error buffer contains new error information. */
 #define ASC_FLAG_HAS_ERROR  0x02u
 
-/** Flag is set, when SDL wants to quit the application. */
+/** Flag is set when SDL wants to quit the application. */
 #define ASC_FLAG_QUIT 0x80000000u
 
 /**
--- a/src/ascension/shader.h	Thu Mar 20 20:36:09 2025 +0100
+++ b/src/ascension/shader.h	Fri Apr 18 19:34:31 2025 +0200
@@ -56,6 +56,7 @@
  *
  * @param files the structure containing the file names
  * @param codes the structure where to store the loaded codes
+ * @return zero on success, non-zero on failure
  */
 int asc_shader_load_code_files(AscShaderCodeFiles files, AscShaderCodes *codes);
 
@@ -67,14 +68,6 @@
 void asc_shader_free_codes(AscShaderCodes codes);
 
 /**
- * Destroys a shader program.
- *
- * @param program the program
- */
-void asc_shader_program_destroy(AscShaderProgram program);
-
-
-/**
  * Creates a shader program.
  *
  * @param codes the (zero-terminated) source codes
@@ -82,4 +75,19 @@
  */
 AscShaderProgram asc_shader_program_create(AscShaderCodes codes);
 
+/**
+ * Destroys a shader program.
+ *
+ * @param program the program
+ */
+void asc_shader_program_destroy(AscShaderProgram *program);
+
+/**
+ * Loads and initializes the sprite shader.
+ *
+ * @param sprite the structure to initialize
+ * @return zero on success, non-zero on failure
+ */
+int asc_shader_sprite_init(AscShaderSprite *sprite);
+
 #endif //ASCENSION_SHADER_H
--- a/src/glcontext.c	Thu Mar 20 20:36:09 2025 +0100
+++ b/src/glcontext.c	Fri Apr 18 19:34:31 2025 +0200
@@ -48,25 +48,14 @@
     cx_strfree(&buf);
 }
 
-static void asc_shader_initialize_predefined(AscGLContext *ctx) {
-    // TODO: deal with different working dirs
-    AscShaderSprite *sprite = &ctx->shader.sprite;
-    AscShaderCodes codes;
-    if (asc_shader_load_code_files((AscShaderCodeFiles){
-        .vtx = "./shader/sprite_vtx.glsl",
-        .frag = "./shader/sprite_frag.glsl"
-    }, &codes)) {
-        asc_error("Loading sprite shader failed.");
-        return;
-    }
-    sprite->program = asc_shader_program_create(codes);
-    sprite->depth = glGetUniformLocation(sprite->program.id, "depth");
-    sprite->tex = glGetUniformLocation(sprite->program.id, "texture");
-    asc_shader_free_codes(codes);
+static int asc_shader_initialize_predefined(AscGLContext *ctx) {
+    int ret = 0;
+    ret |= asc_shader_sprite_init(&ctx->shader.sprite);
+    return ret;
 }
 
 static void asc_shader_destroy_predefined(AscGLContext *ctx) {
-    asc_shader_program_destroy(ctx->shader.sprite.program);
+    asc_shader_program_destroy(&ctx->shader.sprite.program);
 }
 
 bool asc_gl_context_initialize(
@@ -98,7 +87,10 @@
             return false;
         }
 
-        asc_shader_initialize_predefined(ctx);
+        if (asc_shader_initialize_predefined(ctx)) {
+            SDL_GL_DeleteContext(ctx->glctx);
+            return false;
+        }
 
         return true;
     } else {
--- a/src/shader.c	Thu Mar 20 20:36:09 2025 +0100
+++ b/src/shader.c	Fri Apr 18 19:34:31 2025 +0200
@@ -110,12 +110,32 @@
     }
 }
 
-void asc_shader_program_destroy(AscShaderProgram program) {
-    if (program.id > 0) {
-        asc_dprintf("Delete Shader Program %u", program.id);
-        glDeleteProgram(program.id);
+void asc_shader_program_destroy(AscShaderProgram *program) {
+    if (program->id > 0) {
+        asc_dprintf("Delete Shader Program %u", program->id);
+        glDeleteProgram(program->id);
     }
-    program.id = 0;
+    program->id = 0;
+}
+
+int asc_shader_sprite_init(AscShaderSprite *sprite) {
+    AscShaderCodes codes;
+    if (asc_shader_load_code_files((AscShaderCodeFiles){
+        .vtx = "./shader/sprite_vtx.glsl",
+        .frag = "./shader/sprite_frag.glsl"
+    }, &codes)) {
+        asc_error("Loading sprite shader failed.");
+        return 1;
+    }
+    sprite->program = asc_shader_program_create(codes);
+    if (asc_has_error()) {
+        asc_shader_free_codes(codes);
+        return 1;
+    }
+    sprite->depth = glGetUniformLocation(sprite->program.id, "depth");
+    sprite->tex = glGetUniformLocation(sprite->program.id, "texture");
+    asc_shader_free_codes(codes);
+    return 0;
 }
 
 AscShaderProgram asc_shader_program_create(AscShaderCodes codes) {
--- a/src/text.c	Thu Mar 20 20:36:09 2025 +0100
+++ b/src/text.c	Fri Apr 18 19:34:31 2025 +0200
@@ -113,11 +113,12 @@
         char const* format,
         ...
 ) {
+    AscText *text_node = (AscText*) node;
     va_list ap;
     va_start(ap, format);
     cx_vsprintf(
-            &(((AscText*)node)->text.ptr),
-            &(((AscText*)node)->text.length),
+            &text_node->text.ptr,
+            &text_node->text.length,
             format,
             ap
     );
--- a/test/snake.c	Thu Mar 20 20:36:09 2025 +0100
+++ b/test/snake.c	Fri Apr 18 19:34:31 2025 +0200
@@ -34,7 +34,7 @@
     static uint64_t last_fps = 0;
     static uint64_t debounce = ASC_NANOS_SECOND - 1;
     debounce += asc_context.frame_nanos;
-    // only update text every seconds
+    // only update text every second
     if (debounce >= ASC_NANOS_SECOND) {
         debounce = 0;
         uint64_t fps = ASC_NANOS_SECOND;

mercurial