TweetFollow Us on Twitter

Regions with CLR
Volume Number:2
Issue Number:2
Column Tag:Basic School

Basic Does Regions (with CLR!)

By Dave Kelly, General Dynamics Corp., MacTutor Editorial Board

Hello! This month we will explore the Macintosh Quickdraw routines with a trip into the world of regions. This may be a review for some of you that program in other languages such as Pascal or C as this subject has been covered quite well in past issues of MacTutor (See Vol. 1 No. 3 "Quickdraw does Regions" or Vol 1 No. 4 "Ports O' Call in Quickdraw"). Until recently it has been impossible to meaningfully talk about regions from MSBASIC. Wth the Clear Lake Research (CLR) Libraries available it is now possible to give BASIC equal time with the other languages when referring to Quickdraw. Quickdraw routines are called from BASIC in much the same method used in Pascal or other languages. In fact, we recommend that you review the Pascal columns referred to above as they have information which may pertain to any language, not just Pascal. The same is true for this column when discussing calls to Quickdraw or other routines used by all languages.

Fig. 1 Screen dump of our regions program

First a bit of a review for those that are not familiar with Quickdraw regions and associated calls. We have available to us via the CLR Libraries [Note: These libraries are available from the MacTutor mail order store for $50 for both the libraries and the speech routines. -Ed.], over 20 statements involving regions. These are found listed with their functions on page 14 of the CLR manual or you may refer to Chapter 4 of Macintosh™ Revealed, Volume One by Chernicoff and published by Hayden Press. Instead of going into a detailed study of Quickdraw I refer you to the previous issues of MacTutor mentioned above. (Back issues are available through the MacTutor Store).

A few things to remember when working with Quickdraw: The horizontal coordinates increase from left to right and the vertical coordinates increase from top to bottom. You may want to think of a region as an area on the screen with a set of points inside the region and a set of points outside. A point in the region cannot be both inside and outside the region. It should also be noted that a graphic point is located to the 'right' and 'below' its corresponding mathematical coordinate.

The Region/Clip Demo program (note: requires CLR Libraries) will set up a region and by using the GetClip and SetClip statements (CLR) we can specify the region which we want the text (or graphics) to be drawn to. In this way, only drawing that occurs within the region will show up on the screen. Any drawing outside the region will not show up. This could be useful when you want to draw in a particular part of the screen without disturbing the contents of another part of the screen. The demo program first opens up the appropriate library:

LIBRARY"ToolLib"

Remember to include the disk volume name if necessary. I would advise you to use the Statement Mover program to move the libraries that you use over to the BASIC program file so they will always follow the program no matter what disk you put it on.

Next we set up some of the array variables used by the program and associated library routines. If the variable is undeclared the libraries cannot work. Also be sure to dimension the arrays properly. For example, if the pattern%() array below were dimensioned to 0 instead of three, the routine SetRect would go ahead and try to store 4 variables, but would only be able to find one. The results could be unpredictable. Save your work before running your program in case there might be any misteaks (ha, ha). The SetRect command sets the left, top, right, and bottom coordinates of the array equal to the indicated values respectively.

DIM R%(3),OvalR%(3),fontinfo%(3),pattern%(3)
x1%=50:y1%=20:x2%=450:y2%=200
SetRect R%(0),x1%,y1%,x2%,y2%
SetRect pattern%(0),&HAA,&HAA,&HAA,&HAA
SetRect pattern%(0),&HAA,&HAA,&HAA,&HAA

The window is opened and the background is set to the background pattern defined in the pattern%() array. The screen is cleared so that the background pattern will be redrawn. The pattern%() array is cleared with SetRect and the background pattern cleared so that subsequent CLS statements will clear the screen to white. Note that the CLS command only clears within the clipping region. When a BASIC window is opened up the clipping region defaults to the window size.

