skip sprite rendering when there are no sprites in the scene

Sat, 19 Apr 2025 19:30:46 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 19 Apr 2025 19:30:46 +0200
changeset 87
874a02a683c5
parent 86
943bf9d7c6d6
child 88
6234b7ea48f3

skip sprite rendering when there are no sprites in the scene

src/ascension/scene.h file | annotate | diff | comparison | revisions
src/scene.c file | annotate | diff | comparison | revisions
--- a/src/ascension/scene.h	Sat Apr 19 15:06:24 2025 +0200
+++ b/src/ascension/scene.h	Sat Apr 19 19:30:46 2025 +0200
@@ -65,7 +65,7 @@
  * @param camera the camera to obtain the view and projection matrix from
  */
 __attribute__((__nonnull__))
-void asc_scene_draw(AscSceneNode *root, asc_recti viewport, AscCamera *camera);
+void asc_scene_draw(AscSceneNode *root, asc_recti viewport, const AscCamera *camera);
 
 /**
  * Creates an empty node that may serve as a container for other nodes.
--- a/src/scene.c	Sat Apr 19 15:06:24 2025 +0200
+++ b/src/scene.c	Sat Apr 19 19:30:46 2025 +0200
@@ -59,7 +59,7 @@
     );
 }
 
-void asc_scene_draw(AscSceneNode *root, asc_recti viewport, AscCamera *camera) {
+void asc_scene_draw(AscSceneNode *root, asc_recti viewport, const AscCamera *camera) {
     // create render groups
     CxList *render_group[ASC_RENDER_GROUP_COUNT];
     cx_for_n(i, ASC_RENDER_GROUP_COUNT) {
@@ -130,7 +130,6 @@
     // -------------------------
     // process the render groups
     // -------------------------
-    AscShaderProgram *shader;
     CxIterator render_iter;
 
     // 2D Elements
@@ -140,26 +139,30 @@
 
     // Sprites
     // -------
-    shader = &asc_active_window->glctx.shader.sprite.program;
-    glUseProgram(shader->id);
-    glUniformMatrix4fv(shader->projection, 1,
-                       GL_FALSE, camera->projection);
-    glUniformMatrix4fv(shader->view, 1,
-                           GL_FALSE, camera->view);
+    size_t sprite_count = cxListSize(render_group[ASC_RENDER_GROUP_SPRITE_OPAQUE])
+        + cxListSize(render_group[ASC_RENDER_GROUP_SPRITE_BLEND]);
+    if (sprite_count > 0) {
+        AscShaderProgram *shader = &asc_active_window->glctx.shader.sprite.program;
+        glUseProgram(shader->id);
+        glUniformMatrix4fv(shader->projection, 1,
+                           GL_FALSE, camera->projection);
+        glUniformMatrix4fv(shader->view, 1,
+                               GL_FALSE, camera->view);
 
-    // render opaque sprites from front to back
-    glDisable(GL_BLEND);
-    render_iter = cxListBackwardsIterator(render_group[ASC_RENDER_GROUP_SPRITE_OPAQUE]);
-    cx_foreach(AscSprite const *, node, render_iter) {
-        asc_sprite_draw(node);
-    }
+        // render opaque sprites from front to back
+        glDisable(GL_BLEND);
+        render_iter = cxListBackwardsIterator(render_group[ASC_RENDER_GROUP_SPRITE_OPAQUE]);
+        cx_foreach(AscSprite const *, node, render_iter) {
+            asc_sprite_draw(node);
+        }
 
-    // render sprites with alpha value from back to front
-    glEnable(GL_BLEND);
-    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-    render_iter = cxListIterator(render_group[ASC_RENDER_GROUP_SPRITE_BLEND]);
-    cx_foreach(AscSprite const *, node, render_iter) {
-        asc_sprite_draw(node);
+        // render sprites with alpha value from back to front
+        glEnable(GL_BLEND);
+        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+        render_iter = cxListIterator(render_group[ASC_RENDER_GROUP_SPRITE_BLEND]);
+        cx_foreach(AscSprite const *, node, render_iter) {
+            asc_sprite_draw(node);
+        }
     }
 
     // deallocate render groups

mercurial