Sat, 05 Jul 2025 23:11:36 +0200
add asc_behavior_get_name()
/* * 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/context.h" #include "ascension/scene.h" #include "ascension/util.h" #include "ascension/error.h" #include <cx/array_list.h> #include <cx/tree.h> #include <assert.h> static void asc_behavior_destroy(void *b) { AscBehavior *behavior = b; asc_dprintf("Destroy behavior: %"CX_PRIstr, CX_SFMT(behavior->name)); cx_strfree(&behavior->name); if (behavior->destroy_func) { behavior->destroy_func(behavior); } } static CxList *asc_behavior_new_list(void) { CxList *list = cxArrayListCreate(NULL, NULL, sizeof(AscBehavior), 4); cxDefineDestructor(list, asc_behavior_destroy); return list; } AscBehavior *asc_behavior_add_(AscSceneNode *node, struct asc_behavior_create_args args) { if (node->behaviors == NULL) { node->behaviors = asc_behavior_new_list(); } AscBehavior *behavior = cxListEmplace(node->behaviors); assert(behavior != NULL); behavior->enabled = true; behavior->killed = false; behavior->node = node; behavior->func = args.func; behavior->destroy_func = args.destroy_func; behavior->data = args.data; behavior->interval = args.interval; behavior->last_execution = 0; asc_behavior_name(behavior, args.name); asc_dprintf("Create behavior: %"CX_PRIstr, CX_SFMT(behavior->name)); return behavior; } void asc_behavior_remove(AscBehavior *behavior) { // TODO: implement some sort of ID for behaviors which can also be used for logging behavior->killed = true; behavior->enabled = false; } void asc_behavior_trigger(AscBehavior *behavior) { assert(behavior->enabled); if (behavior->last_execution + behavior->interval > asc_context.total_nanos) return; behavior->func(behavior); behavior->last_execution = asc_context.total_nanos; } void asc_behavior_name(AscBehavior *behavior, const char *name) { cx_strfree(&behavior->name); if (name == NULL) { behavior->name = asc_util_gen_name(behavior); } else { behavior->name.ptr = strdup(name); behavior->name.length = strlen(name); } } cxstring asc_behavior_get_name(const AscBehavior *behavior) { assert(behavior != NULL); return cx_strcast(behavior->name); }