Fullscreen drawing in real-time ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
DigitalDreams
User
User
Posts: 18
Joined: Mon Oct 30, 2023 10:50 pm

Re: Fullscreen drawing in real-time ?

Post by DigitalDreams »

mk-soft wrote: Wed Nov 01, 2023 6:51 pm It has to be a little more code.
An event loop is ALWAYS needed to process the events. Otherwise your programme will no longer react.

Code: Select all

If ExamineDesktops()
  Define ScreenX.i = DesktopWidth(0)
  Define ScreenY.i = DesktopHeight(0)
  Define ScreenD.i = DesktopDepth(0)
  Define ScreenF.i = DesktopFrequency(0)
Else
  MessageRequester("Error", "Examine Desktop Faild!", #PB_MessageRequester_Error)
  End
EndIf

Procedure Draw(ScreenX, ScreenY)
  StartDrawing(WindowOutput(0))
  LineXY(Random(ScreenX), Random(ScreenY),Random(ScreenX), Random(ScreenY), RGB(Random(255), Random(255), Random(255)))
  StopDrawing()
EndProcedure

If OpenWindow(0,0,0,ScreenX,ScreenY,"",#PB_Window_BorderLess)
  
  ; Add ESC as Menu 1000
  AddKeyboardShortcut(0, #PB_Shortcut_Escape, 1000)
  
  AddWindowTimer(0, 1, 10)
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Break
        
      Case #PB_Event_Menu
        Select EventMenu()
          Case 1000
            Break
            
        EndSelect
        
      Case #PB_Event_Timer
        Select EventTimer()
          Case 1
            Draw(ScreenX, ScreenY)
            
        EndSelect
    EndSelect
    
  ForEver
  
EndIf
Thanks but that is MUCH slower....
pjay
Enthusiast
Enthusiast
Posts: 253
Joined: Thu Mar 30, 2006 11:14 am

Re: Fullscreen drawing in real-time ?

Post by pjay »

I think mk-soft has given you a good foundation example to demonstrate handling events as is necessary.

To speed it up, adjust the Draw() function to render multiple lines at once:

Code: Select all

Procedure Draw(ScreenX, ScreenY)
  StartDrawing(WindowOutput(0))
  For x = 1 To 100
    LineXY(Random(ScreenX), Random(ScreenY),Random(ScreenX), Random(ScreenY), RGB(Random(255), Random(255), Random(255)))
  Next
  StopDrawing()
EndProcedure
Out of interest, how many lines do you need to render per frame?
User avatar
mk-soft
Always Here
Always Here
Posts: 6253
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Fullscreen drawing in real-time ?

Post by mk-soft »

Ca. 30 frames per second and 100 lines per frame

For game programming, of course, you take the screen libraries.
But this is also sufficient for small graphics applications.

Code: Select all

If ExamineDesktops()
  Define ScreenX.i = DesktopWidth(0)
  Define ScreenY.i = DesktopHeight(0)
  Define ScreenD.i = DesktopDepth(0)
  Define ScreenF.i = DesktopFrequency(0)
Else
  MessageRequester("Error", "Examine Desktop Faild!", #PB_MessageRequester_Error)
  End
EndIf

Procedure Draw(ScreenX, ScreenY)
  Protected i
  
  StartDrawing(WindowOutput(0))
  For i = 1 To 100
    LineXY(Random(ScreenX), Random(ScreenY),Random(ScreenX), Random(ScreenY), RGB(Random(255), Random(255), Random(255)))
  Next
  StopDrawing()
EndProcedure

If OpenWindow(0,0,0,ScreenX,ScreenY,"",#PB_Window_BorderLess)
  
  ; Add ESC as Menu 1000
  AddKeyboardShortcut(0, #PB_Shortcut_Escape, 1000)
  
  AddWindowTimer(0, 1, 33) ; <- 30 Frames per second
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Break
        
      Case #PB_Event_Menu
        Select EventMenu()
          Case 1000
            Break
            
        EndSelect
        
      Case #PB_Event_Timer
        Select EventTimer()
          Case 1
            Draw(ScreenX, ScreenY)
            
        EndSelect
    EndSelect
    
  ForEver
  
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 6253
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Fullscreen drawing in real-time ?

Post by mk-soft »

@DigitalDreams
To try something out quickly, you always need a base as a window program. This can be stored as a template and selected and inserted as required.
See PureBasicIDE Menu -> Tools -> Templates

Here are a few templates that I always use.
Link: Base codes for template box
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
juergenkulow
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 25, 2019 10:18 am

Re: Fullscreen drawing in real-time ?

Post by juergenkulow »

Code: Select all

; CreateLine3D with Engine3D
ExamineDesktops()
#Screen=0
ScreenX = DesktopWidth(#Screen)
ScreenY = DesktopHeight(#Screen)
xy=ScreenX*80/ScreenY
OpenWindow(0,0,0,ScreenX,ScreenY,"",#PB_Window_BorderLess)
AddKeyboardShortcut(0, #PB_Shortcut_Escape, #ESC) 
InitEngine3D()  
InitSprite()
InitKeyboard()
InitMouse()
OpenWindowedScreen(WindowID(0), 0, 0, ScreenX, ScreenY)
CreateCamera(0, 0, 0, 100, 100) ;"Fullscreen"-Camera
CameraBackColor(0, RGB(0, $20 , 0))
MoveCamera(0, 0, 0, 100)
CameraLookAt(0, 0, 0, 0)
Repeat
  ;Start=ElapsedMilliseconds()
  For i=0 To 200
    CreateLine3D(i, Random(xy)-xy/2, Random(80)-40, 0, RGB(255, 0, 0), Random(xy)-xy/2, Random(80)-40, 1, RGB(0, 0, 255))
    CreateEntity(i, MeshID(i), #PB_Material_None)
  Next     
  ;Debug ElapsedMilliseconds()-Start
  RenderWorld()
  FlipBuffers() 
  Event = WaitWindowEvent(1)
Until Event=#PB_Event_CloseWindow Or (Event=#PB_Event_Menu And EventMenu()=#ESC); ALT-F4 or ESC   
User avatar
mk-soft
Always Here
Always Here
Posts: 6253
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Fullscreen drawing in real-time ?

Post by mk-soft »

Sorry ;)

Dispatch always all window events ...

Code: Select all

; CreateLine3D with Engine3D

; Constants
#Screen=0
#MenuESC = 1000

; Variables
Global ExitApplication
Global ScreenX
Global ScreenY
Global xy, i

ExamineDesktops()

ScreenX = DesktopWidth(#Screen)
ScreenY = DesktopHeight(#Screen)
xy=ScreenX*80/ScreenY
If OpenWindow(0,0,0,ScreenX,ScreenY,"",#PB_Window_BorderLess)
  AddKeyboardShortcut(0, #PB_Shortcut_Escape, #MenuESC) 
  InitEngine3D()  
  InitSprite()
  InitKeyboard()
  InitMouse()
  If OpenWindowedScreen(WindowID(0), 0, 0, ScreenX, ScreenY)
    CreateCamera(0, 0, 0, 100, 100) ;"Fullscreen"-Camera
    CameraBackColor(0, RGB(0, $20 , 0))
    MoveCamera(0, 0, 0, 100)
    CameraLookAt(0, 0, 0, 0)
    Repeat
      ; Dispatch all window events
      While WindowEvent()
        Select Event()
          Case #PB_Event_CloseWindow
            ExitApplication = #True
          Case #PB_Event_Menu
            Select EventMenu()
              Case #MenuESC
                ExitApplication = #True
            EndSelect
        EndSelect
      Wend
      
      For i=0 To 200
        CreateLine3D(i, Random(xy)-xy/2, Random(80)-40, 0, RGB(255, 0, 0), Random(xy)-xy/2, Random(80)-40, 1, RGB(0, 0, 255))
        CreateEntity(i, MeshID(i), #PB_Material_None)
      Next     
      RenderWorld()
      FlipBuffers() 
    Until ExitApplication
  EndIf
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
DigitalDreams
User
User
Posts: 18
Joined: Mon Oct 30, 2023 10:50 pm

Re: Fullscreen drawing in real-time ?

Post by DigitalDreams »

Thanks all

The 3D option might be the only answer if I can't escape from my fastest example without slowing or crashing the program !

Code: Select all

ExamineDesktops()
Define ScreenX.i = DesktopWidth(0)
Define ScreenY.i = DesktopHeight(0)
Define ScreenD.i = DesktopDepth(0)
Define ScreenF.i = DesktopFrequency(0)


OpenWindow(0,0,0,ScreenX,ScreenY,"",#PB_Window_BorderLess)

StartDrawing(WindowOutput(0))
Repeat
  LineXY(Random(ScreenX), Random(ScreenY),Random(ScreenX), Random(ScreenY), RGB(Random(255), Random(255), Random(255)))
ForEver
StopDrawing()

End
pjay
Enthusiast
Enthusiast
Posts: 253
Joined: Thu Mar 30, 2006 11:14 am

Re: Fullscreen drawing in real-time ?

Post by pjay »

Handling event shouldn't have a noticeable effect on speed.

Code: Select all

OpenWindow(0,0,0,320,240,"",#PB_Window_BorderLess|#PB_Window_Maximize)
AddKeyboardShortcut(0,#PB_Shortcut_Escape,#PB_Event_CloseWindow) ; exit app if escape pressed.
ScreenX = WindowWidth(0) * DesktopResolutionX() : ScreenY = WindowHeight(0) * DesktopResolutionY()

StartDrawing(WindowOutput(0))
Repeat
  Repeat
    ev = WindowEvent()
    If EventMenu() = #PB_Event_CloseWindow : End : EndIf 
  Until ev = 0
  LineXY(Random(ScreenX), Random(ScreenY),Random(ScreenX), Random(ScreenY), RGB(Random(255), Random(255), Random(255)))
ForEver
User avatar
mk-soft
Always Here
Always Here
Posts: 6253
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Fullscreen drawing in real-time ?

Post by mk-soft »

This will require an event loop, we had already written several times.
WITHOUT AN EVENT LOOP IT DOES NOT WORK !!!
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply