Until 3.92 is released i offer you this piece of code. i was bored today
Code: Select all
;Declare Stuff for Cross-Platform Pixel Perfect Collision
Declare SpritePixelCollision2(SprOne.l, SprOneX.l, SprOneY.l, SprTwo.l, SprTwoX.l, SprTwoY.l) ;Cross-Platform SpritePixelCollision Procedure for Win and Linux
Global SpritePixelCollision2Color.l
SpritePixelCollision2Color = RGB(255,0,255) ;set default transparent color to Magenta
and here is the procedure:
Code: Select all
;SpritePixelCollision2
;used like SpritePixelCollision(#Sprite1, x1, y1, #Sprite2, x2, y2)
;makes SpritePixelCollision available for Linux games, too.
;created 17-December-2004 by Benedikt Engelhard (fleecer99@yahoo.de)
Procedure.l SpritePixelCollision2(SprOne.l, SprOneX.l, SprOneY.l, SprTwo.l, SprTwoX.l, SprTwoY.l)
;
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_AmigaOS
;no support for amiga platform
CompilerCase #PB_OS_Linux
; hand-written pixel perfect collision detection for linux,
; using only PureBasic commands
ColX.l = 0
ColY.l = 0
ColH.l = 0
ColW.l = 0
If SprOneX = SprTwoX
ColX = SprOneX
if SpriteWidth(SprOne) = SpriteWidth(SprTwo)
ColW = SpriteWidth(SprOne)
else
if SpriteWidth(SprOne) > SpriteWidth(SprTwo)
ColW = SpriteWidth(SprTwo)
else
ColW = SpriteWidth(SprOne)
endif
endif
Else
if SprOneX < SprTwoX
ColW = (SprOneX + SpriteWidth(SprOne)) - SprTwoX
ColX = SprTwoX
else
ColW = (SprTwoX + SpriteWidth(SprTwo)) - SprOneX
ColX = SprOneX
endif
EndIf
if ColW > SpriteWidth(SprTwo)
ColW = SpriteWidth(SprTwo)
endif
if ColW > SpriteWidth(SprOne)
ColW = SpriteWidth(SprOne)
endif
If SprOneY = SprTwoY
ColY = SprOneY
if SpriteHeight(SprOne) = SpriteHeight(SprTwo)
ColH = SpriteHeight(SprOne)
else
if SpriteHeight(SprOne) > SpriteHeight(SprTwo)
ColH = SpriteHeight(SprTwo)
else
ColH = SpriteHeight(SprOne)
endif
endif
Else
if SprOneY < SprTwoY
ColH = (SprOneY + SpriteHeight(SprOne)) - SprTwoY
ColY = SprTwoY
else
ColH = (SprTwoY + SpriteHeight(SprTwo)) - SprOneY
ColY = SprOneY
endif
EndIf
if ColH > SpriteHeight(SprTwo)
ColH = SpriteHeight(SprTwo)
endif
if ColH > SpriteHeight(SprOne)
ColH = SpriteHeight(SprOne)
endif
Dim SprOnePixels.b(ColW-1,ColH-1)
Dim SprTwoPixels.b(ColW-1,ColH-1)
g.l=0
h.l=0
StartDrawing(SpriteOutput(SprOne))
nx.l=0
ny.l=0
if ColX > SprOneX
nx = ColX - SprOneX
endif
if ColY > SprOneY
ny = ColY - SprOneY
endif
for g = 0 to ColW-1
for h = 0 to ColH-1
if Point(nx+g, ny+h) = SpritePixelCollision2Color
SprOnePixels(g, h)=0
else
SprOnePixels(g, h)=1
endif
next h
next g
StopDrawing()
StartDrawing(SpriteOutput(SprTwo))
nx.l=0
ny.l=0
if ColX > SprTwoX
nx = ColX - SprTwoX
endif
if ColY > SprTwoY
ny = ColY - SprTwoY
endif
for g = 0 to ColW-1
for h = 0 to ColH-1
if Point(nx+g, ny+h) = SpritePixelCollision2Color
SprTwoPixels(g, h)=0
else
SprTwoPixels(g, h)=1
endif
next h
next g
StopDrawing()
;Uncomment the following lines to see the collision rectangle:
;If StartDrawing(ScreenOutput())
;Box(ColX, ColY, ColW, ColH ,RGB(255,0,0))
for g=0 to ColW-1
for h = 0 to ColH-1
if SprOnePixels(g,h)=1 And SprTwoPixels(g,h)=1
ProcedureReturn 1 ;say yes - there is a pixel collision
endif
next h
next g
; StopDrawing()
;EndIf
ProcedureReturn 0 ;no, no pixel collision
CompilerCase #PB_OS_Windows
; in Windows, we use Fred's built-in routine:
ProcedureReturn SpritePixelCollision(SprOne, SprOneX, SprOneY, SprTwo, SprTwoX, SprTwoY)
CompilerEndSelect
EndProcedure
use it like the normal SpritePixelCollision command!
if anyone wants to contribute, please feel free to optimize this procedure
