merge different bools of AscSceneNode into flags

Fri, 12 Apr 2024 22:43:00 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 12 Apr 2024 22:43:00 +0200
changeset 61
b7954818a6b7
parent 60
3954c551ded3
child 62
5a592625e2f9

merge different bools of AscSceneNode into flags

src/Makefile file | annotate | diff | comparison | revisions
src/ascension/scene.h file | annotate | diff | comparison | revisions
src/ascension/utils.h file | annotate | diff | comparison | revisions
src/scene.c file | annotate | diff | comparison | revisions
test/Makefile file | annotate | diff | comparison | revisions
--- a/src/Makefile	Fri Apr 12 22:23:31 2024 +0200
+++ b/src/Makefile	Fri Apr 12 22:43:00 2024 +0200
@@ -91,7 +91,7 @@
  ascension/transform.h ascension/camera.h ascension/context.h \
  ascension/window.h ascension/glcontext.h ascension/primitives.h \
  ascension/mesh.h ascension/shader.h ascension/scene.h \
- ascension/ui/font.h ascension/shader.h
+ ascension/ui/font.h ascension/utils.h ascension/shader.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
@@ -103,10 +103,11 @@
 $(BUILD_DIR)/text.o: text.c ascension/ui/text.h ascension/ui/font.h \
  ascension/ui/../scene.h ascension/ui/../datatypes.h \
  ascension/ui/../transform.h ascension/ui/../camera.h \
- ascension/ui/../texture.h ascension/context.h ascension/datatypes.h \
- ascension/window.h ascension/glcontext.h ascension/primitives.h \
- ascension/mesh.h ascension/shader.h ascension/scene.h \
- ascension/ui/font.h ascension/error.h ascension/shader.h
+ ascension/ui/../texture.h ascension/ui/../utils.h ascension/context.h \
+ ascension/datatypes.h ascension/window.h ascension/glcontext.h \
+ ascension/primitives.h ascension/mesh.h ascension/shader.h \
+ ascension/scene.h ascension/ui/font.h ascension/error.h \
+ ascension/shader.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
--- a/src/ascension/scene.h	Fri Apr 12 22:23:31 2024 +0200
+++ b/src/ascension/scene.h	Fri Apr 12 22:43:00 2024 +0200
@@ -62,9 +62,6 @@
     asc_transform transform;
     asc_transform world_transform;
     enum AscRenderGroup render_group;
-    bool hidden;
-    bool need_graphics_update;
-    bool need_transform_update;
     /**
      * Custom flags for this node.
      * The #ASC_SCENE_NODE_FLAGS_MASK bits are reserved for general flags.
@@ -72,7 +69,10 @@
     uint32_t flags;
 };
 
-#define ASC_SCENE_NODE_FLAGS_MASK 0xFF000000
+#define ASC_SCENE_NODE_FLAGS_MASK       0xFF000000
+#define ASC_SCENE_NODE_UPDATE_GRAPHICS  0x01000000
+#define ASC_SCENE_NODE_UPDATE_TRANSFORM 0x02000000
+#define ASC_SCENE_NODE_HIDDEN           0x80000000
 
 /**
  * Place this as first member of a structure that shall be used as a scene node.
@@ -161,12 +161,11 @@
         asc_scene_update_func behavior
 );
 
-#define asc_node_update(node) \
-    ((AscSceneNode*)node)->need_graphics_update = true
-
+__attribute__((__nonnull__))
+void asc_node_update(AscSceneNode *node);
 
 __attribute__((__nonnull__))
-void asc_update_transform(AscSceneNode *node);
+void asc_node_update_transform(AscSceneNode *node);
 
 
 __attribute__((__nonnull__)) static inline
@@ -174,7 +173,7 @@
     node->position.x = x;
     node->position.y = y;
     node->position.z = z;
-    asc_update_transform(node);
+    asc_node_update_transform(node);
 }
 
 __attribute__((__nonnull__)) static inline
@@ -182,7 +181,7 @@
     node->position.x = (float)x;
     node->position.y = (float)y;
     node->position.z = 0.f;
-    asc_update_transform(node);
+    asc_node_update_transform(node);
 }
 
 __attribute__((__nonnull__)) static inline
@@ -195,7 +194,7 @@
     node->scale.width = width;
     node->scale.height = height;
     node->scale.depth = depth;
-    asc_update_transform(node);
+    asc_node_update_transform(node);
 }
 
 __attribute__((__nonnull__)) static inline
@@ -203,7 +202,7 @@
     node->scale.width = (float)width;
     node->scale.height = (float)height;
     node->scale.depth = 1.f;
-    asc_update_transform(node);
+    asc_node_update_transform(node);
 }
 
 __attribute__((__nonnull__)) static inline
--- a/src/ascension/utils.h	Fri Apr 12 22:23:31 2024 +0200
+++ b/src/ascension/utils.h	Fri Apr 12 22:43:00 2024 +0200
@@ -32,9 +32,9 @@
 
 #define asc_test_flag(reg, flag) ((reg & flag) == flag)
 #define asc_test_flag_masked(reg, mask, flag) ((reg & mask) == flag)
-#define asc_clear_flag(reg, flag) (reg &= ~flag)
+#define asc_clear_flag(reg, flag) (reg &= ~(flag))
 #define asc_set_flag(reg, flag) (reg |= flag)
-#define asc_set_flag_masked(reg, mask, flag) (reg = (reg & ~mask) | flag)
+#define asc_set_flag_masked(reg, mask, flag) (reg = (reg & ~(mask)) | flag)
 
 #endif /* ASCENSION_UTILS_H */
 
