- 15 Jul 2014 02:06:16 pm
- Last edited by KermMartian on 15 Jul 2014 02:55:38 pm; edited 1 time in total
Oh god, why do you hate whitespace so much, ProgrammerNerd? Here's a more consistent and readable version of your routine, with no major code changes (please see ProgrammerNerd's post for more details on using this):
Code:
Code:
/* Note it is up to the caller to ensure that alpha is in range of 1-31.
* Also make sure width is a multiple of two.
*/
void CopySpriteMaskedAlpha(unsigned *data, unsigned x, unsigned y, unsigned width, unsigned height,
unsigned maskcolor, unsigned alpha)
{
unsigned short* v = (unsigned short*)0xa8000000;
unsigned* VRAM, w, alphai;
width >>= 1; // Most compilers know int/2 = int>>1; just making sure
v += 384 * y + x;
VRAM = v;
alphai = 32 - alpha;
// Note I have decided to do a minor tradeoff for speed.
// Make sure your alpha pixels are multiple of two*/
maskcolor |= maskcolor << 8;
// Note that this will not work for height=0 or width=0
while(height--) {
w = width;
while(w--) {
unsigned color1 = *data++;
if (color1 != maskcolor) {
// Note this is based on the source code of Fblend's function fblend_rect_trans_16
unsigned rbg_source, grb_source, temp1;
// Split up components to allow us to do operations
// on all of them at the same time
rbg_source = *VRAM & 0xF81F07E0,
grb_source = *VRAM & 0x07E0F81F;
// Multiply the source by the factor
rbg_source = ((rbg_source >> 5) * alpha) & 0xF81F07E0;
grb_source = ((grb_source * alpha) >> 5) & 0x07E0F81F;
// Split up RGB -> R-B and G
temp1 = color1 & 0x7E0F81F;
color1 &= 0xF81F07E0;
// Multiply the source by the factor
color1 = ((((color1 >> 5) * alphai) & 0xF81F07E0) + rbg_source) & 0xF81F07E0;
temp1 = ((((temp1 * alphai) >> 5) & 0x07E0F81F) + grb_source) & 0x07E0F81F;
color1 |= temp1;
*VRAM++ = color1;
} else {
++VRAM;
}
}
VRAM += (384/2) - width;
}
}