src/behavior.c

changeset 108
d619bf7dd87b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/behavior.c	Sat May 03 19:48:57 2025 +0200
@@ -0,0 +1,64 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ * Copyright 2025 Mike Becker. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "ascension/behavior.h"
+#include "ascension/error.h"
+
+#include <cx/array_list.h>
+
+static void asc_behavior_destroy(void *b) {
+    AscBehavior *behavior = b;
+    if (behavior->destroy_func) {
+        behavior->destroy_func(behavior);
+    }
+}
+
+static CxList *asc_behavior_new_list(void) {
+    // TODO: add comparator when we know how to identifier behaviors in order to remove them
+    CxList *list = cxArrayListCreate(NULL, NULL, sizeof(AscBehavior), 4);
+    cxDefineDestructor(list, asc_behavior_destroy);
+    return list;
+}
+
+AscBehavior *asc_behavior_add_impl(AscSceneNode *node, struct asc_behavior_create_args args) {
+    // TODO: replace cxListAdd() with cxListEmplace() when upgrading to UCX 3.2
+    AscBehavior behavior = {0};
+    behavior.node = node;
+    behavior.func = args.func;
+    behavior.destroy_func = args.destroy_func;
+    behavior.data = args.data;
+    if (node->behaviors == NULL) {
+        node->behaviors = asc_behavior_new_list();
+    }
+    if (cxListAdd(node->behaviors, &behavior)) {
+        // TODO: output ID of node once we have implemented that
+        asc_error("Failed to add behavior to scene node.");
+        return NULL;
+    }
+    // TODO: replace with cxListLast() when upgrading to UCX 3.2
+    return cxListAt(node->behaviors, cxListSize(node->behaviors) - 1);
+}

mercurial