WINDOW 1,"",(2,30)-(508,275),4
CALL BACKPAT (VARPTR(pattern%(0)))
CLS
SetRect pattern%(0),0,0,0,0
CALL BACKPAT(VARPTR(pattern%(0)))

Another CLR Library statement allows us to determine the size of the currently selected font. The program sets the TEXTSIZE to 9 and then calls the getfontinfo routine. The font info is returned in the array fontinfo%(). The variable fontinfo%(0) returns the ascent, fontinfo%(1) returns the descent, fontinfo%(2) returns the maximum character width, and fontinfo%(3) returns the vertical distance between the descent line and the ascent line below it. The program needed to know the total spacing for each line of text which is given by the sum of the ascent, descent, and the vertical distance between the descent line and the ascent line below it. Note that a call to getfontinfo would be useful for programs trying to determine the number of lines of text to print per page (or screen) when using various sizes and styles of fonts.

CALL TEXTSIZE(9)
getfontinfo fontinfo%(0)
fh%=fontinfo%(0)+fontinfo%(1)

Next the region handle variables are defined and the clipping region represented by the screen is stored away so it may be restored again later. If the OldReg! handle were not saved in this way, the last clipping region would be used the next time printing to the window. To define a new region, execute a NewRgn command. This allocates space for the new region and the region is initialized to the empty region. GetClip sets the region with the handle indicated (in this case OldReg! is the handle) to the current clip region.

handle!=0
OldReg!=0
NewRgn OldReg!
getclip OldReg!

We now define the region which we want to use as our clipping region. First we initialize the region with NewRgn with handle! . Next we may define the contents of the region. A series of graphics statements will make up the region we wish to create. We open the region with OpenRgn and follow it with whatever graphics commands we desire such as FRAMEOVAL, FRAMERECT, LINE, and others. Note that arcs cannot form part of the region definition. The end of the definition is indicated with the CloseRgn handle! statement where handle! is the handle! of the region being defined. Next the clipping region is set to the region with handle! with the statement Setclip handle! . All points either printed or drawn outside the region will not be shown. Only those points inside the region may be seen when printing is done inside the region.

NewRgn handle!
OpenRgn
    FRAMEOVAL VARPTR(R%(0))
CloseRgn handle!
Setclip handle!

The next several statements of the program print text in the region which has been set as the clipping region. Note that CLR has provided us with the Drawtext text$ statement to drawtext much faster than with the BASIC PRINT statement. To use drawtext you must move the quickdraw pen to the desired pixel position and issue the drawtext statement. It is harder to use that the BASIC printing statements but the speed difference makes it worth using when speed counts.

There are other statements which may be used with regions. One region may be subtracted from another with DiffRgn or use XorRgn or UnionRgn to combine regions. These and other commands will be left for future MacTutor columns. The program uses the framergn command to draw a border around the edge of the region. The current pensize is used for the border line.

CALL PENSIZE (2,2)
framergn handle!

Now for a word of caution: If your program should crash for some reason before your region is disposed of, you should be prepared to manually type in the disposeRgn statement and appropriate handle or handles for the regions used. Remember to dispose of all regions before quiting your program as the space for the regions has been allocated and will not be returned unless the regions have been disposed of. Otherwise, you may find yourself wondering why you are running out of room on the heap. The statement is:

disposergn handle!

A few words on the CLR Libraries clipping statements: The CLR Libraries manual mentions a statement named ClipRect which is supposed to set the clipping region to a rectangle. The ClipRect routine is not included on my ToolLib file. No need to panic yet, however, because the same command can be reconstructed with a combination of region calls and clipping calls. One solution is to use the following sequence to do a ClipRect (set the clipping region to a rectangle):

