J'aimerais optimiser ce code, afin que ça rame moins.
Si vous avez des idées d'optimisations possible, ça m'intéresse

Code : Tout sélectionner
;{ Enumeration
#PanelToolW = 105
Enumeration ; images
#ImageFond
#Calque1
#Calque2
#Calque3
#ImageAffichage
EndEnumeration
Enumeration ;Gadgets
#Canvas
#G_BrushSize
#G_BrushScatter
#G_BrushColor
#G_BrushAlpha
EndEnumeration
;}
;{ Structures
Structure St_Brush
Scatter.w
Size.w
Color.i
Alpha.i
EndStructure
Global Brush.St_Brush
Brush\Scatter = 2
Brush\Color = RGBA(0,0,0,255)
Brush\Size = 5
Brush\Alpha = 255
;}
;{ procedures
Procedure ThickLine(x1, y1, x2, y2, size, color=0)
; BY BP - forum english
Protected dx, dy, e2.f, err, s_x, s_y, a_err
If size < 1 : size = 1 : EndIf
size - 1
dx = Abs(x2 - x1) : dy = Abs(y2 - y1) : err = dx - dy
If x1 < x2 : s_x = 1 : Else : s_x = -1 : EndIf
If y1 < y2 : s_y = 1 : Else : s_y = -1 : EndIf
Repeat
; add scatter
rndY = Random(Brush\Scatter) - Random(Brush\Scatter)
rndX = Random(Brush\Scatter) - Random(Brush\Scatter)
; drawing
Circle(x1 + rndX, y1 + rndY, size, color)
If x1 = x2 And y1 = y2 : Break : EndIf
e2 = err + err ;* Brush\Size * Brush\Pass/100
If e2 > -dy
err - dy
x1 + s_x
EndIf
If e2 < dx
err + dx
y1 + s_y
EndIf
ForEver
EndProcedure
Macro UpdateDrawing()
If StartDrawing(CanvasOutput(#Canvas))
; draw background
DrawImage(ImageID(#ImageFond), 0, 0)
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawAlphaImage(ImageID(#calque1), 0, 0)
StopDrawing()
EndIf
EndMacro
;}
;{ openwindow
If OpenWindow(0, 0, 0, 1000, 600, "Drawing Optimisation", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
If ContainerGadget(#PB_Any, 0, 0, #PanelToolW-5, 600, #PB_Container_Single)
TrackBarGadget(#G_BrushSize, 2, 50, #PanelToolW-10, 20, 1, 50)
TrackBarGadget(#G_BrushScatter, 2, 80, #PanelToolW-10, 20, 0, 50)
TrackBarGadget(#G_BrushAlpha, 2, 110, #PanelToolW-10, 20, 0, 255)
ButtonGadget(#G_BrushColor, 1, 140,#PanelToolW-10, 20, "Color")
CloseGadgetList()
EndIf
; If ScrollAreaGadget(#PB_Any, #PanelToolW , 0, WindowWidth(0) - #PanelToolW - 5, WindowHeight(0), 1000, 1000)
If CanvasGadget(#Canvas, #PanelToolW, 0, 800, 600) : EndIf
; CloseGadgetList()
; EndIf
;{ the layers, images...
If CreateImage(#calque1, 800, 600, 32, #PB_Image_Transparent) : EndIf
; background layer
If CreateImage(#ImageFond, 800, 600, 32) And StartDrawing(ImageOutput(#ImageFond))
#box_size=7
Box(0, 0, 800, 600, $FFFFFF)
For y=0 To 600 Step #box_size * 2
For x=0 To 800 Step #box_size * 2
Box(x, y, #box_size, #box_size, $C0C0C0)
Box(x+#box_size, y+#box_size, #box_size, #box_size, $C0C0C0)
Next
Next
StopDrawing()
EndIf
; drawing layer
If CreateImage(#ImageAffichage, 800, 600, 32) And StartDrawing(ImageOutput(#ImageFond))
DrawImage(ImageID(#ImageFond), 0, 0)
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawAlphaImage(ImageID(#calque1), 0, 0)
StopDrawing()
EndIf
SmartWindowRefresh(0, 1)
;}
UpdateDrawing()
;}
Repeat
Event = WaitWindowEvent()
EventGadget = EventGadget()
EventType = EventType()
Select event
Case #PB_Event_Gadget
Select EventGadget
Case #G_BrushSize
brush\Size = GetGadgetState(#G_BrushSize)
Case #G_BrushScatter
brush\Scatter = GetGadgetState(#G_BrushScatter)
Case #G_BrushColor
color = ColorRequester(brush\color)
Brush\Color = RGBA(Red(color), Green(color), Blue(color), brush\alpha)
Case #G_BrushAlpha
brush\Alpha = GetGadgetState(#G_BrushAlpha)
Brush\Color = RGBA(Red(Brush\Color), Green(Brush\Color), Blue(Brush\Color), brush\alpha)
Case #canvas
Select eventType
Case #PB_EventType_LeftButtonDown
X = WindowMouseX(0) - #PanelToolW
Y = WindowMouseY(0)
StartX = WindowMouseX(0) - #PanelToolW
StartY = WindowMouseY(0)
paint = 1
Case #PB_EventType_LeftButtonUp
paint = 0
Case #PB_EventType_MouseMove
If paint
StartX = WindowMouseX(0) - #PanelToolW
StartY = WindowMouseY(0)
If StartDrawing(ImageOutput(#calque1))
DrawingMode(#PB_2DDrawing_AlphaBlend)
ThickLine(StartX, StartY, X, Y, Brush\Size, Brush\color)
StopDrawing()
EndIf
X = WindowMouseX(0) - #PanelToolW
Y = WindowMouseY(0)
UpdateDrawing()
EndIf
EndSelect
EndSelect
EndSelect
Until event =#PB_Event_CloseWindow
EndIf