1 #ifndef __R_COLLISION_TREE_H
2 #define __R_COLLISION_TREE_H
3 
4 /*
5 Copyright 2011 Jared Krinke.
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24 */
25 
26 #include "r_entity.h"
27 #include "r_list.h"
28 #include "r_hash_table.h"
29 
30 typedef struct
31 {
32     r_entity_t      *entity;
33     unsigned int    entity_version;
34 } r_collision_tree_entry_t;
35 
36 typedef r_list_t r_collision_tree_entry_list_t;
37 
38 typedef enum
39 {
40     R_COLLISION_TREE_CHILD_NE = 0,
41     R_COLLISION_TREE_CHILD_NW,
42     R_COLLISION_TREE_CHILD_SW,
43     R_COLLISION_TREE_CHILD_SE,
44     R_COLLISION_TREE_CHILD_COUNT,
45 } r_collision_tree_child_t;
46 
47 typedef struct _r_collision_tree_node
48 {
49     r_vector2d_t                    min;
50     r_vector2d_t                    max;
51     r_collision_tree_entry_list_t   entries;
52 
53     struct _r_collision_tree_node   *parent;
54     struct _r_collision_tree_node   *children;
55 } r_collision_tree_node_t;
56 
57 typedef struct
58 {
59     r_collision_tree_node_t root;
60     r_hash_table_t          entity_to_node;
61 } r_collision_tree_t;
62 
63 typedef r_status_t (*r_collision_handler_t)(r_state_t *rs, r_entity_t *e1, r_entity_t *e2, void *data);
64 
65 extern r_status_t r_collision_tree_init(r_state_t *rs, r_collision_tree_t *tree);
66 extern r_status_t r_collision_tree_insert(r_state_t *rs, r_collision_tree_t *tree, r_entity_t *entity);
67 extern r_status_t r_collision_tree_remove(r_state_t *rs, r_collision_tree_t *tree, r_entity_t *entity);
68 extern r_status_t r_collision_tree_clear(r_state_t *rs, r_collision_tree_t *tree);
69 extern r_status_t r_collision_tree_cleanup(r_state_t *rs, r_collision_tree_t *tree);
70 
71 /* Note that it is not safe to manipulate the tree while iterating */
72 extern r_status_t r_collision_tree_for_each_collision(r_state_t *rs, r_collision_tree_t *tree, r_collision_handler_t collide, void *data);
73 extern r_status_t r_collision_tree_for_each_collision_filtered(r_state_t *rs, r_collision_tree_t *tree, unsigned int group1, unsigned int group2, r_collision_handler_t collide, void *data);
74 
75 #endif
76 
77