Funktion sollte selbsterklärend sein. Für weniger als 32Bit nicht vergessen die Bildtiefe anzupassen.
Es handelt sich um ein leicht modifiziertes Beispiel aus der Hilfe.
Code: Alles auswählen
Structure Pixel
Pixel.l
EndStructure
Procedure SpriteReplaceColor(sprite.l,color1.l, color2.l) ;replace color1 by color2
Protected Buffer.l
Protected Pitch.l
Protected PixelFormat.l
Protected x.l
Protected y.l
Protected Width.l
Protected Height.l
Protected *Line.Pixel
If StartDrawing(SpriteOutput(sprite))
Buffer = DrawingBuffer()
Pitch = DrawingBufferPitch()
PixelFormat = DrawingBufferPixelFormat()
If PixelFormat = #PB_PixelFormat_32Bits_BGR
color1 = (color1 & $0000FF) << 16 | (color1 & $00FF00) | (color1 & $FF0000) >> 16
color2 = (color2 & $0000FF) << 16 | (color2 & $00FF00) | (color2 & $FF0000) >> 16
EndIf
Width = SpriteWidth(sprite)
Height = SpriteHeight(sprite)
For y = 0 To Height-1
*Line.Pixel = Buffer+Pitch*y
For x = 0 To Width-1
If *Line\Pixel = color1
*Line\Pixel = color2
EndIf
*Line+4
Next
Next
StopDrawing()
EndIf
EndProcedure
;-------------------- Test ---------------------------
InitSprite()
InitKeyboard()
OpenScreen(800,600,32,"klj")
Define a.l
Define b.l
Define Sprite_a = CreateSprite(#PB_Any,40,40)
StartDrawing(SpriteOutput(Sprite_a))
For a=0 To 3
For b=0 To 3
If (a+b)%2 = 1
Box(10*a,10*b,10,10,RGB(0,255,0))
Else
Box(10*a,10*b,10,10,RGB(255,0,255))
EndIf
Next
Next
StopDrawing()
Define Sprite_b = CopySprite(Sprite_a,#PB_Any)
Define Sprite_c = CopySprite(Sprite_a,#PB_Any)
SpriteReplaceColor(Sprite_b,RGB(0,255,0), RGB(255,0,0))
SpriteReplaceColor(Sprite_c,RGB(255,0,255),RGB(255,0,0))
Repeat
ExamineKeyboard()
ClearScreen(RGB(0,0,0))
DisplaySprite(Sprite_a,75, 50)
DisplaySprite(Sprite_b,50, 100)
DisplaySprite(Sprite_c,100,100)
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)