2 Bitmask 1.7 - A pixel-perfect collision detection library.
4 Copyright (C) 2002-2005 Ulf Ekstrom except for the bitcount
5 function which is copyright (C) Donald W. Gillies, 1992.
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.
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.
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
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().
35 # define INLINE inline
38 # define INLINE __inline
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))
50 typedef struct bitmask
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.
60 bitmask_t *bitmask_create(int w, int h);
62 /* Frees all the memory allocated by bitmask_create for m. */
63 void bitmask_free(bitmask_t *m);
65 /* Create a copy of the given bitmask. */
66 bitmask_t *bitmask_copy(bitmask_t *m);
68 /* Clears all bits in the mask */
69 void bitmask_clear(bitmask_t *m);
71 /* Sets all bits in the mask */
72 void bitmask_fill(bitmask_t *m);
74 /* Flips all bits in the mask */
75 void bitmask_invert(bitmask_t *m);
77 /* Counts the bits in the mask */
78 unsigned int bitmask_count(bitmask_t *m);
80 /* Returns nonzero if the bit at (x,y) is set. Coordinates start at
82 static INLINE int bitmask_getbit(const bitmask_t *m, int x, int y)
84 return (m->bits[x/BITMASK_W_LEN*m->h + y] & BITMASK_N(x & BITMASK_W_MASK)) != 0;
87 /* Sets the bit at (x,y) */
88 static INLINE void bitmask_setbit(bitmask_t *m, int x, int y)
90 m->bits[x/BITMASK_W_LEN*m->h + y] |= BITMASK_N(x & BITMASK_W_MASK);
93 /* Clears the bit at (x,y) */
94 static INLINE void bitmask_clearbit(bitmask_t *m, int x, int y)
96 m->bits[x/BITMASK_W_LEN*m->h + y] &= ~BITMASK_N(x & BITMASK_W_MASK);
99 /* Returns nonzero if the masks overlap with the given offset.
100 The overlap tests uses the following offsets (which may be negative):
110 int bitmask_overlap(const bitmask_t *a, const bitmask_t *b, int xoffset, int yoffset);
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);
118 /* Returns the number of overlapping 'pixels' */
119 int bitmask_overlap_area(const bitmask_t *a, const bitmask_t *b, int xoffset, int yoffset);
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);
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
128 void bitmask_draw(bitmask_t *a, const bitmask_t *b, int xoffset, int yoffset);
130 void bitmask_erase(bitmask_t *a, const bitmask_t *b, int xoffset, int yoffset);
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);
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.
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);
146 } /* End of extern "C" { */