Découpe automatique de sprites. Il est possible d'ajuster la taille des sprites pour quelle soit minimal autour de l'élement si c'est nécessaire. Je le rajoute si tu le souhaite.
Code : Tout sélectionner
;Automatic PNG Crop by Fig
Image.s=OpenFileRequester("Choisissez un fichier à charger", FichierParDefaut$, Filtre$, Filtre)
If InitSprite() = 0 Or InitKeyboard()=0 Or InitMouse()=0:MessageRequester("Error","Error DirectX",0):EndIf
If OpenWindow(0,0,0,1024,768, "Découpe automatique de sprite ", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget)
If OpenWindowedScreen(WindowID(0),0,0,1024,768,0,0,0,#PB_Screen_NoSynchronization)=0
MessageRequester("Erreur", "Impossible d'ouvrir un écran dans la fenêtre!", 0)
End
EndIf
EndIf
UsePNGImageDecoder()
Structure selection
x1.i
x2.i
EndStructure
Structure pixel
pixel.l
EndStructure
;charger le sprite à découper
If image<>""
LoadSprite(1,image,#PB_Sprite_AlphaBlending)
EndIf
Procedure PointFast(x.i,y.i,buffer.i,pitch.i)
*Line.Pixel = Buffer+Pitch*y+x*4
couleur.l=*line\pixel
If Alpha(couleur):ProcedureReturn 1:EndIf
ProcedureReturn 0
EndProcedure
Procedure CropAll(sprite.i)
Dim check.i(SpriteWidth(sprite),SpriteHeight(sprite))
newsprite=sprite
NewList explore.point()
NewMap selection.selection()
For dy=0 To SpriteHeight(sprite)-1
For dx=0 To SpriteWidth(sprite)-1
If check(dx,dy)=1:Continue:EndIf
x=dx:y=dy
;{- baguette magique en X,Y: selection tout ce qui n'a pas un alpha de 0
StartDrawing(SpriteOutput(sprite))
Buffer = DrawingBuffer()
Pitch = DrawingBufferPitch()
DrawingMode(#PB_2DDrawing_AlphaBlend)
color.i=Point(x,y)
DrawingMode(#PB_2DDrawing_Default)
StopDrawing()
If Alpha(color)=0:Continue:EndIf
check(x,y)=1
AddElement(explore())
explore()\x=x
explore()\y=y
time = ElapsedMilliseconds()
Repeat
x=Explore()\x
y=Explore()\y
DeleteElement(explore())
xi=x
xj=x
;agrandit vers la gauche
While xi>0 And PointFast(xi,y,buffer,pitch)
check(xi,y)=1
xi-1
Wend
xi+1
AddMapElement(selection(),Str(y))
selection()\x1=xi
;Agrandit vers la droite
While xj<SpriteWidth(sprite) And PointFast(xj,y,buffer,pitch)
check(xj,y)=1
xj+1
Wend
xj-1
selection()\x2=xj
;nord
flag=0
yy=y-1
If yy>0
; SectionFound = #False
For i=xi To xj
If check(i,yy):Continue:EndIf
If PointFast(i,yy,buffer,pitch)
If Not SectionFound
AddElement(Explore())
Explore()\x=i
Explore()\y=yy
SectionFound = #True
EndIf
Else
SectionFound = #False
EndIf
Next i
EndIf
;sud
flag=0
yy+2
If yy<SpriteHeight(sprite)
SectionFound = #False
For i=xi To xj
If check(i,yy):Continue:EndIf
If PointFast(i,yy,buffer,pitch)
If Not SectionFound
AddElement(Explore())
Explore()\x=i
Explore()\y=yy
SectionFound = #True
EndIf
Else
SectionFound = #False
EndIf
Next i
EndIf
Until ListSize(explore())=0
ClearList(explore())
;}
newsprite+1
;on duplique le sprite original
CopySprite(sprite,newsprite,#PB_Sprite_AlphaBlending)
;on efface dans ce sprite les zones qui ne sont pas selectionnées
StartDrawing(SpriteOutput(newsprite))
Memsprite3=DrawingBuffer()
Pitch3=DrawingBufferPitch()
StopDrawing()
For zy=0 To SpriteHeight(newsprite)-1
*line3.pixel=memsprite3+pitch3*zy
For zx=0 To SpriteWidth(newsprite)-1
If FindMapElement(selection(),Str(zy))=0 Or zx<selection()\x1 Or zx>selection()\x2
*Line3\Pixel=0
EndIf
*line3+4
Next zx
Next zy
ClearMap(selection())
Next dx
Next dy
EndProcedure
;découpe le sprite 1 et sauvegarde les morceaux dans les sprites suivants... 2,3 etc...
CropAll(1)
Repeat
Repeat:Until WindowEvent()=0
FlipBuffers()
ClearScreen(#White)
ExamineKeyboard()
;affiche les sprites créés
x=0:y=0
For i=1 To 5
If IsSprite(i)
DisplayTransparentSprite(i,x,y)
x+SpriteWidth(i)
If x+SpriteWidth(i)>1024:x=0:y+SpriteHeight(i):EndIf
EndIf
Next i
Until KeyboardPushed(#PB_Key_Escape)