Graphic segments perplex quite a few folks. I hope to clarify their use and help you choose the most appropriate strategy for managing segments within your project.
You need to manage segments as they consume memory. Even if you don’t use the flush command to create permanent segments you will find the current segment consumes memory as you draw.
The main purpose of a segment is to preserve the drawn graphics so that they may be redrawn. If the program window is minimized or covered by another window and graphics have not been flushed into a segment they will be lost. You will be looking at a blank white window when it is restored.
For the more adventurous, segments allow multiple graphic scenes to be flicked on and off screen.
The drawing history past and present
Liberty drawing commands do more than paint pixels on the screen, the drawing commands you issue are recorded in memory. The commands are recorded sequentially and grouped together in segments. You form a segment by issuing a #1.gb "flush" command.
You need to be aware that this recording is always on. From the moment the program starts and immediately after a flush command, a new current segment is in play.
Flushed segments are the past, only they will be restored if the screen is minimized or covered by another window. The current segment is the present, current segment drawing will be lost unless flushed into the past or preserved in another way.
Naming and numbering
Segments are identified by an ever increasing number. The first segment created will be numbered 1, the next 2 and so on. Even if we delete segments the next segment will be numbered one higher than the last.
Liberty BASIC allows us to establish the number of the current segment and store that in an appropriate program variable by issuing a #1.gb "segment variablename" command. Even more conveniently it allows us to store the number directly to a system variable with a #1.gb "flush segmentname" command. segmentname adopts the number of the segment and can be used to name the segment when you later redraw or delete it.
A couple of things catch folk out in naming and numbering. First is that flush both saves the segment and establishes a new segment. If you obtain the current segment number before you flush, then you will have the number of the soon to be flushed segment. If you obtain it after the flush, you have actually obtained the number of the new current segment. Simply deduct one in that situation if you actually wanted the number of the last flushed segment.
Second thing is that the segmentname system variable is out of scope to the program. It can be used as a literal name in #1.gb "redraw segmentname" and #1.gb "delsegment segmentname" commands but you cannot use it as a program variable. If you establish a program variable of the same name it is an entirely different variable.
Managing segments
Segments can be deleted with #1.gb "delsegment n" or #1.gb "delsegment segmentname", redrawn individually with #1.gb "redraw n" or #1.gb "redraw segmentname" or all undeleted segments redrawn with the command,#1.gb "redraw".
Redraw will paint the named segment to the front of the screen, this changes the Z order of the drawing on screen. redraw will restore the Z order and paint undeleted segments in their original Z order.
You can erase all segments by issuing a #1.gb "cls" command. This deletes all flushed segments, the current segment and wipes the screen. You can clear the current segment by issuing a #1.gb "discard" command.
Decouple the screen
Only the redraw and cls command have any impact on what you see on the screen, other commands act only on the segments held in memory. Delsegment will have no impact on the screen, discard will have no impact on the screen but the segment targeted will be deleted. Decouple the screen and the drawing history in your mind.
Start with a clean sheet
When you start, start with a clean sheet, use discard. Think what is in your current segment, think what you have flushed already . You might use cls if you wanted to start completely fresh.
????? is CLS recorded as the first command in a segment????????
Memory use is zero and there are no forgotten drawing commands.
'good practice to start with a clean sheet#1.gb "cls"'or#1.gb "discard"
Static graphics
If you are painting graphics that will not be changed, perhaps a company logo then simply draw and flush once.
Memory use is managed to one segment.
'this is the static graphic examplenomainwinWindowWidth=600WindowHeight=400UpperLeftX=(DisplayWidth-WindowWidth)/2UpperLeftY=(DisplayHeight-WindowHeight)/2graphicbox#1.gb,50,25,500,300open"Static Graphic Example"for window_nf as#1#1"trapclose [quit]"'put the pen down and set the font'note cls has no impact on these settings#1.gb "down ; font comic_sans 48"'good practice to start with a clean sheet#1.gb "cls"'draw my static graphics#1.gb "fill cyan ; backcolor red ; color red"#1.gb "place 100 50 ; boxfilled 300 150"#1.gb "place 300 150 ; circlefilled 100"#1.gb "backcolor cyan ; color red"#1.gb "place 50 100 ;\1"'now flush the graphics once and once only#1.gb "flush"wait'click on the title bar of the windows and'slide the window off screen and back,'the graphics are retained. Rem out the'flush command and try again.[quit]close#1end
Refreshed graphics
If you are painting fresh graphics, that completely replace the previous graphics, then you must delsegment the previous segment prior to drawing and flushing the next. That way there is only ever one segment in memory.
Memory use is managed to one segment.
'this is the refreshed graphic example, only one segment'is ever retained in memory.nomainwinWindowWidth=600WindowHeight=400UpperLeftX=(DisplayWidth-WindowWidth)/2UpperLeftY=(DisplayHeight-WindowHeight)/2button#1.b,"Draw Graphic",[nextdrawing], UL,250,340textbox#1.tb 350,340,100,25graphicbox#1.gb,50,25,500,300open"Refreshed Graphic Example"for window_nf as#1#1"trapclose [quit]"'put the pen down and set the font'note cls has no impact on these settings#1.gb "down ; font comic_sans 48"'good practice to start with a clean sheet#1.gb "cls"
drawing=1wait[nextdrawing]'first delete the last segment, it does not matter that'first time round this loop there isn't a segment to delete'Liberty will ignore the command#1.gb "delsegment seg"'now paint over the last graphics on screenif drawing=1then'draw graphics 1#1.gb "fill pink ; backcolor red ; color red"#1.gb "place 100 50 ; boxfilled 300 150"#1.gb "place 300 150 ; circlefilled 100"#1.gb "backcolor pink"#1.gb "place 50 100 ;\1"endifif drawing=2then'draw graphics 2#1.gb "fill yellow ; backcolor green ; color green"#1.gb "place 300 50 ; boxfilled 400 200"#1.gb "place 300 200 ; circlefilled 100"#1.gb "backcolor yellow"#1.gb "place 250 50 ;\2"endifif drawing=3then'draw graphics 3#1.gb "fill cyan ; backcolor yellow ; color yellow"#1.gb "place 100 200 ; boxfilled 350 250"#1.gb "place 100 200 ; circlefilled 50"#1.gb "place 350 200 ; circlefilled 50"#1.gb "backcolor cyan ; color yellow"#1.gb "place 200 170 ;\3"endif
drawing=drawing+1if drawing=4then drawing=1'now flush the graphics and store the segment'number in the variable seg#1.gb "flush seg"'The seg variable is catching the segment id and will'increase everytime through the loop'print the segment id number for info'note the name variable "seg" is for internal use.'to obtain and use the segment number in a variable'of your own, use segment variablename'this will provide the current segment number,'deduct one to get the true number of the last'flushed segment.#1.gb "segment currentsegmentnumber"
lastflushed=currentsegmentnumber-1#1.tb "Seg ID:";lastflushed
wait[quit]close#1end
Animated graphics
If you are painting animated graphics by overdrawing and redrawing repetitively you must manage memory and repetitively discard the current segment. The screen will behave as you expect but unless you discard the current segment memory use will build.
Memory use is minimal.
nomainwinWindowWidth=600WindowHeight=400UpperLeftX=(DisplayWidth-WindowWidth)/2UpperLeftY=(DisplayHeight-WindowHeight)/2graphicbox#1.gb,50,25,500,300open"Animated Graphic Example"for window_nf as#1#1"trapclose [quit]"#1.gb "down"
x=100
y=100
dx=10
dy=10timer17,[draw]wait[draw]'discard the current drawing history#1.gb "discard"'overdraw, move and draw the ball
oldx=x
oldy=y
x=x+dx
y=y+dy
if x>475then x=475: dx=dx*-.9
if x<25then x=25: dx=dx*-.9
if y>275then y=275: dy=dy*-.9
if y<25then y=25: dy=dy*-.9
#1.gb "backcolor buttonface ; color buttonface"#1.gb "place ";oldx;" ";oldy
#1.gb "circlefilled 25"#1.gb "backcolor red ; color red"#1.gb "place ";x;" ";y
#1.gb "circlefilled 25"wait[quit]timer0close#1end
Background and foreground graphics
If you are going to have a mostly static background and some constantly changing foreground graphics, say sliders or dials then draw the background, flush that as the first segment. You will retain that segment. Now draw and flush the foreground segment. Next time through delsegment the foreground segment redraw the background and then draw and flush the new foreground.
Memory use will not exceed two segments.
'this is the background graphic example, only two segments'are ever retained in memory.nomainwinWindowWidth=600WindowHeight=400UpperLeftX=(DisplayWidth-WindowWidth)/2UpperLeftY=(DisplayHeight-WindowHeight)/2button#1.b,"Draw Graphic",[nextdrawing], UL,250,340graphicbox#1.gb,50,25,500,300open"Refreshed Graphic Example"for window_nf as#1#1"trapclose [quit]"'put the pen down and set the font'note cls has no impact on these settings#1.gb "down ; font comic_sans 48"'good practice to start with a clean sheet#1.gb "cls"'draw the background#1.gb "fill pink ; backcolor red ; color red"#1.gb "place 100 50 ; boxfilled 300 150"#1.gb "place 300 150 ; circlefilled 100"#1.gb "backcolor pink"#1.gb "place 50 100 ;\1"'now flush as the named segment "backgroundimage"#1.gb "flush backgroundimage"
drawing=2wait[nextdrawing]'first delete the last foreground segment, it does not matter that'first time round this loop it isn't defined'Liberty will ignore the command#1.gb "delsegment foregroundimage"'redraw the background using its segment name#1.gb "redraw backgroundimage"'now paint the new foreground image'notice that the foreground images do not use'full screen fills.if drawing=2then'draw graphics 2#1.gb "backcolor green ; color green"#1.gb "place 300 50 ; boxfilled 400 200"#1.gb "place 300 200 ; circlefilled 100"#1.gb "backcolor red"#1.gb "place 180 120 ;\2"endifif drawing=3then'draw graphics 3#1.gb "backcolor yellow ; color yellow"#1.gb "place 100 200 ; boxfilled 350 250"#1.gb "place 100 200 ; circlefilled 50"#1.gb "place 350 200 ; circlefilled 50"#1.gb "backcolor red ; color yellow"#1.gb "place 230 170 ;\3"endif
drawing=drawing+1if drawing=4then drawing=2'now flush the current history as the new foreground segment'using the foregroundimage name#1.gb "flush foregroundimage"wait[quit]close#1end
Multiple segment graphics
If you want to get fancy you can maintain several segments in memory and change the order they appear on screen. Now most often this strategy will use partial or transparent graphics. By that I mean they will fill only parts of the screen. If you use fills they will cover graphics behind. The segments are maintained in memory in the Z order they were created. They can be pulled to the front of the screen with redraw. So changing the Z order on screen. If the segment has a full screen fill in it, all graphics will be hidden when that segment is redrawn. You might choose to do this and have a background segment, in this way you can hide and show any segments you wish.
Getting to grips with Segments
Rod BirdTable of Contents
Intro
Graphic segments perplex quite a few folks. I hope to clarify their use and help you choose the most appropriate strategy for managing segments within your project.You need to manage segments as they consume memory. Even if you don’t use the flush command to create permanent segments you will find the current segment consumes memory as you draw.
The main purpose of a segment is to preserve the drawn graphics so that they may be redrawn. If the program window is minimized or covered by another window and graphics have not been flushed into a segment they will be lost. You will be looking at a blank white window when it is restored.
For the more adventurous, segments allow multiple graphic scenes to be flicked on and off screen.
The drawing history past and present
Liberty drawing commands do more than paint pixels on the screen, the drawing commands you issue are recorded in memory. The commands are recorded sequentially and grouped together in segments. You form a segment by issuing a #1.gb "flush" command.You need to be aware that this recording is always on. From the moment the program starts and immediately after a flush command, a new current segment is in play.
Flushed segments are the past, only they will be restored if the screen is minimized or covered by another window. The current segment is the present, current segment drawing will be lost unless flushed into the past or preserved in another way.
Naming and numbering
Segments are identified by an ever increasing number. The first segment created will be numbered 1, the next 2 and so on. Even if we delete segments the next segment will be numbered one higher than the last.Liberty BASIC allows us to establish the number of the current segment and store that in an appropriate program variable by issuing a #1.gb "segment variablename" command. Even more conveniently it allows us to store the number directly to a system variable with a #1.gb "flush segmentname" command. segmentname adopts the number of the segment and can be used to name the segment when you later redraw or delete it.
A couple of things catch folk out in naming and numbering. First is that flush both saves the segment and establishes a new segment. If you obtain the current segment number before you flush, then you will have the number of the soon to be flushed segment. If you obtain it after the flush, you have actually obtained the number of the new current segment. Simply deduct one in that situation if you actually wanted the number of the last flushed segment.
Second thing is that the segmentname system variable is out of scope to the program. It can be used as a literal name in #1.gb "redraw segmentname" and #1.gb "delsegment segmentname" commands but you cannot use it as a program variable. If you establish a program variable of the same name it is an entirely different variable.
Managing segments
Segments can be deleted with #1.gb "delsegment n" or #1.gb "delsegment segmentname", redrawn individually with #1.gb "redraw n" or #1.gb "redraw segmentname" or all undeleted segments redrawn with the command,#1.gb "redraw".Redraw will paint the named segment to the front of the screen, this changes the Z order of the drawing on screen. redraw will restore the Z order and paint undeleted segments in their original Z order.
You can erase all segments by issuing a #1.gb "cls" command. This deletes all flushed segments, the current segment and wipes the screen. You can clear the current segment by issuing a #1.gb "discard" command.
Decouple the screen
Only the redraw and cls command have any impact on what you see on the screen, other commands act only on the segments held in memory. Delsegment will have no impact on the screen, discard will have no impact on the screen but the segment targeted will be deleted. Decouple the screen and the drawing history in your mind.Start with a clean sheet
When you start, start with a clean sheet, use discard. Think what is in your current segment, think what you have flushed already . You might use cls if you wanted to start completely fresh.????? is CLS recorded as the first command in a segment????????
Static graphics
If you are painting graphics that will not be changed, perhaps a company logo then simply draw and flush once.Refreshed graphics
If you are painting fresh graphics, that completely replace the previous graphics, then you must delsegment the previous segment prior to drawing and flushing the next. That way there is only ever one segment in memory.Animated graphics
If you are painting animated graphics by overdrawing and redrawing repetitively you must manage memory and repetitively discard the current segment. The screen will behave as you expect but unless you discard the current segment memory use will build.Background and foreground graphics
If you are going to have a mostly static background and some constantly changing foreground graphics, say sliders or dials then draw the background, flush that as the first segment. You will retain that segment. Now draw and flush the foreground segment. Next time through delsegment the foreground segment redraw the background and then draw and flush the new foreground.Multiple segment graphics
If you want to get fancy you can maintain several segments in memory and change the order they appear on screen. Now most often this strategy will use partial or transparent graphics. By that I mean they will fill only parts of the screen. If you use fills they will cover graphics behind. The segments are maintained in memory in the Z order they were created. They can be pulled to the front of the screen with redraw. So changing the Z order on screen. If the segment has a full screen fill in it, all graphics will be hidden when that segment is redrawn. You might choose to do this and have a background segment, in this way you can hide and show any segments you wish.