What is InvalidateRect_() ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
charvista
Addict
Addict
Posts: 969
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: What is InvalidateRect_() ?

Post by charvista »

@RASHAD Hi
Wow, I appreciate the code enhancements, my friend. Looks like you worked all night...
You teached me a lot here and am very grateful - thank you.
As the code has radically changed, I cannot change my current code. But the next one :wink:

Only backside is that it uses some API, so not portable - but I'm using Windows only, so no problem.
- Windows 11 Home 64-bit
- PureBasic 6.30 (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 8K monitor with DPI @ 300%
User avatar
Michael Vogel
Addict
Addict
Posts: 2847
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: What is InvalidateRect_() ?

Post by Michael Vogel »

Some more variations (#ScreenMode=0/1)...

Code: Select all

#ScreenMode=1

Enumeration
    #Window
    #Image_Gadget
    #Image_Gradient
    #Image_Text
    #Font
EndEnumeration

Procedure ImageGadgetText(gadget,background,text.s,mode=#False,color=#Black,shadow=0)

    Protected x,y

    If mode
        StartDrawing(ScreenOutput())
        DrawImage(ImageID(background),0,0)
    Else
        CopyImage(background,#Image_Text)
        StartDrawing(ImageOutput(#Image_Text))
    EndIf

    DrawingMode(#PB_2DDrawing_Transparent)
    DrawingFont(FontID(#Font))

    x=(ImageWidth(background)-TextWidth(text))>>1
    y=(ImageHeight(background)-TextHeight(text))>>1

    If Shadow=2
        DrawText(X+2,Y+2,Text,$202020); double shadow always dark gray
    EndIf
    If Shadow=2 Or Shadow=1
        DrawText(X+1,Y+1,Text,$000000) ;shadows always black
    EndIf
    DrawText(X,Y,Text.s,Color)
    StopDrawing()

    If mode
        FlipBuffers()
    Else
        SetGadgetState(gadget,ImageID(#Image_Text))
    EndIf

EndProcedure
Procedure ImageGradient(image,w,h)

    CreateImage(image,w,h,32)

    StartDrawing(ImageOutput(image))
    Box(0,0,w,h,$FFFFFF)
    DrawingMode(#PB_2DDrawing_Gradient)
    BackColor($0000FF)
    GradientColor(0.4, $00FFFF)
    GradientColor(0.6, $FFFF00)
    FrontColor($FF0000)
    LinearGradient(0,0,w,h)
    Box(0,0,w,h)
    StopDrawing()

    ProcedureReturn ImageID(image)

EndProcedure

LoadFont(#Font,"Tahoma",8,#PB_Font_Bold)
Win=OpenWindow(#Window,0,0,600,500,"Drawing Example",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
ImageGradient(#Image_Gradient,600,500)

If #ScreenMode
    InitSprite()
    OpenWindowedScreen(Win,0,0,600,500,0,0,0)
Else
    ImageGadget(#Image_Gadget,0,0,600,500,ImageID(#Image_Gradient))
    DisableGadget(#Image_Gadget,#True)
EndIf

ImageGadgetText(#Image_Gadget,#Image_Gradient,"A loop 1..1000 will be launched...",#ScreenMode,#White,2)
Delay(1000)
For i=1 To 1000
    ImageGadgetText(#Image_Gadget,#Image_Gradient,"Program Loop is now at "+Str(i),#ScreenMode)
Next

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
User avatar
charvista
Addict
Addict
Posts: 969
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: What is InvalidateRect_() ?

Post by charvista »

Nec plus ultra, Michael !
No flickerings during the loop, absoultely perfect. Even X and Y are professionally set.
I thought that Sprites were only for games' screens, apparently it can also be used in normal applications. I have to study this closer, as it has his advantages.

I added a progress bar.
The progress bar is flickering, what should I do to avoid it flickering?

Code: Select all

;(c) Michael Vogel
#ScreenMode=2

    Enumeration
        #Window
        #Image_Gadget
        #Image_Gradient
        #Image_Text
        #Font
        #ProgBar
    EndEnumeration

    Procedure ImageGadgetText(gadget,background,text.s,mode=#False,color=#Black,shadow=0)

        Protected x,y

        If mode
            StartDrawing(ScreenOutput())
            DrawImage(ImageID(background),0,0)
        Else
            CopyImage(background,#Image_Text)
            StartDrawing(ImageOutput(#Image_Text))
        EndIf

        DrawingMode(#PB_2DDrawing_Transparent)
        DrawingFont(FontID(#Font))

        x=(ImageWidth(background)-TextWidth(text))>>1
        y=(ImageHeight(background)-TextHeight(text))>>1

        If Shadow=2
            DrawText(X+2,Y+2,Text,$202020); double shadow always dark gray
        EndIf
        If Shadow=2 Or Shadow=1
            DrawText(X+1,Y+1,Text,$000000) ;shadows always black
        EndIf
        DrawText(X,Y,Text.s,Color)
        StopDrawing()
        
        If mode
            FlipBuffers()
        Else
            SetGadgetState(gadget,ImageID(#Image_Text))
        EndIf

    EndProcedure
    Procedure ImageGradient(image,w,h)

        CreateImage(image,w,h,32)

        StartDrawing(ImageOutput(image))
        Box(0,0,w,h,$FFFFFF)
        DrawingMode(#PB_2DDrawing_Gradient)
        BackColor($0000FF)
        GradientColor(0.4, $00FFFF)
        GradientColor(0.6, $FFFF00)
        FrontColor($FF0000)
        LinearGradient(0,0,w,h)
        Box(0,0,w,h)
        StopDrawing()

        ProcedureReturn ImageID(image)

    EndProcedure

    LoadFont(#Font,"Tahoma",8,#PB_Font_Bold)
    Win=OpenWindow(#Window,0,0,600,500,"Drawing Example",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
    ImageGradient(#Image_Gradient,600,500)

    If #ScreenMode
        InitSprite()
        OpenWindowedScreen(Win,0,0,600,500,0,0,0)
    Else
        ImageGadget(#Image_Gadget,0,0,600,500,ImageID(#Image_Gradient))
        DisableGadget(#Image_Gadget,#True)
    EndIf

    ImageGadgetText(#Image_Gadget,#Image_Gradient,"A loop 1..1000 will be launched...",#ScreenMode,#White,2)
    Delay(1000)
    
    ProgressBarGadget(#ProgBar,10,WindowHeight(#Window)-40,WindowWidth(#Window)-20,30,0,1000,#PB_ProgressBar_Smooth)
     
    For i=1 To 1000
        ImageGadgetText(#Image_Gadget,#Image_Gradient,"Program Loop is now at "+Str(i),#ScreenMode)
        SetGadgetState(#ProgBar,i)
    Next

    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
- Windows 11 Home 64-bit
- PureBasic 6.30 (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 8K monitor with DPI @ 300%
User avatar
Michael Vogel
Addict
Addict
Posts: 2847
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: What is InvalidateRect_() ?

Post by Michael Vogel »

charvista wrote: The progress bar is flickering, what should I do to avoid it flickering?
Hmm, closing your eyes? :oops:

Never played around with OpenwindowedScreen before nor mixing it up with gadgets, without the screen command which allow syncing with the screen refresh of the monitor to avoid flickering, I'm not sure to get it solved easily...

Some guesses (quick tries failed for now to solve your problem):
• RedrawWindow_(ID,#Null,#Null,#RDW_VALIDATE)
• SendMessage_(MyID,#WM_SETREDRAW,#False,0)
• drawing everything on a hidden window and grabbing the content to the openwindowed screen (definitely a lot of work)
• doing a vsync (but how?)

Good luck :)
Post Reply