RodBird
Feb 26, 2011
=Flushing strategies==Getting to grips with Segments= //Rod Bird// [[toc]] ---- ==Intro==Flush is a command that perplexesGraphic segments perplex quite a few folks. I hope to clarifyitstheir use and help you choose the most appropriatedrawing and flushingstrategy for managing segments within your project.The Help File is very clearly worded, as you read this document refresh your mind on each of the noted graphics commands.The main purpose of flush isYou need topreserve the drawn graphics so thatmanage segments as theymay be redrawn, shouldconsume memory. Even if you don’t use theprogram window be minimized or covered by another window. If the graphics have not been flushedflush command to create permanent segments you willbe looking at a blank white window when it is restored or uncovered.find the current segment consumes memory as you draw.ForThe 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,flush allowssegments 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 inSEGMENTS.segments. You form a segment by issuing aFLUSH#1.gb "flush" command.RealizeYou need to be aware that this recording is always on. From the moment the program starts and immediately after a flush command, a newCURRENT SEGMENTcurrent 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.IfEven if we delete segmentsit makes no difference,the next segment will be numbered one higher than the last.Liberty allows us to establish the number of a segment and store that in an appropriate program variable by issuing a SEGMENT variablename command. Even more conveniently it allows us to store the number directly to a system variable with a SEGMENT name command.The one thing that catches most folks out is that flush savesLiberty BASIC allows us to establish the number of the current segmentbut also immediately establishesand store that in an appropriate program variable by issuing anew segment. If you obtain#1.gb "segment variablename" command. Even more conveniently it allows us to store thesegmentnumberbefore you flush then you havedirectly to a system variable with a #1.gb "flush segmentname" command. segmentname adopts the number of theflushed segment, if you obtain it aftersegment and can be used to name theflushsegment when youhave actually obtained the number of the new current segment. Simply deduct one in that situation.later redraw or delete it.==Managing segments== Segments canA 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 bedeleted with DELSEGMENT (name), redrawn individually with REDRAW (name) or all undeleted segments redrawn withflushed 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 thecommand, REDRAW.last flushed segment.REDRAW (name) will paintSecond thing is that thenamed segmentsegmentname system variable is out of scope to thefrontprogram. 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 thescreen, this changes the Z order of the drawing on screen. REDRAW will appear to restore the Z order and paint undeleted segments in their original Z order.same name it is an entirely different variable.You==Managing segments== Segments canerasebe 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 segmentsby issuing a CLS command. This deletes all flushed segments, the current segment and wipes the screen. You can clearredrawn with thecurrent segment by issuing a DISCARD command.command,#1.gb "redraw".==DecoupleRedraw will paint thescreen and drawing history== Onlynamed segment to theredraw and cls command have any impact on what you see onfront of the screen,other commands act only onthis changes thesegments held in memory. Delsegment (name) will have no impact onZ order of thescreen, discard will have no impactdrawing onthe screen but the segment (name)screen. redraw willbe deleted and the current segment will be wiped clean. Decouplerestore thescreenZ order andthe drawing historypaint undeleted segments inyour mind.their original Z order.==Manage memory== It is important to manageYou can erase all segmentsas they consume memory. Even if you don’t use flush it is important to manageby issuing a #1.gb "cls" command. This deletes all flushed segments, the current segmentmemory with discard.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, useDISCARD.discard. Think what is in your current segment, think what you have flushed already . You might useCLScls if you wanted to start completely fresh. ????? is CLS recorded as the first command in a segment???????? [[image:scr1.png]][[image:mu1.png]]Memory use is zero and there are no forgotten drawing commands. [[code format="lb"]] 'good practice to start with a clean sheet #1.gb "cls" 'or #1.gb "discard" [[code]] ==Static graphics== If you are painting graphics that will not be changed,sayperhaps aCompanycompany logo then simply draw and flush once. [[image:scr2.png]][[image:mu1.png]]Memory use is managed to one segment. [[code format="lb"]] 'this is the static graphic example nomainwin WindowWidth = 600 WindowHeight = 400 UpperLeftX = (DisplayWidth-WindowWidth)/2 UpperLeftY = (DisplayHeight-WindowHeight)/2 graphicbox #1.gb, 50,25,500,300 open "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 #1 end [[code]] ==Refreshed graphics== If you are painting fresh graphics, that completely replace the previous graphics, then you mustDELSEGMENTdelsegment the previous segment prior to drawing and flushing the next. That way there is only ever one segment in memory.This is a task that can get folks tied in knots. If you find that your program runs out of memory it is probably because you are not obtaining the correct segment number to delete.[[image:scr8.png]][[image:mu1.png]]Memory use is managed to one segment. [[code format="lb"]] 'this is the refreshed graphic example, only one segment 'is ever retained in memory. nomainwin WindowWidth = 600 WindowHeight = 400 UpperLeftX = (DisplayWidth-WindowWidth)/2 UpperLeftY = (DisplayHeight-WindowHeight)/2 button #1.b, "Draw Graphic", [nextdrawing], UL, 250, 340 textbox #1.tb 350, 340, 100, 25 graphicbox #1.gb, 50,25,500,300 open "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=1 wait [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 screen if drawing=1 then '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" end if if drawing=2 then '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" end if if drawing=3 then '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" end if drawing=drawing+1 if drawing=4 then 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 #1 end [[code]] ==Animated graphics== If you are painting animated graphics by overdrawing and redrawing repetitively you must manage memory and repetitivelyDISCARDdiscard the current segment. The screen will behave as you expect but unless youDISCARDdiscard the current segmenthistorymemory use will build. [[image:scr3.png]][[image:mu1.png]]Memory use is minimal. [[code format="lb"]] nomainwin WindowWidth = 600 WindowHeight = 400 UpperLeftX = (DisplayWidth-WindowWidth)/2 UpperLeftY = (DisplayHeight-WindowHeight)/2 graphicbox #1.gb, 50,25,500,300 open "Animated Graphic Example" for window_nf as #1 #1 "trapclose [quit]" #1.gb "down" x=100 y=100 dx=10 dy=10 timer 17, [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>475 then x=475 : dx=dx*-.9 if x<25 then x=25 : dx=dx*-.9 if y>275 then y= 275 : dy=dy*-.9 if y<25 then 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] timer 0 close #1 end [[code]] ==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,FLUSHflush that as the first segment. You will retain that segment. Now draw andFLUSHflush the foreground segment. Next time throughDELSEGMENTdelsegment the foreground segmentREDRAWredraw the background and then draw andFLUSHflush the new foreground. [[image:scr4.png]][[image:mu1.png]]Memory use will not exceed two segments. [[code format="lb"]] 'this is the background graphic example, only two segments 'are ever retained in memory. nomainwin WindowWidth = 600 WindowHeight = 400 UpperLeftX = (DisplayWidth-WindowWidth)/2 UpperLeftY = (DisplayHeight-WindowHeight)/2 button #1.b, "Draw Graphic", [nextdrawing], UL, 250, 340 graphicbox #1.gb, 50,25,500,300 open "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=2 wait [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=2 then '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" end if if drawing=3 then '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" end if drawing=drawing+1 if drawing=4 then drawing=2 'now flush the current history as the new foreground segment 'using the foregroundimage name #1.gb "flush foregroundimage" wait [quit] close #1 end [[code]] ==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 useFillsfills 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 withREDRAW (name). And appear to changeredraw. 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. [[image:scr5.png]][[image:scr6.png]][[image:scr7.png]][[image:mu2.png]]Memory use builds depending on segments used.