Advise on the following example

General FreeBASIC programming questions.
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Advise on the following example

Post by Gablea »

Code: Select all

	'           Coded by : Andrew Gable
	' 	      Designed by : Andrew Gable
	'  Software owned by : Andrew Gable and algPoS, Northampton
	' This is a Example of how to use the Page copying mode to smooth out the graphics and Hopefully make the application
	' More stable
	
'Screen copy example
	#Include "string.bi"
	#Include "vbcompat.bi"

	Declare Sub DrawPoSScreen
	Declare Sub CopyPage

	ScreenRes 800, 600, 32, 2	'Set the Screen to 800x600 with 32 depth and 2 Video pages
	ScreenSet 1, 0		' Set the hiden page as the default to be drawn to

'Main Loop

	Do
		DrawPoSScreen		' Draws the PoS Screen on to the Invisible Page	
		CopyPage				'Moves the hiden page to the Screen
 	Loop Until Len(Inkey$)
 
Private Sub DrawPoSScreen
	Dim DisplayChrs As Integer = 98
	
	Cls

	Locate 1,1 : Print Chr(201) & String(DisplayChrs,Chr(205)) & Chr(187);
	Locate 2,1 : Print Chr(186) & String(DisplayChrs," ") & Chr(186);
	Locate 3,1 : Print Chr(186) & String(DisplayChrs," ") & Chr(186);
	Locate 4,1 : Print Chr(186) & String(DisplayChrs," ") & Chr(186);
	Locate 5,1 : Print Chr(200) & String(DisplayChrs,Chr(205)) & Chr(188);
	
	Locate 2,10 : Print " " &  "Hello This is the PoS Screen";
	Locate 3,10 : Print " " & "Current Time : " & 	Format(Now, "hh:mm:ss");
	Locate 4,10 : Print " " & "Current Date : " & Format(Now,"dd/mm/yyyy");

End Sub

Private Sub CopyPage
	PCopy 1, 0 ' Move the hiden page to the visible page (shows it on the Display)
	ScreenSet 1, 0	
End Sub
Hi Everyone

As you can see I have taken myself BACK to the basics to figure something out

What I want to do is have a function that can run independently of the main loop but still update the screen

how would i add to my example code "FreeBASIC is Cool" and have it bounce around the screen independently to my time loop? (a bit like a screen saver)

