Windows Console - Area Fill Subroutine

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
dmontaine
Posts: 21
Joined: Mar 15, 2018 7:13

Windows Console - Area Fill Subroutine

Post by dmontaine »

This program, box, contains the subroutine FillArea. You can specify either spaces or one of three different fill characters, and choose the location, size and color of the fill. The program uses the US English keyboard codes with the Consolas font. Users may need to modify it for other languages and fonts. If you do, please post the modified program back to this thread.

Code: Select all

' Program: Fill
' Target: Windows 10/11 Console
' Fill an area with spades of fill characters
' Tested with US English Windows 10 and Consolas font
' Subroutine FillArea Copyright 2022 Donald S Montaine
' Released under the Creative Commons CC-BY license

'=========================================
' SUBROUTINES
'=========================================

sub FillArea(Byval FillType   as string,  _        ' "S" Space, "L" Light, "M" Medium, "H" Heavy
            Byval StartY      as integer, _        ' Row of upper left corner of fill
            Byval StartX      as integer, _        ' Column of upper left corder of fille
            Byval FillWidth   as integer, _        ' Width of the fill
            Byval FillHeight  as integer, _        ' Height of the fille
            Byval AreaColor   as integer)          ' Color of the fill

   ' Variable definitions
   dim ctr        as integer        ' loop counter
   dim FillChar   as integer        ' the code of the character used to fill the area
   dim FillString as string         ' String used to fill the area
   
   ' Codes for character used to build FillString  
   select case FillType
      case "S"  : FillChar = 32
      case "L"  : FillCHar = 176
      case "M"  : FillChar = 177
      case "H"  : FillChar = 178
      case else : return
   end select
   
   For ctr = 1 to FillWidth
      FillString += chr(FillChar)
   next

   ' Draw the Box
   locate StartY, StartX
   color AreaColor
   
   for ctr as integer = StartY to StartY+FillHeight
      locate ctr,StartX
      print FillString ;
   next
end sub

'========================================
' MAIN PROGRAM
'++++++++++++++++++++++++++++++++++++++++

dim LocY as integer                   ' column location of upper left corner of box
dim LocX as integer                   ' row location of upper left corner of box
dim Wid  as integer                   ' width of box
dim Hgt  as integer                   ' height of box

' Colors Enumeration (0 - 15) ie Black = 1, Cyan = 4 etc
Enum
   Black : Blue : Green : Cyan : Red : Majenta : Brown : LightGrey : DarkGrey 
   BrightBlue : BrightGreen : BrightCyan : BrightRed : BrightMajenta : Yellow : White
end enum

width 80,24                          ' set width and height of console

FillArea("S",5,10,60,14,Black)
sleep 500
FillArea("L",5,10,60,14,BrightCyan)
sleep 750
FillArea("H",7,20,40,10,BrightBlue)
sleep 750
FillArea("S",5,10,60,14,Black)
sleep 1000
FillArea("L",5,10,60,14,Green)
sleep 750
FillArea("H",7,20,40,10,BrightGreen)
sleep 500
FillArea("H",10,36,10,4,Black)
sleep 500
locate 24,1
print "Press any key to exit " ;
sleep
Post Reply