Page 1 of 2
Fullscreen drawing in real-time ?
Posted: Tue Oct 31, 2023 6:40 pm
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)
Re: Fullscreen drawing in real-time ?
Posted: Tue Oct 31, 2023 7:20 pm
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
Re: Fullscreen drawing in real-time ?
Posted: Tue Oct 31, 2023 7:28 pm
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
Re: Fullscreen drawing in real-time ?
Posted: Tue Oct 31, 2023 10:25 pm
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
Re: Fullscreen drawing in real-time ?
Posted: Tue Oct 31, 2023 10:33 pm
by DigitalDreams
Thanks all, will fiddle with the examples....
Re: Fullscreen drawing in real-time ?
Posted: Tue Oct 31, 2023 10:51 pm
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
Re: Fullscreen drawing in real-time ?
Posted: Tue Oct 31, 2023 10:56 pm
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.
Re: Fullscreen drawing in real-time ?
Posted: Wed Nov 01, 2023 10:32 am
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
Re: Fullscreen drawing in real-time ?
Posted: Wed Nov 01, 2023 11:30 am
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.
Re: Fullscreen drawing in real-time ?
Posted: Wed Nov 01, 2023 11:43 am
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
Re: Fullscreen drawing in real-time ?
Posted: Wed Nov 01, 2023 1:17 pm
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.
Re: Fullscreen drawing in real-time ?
Posted: Wed Nov 01, 2023 1:55 pm
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.
Re: Fullscreen drawing in real-time ?
Posted: Wed Nov 01, 2023 3:35 pm
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 !
Re: Fullscreen drawing in real-time ?
Posted: Wed Nov 01, 2023 4:06 pm
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
Re: Fullscreen drawing in real-time ?
Posted: Wed Nov 01, 2023 6:51 pm
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