Screen goes black temporarily near start of program.

Just starting out? Need help? Post your questions and find answers here.
matalog
Enthusiast
Enthusiast
Posts: 165
Joined: Tue Sep 05, 2017 10:07 am

Screen goes black temporarily near start of program.

Post by matalog »

I have this program, which runs fine. Although, you can see that a second or so after it starts, the screen goes black, then returns to display the colours before blacking out, and adds the additional afterwards. I want to know what is it about the program that causes it to go blac for a quarter of a second?

Code: Select all

InitKeyboard()   
InitSprite()
UseJPEGImageEncoder()



#width=1000
#height=1000
#height1=4000
#width1=4000
#ImgGadget=0

Global.i x,y,xo,yo,ti,c,h,col
Global.i c=1,re=1,gr=1,bl=1
Global.i reo=1,gro=1,blo=1
x=#width/2:y=#height/2
Global.i h=1:ti=1
Global main=OpenWindow(#PB_Any,10,10,#width,#height,"Drawing")
Global wmain=OpenWindowedScreen(WindowID(main),0,0,#width,#height)
Global image=CreateImage(#PB_Any, #width1,#height1) 
ImageGadget(#ImgGadget, 0, 0, #width1, #height1, ImageID(image))
Global imagesc=CreateImage(#PB_Any, #width,#height)
ImageGadget(#ImgGadget, 0, 0, #width, #height, ImageID(imagesc))

Procedure SDRAW()

  StartDrawing(ImageOutput(image))
  Circle(Random(3800,200),Random(3800,200),Random(200,1),Random(16777216,1))
  
 StopDrawing()
 
EndProcedure
Repeat
  
     ExamineKeyboard()
Event = WindowEvent()

    SDRAW()

     CopyImage(image,copy)
    ResizeImage(copy,1000,1000)
    StartDrawing(ImageOutput(imagesc))
    DrawImage(ImageID(scaled),0,0)
    StopDrawing()
    SetGadgetState(#ImgGadget, ImageID(imagesc))
    ;FlipBuffers()
 ;Delay(100)

           If Event = #PB_Event_CloseWindow
             End
           EndIf
    
     If KeyboardPushed(#PB_Key_Escape)
    quit=#True
  EndIf
  If KeyboardPushed(#PB_Key_Space)
  
  Delay(800)  
  EndIf
  If KeyboardPushed(#PB_Key_W)
    SaveImage(image,"Randraw - "+FormatDate("%yyyy%mm%dd_%hh%ii%ss", Date())+Str(Random(9999,1))+".bmp", #PB_ImagePlugin_BMP)
  Delay(500)  
  EndIf
  If Event = #PB_Event_CloseWindow Or KeyboardPushed(#PB_Key_Escape)
    Quit=#True
  ElseIf Event = #PB_Event_Gadget
    EventGadget = EventGadget()
    Select EventGadget        
    EndSelect
  EndIf
    Until quit=#True
    
    
I use the exact same program, with different drawing methods, and the more advanced the program is, the longer it takes for the screen to go black, and the longer it takes for the screen to recover from going black.

I assume it is something to do with how I update the window or have the program react with the window.

One thing I have noticed is that, the mouse pointer appears as a timer until the screen goes black, and after it is back from being black, then the pointer is a proper mouse pointer again. Any ideas what might cause this?
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Screen goes black temporarily near start of program.

Post by Demivec »

Instead of :

Code: Select all

Event = WindowEvent()
; . . .
If Event = #PB_Event_CloseWindow
  End
EndIf
use:

Code: Select all

  Repeat
    Event = WindowEvent()
    If Event = #PB_Event_CloseWindow
      End
    EndIf
  Until Event = 0
  
matalog
Enthusiast
Enthusiast
Posts: 165
Joined: Tue Sep 05, 2017 10:07 am

Re: Screen goes black temporarily near start of program.

Post by matalog »

Demivec wrote: Tue Jul 27, 2021 1:14 am Instead of :

Code: Select all

Event = WindowEvent()
; . . .
If Event = #PB_Event_CloseWindow
  End
EndIf
use:

Code: Select all

  Repeat
    Event = WindowEvent()
    If Event = #PB_Event_CloseWindow
      End
    EndIf
  Until Event = 0
  

I'm not sure what you are telling me to replace in the program.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Screen goes black temporarily near start of program.

Post by infratec »

At the moment you get only one window event per loop.
You need to get all events per loop.

But that's not the reason for the black screen.
The reason is your unneeded overlapping screen.

This is a workaround:

Code: Select all

#width=1000
#height=1000
#height1=4000
#width1=4000
#ImgGadget=0


Global.i x,y,xo,yo,ti,c,h,col
Global.i c=1,re=1,gr=1,bl=1
Global.i reo=1,gro=1,blo=1

Global.i h=1:ti=1


Procedure SDRAW(image.i)
  
  If StartDrawing(ImageOutput(image))
    Circle(Random(3800,200),Random(3800,200),Random(200,1),Random(16777216,1))
    StopDrawing()
  EndIf
  
EndProcedure



InitKeyboard()   
InitSprite()
UseJPEGImageEncoder()


x = #width / 2
y = #height / 2

Global main = OpenWindow(#PB_Any, 10, 10, #width, #height, "Drawing")
Global wmain = OpenWindowedScreen(WindowID(main), 0, 0, 1, 1);#width, #height)

Global image = CreateImage(#PB_Any, #width1,#height1) 
ImageGadget(#ImgGadget, 0, 0, #width1, #height1, ImageID(image))

Global imagesc = CreateImage(#PB_Any, #width,#height)
ImageGadget(#ImgGadget, 0, 0, #width, #height, ImageID(imagesc))


Repeat
  
  Repeat
    Event = WindowEvent()
    Select Event
      Case #PB_Event_Gadget
        EventGadget = EventGadget()
        Select EventGadget        
        EndSelect
      Case #PB_Event_CloseWindow
        Quit=#True
    EndSelect
  Until Event = 0
  
  
  SDRAW(image)
  
  CopyImage(image, copy)
  ResizeImage(copy, 1000, 1000)
  If StartDrawing(ImageOutput(imagesc))
    DrawImage(ImageID(scaled), 0, 0)
    StopDrawing()
    
    SetGadgetState(#ImgGadget, ImageID(imagesc))
  EndIf
  
  
  ExamineKeyboard()
  
  If KeyboardPushed(#PB_Key_Escape)
    quit=#True
  EndIf
  
  If KeyboardPushed(#PB_Key_Space)
    ;Delay(800)
  EndIf
  
  If KeyboardPushed(#PB_Key_W)
    SaveImage(image,"Randraw - "+FormatDate("%yyyy%mm%dd_%hh%ii%ss", Date())+Str(Random(9999,1))+".bmp", #PB_ImagePlugin_BMP)
  EndIf
  
  If KeyboardPushed(#PB_Key_Escape)
    Quit=#True
  EndIf
  
Until quit
But why do you need a screen ???

Use AddKeyboardShortcut() instead.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Screen goes black temporarily near start of program.

Post by infratec »

Maybe this works for you:

Code: Select all

EnableExplicit

#width = 1000
#height = 1000
#height1 = 4000
#width1 = 4000

#ImgGadget = 0

Enumeration
  #Menu_Escape
  #Menu_Space
  #Menu_W
EndEnumeration



Procedure SDRAW(image.i)
  
  If StartDrawing(ImageOutput(image))
    Circle(Random(3800,200),Random(3800,200),Random(200,1),Random($FFFFFF,1))
    StopDrawing()
  EndIf
  
EndProcedure


Define.i Event, EventGadget, copy, quit, main, image, imagesc

main = OpenWindow(#PB_Any, 10, 10, #width, #height, "Drawing")

image = CreateImage(#PB_Any, #width1,#height1) 
ImageGadget(#ImgGadget, 0, 0, #width1, #height1, ImageID(image))

imagesc = CreateImage(#PB_Any, #width,#height)
ImageGadget(#ImgGadget, 0, 0, #width, #height, ImageID(imagesc))

AddKeyboardShortcut(main, #PB_Shortcut_Escape, #Menu_Escape)
AddKeyboardShortcut(main, #PB_Shortcut_Space, #Menu_Space)
AddKeyboardShortcut(main, #PB_Shortcut_W, #Menu_W)

AddWindowTimer(main, 1, 100)

Repeat
  
  Event = WaitWindowEvent()
  Select Event
    Case #PB_Event_Timer
      Select EventTimer()
        Case 1
          SDRAW(image)
          
          CopyImage(image, copy)
          ResizeImage(copy, #width, #height)
          If StartDrawing(ImageOutput(imagesc))
            DrawImage(ImageID(copy), 0, 0)
            StopDrawing()
            
            SetGadgetState(#ImgGadget, ImageID(imagesc))
          EndIf          
      EndSelect
    Case #PB_Event_Menu
      Select EventMenu()
        Case #Menu_Escape
          quit = #True
        Case #Menu_Space
          Delay(800)
        Case #Menu_W
          SaveImage(image,"Randraw - "+FormatDate("%yyyy%mm%dd_%hh%ii%ss", Date())+Str(Random(9999,1))+".bmp", #PB_ImagePlugin_BMP)
      EndSelect
    Case #PB_Event_Gadget
      EventGadget = EventGadget()
      Select EventGadget        
      EndSelect
    Case #PB_Event_CloseWindow
      Quit=#True
  EndSelect
  
Until quit
matalog
Enthusiast
Enthusiast
Posts: 165
Joined: Tue Sep 05, 2017 10:07 am

Re: Screen goes black temporarily near start of program.

Post by matalog »

infratec wrote: Tue Jul 27, 2021 10:20 pm Maybe this works for you:

Code: Select all

EnableExplicit

#width = 1000
#height = 1000
#height1 = 4000
#width1 = 4000

#ImgGadget = 0

Enumeration
  #Menu_Escape
  #Menu_Space
  #Menu_W
EndEnumeration



Procedure SDRAW(image.i)
  
  If StartDrawing(ImageOutput(image))
    Circle(Random(3800,200),Random(3800,200),Random(200,1),Random($FFFFFF,1))
    StopDrawing()
  EndIf
  
EndProcedure


Define.i Event, EventGadget, copy, quit, main, image, imagesc

main = OpenWindow(#PB_Any, 10, 10, #width, #height, "Drawing")

image = CreateImage(#PB_Any, #width1,#height1) 
ImageGadget(#ImgGadget, 0, 0, #width1, #height1, ImageID(image))

imagesc = CreateImage(#PB_Any, #width,#height)
ImageGadget(#ImgGadget, 0, 0, #width, #height, ImageID(imagesc))

AddKeyboardShortcut(main, #PB_Shortcut_Escape, #Menu_Escape)
AddKeyboardShortcut(main, #PB_Shortcut_Space, #Menu_Space)
AddKeyboardShortcut(main, #PB_Shortcut_W, #Menu_W)

AddWindowTimer(main, 1, 100)

Repeat
  
  Event = WaitWindowEvent()
  Select Event
    Case #PB_Event_Timer
      Select EventTimer()
        Case 1
          SDRAW(image)
          
          CopyImage(image, copy)
          ResizeImage(copy, #width, #height)
          If StartDrawing(ImageOutput(imagesc))
            DrawImage(ImageID(copy), 0, 0)
            StopDrawing()
            
            SetGadgetState(#ImgGadget, ImageID(imagesc))
          EndIf          
      EndSelect
    Case #PB_Event_Menu
      Select EventMenu()
        Case #Menu_Escape
          quit = #True
        Case #Menu_Space
          Delay(800)
        Case #Menu_W
          SaveImage(image,"Randraw - "+FormatDate("%yyyy%mm%dd_%hh%ii%ss", Date())+Str(Random(9999,1))+".bmp", #PB_ImagePlugin_BMP)
      EndSelect
    Case #PB_Event_Gadget
      EventGadget = EventGadget()
      Select EventGadget        
      EndSelect
    Case #PB_Event_CloseWindow
      Quit=#True
  EndSelect
  
Until quit
That works much better and is much more responsive. Thanks for that.
Post Reply