Imported contents: kraptor_final_apr_03_2004.tar.gz
[kraptor.git] / include / pmask.h
blob7455a398f61a08a1bdcfefb82a34a739325d3514
1 #ifndef pmask_H
2 #define pmask_H
4 /*
5 This is the Pixel MASK library, which does pixel-perfect collisions using
6 bit masks.
8 There are several configuration options here, hope they aren't too
9 confusing.
12 #define PMASK_VERSION 1
14 #define USE_ALLEGRO
15 //#define USE_SDL
17 #ifdef USE_ALLEGRO
18 struct BITMAP;
19 #endif
20 #ifdef USE_SDL
21 struct SDL_Surface;
22 #endif
24 #ifdef __cplusplus
25 #define pmask_USE_INLINE
26 extern "C" {
27 #endif
29 //This is the bounding box collision detection macro.
30 //It is a general purpose macro. Pass it the coordinates of one rectangle and the width and
31 //height of it, then pass the coordinates of a second rectangle and the width and height of
32 //it. It will return 0 if theres not collision or 1 if there is a collision between the
33 //rectangles (the rectangles overlap).
34 //This macro works looking for out of range values, if some value is out of
35 //range it returns 0, if all the values are in range it returns true.
36 #define check_bb_collision_general(x1,y1,w1,h1,x2,y2,w2,h2) (!( ((x1)>=(x2)+(w2)) || ((x2)>=(x1)+(w1)) || ((y1)>=(y2)+(h2)) || ((y2)>=(y1)+(h1)) ))
37 #define check_bb_collision(mask1,mask2,x1,y1,x2,y2) check_bb_collision_general(x1,y1,mask1->w,mask1->h,x2,y2,mask2->w,mask2->h)
39 //MASK_WORD_TYPE and MASK_WORD_BITBITS can be changed for your platform
41 //MASK_WORD_TYPE should be the largest fast integer type available
42 #define MASK_WORD_TYPE unsigned long int
44 //MASK_WORD_BITBITS should be the log base 2
45 //of the number of bits in MASK_WORD_TYPE
46 //e.g. 4 for 16-bit ints, 5 for 32-bit ints, 6 for 64-bit ints
47 #define MASK_WORD_BITBITS 5
50 //if SINGLE_MEMORY_BLOCK is defined
51 //then each mask will be allocated as
52 //only a single memory block.
53 //this means that masks will (in theory)
54 //be ever-so-slightly faster and more memory efficient
55 //however, if in single memory block mode
56 //then the masks are variable-sized
57 //so you can not use an array of them
58 //#define MASK_SINGLE_MEMORY_BLOCK
60 typedef struct PMASK
62 short int w;//width
63 short int h;//height
64 #ifdef MASK_SINGLE_MEMORY_BLOCK
65 MASK_WORD_TYPE mask[1];//mask data (single memory block)
66 #else
67 MASK_WORD_TYPE *mask;//mask data (pointer at second memory block)
68 #endif
69 } PMASK;
71 void install_pmask(); //sets up library
73 int check_pmask_collision(struct PMASK *mask1, struct PMASK *mask2, int x1, int y1, int x2, int y2); //checks for collision (0 = no collision, 1 = collision)
75 int get_pmask_pixel(struct PMASK *mask, int x, int y) ; //returns 0 if mask is clear at those coordinates, 1 if not clear
76 void set_pmask_pixel(struct PMASK *mask, int x, int y, int value) ;//makes mask clear at those coordinates if value is zero, others makes mask not-clear at those coordinates
78 void init_pmask (struct PMASK *mask, int w, int h); //initializes a PMASK
79 void pmask_load_pixels (struct PMASK *mask, void *pixels, int pitch, int bytes_per_pixel, int trans_color);//loads a pmask with pixel data from memory
80 void pmask_load_func (struct PMASK *mask, int x, int y, void *surface, int trans_color, int (*func)(void*,int,int));//loads a pmask with pixel data from a function
81 void deinit_pmask(PMASK *mask);//de-initializes a pmask
83 PMASK *create_pmask (int w, int h);//creates a new pmask and initializes it
84 void destroy_pmask(struct PMASK *mask);//destroys a pmask created by create_pmask
86 #if defined USE_ALLEGRO
87 void init_allegro_pmask(struct PMASK *mask, struct BITMAP *sprite);
88 PMASK *create_allegro_pmask(struct BITMAP *sprite);
89 #endif
91 #if defined USE_SDL
92 void init_sdl_pmask(struct PMASK *mask, struct SDL_Surface *sprite, int trans_color);
93 PMASK *create_sdl_pmask(struct SDL_Surface *sprite, int trans_color);
94 #endif
96 #ifdef __cplusplus
98 #endif
100 #define MASK_WORD_SIZE sizeof(MASK_WORD_TYPE)
101 #define MASK_WORD_BITS (MASK_WORD_SIZE*8)
103 #if defined pmask_USE_INLINED
104 inline int _get_pmask_pixel(struct PMASK *mask, int x, int y) {
105 //no bounds checking
106 return 1 & (mask->mask[(mask->h * (x >> MASK_WORD_BITBITS)) + y] >> (x & (MASK_WORD_BITS-1)));
108 inline void _set_pmask_pixel(struct PMASK *mask, int x, int y, int value) {
109 //no bounds checking
110 if (value) {
111 mask->mask[(mask->h * (x >> MASK_WORD_BITBITS)) + y] |= 1 << (x & (MASK_WORD_BITS-1));
112 } else {
113 mask->mask[(mask->h * (x >> MASK_WORD_BITBITS)) + y] &=~(1 << (x & (MASK_WORD_BITS-1)));
116 #else
117 #define _get_pmask_pixel get_pmask_pixel
118 #define _set_pmask_pixel set_pmask_pixel
119 #endif
122 #endif /* ifndef PPCOL_H */