src/ascension/scene.h

Tue, 16 Apr 2024 22:20:17 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 16 Apr 2024 22:20:17 +0200
changeset 63
e3cacdd636e4
parent 61
b7954818a6b7
permissions
-rw-r--r--

implement mouse motion and key press events

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  * Copyright 2023 Mike Becker. All rights reserved.
     4  *
     5  * Redistribution and use in source and binary forms, with or without
     6  * modification, are permitted provided that the following conditions are met:
     7  *
     8  *   1. Redistributions of source code must retain the above copyright
     9  *      notice, this list of conditions and the following disclaimer.
    10  *
    11  *   2. Redistributions in binary form must reproduce the above copyright
    12  *      notice, this list of conditions and the following disclaimer in the
    13  *      documentation and/or other materials provided with the distribution.
    14  *
    15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    25  * POSSIBILITY OF SUCH DAMAGE.
    26  */
    28 #ifndef ASCENSION_SCENE_H
    29 #define ASCENSION_SCENE_H
    31 #include "datatypes.h"
    32 #include "transform.h"
    33 #include "camera.h"
    35 #include <cx/list.h>
    37 typedef struct AscSceneNode AscSceneNode;
    39 typedef void(*asc_scene_free_func)(AscSceneNode*);
    40 typedef void(*asc_scene_update_func)(AscSceneNode*);
    41 typedef void(*asc_scene_draw_func)(AscSceneNode const*);
    43 enum AscRenderGroup {
    44     ASC_RENDER_GROUP_SPRITE_OPAQUE,
    45     ASC_RENDER_GROUP_SPRITE_BLEND,
    46     ASC_RENDER_GROUP_COUNT
    47 };
    49 struct AscSceneNode {
    50     AscSceneNode *parent;
    51     AscSceneNode *prev;
    52     AscSceneNode *next;
    53     AscSceneNode *children;
    54     CxList *behaviors;
    55     asc_scene_free_func free_func;
    56     asc_scene_update_func update_func;
    57     asc_scene_draw_func draw_func;
    58     unsigned depth; // TODO: do we really need this bullshit?
    59     asc_vec3f position;
    60     asc_vec3f rotation;
    61     asc_vec3f scale;
    62     asc_transform transform;
    63     asc_transform world_transform;
    64     enum AscRenderGroup render_group;
    65     /**
    66      * Custom flags for this node.
    67      * The #ASC_SCENE_NODE_FLAGS_MASK bits are reserved for general flags.
    68      */
    69     uint32_t flags;
    70 };
    72 #define ASC_SCENE_NODE_FLAGS_MASK       0xFF000000
    73 #define ASC_SCENE_NODE_UPDATE_GRAPHICS  0x01000000
    74 #define ASC_SCENE_NODE_UPDATE_TRANSFORM 0x02000000
    75 #define ASC_SCENE_NODE_HIDDEN           0x80000000
    77 /**
    78  * Place this as first member of a structure that shall be used as a scene node.
    79  */
    80 #define extend_asc_scene_node AscSceneNode base
    82 /**
    83  * Draws the scene with the specified root node.
    84  *
    85  * @param root the root node of the scene graph
    86  * @param viewport the window viewport the scene shall be drawn to
    87  * @param camera the camera to obtain the view and projection matrix from
    88  */
    89 __attribute__((__nonnull__))
    90 void asc_scene_draw(AscSceneNode *root, asc_recti viewport, AscCamera *camera);
    92 /**
    93  * Creates an empty node that may serve as a container for other nodes.
    94  *
    95  * The free_func of this node will be a simple free().
    96  *
    97  * @return the new node
    98  */
    99 AscSceneNode *asc_scene_node_empty(void);
   101 /**
   102  * Unlinks the node from its parent and frees the entire subtree.
   103  *
   104  * The free_func of this node and all child nodes is called, starting
   105  * with the leaf nodes and terminating with \p node.
   106  *
   107  * @param node the node to unlink
   108  */
   109 void asc_scene_node_free(AscSceneNode *node);
   111 /**
   112  * Links a node to a (new) parent.
   113  *
   114  * @param parent the (new) parent
   115  * @param node the node to link
   116  */
   117 __attribute__((__nonnull__))
   118 void asc_scene_node_link(
   119         AscSceneNode *restrict parent,
   120         AscSceneNode *restrict node
   121 );
   123 /**
   124  * Unlinks a node from its parent.
   125  *
   126  * This might be useful to temporarily remove a subtree from a scene.
   127  * To permanently remove the node use asc_scene_node_free().
   128  *
   129  * @param node the node to unlink
   130  */
   131 __attribute__((__nonnull__))
   132 void asc_scene_node_unlink(AscSceneNode *node);
   134 /**
   135  * Adds a behavior function to the node.
   136  *
   137  * A behavior function MUST NOT be added more than once to the same node.
   138  * This will not be checked.
   139  *
   140  * @param node the node
   141  * @param behavior the behavior function
   142  */
   143 __attribute__((__nonnull__))
   144 void asc_scene_add_behavior(
   145         AscSceneNode *node,
   146         asc_scene_update_func behavior
   147 );
   149 /**
   150  * Removes a behavior function from the node.
   151  *
   152  * If the behavior function is not attached to this node, this function
   153  * does nothing.
   154  *
   155  * @param node the node
   156  * @param behavior the behavior function
   157  */
   158 __attribute__((__nonnull__))
   159 void asc_scene_remove_behavior(
   160         AscSceneNode *node,
   161         asc_scene_update_func behavior
   162 );
   164 __attribute__((__nonnull__))
   165 void asc_node_update(AscSceneNode *node);
   167 __attribute__((__nonnull__))
   168 void asc_node_update_transform(AscSceneNode *node);
   171 __attribute__((__nonnull__)) static inline
   172 void asc_set_position(AscSceneNode *node, float x, float y, float z) {
   173     node->position.x = x;
   174     node->position.y = y;
   175     node->position.z = z;
   176     asc_node_update_transform(node);
   177 }
   179 __attribute__((__nonnull__)) static inline
   180 void asc_set_position2d(AscSceneNode *node, int x, int y) {
   181     node->position.x = (float)x;
   182     node->position.y = (float)y;
   183     node->position.z = 0.f;
   184     asc_node_update_transform(node);
   185 }
   187 __attribute__((__nonnull__)) static inline
   188 asc_vec2i asc_get_position2d(AscSceneNode *node) {
   189     return (asc_vec2i) {(int) node->position.x, (int) node->position.y};
   190 }
   192 __attribute__((__nonnull__)) static inline
   193 void asc_set_scale(AscSceneNode *node, float width, float height, float depth) {
   194     node->scale.width = width;
   195     node->scale.height = height;
   196     node->scale.depth = depth;
   197     asc_node_update_transform(node);
   198 }
   200 __attribute__((__nonnull__)) static inline
   201 void asc_set_scale2d(AscSceneNode *node, int width, int height) {
   202     node->scale.width = (float)width;
   203     node->scale.height = (float)height;
   204     node->scale.depth = 1.f;
   205     asc_node_update_transform(node);
   206 }
   208 __attribute__((__nonnull__)) static inline
   209 asc_vec2i asc_get_scale2d(AscSceneNode *node) {
   210     return (asc_vec2i) {(int) node->scale.width, (int) node->scale.height};
   211 }
   213 #endif // ASCENSION_SCENE_H

mercurial