ARTICLE - Graphics Drawing Rules
- a Liberty BASIC graphics tutorial
- copyright 2002, Alyce Watson (alycewatson@charter.net)

Home

Paths and File Names
Observed online
Resources for the beginner
Graphics Drawing Rules
beginner's guide to API and DLL
Drawing IN MEMORY
Radiobuttons via API
NumbWord
Working with Comboboxes

BINARY RASTER OPERATIONS
The Liberty BASIC drawing rules are examples of Binary Raster Operations, sometimes referred to as ROP2.

Each raster-operation code represents a Boolean operation in which the values of the pixels in the selected pen and the destination image are combined. The manner in which they are combined is described below. The destination image in this case means the graphics that are contained in a graphicbox or graphics window at the time the drawing is accomplished.

Here is the Liberty BASIC syntax for setting the drawingrule:

print #handle, "rule rulename"
print #handle, "rule xor"     'as it would appear in a program
print #handle, "rule over"    'as it would appear in a program
or
print #handle, "rule "; _R2_NOTXORPEN 'as it would appear in a program

This command specifies whether drawing overwrites (rulename OVER) on the screen or uses the exclusive-OR technique (rulename XOR). You can also use Windows constants to select a drawing rule (as shown above). Note that the Liberty BASIC named rules, OVER and XOR must be placed within the quote marks. If Windows constants are used for the drawing rule command, they must be preceded by an underscore and placed outside of the quote marks.

The most-used rules are the two named Liberty BASIC rules: OVER and XOR. OVER causes pixels drawn with graphics drawing commands such as LINE, BOX or CIRCLE to appear in the currently selected COLOR, replacing the pixels on the screen. XOR combines the color of the drawing pen with the pixels on the screen using the exclusive-OR operation. Read below for more about using RULE XOR.

The drawing rule can be changed at any time during graphics drawing, and affects only the graphics drawn after the rule is changed. The rule can be changed many times, creating an unlimited number of interesting or unusual effects. Look below for an explanation of the binary raster operations, then experiment with them in your graphics to see the possibilities.

Here are the constants that Windows defines:

_R2_BLACK
Pixel is always 0.

_R2_COPYPEN Liberty BASIC's rule OVER.
Pixel is the pen color.

_R2_MASKNOTPEN
Pixel is a combination of the colors common to both the screen and the inverse of the pen.

_R2_MASKPEN
Pixel is a combination of the colors common to both the pen and the screen.

_R2_MASKPENNOT
Pixel is a combination of the colors common to both the pen and the inverse of the screen.

_R2_MERGENOTPEN
Pixel is a combination of the screen color and the inverse of the pen color.

_R2_MERGEPEN
Pixel is a combination of the pen color and the screen color.

_R2_MERGEPENNOT
Pixel is a combination of the pen color and the inverse of the screen color.

_R2_NOP
Pixel remains unchanged.

_R2_NOT
Pixel is the inverse of the screen color.

_R2_NOTCOPYPEN
Pixel is the inverse of the pen color.

_R2_NOTMASKPEN
Pixel is the inverse of the R2_MASKPEN color.

_R2_NOTMERGEPEN
Pixel is the inverse of the R2_MERGEPEN color.

_R2_NOTXORPEN
Pixel is the inverse of the R2_XORPEN color.

_R2_WHITE
Pixel is always 1.

_R2_XORPEN Liberty BASIC's rule XOR
Pixel is a combination of the colors in the pen and in the screen, but not in both.


RULE XOR
Rule XOR gives us a very handy ability. It allows us to draw graphics, then to draw them again later in the same spot and erase them, leaving the pixels on the screen as they were before the graphics were drawn. Look in the FreeForm program code that comes with Liberty BASIC. Objects are drawn on the screen and moved about using rule XOR. While an object is being moved by the user, it is drawn once to be displayed on the screen. When the mouse moves as the user drags the object, it is drawn a second time at the same location to "erase" it and restore the screen to its previous appearance. It is then drawn in its new location, only to be drawn again on that spot to "erase" it when it is moved again. This continues as long as the object is being moved by the mouse. When it is in the desired position, the rule is changed back to rule OVER and the object is drawn again, not to be erased this time.

Freeform is a large and complex program. For a simple example of rule XOR at work, run the following small demo program. It begins with some text on the screen. When the "Do Change" menu is clicked, graphics are drawn using rule XOR. Click the "Do Change" menu again, and the screen is restored to the simple text it displayed at the start. The graphics have been "erased", leaving the appearance of the screen exactly the same as it was before the drawing took place. Notice that when using rule XOR, drawn colors are dependent upon the existing colors on the screen. "Red" will not necessarily appear red. A "red" line may change colors many times as it passes over pixels of different colors that are already on the screen. Change the colors and backcolors in the code below to see how color is affected by rule XOR. Rule XOR is not the best choice when consistency of colors is needed. It is the best choice when temporary graphics must be drawn and removed, leaving the screen appearance unchanged.

nomainwin
UpperLeftX=100:UpperLeftY=10
WindowWidth=400:WindowHeight=440
menu #1, "&Change","&Do Change",[doChange],_
    "E&xit",[quit]
open "XOR Demo" for graphics_nsb_nf as #1
print #1, "trapclose [quit]"

'drawn with default OVER:
print #1, "down; color blue; backcolor yellow"
print #1, "fill yellow; size 5"
print #1, "place 20 140"
print #1, "font arial 20"
print #1, "\XOR Drawing Rule Demo!"

'switch to XOR:
print #1, "rule xor"
wait

[doChange]
print #1, "place 240 240"
print #1, "color red; backcolor cyan"
print #1, "circlefilled 140"

print #1, "place 10 70"
print #1, "color darkgreen; backcolor white"
print #1, "boxfilled 210 240"

print #1, "place 210 100"
print #1, "color blue; backcolor pink"
print #1, "ellipsefilled 100 125"

wait

[quit]
close #1:end

  

Home

Paths and File Names
Observed online
Resources for the beginner
Graphics Drawing Rules
beginner's guide to API and DLL
Drawing IN MEMORY
Radiobuttons via API
NumbWord
Working with Comboboxes