Use an absolute directory under /usr/share/games/ for game data
[kraptor.git] / include / pmask.h
blobe1ec9d4cb48725791b9e08e08bb839cc1064b65e
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 #if defined(__alpha__) || defined(__ia64__) || defined(__x86_64__)
48 #define MASK_WORD_BITBITS 6
49 #else
50 #define MASK_WORD_BITBITS 5
51 #endif
53 //if SINGLE_MEMORY_BLOCK is defined
54 //then each mask will be allocated as
55 //only a single memory block.
56 //this means that masks will (in theory)
57 //be ever-so-slightly faster and more memory efficient
58 //however, if in single memory block mode
59 //then the masks are variable-sized
60 //so you can not use an array of them
61 //#define MASK_SINGLE_MEMORY_BLOCK
63 typedef struct PMASK
65 short int w;//width
66 short int h;//height
67 #ifdef MASK_SINGLE_MEMORY_BLOCK
68 MASK_WORD_TYPE mask[1];//mask data (single memory block)
69 #else
70 MASK_WORD_TYPE *mask;//mask data (pointer at second memory block)
71 #endif
72 } PMASK;
74 void install_pmask(); //sets up library
76 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)
78 int get_pmask_pixel(struct PMASK *mask, int x, int y) ; //returns 0 if mask is clear at those coordinates, 1 if not clear
79 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
81 void init_pmask (struct PMASK *mask, int w, int h); //initializes a PMASK
82 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
83 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
84 void deinit_pmask(PMASK *mask);//de-initializes a pmask
86 PMASK *create_pmask (int w, int h);//creates a new pmask and initializes it
87 void destroy_pmask(struct PMASK *mask);//destroys a pmask created by create_pmask
89 #if defined USE_ALLEGRO
90 void init_allegro_pmask(struct PMASK *mask, struct BITMAP *sprite);
91 PMASK *create_allegro_pmask(struct BITMAP *sprite);
92 #endif
94 #if defined USE_SDL
95 void init_sdl_pmask(struct PMASK *mask, struct SDL_Surface *sprite, int trans_color);
96 PMASK *create_sdl_pmask(struct SDL_Surface *sprite, int trans_color);
97 #endif
99 #ifdef __cplusplus
101 #endif
103 #define MASK_WORD_SIZE sizeof(MASK_WORD_TYPE)
104 #define MASK_WORD_BITS (MASK_WORD_SIZE*8)
106 #if defined pmask_USE_INLINED
107 inline int _get_pmask_pixel(struct PMASK *mask, int x, int y) {
108 //no bounds checking
109 return 1 & (mask->mask[(mask->h * (x >> MASK_WORD_BITBITS)) + y] >> (x & (MASK_WORD_BITS-1)));
111 inline void _set_pmask_pixel(struct PMASK *mask, int x, int y, int value) {
112 //no bounds checking
113 if (value) {
114 mask->mask[(mask->h * (x >> MASK_WORD_BITBITS)) + y] |= 1 << (x & (MASK_WORD_BITS-1));
115 } else {
116 mask->mask[(mask->h * (x >> MASK_WORD_BITBITS)) + y] &=~(1 << (x & (MASK_WORD_BITS-1)));
119 #else
120 #define _get_pmask_pixel get_pmask_pixel
121 #define _set_pmask_pixel set_pmask_pixel
122 #endif
125 #endif /* ifndef PPCOL_H */