Mail Archives: djgpp/1997/03/31/03:47:00
>I have a problem using this little function :
>unsigned int Maschera[30];
>void Crea_Mask ( BITMAP *p )
>{
> int a, b;
> for (a = 0; a < p->h; a++)
> {
> Maschera[a] = 0;
> for (b = 0; b < p->w; b++)
> if (p->line[a][b] != 0)
> Maschera[a] = Maschera[a] | (int) pow ((int) 2, (int) (p->w - b));
> }
>}
>into a program. After a while it return a SIGFPE error (I really do not
>understand why. I used UINT and *not* float).
1. Pow accepts two double arguments and returns double.
In normal case compiler will make implicit type casting from int to double.
But if you did not declare pow (e.g. by including math.h),
compiler will not know of arguments types and
will produce incorrect result. Use -Wall switch to see
if it is the case.
2. Maybe p->h is larger than 30?
P.S. For getting power of two it is better to use either table lookup:
unsigned int power_of_two[32] = { 1, 2, 4, 8, ... };
..
Maschera[a] |= power_of_two[p->w - b]; /* p->w must be less than 32. */
or bit shifts:
int a, b;
unsigned int bit_mask;
for (...)
{
/* p->w must be less than 32. */
for (b = 0, bit_mask = power_of_two[p->w]; b < p->w; b++, bit_mask <<= 1)
if (...)
Maschera[a] |= bit_mask;
}
>As you can understand, the function take a sprite and create the mask for a
>pixel collision detection.
>Thank you!
- Raw text -