|★ APPLICATIONS ★ PROGRAMMATION ★ FILL ROUTINE|AMSTRAD COMPUTER USER) ★|
|Fill routine (Amstrad Computer User)||Applications Programmation|
Fill of Machine Code
This program contains a general purpose routine which will fill any enclosed area of the screen in any chosen colour except the current paper colour. It will work in all screen modes and its operation can be aborted at any time before the area is completely filled.
The routine operates by scanning left and right from the starting point, filling in any unpainted pixels, until it meets a vertical boundary. It then stores its current values on a stack and moves up a line from where it calls itself, recursively, and repeats the process until it meets a horizontal boundary. When this happens the program retraces its steps - by retrieving previous values from the stack and continuing from where they left off -moving downwards if necessary, until all stored values have been removed from the stack and all pixels within the boundary have been filled.
This process, however, has the disadvantage of altering some of the values on the stack as it proceeds so that a complete record of all operations is no longer available.
The effect of this is that it is not easy to reverse the process if anything goes wrong; for instance, if the shape to be filled has a small gap in it then the ink will ‘escape' and proceed to fill in the whole screen. For this reason a second stack has been used to store such information as is necessary to enable the process to be aborted at any time and the partly filled area to be returned to normal. This use of two stacks, requires a large amount of memory for storage and in theory this could amount to more than the entire screen itself takes up. In practice though, two stacks of 2K each should be sufficient for all but the most intricate and elaborate shapes.
To see the routine in action type in the BASIC program and run it. If you have made a mistake in entering the data then the program will stop and inform you of the offending line which can then be corrected and the program re-run. Note that you must enter the checksum at the end of each DATA statement.
If all is well the screen will clear to mode 0 and an irregular shape will appear on the screen inside a large square. The program will then proceed to fill the area around the shape and when this is completed it will fill the shape itself, but in a different colour. After a short delay the whole process will be repeated using modes 1 and 2 and will then start all over again with mode 0. Note that in the highest resolution mode both colours will be the same since in this mode only two colours can be present on the screen. Pressing any key will abort the current fill stage and ‘unfill' the painted area before proceeding to the next stage.
When any section is completely filled there is ample time, especially in the higher resolution modes, to decide whether or not to abort. To stop the program completely press [ESC] twice.
The machine code routine can be stored anywhere in memory by changing the value of start in line 70. If you relocate the program in this way and find that it does not function correctly and if it worked before you relocated it then you have almost certainly missed out a ‘3' somewhere in the DATA statements. Note that when relocating the code you must leave at least 4K for the stacks and about 450 bytes for the routine itself so it should not be loaded above about &9900 or 39168 decimal.
The demonstration program can be removed by deleting lines 310 to 440 and 480 to 520 and the remaining lines can be used as part of a larger program. Note that your program must set X and Y to a point inside the shape being filled before calling the routine. Also the required graphics ink must be previously selected by PLOT a,b,n where a,b is an imaginary point off screen - such as 800,800 - and n is the required ink number.