[Super-tux-commit] supertux/src bitmask.cpp,1.1,1.2
Brought to you by:
wkendrick
From: Tobias G. <to...@us...> - 2004-08-08 13:07:32
|
Update of /cvsroot/super-tux/supertux/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17499/src Modified Files: bitmask.cpp Log Message: Finally tested and implemented the bitmask_create_SDL function right. It should actually work now and is a good option for an advanced collision detection implementation. Index: bitmask.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/bitmask.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- bitmask.cpp 17 Mar 2004 19:19:10 -0000 1.1 +++ bitmask.cpp 8 Aug 2004 13:07:22 -0000 1.2 @@ -48,38 +48,33 @@ bitmask *bitmask_create_SDL(SDL_Surface* surf) { + bitmask* pbitmask = bitmask_create(surf->w, surf->h); int w,h; - int bpp; - Uint8* p; + SDL_PixelFormat* fmt; + Uint32 temp, pixel; + Uint8 alpha; - bitmask *temp = (bitmask*) malloc(sizeof(bitmask)); - if (! temp) + if(surf->format->BytesPerPixel != 4) //This function only works for true-color surfaces with an alpha channel return 0; - temp->w = surf->w; - temp->h = surf->h; - temp->bits = (long unsigned int*) calloc(surf->h*((surf->w - 1)/BITW_LEN + 1),sizeof(BITW)); - if (! temp->bits) - { - free(temp); - return 0; - } - else - return temp; - - bpp = surf->format->BytesPerPixel; + + fmt = surf->format; for(h = 0; h <= surf->h; ++h) { - for(w = 0; w <= surf->h; ++w) + for(w = 0; w <= surf->w; ++w) { - p = (Uint8 *)surf->pixels + h*surf->pitch + w*bpp; - if(*p == 255) - bitmask_setbit(temp,w,h); + pixel = *((Uint32*)((Uint8 *)surf->pixels + h * surf->pitch + w * 4)); + /* Get Alpha component */ + temp=pixel&fmt->Amask; /* Isolate alpha component */ + temp=temp>>fmt->Ashift;/* Shift it down to 8-bit */ + temp=temp<<fmt->Aloss; /* Expand to a full 8-bit number */ + alpha=(Uint8)temp; + if (fmt->Amask == 0 || alpha != 0) + bitmask_setbit(pbitmask,w,h); } } - + return pbitmask; } - void bitmask_free(bitmask *m) { free(m->bits); |