The end goal is to have the Time and date independent to anything the Program is doing (In the perfect world i would like the example app to run on DOS as well as Linux but I am happy for any help anyone can give (even if it just to run on Linux)
angros47
Posts: 2323
Joined: Jun 21, 2005 19:04

Re: Advise on the following example

Post by angros47 »

In your program, the main loop is:

Code: Select all

	Do
		DrawPoSScreen		' Draws the PoS Screen on to the Invisible Page	
		CopyPage				'Moves the hiden page to the Screen
 	Loop Until Len(Inkey$)
(speaking of that, you should add a "Sleep 1" line inside that loop, on multitasking systems it will prevent your program to take too many CPU cycles)

There are three solutions:
-you can create a subroutine to draw your moving message, and call it in your cycle, between DrawPoSScreen and CopyPage. Your subroutine will likely need some STATIC variables inside it, so it will preserve their values, and will "remember" where the message was displayed from one frame to the next one
- You can use a coroutine: https://freebasic.net/forum/viewtopic.php?f=17&t=28417
- you can use a thread https://www.freebasic.net/wiki/KeyPgThreadCreate
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Advise on the following example

Post by D.J.Peters »

If you work with 2 pages you need only the flip command to make the hidden workpage visible.

Joshy

Code: Select all

#Include "vbcompat.bi"
Private Sub PrintOnHidden
  Cls
  Locate 1,10 : ? Format(Now,"hh:mm:ss")
  Locate 2,10 : ? Format(Now,"dd/mm/yyyy")
End Sub

ScreenRes 800, 600, 32, 2 ' two pages
ScreenSet 1, 0            ' make the workpage hidden 

while inkey()=""
  PrintOnHidden ' print on the hidden workpage
  flip          ' show the changes from hidden page
  sleep 10 
wend
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: Advise on the following example

Post by Gablea »

D.J.Peters wrote: Jul 09, 2022 22:45 If you work with 2 pages you need only the flip command to make the hidden workpage visible.

Joshy

Code: Select all

#Include "vbcompat.bi"
Private Sub PrintOnHidden
  Cls
  Locate 1,10 : ? Format(Now,"hh:mm:ss")
  Locate 2,10 : ? Format(Now,"dd/mm/yyyy")
End Sub

ScreenRes 800, 600, 32, 2 ' two pages
ScreenSet 1, 0            ' make the workpage hidden 

while inkey()=""
  PrintOnHidden ' print on the hidden workpage
  flip          ' show the changes from hidden page
  sleep 10 
wend

Hi @D.J.Peters

Thank for that I did not know that Flip is a command

I assume if i wanted to update the program and use graphics this would still work (Loading the graphics to the hidden screen and then flipping it)
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: Advise on the following example

Post by Gablea »

angros47 wrote: Jul 09, 2022 22:34 In your program, the main loop is:

Code: Select all

	Do
		DrawPoSScreen		' Draws the PoS Screen on to the Invisible Page	
		CopyPage				'Moves the hiden page to the Screen
 	Loop Until Len(Inkey$)
(speaking of that, you should add a "Sleep 1" line inside that loop, on multitasking systems it will prevent your program to take too many CPU cycles)

There are three solutions:
-you can create a subroutine to draw your moving message, and call it in your cycle, between DrawPoSScreen and CopyPage. Your subroutine will likely need some STATIC variables inside it, so it will preserve their values, and will "remember" where the message was displayed from one frame to the next one
- You can use a coroutine: https://freebasic.net/forum/viewtopic.php?f=17&t=28417
- you can use a thread https://www.freebasic.net/wiki/KeyPgThreadCreate
Hi @angros47

If I was to use the Thread Option would that allow the thread to draw to the hidden screen at the same time the other screens are drawing to it?

And Could I use thread to do other work (that does not draw to the screen (for example polls a Serial Port for data)
paul doe
Moderator
Posts: 1732
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Advise on the following example

Post by paul doe »

Code: Select all

	'           Coded by : Andrew Gable
	' 	      Designed by : Andrew Gable
	'  Software owned by : Andrew Gable and algPoS, Northampton
	' This is a Example of how to use the Page copying mode to smooth out the graphics and Hopefully make the application
	' More stable
	
'Screen copy example
	#Include "string.bi"
	#Include "vbcompat.bi"

	Declare Sub DrawPoSScreen
	Declare Sub CopyPage

	ScreenRes 800, 600, 32, 2	'Set the Screen to 800x600 with 32 depth and 2 Video pages
	ScreenSet 1, 0		' Set the hiden page as the default to be drawn to

'Main Loop

sub ssaver( x0 as long, y0 as long, x1 as long, y1 as long, text as string )
  static as long x = 1, y = 1, dx = 1, dy = 1
  
  x += dx
  y += dy
  
  if( y > y1 ) then
    y = y1
    dy = -dy
  end if
  
  if( y < y0 ) then
    y = y0
    dy = -dy
  end if
  
  if( x < x0 ) then
    x = x0
    dx = -dx
  end if
  
  if( x + len( text ) > x1 ) then
    x = x1 - len( text )
    dx = -dx
  end if
  
  locate y, x
  ? text;
end sub

	Do
		DrawPoSScreen		' Draws the PoS Screen on to the Invisible Page	
		ssaver( 1, 8, 800 \ 8, 600 \ 8, "FreeBasic is cool!" )
		
		flip()
		'CopyPage				'Moves the hiden page to the Screen
		sleep( 1, 1 )
 	Loop Until Len(Inkey)
 
Private Sub DrawPoSScreen
	Dim DisplayChrs As Integer = 98
	
	Cls

	Locate 1,1 : Print Chr(201) & String(DisplayChrs,Chr(205)) & Chr(187);
	Locate 2,1 : Print Chr(186) & String(DisplayChrs," ") & Chr(186);
	Locate 3,1 : Print Chr(186) & String(DisplayChrs," ") & Chr(186);
	Locate 4,1 : Print Chr(186) & String(DisplayChrs," ") & Chr(186);
	Locate 5,1 : Print Chr(200) & String(DisplayChrs,Chr(205)) & Chr(188);
	
	Locate 2,10 : Print " " &  "Hello This is the PoS Screen";
	Locate 3,10 : Print " " & "Current Time : " & 	Format(Now, "hh:mm:ss");
	Locate 4,10 : Print " " & "Current Date : " & Format(Now,"dd/mm/yyyy");

End Sub

'Private Sub CopyPage
'	PCopy 1, 0 ' Move the hiden page to the visible page (shows it on the Display)
'	ScreenSet 1, 0	
'End Sub
There's no need for threads, nor coroutines, nor anything else. You actually have to take steps in FreeBasic for it to not behave in this way (ie stopping the program flow until something happens, like pressing a key)
angros47
Posts: 2323
Joined: Jun 21, 2005 19:04

Re: Advise on the following example

Post by angros47 »

@Gablea
Yes, to both questions
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Advise on the following example

Post by fxm »

Gablea wrote: Jul 09, 2022 23:25 If I was to use the Thread Option would that allow the thread to draw to the hidden screen at the same time the other screens are drawing to it?
Yes but the text cursor is not thread dependent, so for example in the thread you have to save the cursor position before using it to print text and then restore it after, and all protected by mutual exclusion (using a mutex) with the printing from the main thread.

Another solution is to use only the graphics cursor which is thread dependent:
Replace PRINT keyword with DRAW STRING keyword everywhere in the thread for example (or in the main thread or in both). This is more complex for printing on screen, but saving cursor position and mutual exclusion are no longer needed.

And this is only the beginning of the constraints that must be overcome in order to implement proper double buffering in a multithreading context.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Advise on the following example

Post by dodicat »

DOS (eventually)
So threading is out or very complex I believe in DOS.
Personally I would not use screenset/flip, or any copying on other graphics pages.
Keep it very simple.
screenlock
cls

GRAPHICS procedures including pos screen

screenunlock
sleep 1

Example:

Code: Select all

'           Coded by : Andrew Gable
' 	      Designed by : Andrew Gable
'  Software owned by : Andrew Gable and algPoS, Northampton
' This is a Example of how to use the Page copying mode to smooth out the graphics and Hopefully make the application
' More stable

#Include "string.bi"
#Include "vbcompat.bi"

Declare Sub DrawPoSScreen
Declare Sub readstring(st As String,message As String)
Declare Sub showstring(message As String)
Screenres 800, 600, 32', 2	'Set the Screen to 800x600 with 32 depth 

Width 800\8,600\16'get a nice big font

'Main Loop
Dim As String message
Do
    Screenlock' Draws the PoS Screen and other things on to the screen memory buffer, which is now hidden	
    Cls
    DrawPoSScreen
    Locate 10,5
    readstring("Enter some characters from the keyboard (F10 to quit) ",message)
    showstring(message)
    Screenunlock' now shows the screen memory buffer
    Sleep 1  'must have a little sleep to catch up.
Loop Until Multikey(&h44)'= F10

Private Sub DrawPoSScreen
	Dim DisplayChrs As Integer = 98
	Locate 1,1 : Print Chr(201) & String(DisplayChrs,Chr(205)) & Chr(187);
	Locate 2,1 : Print Chr(186) & String(DisplayChrs," ") & Chr(186);
	Locate 3,1 : Print Chr(186) & String(DisplayChrs," ") & Chr(186);
	Locate 4,1 : Print Chr(186) & String(DisplayChrs," ") & Chr(186);
	Locate 5,1 : Print Chr(200) & String(DisplayChrs,Chr(205)) & Chr(188);
	
	Locate 2,10 : Print " " &  "Hello This is the PoS Screen";
	Locate 3,10 : Print " " & "Current Time : " & 	Format(Now, "hh:mm:ss");
	Locate 4,10 : Print " " & "Current Date : " & Format(Now,"dd/mm/yyyy");
End Sub

Sub readstring(st As String,message As String)
    Static As String j,blink
    Static As Double t,lt
    Static As Long lft
    If lt-t>.5 Then blink="_"
    If lt-t>1 Then t=Timer:blink=" "
    Dim As String i=Inkey
    lft=Iif(Len(i),i[0],-1)
    If Lft=08 Then j=Mid(j,1,Len(j)-1)'backspace
    If lft>=0 Andalso lft<=254 Andalso lft<>8 Then j=j+Chr(Lft)
    If Lft=27 Then j=""               'esc clears the line
    If lft<>13 Then
        Print st & j & blink
    Else
        j=Rtrim(j,Chr(13))
        message=j
        j=""
    End If
    lt=Timer
End Sub

Sub showstring(message As String)
    Locate 15,5
    Print message
End Sub

 
angros47
Posts: 2323
Joined: Jun 21, 2005 19:04

Re: Advise on the following example

Post by angros47 »

Threads are available in DOS
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: Advise on the following example

Post by Gablea »

angros47 wrote: Jul 10, 2022 10:39 Threads are available in DOS
They are?

I was reading the treadcreate page and it said due to the limitations of DOS threads are not available unless that changed and the page has not been updated.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Advise on the following example

Post by dodicat »

From the help pages:
ThreadSelf is not available with the DOS version of FreeBASIC, because multithreading is not supported by DOS kernel nor the used extender.
Threadcreate is not available with the DOS version / target of FreeBASIC, because multithreading is not supported by DOS kernel nor the used extender.
ThreadWait is not available with the DOS version of FreeBASIC, because multithreading is not supported by DOS kernel nor the used extender.
ThreadDetach is not available with the DOS version of FreeBASIC, because multithreading is not supported by DOS kernel nor the used extender.
Also similar for the mutex procedures.
You need an example if the help pages do not apply.
I don't think the DOS fb version will work here on Win 10 64 bits, and I don't have an emulator here.
I don't even have Linux just now, so I might as well say toodle-oo for the time being.
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: Advise on the following example

Post by Gablea »

dodicat wrote: Jul 10, 2022 12:57 From the help pages:
ThreadSelf is not available with the DOS version of FreeBASIC, because multithreading is not supported by DOS kernel nor the used extender.
Threadcreate is not available with the DOS version / target of FreeBASIC, because multithreading is not supported by DOS kernel nor the used extender.
ThreadWait is not available with the DOS version of FreeBASIC, because multithreading is not supported by DOS kernel nor the used extender.
ThreadDetach is not available with the DOS version of FreeBASIC, because multithreading is not supported by DOS kernel nor the used extender.
Also similar for the mutex procedures.
You need an example if the help pages do not apply.
I don't think the DOS fb version will work here on Win 10 64 bits, and I don't have an emulator here.
I don't even have Linux just now, so I might as well say toodle-oo for the time being.
I'm happy to use Linux as I'm getting my head around that a lot now ;)
angros47
Posts: 2323
Joined: Jun 21, 2005 19:04

Re: Advise on the following example

Post by angros47 »

Gablea wrote: Jul 10, 2022 11:01 They are?

I was reading the treadcreate page and it said due to the limitations of DOS threads are not available unless that changed and the page has not been updated.
Yes, it needs to be updated
viewtopic.php?t=31690
coderJeff
Site Admin
Posts: 4323
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Advise on the following example

Post by coderJeff »

angros47 wrote: Jul 10, 2022 17:18
Gablea wrote: Jul 10, 2022 11:01 They are?

I was reading the treadcreate page and it said due to the limitations of DOS threads are not available unless that changed and the page has not been updated.
Yes, it needs to be updated
viewtopic.php?t=31690
Probably testing and changes to fbc too required. I haven't tested myself and the only feedback I really seen so far was on discord showing how fbc and/or generated EXE faults on various attempts.
Post Reply