src/ascension/behavior.h

Fri, 13 Jun 2025 18:09:49 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 13 Jun 2025 18:09:49 +0200
changeset 148
9f030f402699
parent 112
3e956c96dd6c
permissions
-rw-r--r--

implement interval for behaviors - fixes #383

/*
 * 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.
 */


#ifndef ASC_BEHAVIOR_H
#define ASC_BEHAVIOR_H

#include "scene_node.h"

typedef struct asc_behavior_s AscBehavior;

typedef void(*asc_behavior_func)(AscBehavior*);
typedef void(*asc_behavior_destroy_func)(AscBehavior*);

struct asc_behavior_s {
    AscSceneNode *node;
    asc_behavior_func func;
    asc_behavior_destroy_func destroy_func;
    void *data;
    uint64_t last_execution;
    uint64_t interval;
    bool enabled;
    // TODO: more useful attributes
};

struct asc_behavior_create_args {
    /**
     * The function that is invoked when the behavior is destroyed.
     */
    asc_behavior_destroy_func destroy_func;
    /**
     * The function that is invoked when the behavior is triggered.
     */
    asc_behavior_func func;
    /**
     * Pointer to additional custom data.
     */
    void *data;
    /**
     * Minimum delay between two successive executions in nanoseconds.
     */
    uint64_t interval;
};

AscBehavior *asc_behavior_add_(AscSceneNode *node, struct asc_behavior_create_args args);
#define asc_behavior_add(node,...) asc_behavior_add_(node, (struct asc_behavior_create_args){__VA_ARGS__})

// TODO: asc_behavior_remove()

void asc_behavior_trigger(AscBehavior *behavior);

static inline void asc_behavior_enable(AscBehavior *behavior) {
    behavior->enabled = true;
}

static inline void asc_behavior_disable(AscBehavior *behavior) {
    behavior->enabled = false;
}

#endif

mercurial