--- a/src/scene.c	Fri Apr 12 22:23:31 2024 +0200
+++ b/src/scene.c	Fri Apr 12 22:43:00 2024 +0200
@@ -28,6 +28,7 @@
 #include "ascension/scene.h"
 
 #include "ascension/context.h"
+#include "ascension/utils.h"
 
 #include <cx/linked_list.h>
 #include <cx/array_list.h>
@@ -84,7 +85,7 @@
         node->depth = iter.depth;
 
         // skip hidden nodes (and all their children)
-        if (node->hidden) {
+        if (asc_test_flag(node->flags, ASC_SCENE_NODE_HIDDEN)) {
             cxTreeVisitorContinue(iter);
         }
 
@@ -97,16 +98,15 @@
         }
 
         // TODO: implement culling
-        // TODO: implement a hidden flag (requires UCX tree-continue function)
 
         // check if geometry needs update
-        if (node->need_graphics_update) {
+        if (asc_test_flag(node->flags, ASC_SCENE_NODE_UPDATE_GRAPHICS)) {
             assert(node->update_func != NULL);
-            node->need_graphics_update = false;
+            asc_clear_flag(node->flags, ASC_SCENE_NODE_UPDATE_GRAPHICS);
             node->update_func(node);
         }
-        if (node->need_transform_update) {
-            node->need_transform_update = false;
+        if (asc_test_flag(node->flags, ASC_SCENE_NODE_UPDATE_TRANSFORM)) {
+            asc_test_flag(node->flags, ASC_SCENE_NODE_UPDATE_TRANSFORM);
             asc_transform_from_parts(
                     node->transform,
                     node->position,
@@ -242,12 +242,21 @@
     }
 }
 
-void asc_update_transform(AscSceneNode *node) {
-    if (node->need_transform_update) return;
+void asc_node_update(AscSceneNode *node) {
+    asc_set_flag(node->flags, ASC_SCENE_NODE_UPDATE_GRAPHICS);
+}
+
+void asc_node_update_transform(AscSceneNode *node) {
+    // fast skip if node is already marked
+    if (asc_test_flag(node->flags, ASC_SCENE_NODE_UPDATE_TRANSFORM)) {
+        return;
+    }
 
     CxTreeIterator iter = asc_scene_node_iterator(node, false);
     cx_foreach(AscSceneNode*, n, iter) {
-        // TODO: break/continue when subtree is already marked for update
-        n->need_transform_update = true;
+        if (asc_test_flag(n->flags, ASC_SCENE_NODE_UPDATE_TRANSFORM)) {
+            cxTreeIteratorContinue(iter);
+        }
+        asc_set_flag(n->flags, ASC_SCENE_NODE_UPDATE_TRANSFORM);
     }
 }
--- a/test/Makefile	Fri Apr 12 22:23:31 2024 +0200
+++ b/test/Makefile	Fri Apr 12 22:43:00 2024 +0200
@@ -50,7 +50,7 @@
  ../src/ascension/camera.h ../src/ascension/ui/font.h \
  ../src/ascension/ui.h ../src/ascension/ui/text.h \
  ../src/ascension/ui/font.h ../src/ascension/ui/../scene.h \
- ../src/ascension/ui/../texture.h
+ ../src/ascension/ui/../texture.h ../src/ascension/ui/../utils.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 

mercurial