The efficient solution is to paint only the final color
Very true!
Interesting article on the blog. Understanding what a function exactly does, and be aware of the side effects is about the same as taking a medicine that seems to work, but which is not intended for this particular disease, and knowing the possible side effects leads to more thinking. Medical Doctors are in a way also programmers, and they need to know how the Nature has programmed all the ingredients and how the humans have changed their structure... An error can be fatal. Total failure. System crash. Blue screen. Or blue blood of the death.
Freak is right, calling SetGadgetState() for updating each element in the loop slows down enormously. In the code I removed the LockWindowUpdate_() and moved SetGadgetState() where it will be called only once. The result is also MAGICAL! And... it is portable!
In practice, I think I will now need to add a parameter to zChuiPrint() to either update the image or not. If I say no, it is my responsibility to ensure it will be done separately after the loop.
Thank you Freak.
PS: When you are sick, call the correct procedure from your own Memory so it can program (give orders to) the soldiers of your body (red/white cells, etc.) and you will feel already better. This is called auto-suggestion
Code: Select all
Global F13=LoadFont(#PB_Any,"DOSLike",14)
; The font DOSLike can be found at http://www.uwe-sieber.de/dosfon_e.html, go to the Download Section at the bottom of the page,
; and choose the ANSI Codepage 1252 which is DOSLike.ZIP or DOSLike.EXE. The font is not freeware, but you can download it and test it.
Global _ChrW=10; Chui Character Width (in pixels)
Global _ChrH=20; Chui Character Height (in pixels)
ExamineDesktops() ;necessary to get the Desktop width & height
Global _DW.i = DesktopWidth(0); Desktop resolution width
Global _DH.i = DesktopHeight(0); Desktop resolution height
Global _ChuiScreenW=Int(_DW/_ChrW); Chui Screen Width
Global _ChuiScreenH=Int(_DH/_ChrH); Chui Screen Height
Global _ChuiMadoX=2; X-pos (left margin) standard dialogue
Global _ChuiMadoY=1; Y-pos (top margin) standard dialogue
Global _ChuiMadoW=_ChuiScreenW-(2*_ChuiMadoX); Width standard dialogue
Global _ChuiMadoH=_ChuiScreenH-(2*_ChuiMadoY); Height standard dialogue
Procedure zChuiPrint(Array ImA.i(1),X.i,Y.i,String.s,BgColor.i=$000000,Color.i=$FFFFFF,Filling.s="")
StartDrawing(ImageOutput(ImA(0)))
DrawingMode(#PB_2DDrawing_Transparent)
FrontColor(Color.i)
DrawingFont(FontID(F13))
Box(X*_ChrW,Y*_ChrH,Len(String.s+Filling.s)*_ChrW,_ChrH,BgColor.i)
DrawText(X*_ChrW,Y*_ChrH,String.s+Filling.s)
StopDrawing()
;SetGadgetState(ImA(1),ImageID(ImA(0)))
EndProcedure
Procedure.i zBaseImage(Array ImA.i(1),X.i,Y.i,W.i,H.i)
ImCre=CreateImage(#PB_Any,W,H)
ImGad=ImageGadget(#PB_Any,X,Y,W,H,ImageID(ImCre))
DisableGadget(ImGad,#True)
;InvalidateRect_(GadgetID(ImGad),0,1)
ImA(0)=ImCre
ImA(1)=ImGad
ImA(2)=X
ImA(3)=Y
ImA(4)=W
ImA(5)=H
EndProcedure
Procedure zLoop(ParentWin)
DisableWindow(ParentWin,1)
Win=OpenWindow(#PB_Any,_ChuiMadoX*_ChrW-2,_ChuiMadoY*_ChrH-2,_ChuiMadoW*_ChrW+4,_ChuiMadoH*_ChrH+4,"",#PB_Window_BorderLess|#PB_Window_Invisible,WindowID(ParentWin))
SetWindowColor(Win,$0000FF); this will make the red border
Dim ImA(5)
zBaseImage(ImA(),2,2,_ChuiMadoW*_ChrW,_ChuiMadoH*_ChrH)
zChuiPrint(ImA(),9,1,"Fast")
zChuiPrint(ImA(),19,1,"Fast")
zChuiPrint(ImA(),29,1,"Slow")
zChuiPrint(ImA(),39,1,"Slow")
For i=1 To _ChuiMadoH-9; <============================== FAST ================
zChuiPrint(ImA(),10,i+2,Str(i))
Next i
For i=1 To _ChuiMadoH-9; <============================== FAST ================
zChuiPrint(ImA(),20,i+2,Str(i))
Next i
HideWindow(Win,0)
For i=1 To _ChuiMadoH-9; <============================== SLOW ================
zChuiPrint(ImA(),30,i+2,Str(i))
Next i
For i=1 To _ChuiMadoH-9; <============================== SLOW ================
zChuiPrint(ImA(),40,i+2,Str(i))
Next i
zChuiPrint(ImA(),60,10,"Press ALT+F4 to close this window")
SetGadgetState(ImA(1),ImageID(ImA(0))); following Freak's advice, I removed the LockWindowUpdate_() function, removed the SetGadgetState() in the zChuiPrint() procedure and added the SetGadgetState() here. What is the result? FAST!
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
CloseWindow(Win)
DisableWindow(ParentWin,0)
EndProcedure
MainWin=OpenWindow(#PB_Any,0,0,_DW,_DH,"",#PB_Window_BorderLess)
zLoop(Mainwin)
End