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

Fullscreen drawing in real-time ?

Post by DigitalDreams »

I've seen examples that open a window and refer to createimage, startdrawing, stopdrawing and imagegadget yet these seem to simply draw once in the background then display a static result.

How do I open fullscreen and draw in realtime ?, hopefully with the option to also draw text on the same screen (basic info, FPS counter etc)
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Re: Fullscreen drawing in real-time ?

Post by firace »

Here's a quick example to get you started.

Code: Select all

InitSprite() : InitKeyboard()

scrollspeed = 2

t$="CPU-friendly horizontal ticker  -  Press Esc to exit"
LoadFont(1,"Segoe UI",40)

Procedure UpdateScreen()
  Shared x, w, h, t$, scrollspeed
  
  FlipBuffers()
  
  StartDrawing(ScreenOutput())
  Box(0,0,W,H,#Black)
  
  DrawingFont(FontID(1))
  DrawText(x,185,t$, #Green)
  DrawText(w-300,15, str(date()), #Blue)
  DrawText(w-150,85, str(random(1000)), #Cyan)
  a = 0-TextWidth(t$)
  StopDrawing()
  
  If x > a : x = x - scrollspeed : Else : x = W : EndIf
EndProcedure   

OpenWindow(0, 1, 1, 1, 1, "Ex07", #PB_Window_BorderLess | #PB_Window_ScreenCentered | #PB_Window_Maximize)
OpenWindowedScreen(WindowID(0),0,0,WindowWidth(0),WindowHeight(0),1,0,0) 

W=WindowWidth(0) : H=WindowHeight(0) : x=W


Repeat
  UpdateScreen()
  WindowEvent() 
  
  ExamineKeyboard() : If KeyboardPushed(#PB_Key_Escape) : Break  : EndIf 
ForEver
Last edited by firace on Tue Oct 31, 2023 7:48 pm, edited 1 time in total.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Fullscreen drawing in real-time ?

Post by netmaestro »

Small example:

Code: Select all

Structure shrapnel
  x.f 
  y.f 
  u.f 
  v.f 
  life.f 
  s.f
  c.l
EndStructure 

Global NewList dot.shrapnel() 

CreateImage(0, 64, 64, 32,#PB_Image_Transparent)
StartDrawing(ImageOutput(0))
  DrawingMode(#PB_2DDrawing_Gradient|#PB_2DDrawing_AllChannels)      
  BackColor (RGBA(0,0,255,255))
  FrontColor(RGBA(0,0,0,0))
  EllipticalGradient(20, 20, 64, 64)
  Circle(31, 31, 31)   
StopDrawing() 

CreateImage(1, 64, 64, 32,#PB_Image_Transparent)
StartDrawing(ImageOutput(1))
  DrawingMode(#PB_2DDrawing_Gradient|#PB_2DDrawing_AllChannels)      
  BackColor (RGBA(255,0,0,255))
  FrontColor(RGBA(0,0,0,0))
  EllipticalGradient(20, 20, 64, 64)
  Circle(31, 31, 31)   
StopDrawing() 

Procedure BlowemUp(spotx,spoty) 
  For g=0 To 900 
    AddElement(dot()) 
    angle.f=Random(359)*3.141592/180 
    dot()\life=1+Random(254) 
    dot()\u=Cos(angle)*Random(128)/64 
    dot()\v=Sin(angle)*Random(128)/64 
    dot()\v=dot()\v-2  
    dot()\x=spotx 
    dot()\y=spoty 
    dot()\s=Random(15)/10 
    dot()\c = Random($FFFFFF)
  Next g 
EndProcedure 

InitSprite():InitKeyboard()
OpenScreen(640,480,32,"Blowemup")

x=0:y=0:quit=0:f=1:d=1:rt=576:bt=416:x1=350:y1=100:f1=1:d1=1
Repeat 

  If f:x+1:If x>=rt:f=0:EndIf:Else:x-1:If x<=0:f=1:EndIf:EndIf
  If d:y+1:If y>=bt:d=0:EndIf:Else:y-1:If y<=0:d=1:EndIf:EndIf  
  If f1:x1+1:If x1>=rt:f1=0:EndIf:Else:x1-1:If x1<=0:f1=1:EndIf:EndIf
  If d1:y1+1:If y1>=bt:d1=0:EndIf:Else:y1-1:If y1<=0:d1=1:EndIf:EndIf  
  
  StartDrawing(ScreenOutput()) 
    Box(0,0,640,480,$000000)
    DrawAlphaImage(ImageID(0),x,y)
    DrawAlphaImage(ImageID(1),x1,y1)
    If ListSize(dot())<400 
      BlowemUp(100+Random(480),100+Random(280)) 
    EndIf 
    ForEach dot() 
      dot()\x=dot()\x+dot()\u 
      dot()\y=dot()\y+dot()\v 
      dot()\v=dot()\v+0.05 
      If dot()\x=>(0) And dot()\x<=(639) And dot()\y=>(0) And dot()\y<=(479)
        c=dot()\life 
        c=c+c<<8+c<<16 
        Circle(dot()\x,dot()\y,1,dot()\c) 
      EndIf 
      dot()\life=dot()\life-1:If dot()\life<=0:DeleteElement(dot()):EndIf 
    Next 
  StopDrawing() 
  
  FlipBuffers()
  
  ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape)
End 
BERESHEIT
Karellen
User
User
Posts: 84
Joined: Fri Aug 16, 2013 2:52 pm
Location: Germany

Re: Fullscreen drawing in real-time ?

Post by Karellen »

You need a screen to do this. Check the 2d and 3d Multimedia&Game Libs in the manual.
Here is another quick example:

Code: Select all

ExamineDesktops()

width = DesktopWidth(0)
height = DesktopHeight(0)
vx = 2 ; speed of text movement x
vy = 2 ; speed of text movement y

; create a window...
If OpenWindow(0, 0, 0, width, height, "Screen Example", #PB_Window_BorderLess)
  
  AddKeyboardShortcut(0, #PB_Shortcut_Escape, 1)
  
  ; ... and a screen
  If InitSprite() And OpenWindowedScreen(WindowID(0), 0, 0, width, height)
    
    LoadFont(0, "Arial", 96, #PB_Font_Bold)
 
  EndIf
  
EndIf 

; main loop
Repeat
  
  Select WindowEvent()
    Case #PB_Event_CloseWindow
      End
    Case #PB_Event_Menu
      Select EventMenu()
        Case 1
          End
      EndSelect
  EndSelect
  
  ; do all drawing here
  FlipBuffers()
  ClearScreen(RGB(0, 0, 0))
  
  StartDrawing(ScreenOutput())
  
  ; let's draw a moving clock
  DrawingFont(FontID(0))
  text.s = FormatDate("%hh:%ii:%ss", Date())
  DrawText(x,y, text, #Blue)
  
  x+vx
  If x>width-TextWidth(text) Or x<0
    vx=vx*-1
  EndIf
  
  y+vy
  If y>height-TextHeight(text) Or y<0
    vy=vy*-1
  EndIf
  
  StopDrawing()
  
ForEver
Stanley decided to go to the meeting room...
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, will fiddle with the examples....
User avatar
DigitalDreams
User
User
Posts: 18
Joined: Mon Oct 30, 2023 10:50 pm

Re: Fullscreen drawing in real-time ?

Post by DigitalDreams »

Well this just gives me a black screen !?........


If ExamineDesktops()
ScreenX = DesktopWidth(0)
ScreenY = DesktopHeight(0)
ScreenD = DesktopDepth(0)
ScreenF = DesktopFrequency(0)
Else
End
EndIf

InitKeyboard()
InitSprite()

OpenScreen(ScreenX, ScreenY, 32, "MyScreen")
StartDrawing(ScreenOutput())
Box(0,0,ScreenX,ScreenY,#White)
StopDrawing()

Repeat
StartDrawing(ScreenOutput())
LineXY(Random(ScreenX), Random(ScreenY),Random(ScreenX), Random(ScreenY), RGB(Random(255), Random(255), Random(255)))
StopDrawing()
ExamineKeyboard() : If KeyboardPushed(#PB_Key_Escape) : Break : EndIf
ForEver

End
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Fullscreen drawing in real-time ?

Post by netmaestro »

You are drawing off the screen. Instead of ScreenX and Y try 10,10. Another major problem: I don't see a FlipBuffers() anywhere.
BERESHEIT
User avatar
DigitalDreams
User
User
Posts: 18
Joined: Mon Oct 30, 2023 10:50 pm

Re: Fullscreen drawing in real-time ?

Post by DigitalDreams »

Much better but I'm concerned that I'm now drawing on two screens hence halving the speed.??. It does appear rather slow even though running on a 4k screen.

Code: Select all

If ExamineDesktops()
  ScreenX = DesktopWidth(0)
  ScreenY = DesktopHeight(0)
  ScreenD = DesktopDepth(0)
  ScreenF = DesktopFrequency(0)
Else
  End
EndIf

InitKeyboard()
InitSprite()

OpenScreen(ScreenX, ScreenY, 32, "MyScreen")
StartDrawing(ScreenOutput())
Box(0,0,ScreenX,ScreenY,#White)
StopDrawing()

Repeat
  StartDrawing(ScreenOutput())
  FlipBuffers()
  LineXY(Random(ScreenX), Random(ScreenY),Random(ScreenX), Random(ScreenY), RGB(Random(255), Random(255), Random(255)))
  StopDrawing()
  ExamineKeyboard() : If KeyboardPushed(#PB_Key_Escape) : Break  : EndIf 
ForEver

End
Last edited by DigitalDreams on Wed Nov 01, 2023 11:36 am, edited 1 time in total.
User avatar
Kuron
Addict
Addict
Posts: 1626
Joined: Sat Oct 17, 2009 10:51 pm
Location: Pacific Northwest

Re: Fullscreen drawing in real-time ?

Post by Kuron »

You need to put your code on the forums within the code tags, it will make it easier to read. The code tags are the icon with </> on it.

Code: Select all

this is code tags
Last edited by Kuron on Wed Nov 01, 2023 11:57 am, edited 2 times in total.
Best wishes to the PB community. Thank you for the memories. ♥️
miso
Enthusiast
Enthusiast
Posts: 469
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Fullscreen drawing in real-time ?

Post by miso »

I belive (i might prove wrong), that the flipbuffers should be outside of the drawings. I'd put after stopdrawing().

Code: Select all

Procedure.f LERP(a.f,b.f,t.f)
  ProcedureReturn(((1.0-t.f)*a) + (b*t))
  ;ProcedureReturn(a + t*(b-a))
EndProcedure

Procedure.f INVLERP(a.f,b.f,v.f)
  If a=b : ProcedureReturn(1) : EndIf
  ProcedureReturn((v-a) / (b-a))
EndProcedure
  
Procedure.f REMAP(iMin.f,iMAX.f, oMin.f, oMax.f, v.f)  
  Define t.f
  t.f = INVLERP(iMin,iMAX,v)
  ProcedureReturn(LERP(oMin,oMax,t))
EndProcedure

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

InitKeyboard()
InitSprite()

OpenScreen(ScreenX, ScreenY, 32, "MyScreen")

Repeat
  StartDrawing(ScreenOutput())
  Define x.i
   For x = 0 To ScreenX
    Define color.i =Int(REMAP(0,ScreenX,0,255,x))
    LineXY(0, 0,x,ScreenY,RGB(color,0,0) )
    LineXY(screenx, screeny,screenx-x,0,RGB(0,0,color) )
   Next x
  StopDrawing()
    
FlipBuffers()
ExamineKeyboard() : If KeyboardPushed(#PB_Key_Escape) : Break : EndIf
ForEver
End
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

; Fullscreen
ExamineDesktops()
#Screen=0
ScreenX=DesktopWidth(#Screen)
ScreenY=DesktopHeight(#Screen)
OpenWindow(0, 0,0,ScreenX, ScreenY,"",#PB_Window_BorderLess)
Repeat
  StartDrawing(WindowOutput(0))
  start=ElapsedMilliseconds()
  For i=1 To 200
    LineXY(Random(ScreenX), Random(ScreenY),Random(ScreenX), Random(ScreenY), RGB(Random(255), Random(255), Random(255)))
  Next   
  ; Debug ElapsedMilliseconds()-start
  StopDrawing()
  Event=WaitWindowEvent(1)
  If Event=#PB_Event_Repaint
    StartDrawing(WindowOutput(0))
    Box(10,10,ScreenX-20,ScreenY-20,#Blue)
    StopDrawing()
  EndIf   
Until Event=#PB_Event_CloseWindow ; ALT-F4 
Engine3D
Samples for the Engine3D:

Code: Select all

/purebasic/examples/3d$ ls -x
AddMaterialLayer.pb               AttachEntityObject2.pb          AttachEntityObject3.pb
AttachEntityObject.pb             AttachNodeObject.pb             BillboardGrass.pb
Billboard.pb                      BodyPick.pb                     Bridge.pb
CameraFollow.pb                   Camera.pb                       CameraProjectionMode.pb
CameraProjection.pb               CameraTrack.pb                  CameraView.pb
CheckObjectVisibility2.pb         CheckObjectVisibility.pb        CompoundBody.pb
ConeJoint.pb                      ConvertLocalToWorldPosition.pb  ConvertWorldToLocalPosition.pb
CopyAngle.pb                      CopyMaterial.pb                 CreateLine3D.pb
CreateRenderTexture.pb            CreateTextureAlpha.pb           CreateTexture.pb
CubeMapping.pb                    Data                            Demos
EnableManualEntityBoneControl.pb  EntityAnimation2.pb             EntityAnimation3.pb
EntityAnimation.pb                EntityBonePosition2.pb          EntityBonePosition.pb
EntityBoundingBox.pb              EntityCollide.pb                Entity.pb
ExamineWorldCollisions.pb         FetchEntityMaterial.pb          FetchOrientation.pb
Gadget3D.pb                       GenericJoint.pb                 GetScriptMaterial.pb
GetScriptParticle.pb              HingeJoint.pb                   Joint.pb
LightAttenuation.pb               LightDirectionXYZ.pb            LightLookAt.pb
Light.pb                          Makefile                        Material2.pb
MaterialAnimation.pb              MaterialFog.pb                  Material.pb
MaterialTransparent.pb            MeshManual2.pb                  MeshManualCube.pb
MeshManual.pb                     Mesh.pb                         MousePick.pb
MouseRayCast.pb                   NodeAnimation2.pb               NodeAnimation.pb
Node.pb                           Ogre.log                        Particle2.pb
Particle3.pb                      Particle.pb                     Pendulum.pb
PitchYaw.pb                       PointJoint.pb                   PointPick_2D3D.pb
PointPick.pb                      ProjectiveTexturing.pb          RayCast.pb
RayCollide.pb                     RayPick_LightDirection.pb       RayPick.pb
Reflection.pb                     ReloadMaterial.pb               ResetMaterial.pb
ResizeCamera.pb                   Ribbon.pb                       RotateLight.pb
SaveRenderTexture.pb              ScaleMaterial.pb                Screen3DRequester.pb
SetEntityCollisionFilter.pb       SetMeshData.pb                  SetMeshMaterial.pb
SetOrientation.pb                 SetRenderQueue.pb               SkyBox.pb
SkyDome.pb                        SliderJoint.pb                  Sound3D.pb
Spline.pb                         StaticGeometry.pb               TerrainBlend.pb
TerrainHeight.pb                  Terrain.pb                      TerrainPhysic.pb
TerrainShadow.pb                  Text3D.pb                       Texture.pb
Vehicle.pb                        VehicleTerrain.pb               VertexAnimation.pb
VisibilityMask.pb                 Water.pb                        Window3D.pb
WorldDebug.pb                     WorldShadows.pb

purebasic/examples/3d/Demos$ ls -x
CarPhysic.pb              Character.pb  FacialAnimation.pb  FPSFirstPerson.pb  MaterialScripts.pb  MeshManualFlag.pb
MeshManualParametrics.pb  PinBall.pb    Tank.pb             ThirdPerson.pb
SetMeshData.pb
OpenGLGadget.pb
VectorDrawing

@miso: Your program does not output anything on my Linux.
miso
Enthusiast
Enthusiast
Posts: 469
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Fullscreen drawing in real-time ?

Post by miso »

@juergenkulow
That's interesting, I'm puzzled. Maybe with window and windowoutput?... (On win7, I see interpolated colorgradients red and blue.)
On the other hand, I avoid direct screen draws in my programs, creating and displaying sprites is better.

Edit: I can not test in Linux, but I guess screenoutput() might be the cause.
User avatar
DigitalDreams
User
User
Posts: 18
Joined: Mon Oct 30, 2023 10:50 pm

Re: Fullscreen drawing in real-time ?

Post by DigitalDreams »

juergenkulow wrote: Wed Nov 01, 2023 1:17 pm

Code: Select all

  Event=WaitWindowEvent(1)
  If Event=#PB_Event_Repaint
    StartDrawing(WindowOutput(0))
    Box(10,10,ScreenX-20,ScreenY-20,#Blue)
    StopDrawing()
  EndIf   
Until Event=#PB_Event_CloseWindow ; ALT-F4 
Wow thanks, MUCH faster yet I'm struggling to understand your exit mechanism here and why the initial background box is within it ?. Also that moving the mouse effects output !
User avatar
DigitalDreams
User
User
Posts: 18
Joined: Mon Oct 30, 2023 10:50 pm

Re: Fullscreen drawing in real-time ?

Post by DigitalDreams »

The fastest yet thanks to juergenkulow yet struggling to provide an exit (say the esc key) that doesn't effect drawing.....

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
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 »

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
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