.gitignore added
[dotfiles/.git] / .local / include / python3.9 / pygame / include / bitmask.h
1 /*
2     Bitmask 1.7 - A pixel-perfect collision detection library.
3
4     Copyright (C) 2002-2005 Ulf Ekstrom except for the bitcount
5     function which is copyright (C) Donald W. Gillies, 1992.
6
7     This library is free software; you can redistribute it and/or
8     modify it under the terms of the GNU Library General Public
9     License as published by the Free Software Foundation; either
10     version 2 of the License, or (at your option) any later version.
11
12     This library is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15     Library General Public License for more details.
16
17     You should have received a copy of the GNU Library General Public
18     License along with this library; if not, write to the Free
19     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 #ifndef BITMASK_H
22 #define BITMASK_H
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 #include <limits.h>
29 /* Define INLINE for different compilers.  If your compiler does not
30    support inlining then there might be a performance hit in
31    bitmask_overlap_area().
32 */
33 #ifndef INLINE
34 # ifdef __GNUC__
35 #  define INLINE inline
36 # else
37 #  ifdef _MSC_VER
38 #   define INLINE __inline
39 #  else
40 #   define INLINE
41 #  endif
42 # endif
43 #endif
44
45 #define BITMASK_W unsigned long int
46 #define BITMASK_W_LEN (sizeof(BITMASK_W)*CHAR_BIT)
47 #define BITMASK_W_MASK (BITMASK_W_LEN - 1)
48 #define BITMASK_N(n) ((BITMASK_W)1 << (n))
49
50 typedef struct bitmask
51 {
52   int w,h;
53   BITMASK_W bits[1];
54 } bitmask_t;
55
56 /* Creates a bitmask of width w and height h, where
57    w and h must both be greater than or equal to 0.
58    The mask is automatically cleared when created.
59  */
60 bitmask_t *bitmask_create(int w, int h);
61
62 /* Frees all the memory allocated by bitmask_create for m. */
63 void bitmask_free(bitmask_t *m);
64
65 /* Create a copy of the given bitmask. */
66 bitmask_t *bitmask_copy(bitmask_t *m);
67
68 /* Clears all bits in the mask */
69 void bitmask_clear(bitmask_t *m);
70
71 /* Sets all bits in the mask */
72 void bitmask_fill(bitmask_t *m);
73
74 /* Flips all bits in the mask */
75 void bitmask_invert(bitmask_t *m);
76
77 /* Counts the bits in the mask */
78 unsigned int bitmask_count(bitmask_t *m);
79
80 /* Returns nonzero if the bit at (x,y) is set.  Coordinates start at
81    (0,0) */
82 static INLINE int bitmask_getbit(const bitmask_t *m, int x, int y)
83 {
84   return (m->bits[x/BITMASK_W_LEN*m->h + y] & BITMASK_N(x & BITMASK_W_MASK)) != 0;
85 }
86
87 /* Sets the bit at (x,y) */
88 static INLINE void bitmask_setbit(bitmask_t *m, int x, int y)
89 {
90   m->bits[x/BITMASK_W_LEN*m->h + y] |= BITMASK_N(x & BITMASK_W_MASK);
91 }
92
93 /* Clears the bit at (x,y) */
94 static INLINE void bitmask_clearbit(bitmask_t *m, int x, int y)
95 {
96   m->bits[x/BITMASK_W_LEN*m->h + y] &= ~BITMASK_N(x & BITMASK_W_MASK);
97 }
98
99 /* Returns nonzero if the masks overlap with the given offset.
100    The overlap tests uses the following offsets (which may be negative):
101
102    +----+----------..
103    |A   | yoffset
104    |  +-+----------..
105    +--|B
106    |xoffset
107    |  |
108    :  :
109 */
110 int bitmask_overlap(const bitmask_t *a, const bitmask_t *b, int xoffset, int yoffset);
111
112 /* Like bitmask_overlap(), but will also give a point of intersection.
113    x and y are given in the coordinates of mask a, and are untouched
114    if there is no overlap. */
115 int bitmask_overlap_pos(const bitmask_t *a, const bitmask_t *b,
116                         int xoffset, int yoffset, int *x, int *y);
117
118 /* Returns the number of overlapping 'pixels' */
119 int bitmask_overlap_area(const bitmask_t *a, const bitmask_t *b, int xoffset, int yoffset);
120
121 /* Fills a mask with the overlap of two other masks. A bitwise AND. */
122 void bitmask_overlap_mask (const bitmask_t *a, const bitmask_t *b, bitmask_t *c, int xoffset, int yoffset);
123
124 /* Draws mask b onto mask a (bitwise OR). Can be used to compose large
125    (game background?) mask from several submasks, which may speed up
126    the testing. */
127
128 void bitmask_draw(bitmask_t *a, const bitmask_t *b, int xoffset, int yoffset);
129
130 void bitmask_erase(bitmask_t *a, const bitmask_t *b, int xoffset, int yoffset);
131
132 /* Return a new scaled bitmask, with dimensions w*h. The quality of the
133    scaling may not be perfect for all circumstances, but it should
134    be reasonable. If either w or h is 0 a clear 1x1 mask is returned. */
135 bitmask_t *bitmask_scale(const bitmask_t *m, int w, int h);
136
137 /* Convolve b into a, drawing the output into o, shifted by offset.  If offset
138  * is 0, then the (x,y) bit will be set if and only if
139  * bitmask_overlap(a, b, x - b->w - 1, y - b->h - 1) returns true.
140  *
141  * Modifies bits o[xoffset ... xoffset + a->w + b->w - 1)
142  *                [yoffset ... yoffset + a->h + b->h - 1). */
143 void bitmask_convolve(const bitmask_t *a, const bitmask_t *b, bitmask_t *o, int xoffset, int yoffset);
144
145 #ifdef __cplusplus
146 } /* End of extern "C" { */
147 #endif
148
149 #endif