This is was originally coded by IanM, this is the C++ conversion for DGDK. Please test this and let me know your experience with using it.
Original Code:
function SpritePixelCollision(sprite1 as integer, sprite2 as integer)
local Sprite1Base as dword
local Sprite2Base as dword
local Sprite1Addr as dword
local Sprite2Addr as dword
local V as byte
if sprite collision (sprite1, sprite2) > 0
make memblock from image 255,sprite image(sprite1)
make memblock from image 256,sprite image(sprite2)
Sprite1Base=get memblock ptr(255)
Sprite2Base=get memblock ptr(256)
XPos1=sprite x(sprite1)-sprite offset x(sprite1)
YPos1=sprite y(sprite1)-sprite offset y(sprite2)
Width1=*Sprite1Base
inc Sprite1Base, 4
Height1=*Sprite1Base
inc Sprite1Base, 11
XPos2=sprite x(sprite2)-sprite offset x(sprite2)
YPos2=sprite y(sprite2)-sprite offset y(sprite2)
Width2=*Sprite2Base
inc Sprite2Base, 4
Height2=*Sprite2Base
inc Sprite2Base, 11
if XPos2 > XPos1
XStart1=XPos2-XPos1
XStart2=0
XOverlapSize=(Width1-XStart1)-1
if XOverlapSize >= Width2 then XOverlapSize=Width2-1
else
XStart2=XPos1-XPos2
XStart1=0
XOverlapSize=(Width2-XStart2)-1
if XOverlapSize >= Width1 then XOverlapSize=Width1-1
endif
if YPos2 > YPos1
YStart1=YPos2-YPos1
YStart2=0
YOverlapSize=(Height1-YStart1)-1
if YOverlapSize >= Height2 then YOverlapSize=Height2-1
else
YStart2=YPos1-YPos2
YStart1=0
YOverlapSize=(Height2-YStart2)-1
if YOverlapSize >= Height1 then YOperlapSize=Height1-1
endif
XStart1=XStart1*4
XStart2=XStart2*4
Width1=Width1*4
Width2=Width2*4
for YOffset=YOverlapSize to 0 step -1
Sprite1Addr=Sprite1Base + ((YOffset+YStart1)*Width1) + XStart1
Sprite2Addr=Sprite2Base + ((YOffset+YStart2)*Width2) + XStart2
for XOffset=XOverlapSize to 0 step -1
v=*Sprite1Addr
if v <> 0
v=*Sprite2Addr
if v <> 0
delete memblock 255
delete memblock 256
exitfunction 1
endif
endif
inc Sprite1Addr, 4
inc Sprite2Addr, 4
next XOffset
next YOffset
delete memblock 255
delete memblock 256
endif
endFunction 0
DarkGDK Conversion:
bool SpritePixelCollision(int sprite1, int sprite2)
{
DWORD *Sprite1Base, *Sprite2Base, *Sprite1Addr, *Sprite2Addr;
int XPos1, YPos1, Width1, Height1;
int XPos2, YPos2, Width2, Height2;
int XStart1, YStart1, XStart2, YStart2, XOverlapSize, YOverlapSize;
char V;
if (dbSpriteCollision(sprite1, sprite2) > 0)
{
dbMakeMemblockFromImage(255, dbSpriteImage(sprite1));
dbMakeMemblockFromImage(256, dbSpriteImage(sprite2));
Sprite1Base = (DWORD*)dbGetMemblockPtr(255);
Sprite2Base = (DWORD*)dbGetMemblockPtr(256);
XPos1 = dbSpriteX(sprite1) - dbSpriteOffsetX(sprite1);
YPos1 = dbSpriteY(sprite1) - dbSpriteOffsetY(sprite2);
Width1 = *Sprite1Base;
Sprite1Base += 4;
Height1 = *Sprite1Base;
Sprite1Base += 11;
XPos2 = dbSpriteX(sprite2) - dbSpriteOffsetX(sprite2);
YPos2 = dbSpriteY(sprite2) - dbSpriteOffsetY(sprite2);
Width2 = *Sprite2Base;
Sprite2Base += 4;
Height2 = *Sprite2Base;
Sprite2Base += 11;
if (XPos2 > XPos1)
{
XStart1 = XPos2 - XPos1;
XStart2 = 0;
XOverlapSize = (Width1 - XStart1) - 1;
if (XOverlapSize >= Width2)
XOverlapSize = Width2 - 1;
}
else
{
XStart2 = XPos1 - XPos2;
XStart1 = 0;
XOverlapSize = (Width2 - XStart2) - 1;
if (XOverlapSize >= Width1)
XOverlapSize = Width1 - 1;
}
if (YPos2 > YPos1)
{
YStart1 = YPos2 - YPos1;
YStart2 = 0;
YOverlapSize = (Height1 - YStart1) - 1;
if (YOverlapSize >= Height2)
YOverlapSize = Height2 - 1;
}
else
{
YStart2 = YPos1 - YPos2;
YStart1 = 0;
YOverlapSize = (Height2 - YStart2) - 1;
if (YOverlapSize >= Height1)
YOverlapSize = Height1 - 1;
}
XStart1 = XStart1 * 4;
XStart2 = XStart2 * 4;
Width1 = Width1 * 4;
Width2 = Width2 * 4;
// ATTENTION: I don't know if the 'for next' loop includes 0 when going down to 0, that will have to be checked.
for (int YOffset = YOverlapSize; YOffset > 0; YOffset--) // 'for YOffset = YOverlapSize to 0 step -1'
{
Sprite1Addr = Sprite1Base + ((YOffset+YStart1)*Width1) + XStart1;
Sprite2Addr = Sprite2Base + ((YOffset+YStart2)*Width2) + XStart2;
for (int XOffset = XOverlapSize; XOffset > 0; XOffset--) // 'for XOffset = XOverlapSize to 0 step -1'
{
V = *Sprite1Addr;
if (V != 0)
{
V = *Sprite2Addr;
if (V != 0)
{
dbDeleteMemblock(255);
dbDeleteMemblock(256);
return 1;
}
}
Sprite1Addr += 4;
Sprite2Addr += 4;
}
}
dbDeleteMemblock(255);
dbDeleteMemblock(256);
}
return 0;
}
Thanks in advanced for testing this. Please let me know how it turns out.
www.Helios-Online.net