DIM rect%(3)
handle!=0
SetRect rect%(0),x1%,y1%,x2%,y2%
NewRgn handle!
OpenRgn
    FRAMERECT ( VARPTR(Rect%(0))
CloseRgn handle!
Setclip handle!

You may also use SetRectRgn or RectRgn to set a region equal to a rectangle. The statement syntax is:

SetRectRgn hand!,left%,top%,right%,bottom%

The region with handle hand! is set equal to the rectangle with coordinates left%,top%,right%, and bottom%.

RectRgn hand!, rect%(0)

The region specified by the handle hand! is set equal to the rectangle rect%. A region is not created by this, but must have been previously defined with NewRgn.

You may use either of these two statements in place of the OpenRgn and CloseRgn sequence above as desired.

Use GetClip to set a region to the current clipping region and SetClip to set the clipping region to a different region. You may want to use SetClip to switch back and forth between different clipping regions.

Have any interesting programs you have created after experimenting with clipping regions? Feel free to share them with others by sending them in to MacTutor.

'Region/Clip Demo
'by Dave Kelly
'MacTutor ©1986

LIBRARY"ToolLib"
DIM R%(3),OvalR%(3),fontinfo%(3),pattern%(3)
x1%=50:y1%=20:x2%=450:y2%=200
SetRect R%(0),x1%,y1%,x2%,y2%
SetRect pattern%(0),&HAA,&HAA,&HAA,&HAA
WINDOW 1,"",(2,30)-(508,275),4
CALL BACKPAT (VARPTR(pattern%(0)))
CLS
SetRect pattern%(0),0,0,0,0
CALL BACKPAT(VARPTR(pattern%(0)))
CALL TEXTSIZE(9)
getfontinfo fontinfo%(0)
fh%=fontinfo%(0)+fontinfo%(1)
handle!=0
OldReg!=0
NewRgn OldReg!
getclip OldReg!
NewRgn handle!
OpenRgn
    FRAMEOVAL VARPTR(R%(0))
CloseRgn handle!
Setclip handle!
CALL MOVETO(x1%,y1%)
FOR i=1 TO 18
    CALL MOVETO (x1%,y1%+i*fh%)
    FOR j=1 TO 6
        drawtext "Read Mac Tutor!  "
    NEXT j
NEXT i
CALL PENSIZE (2,2)
framergn handle!
disposergn handle!
dh%=50:dy%=25
x1%=x1%+dh%:y1%=y1%+dy%
x2%=x2%-dh%:y2%=y2%-dy%
SetRect R%(0),x1%,y1%,x2%,y2%

NewRgn handle!
OpenRgn
    FRAMEOVAL VARPTR(R%(0))
CloseRgn handle!

Setclip handle!
CLS
CALL MOVETO(x1%,y1%)
FOR i=1 TO 18
    CALL MOVETO (x1%,y1%+i*fh%)
    FOR j= 1 TO 6
        drawtext "It's Grrrreat!  "
    NEXT j
NEXT i
framergn handle!
disposergn handle!
Setclip OldReg!
disposergn OldReg!
LIBRARY CLOSE
END
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »

Price Scanner via MacPrices.net

New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more
The latest Apple iPhone deals from wireless c...
We’ve updated our iPhone Price Tracker with the latest carrier deals on Apple’s iPhone 15 family of smartphones as well as previous models including the iPhone 14, 13, 12, 11, and SE. Use our price... Read more
Boost Mobile will sell you an iPhone 11 for $...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering an iPhone 11 for $149.99 when purchased with their $40 Unlimited service plan (12GB of premium data). No trade-in is required... Read more
Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more
Updated Apple MacBook Price Trackers
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more
Apple Watch Ultra 2 now available at Apple fo...
Apple has, for the first time, begun offering Certified Refurbished Apple Watch Ultra 2 models in their online store for $679, or $120 off MSRP. Each Watch includes Apple’s standard one-year warranty... Read more

Jobs Board

DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
IT Systems Engineer ( *Apple* Platforms) - S...
IT Systems Engineer ( Apple Platforms) at SpaceX Hawthorne, CA SpaceX was founded under the belief that a future where humanity is out exploring the stars is Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.