TweetFollow Us on Twitter

Foundation Of Quickdraw
Volume Number:1
Issue Number:2
Column Tag:features of Mac Pascal

QuickDraw Graphics

By Chris Derossi

One of the most valuable features of Mac Pascal is it ability to take advantage of the Macintosh’s powerful ROM capabilities. To do this, Mac Pascal must be able to access the various routines that are contained within the Mac Toolbox. In this month’s column, we will examine the interface between Mac Pascal and the ROM, and the necessary data structures for accessing quickdraw. Finally, a sample program is included which makes use of quickdraw’s graphics routines.

In order to communicate to the Toolbox, several data structures have to be defined. These data structures are all presented in Pascal format because the routines contained in the Mac ROM are constructed for a Pascal environment. Unlike other programming languages, this makes data compatiblitly pretty much straight forward for Mac Pascal users. But before we get into the details, let’s briefly describe some of the theory behind QuickDraw.

FOUNDATION OF QUICKDRAW

QuickDraw graphics exist in cartesian coordinate planes. What this means is that graphics may be present on any portion of a continuous surface. The QuickDraw planes are finite, and extend from values -32768 to 32767 in both the vertical and horizontal directions. The smallest unit of distance that QuickDraw can work with is 1. An area of 1 by 1 is, therefore, a point. The boundary separating two points at sequential locations of the plane is of size zero. This means that two points placed next to each other are continuous, and have no space between them. Keep in mind that these are theoretical concepts, not screen bit-map specifications.

QUICKDRAW DATA STRUCTURES

In Mac Pascal, the data structure ‘Point’ is a record with two components representing the coordinates of the upper left corner of a point. The two fields of Point are Point.h and Point.v which are the horizontal and vertical coordinates respectively. Each value is of type Integer.

The rectangle is a common object found in QuickDraw. Rectangles are frequently used to create windows, define drawing areas, and to provide parameters for manipualting other objects. A rectangle has four parameters. These are: Top, Left, Bottom, and Right. These parameters may be thought of as four integers, or as the two points TopLeft and BottomRight. In actuality, the second method is used, and the type ‘Rect’ is defined as a record with the two fields Rect.TopLeft and Rect.BotRight. Each of the fields is of type point.

Another object defined with points is the line. Although there is no Mac Pascal data type for this object, it can be characterized by two points, for the beginning and ending positions of the line.

The final data type we will examine this month is the pattern. QuickDraw allows regions to be filled with patterns, as is done with the paint bucket in MacPaint. A pattern is a set of sixty-four points either on or off, arranged in an eight by eight square. To fill an area larger than eight by eight, the Macintosh repeats the pattern both vertically and horizontally as many times as neccessary. The upper left corner of the pattern is aligned with the coordinate (0,0), and is independent of the coordinates of the area being filled.

Since a byte is eight bits, and a bit represents one point, patterns can be represented as a series of eight bytes. In Mac Pascal, the type ‘Pattern’ is an array [0..7] of 0..255, which is effectively eight sequential bytes. There are some predefined patterns including White, Black, and Grey.

DRAWING IN QUICKDRAW

The drawing mechanism in QuickDraw is the Pen. The pen has modifyable properties which include location, width, height, and pattern. There are further properties which will be covered in a later column. A pen of width and height of one, and a pattern of black will always draw with one-point wide, black lines.

In addition to the many objects of QuickDraw, many actions are available to be performed on or with them. Most objects may be Framed, Painted, Filled, and Erased. The QuickDraw actions are called QuickDraw verbs, and they are applied to objects. The verb Frame specifies that the outline of the given object should be drawn. The verb Paint means that the object is drawn completely filled. Each of these actions use the pen for drawing, and as such use the current pen pattern. The verb Erase does not use the pen pattern, but instead uses the pattern currently set for the background. Fill allows you to specify a pattern to use, and is therefore a little bit more versatile than Paint.

For example, FrameRect will draw the outline of a rectangle, while PaintRect will draw a solid rectangle. FillRect will draw a solid rectangle in the given pattern. EraseRect clears the given rectangle to the current background pattern.

Each verb-object combination is a procedure, and takes applicable parameters. The Rect procedures take a rectangle as their only parameter. The FillRect procedure needs a Rect first, and a Pattern second. The rectangle is drawn as a solid with the pattern.

A frequently encountered situation is one where the Macintosh needs to know if a given point is inside a particular rectangle. (i.e. is the mouse’s position inside a certain window?) To satisfy this need, a function titled PtInRect returns a boolean of True if the point is inside the rectangle, and False if not. The parameters of this function are a point first, and then a rectangle.

PROGRAMMING EXAMPLE

Now let us examine the example program that makes use of these data structures, procedures, and functions. The operation of the program is fairly simple and straight forward. The user is presented with a blank background and the mouse pointer. The user forms rectangles by pressing the mouse button when the pointer is at one of the rectangle’s corners, and holds the button down while dragging the mouse to the opposite diagonal corner. In order to provide some feedback about the shape of the rectangle, a ‘rubber banding’ diagonal line follows the mouse until the button is released. When the button is released, the rubber band line is removed, and the rectangle is drawn.

WHERE’S THE MOUSE?

After the rectangle is drawn, the user may place the mouse pointer inside the rectangle, and press the button. As long as the button is held, the Mac continually fills the rectangle with random, changing patterns. Once the mouse button is pressed outside of the rectangle, the pattern is frozen, and a new rectangle is begun. (Please see the sample output in fig. 1.)

The construction of the program is not complex. The variable declarations for the main program consist of three points and one rectangle. The points are used as work variables for reading the mouse location, and the rectangle is the one currently being drawn or filled.

The main program calls two procedures. The first one allows the user to create the rectangle, and provides the rubberbanding effect. The second one creates random patterns and fills the rectangle.

Since the value of a rectangle’s Top must be less than or equal to that of its bottom, two procedures called ‘Min’ and ‘Max’ help keep the points in the right order. The same holds true for the Left and Right values.

The key procedure calls in the Make_Rect procedure are Button, GetMouse, DrawLine, PenPat, FrameRect, and InsetRect. The function Button returns True if the mouse button is pressed, and False if not. GetMouse uses two VAR parameters for obtaining the current horizontal and vertical position of the mouse pointer.

Since we need to draw and erase the rubberbanding line, we use the two pen patterns of Black and White. This has the disadvantage of erasing whatever the line passes over, but you can’t have everything in a simple little program. The procedure PenPat sets the working pen pattern to the passed value.

DrawLine uses two points, this time in the form of four coordinates, to designate the starting and ending points of the line. DrawLine, of course, uses the current pen pattern.

Finally, after the mouse button is released, the rectangle is drawn with FrameRect. This is done with the pen pattern set to black. Since we don’t want to draw over the outline with subsequent FillRect calls, the procedure InsetRect is called to change the coordinates of the rectangle by one on each side, making it slightly smaller.

After the rectangle is drawn, the Fill_Rect procedure is called to fill it in. Note, please, that Fill_Rect is distinct from FillRect. Fill_Rect uses the calls Button, GetMouse, PtInRect, Random, and FillRect to perform its task. Button and GetMouse are used as before. PtInRect determines if the mouse is inside the new rectangle when the button is depressed.

A new pattern is created by calling the function Random eight times within a loop. Random returns a value from -32768 and 32767. Abs and Mod are used to bring the random number in the range 0..255. After the random pattern is created, FillRect is called to draw the pattern into the rectangle.

MODIFYING THE PROGRAM

The program runs forever, and the menu option Halt must be chosen to exit. In order to overcome this, you might want to modify the program to present you with a Black or Grey box (read rectangle) where you’d click the mouse to quit. Where in the program would you draw the rectangle? At what point or points would you have to check to see if it has been clicked? How will you handle rectangles that overlap your ‘exit’ box? These exercises are left to the student!

That’s it for this month. In the next column, we’ll further explore the powerful features of QuickDraw, and find out what makes it so unique and wonderful among graphic tools. We’ll also have another program that utilizes these terrific features. If you like our Pascal column or have programming ideas of your own to contribute, please feel free to write me care of MacTech. Ciao.

program MacTutor_QD_Demo;

{ This program will use the standard features of } {QuickDraw to introduce 
use of QuickDraw data} {structures and routine calls from MacPascal.} 

{ -- By Chris Derossi}

 var
  P1, P2, P3 : Point;
  WorkingRect : Rect;

 function Min (Num1, Num2 : integer) : integer;

{ return the minimum of the two numbers passed.}

 begin
  if Num1 < Num2 then
   Min := Num1
  else
    Min := Num2;
 end;

 function Max (Num1, Num2 : integer) : integer;

{ returns the maximum of two numbers passed.}

 begin
  if Num1 > Num2 then
  Max := Num1
  else
    Max := Num2;
 end;

 procedure Make_Rect (var theRect : Rect);

{ This is the first of the two main procedure. It} 
{allows the user to select diagonal corners of the} 
{rectangle to be drawn, using a ‘rubber band’ line.} 
{When the button is released, the recatangle is} 
{drawn.}

 begin
  repeat
   until Button;  {Wait till the button is pressed}
  GetMouse(P1.h, P1.v); {Where was the mouse?}
  P2 := P1;
  P3 := P1;
  while Button do  {update while button is down.}
   begin
      GetMouse(P3.h, P3.v);
    if (P2.h <> P3.h) or (P2.v <> p3.v) then  
 {the mouse has moved}
    begin
        PenPat(White);
      DrawLine(P1.h, P1.v, P2.h, P2.v);  
 {Erase the old line}
      PenPat(Black);
    DrawLine(P1.h, P1.v, P3.h, P3.v);  
 {Draw the new line}
    P2 := P3;
    end;
   end;

  PenPat(White);  {Erase line for the last time.}
  DrawLine(P1.h, P1.v, P2.h, P2.v);
  PenPat(Black);
  theRect.TopLeft.v := Min(P1.v, P2.v); 
  
{Set the rectangle, making sure}
  theRect.TopLeft.h := Min(P1.h, P2.h);   
{that the top is above the bottom}
  theRect.BotRight.v := Max(P1.v, P2.v); 
{and the left is less than the}
  theRect.BotRight.h := Max(P1.h, P2.h); {right.}

  FrameRect(theRect);   {Draw the rectangle}
  InsetRect(theRect, 1, 1);  
{Shrink it so that the pattern does not draw}
 {over the rectangle frame.}
 end;

 procedure Fill_Rect (theRect : Rect);

  var
   myPat : Pattern;
   Index : Integer;
   Done : Boolean;

 begin
  Done := False;
  while not Done do  
{continue until the mouse is outside of the}
   {new rectangle.}
   begin
    repeat
      until Button;   {Wait for the button}
    GetMouse(P1.h, P1.v);
    if PtInRect(P1, theRect) then  
{is the mouse in the rectangle?}
    begin
    for Index := 0 to 7 do
     myPat[Index] := Abs(Random) mod 256; {make a random pattern}
     FillRect(theRect, myPat);  {Fill the rectangle}
    end
    else
    Done := True;
   end; {While}
 end; {Fill_Rect}


begin  {Main Program}
 while true do
  begin
    Make_Rect(WorkingRect);
    Fill_Rect(WorkingRect);
  end;
end.   {Main}

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Ableton Live 11.3.11 - Record music usin...
Ableton Live lets you create and record music on your Mac. Use digital instruments, pre-recorded sounds, and sampled loops to arrange, produce, and perform your music like never before. Ableton Live... Read more
Affinity Photo 2.2.0 - Digital editing f...
Affinity Photo - redefines the boundaries for professional photo editing software for the Mac. With a meticulous focus on workflow it offers sophisticated tools for enhancing, editing and retouching... Read more
SpamSieve 3.0 - Robust spam filter for m...
SpamSieve is a robust spam filter for major email clients that uses powerful Bayesian spam filtering. SpamSieve understands what your spam looks like in order to block it all, but also learns what... Read more
WhatsApp 2.2338.12 - Desktop client for...
WhatsApp is the desktop client for WhatsApp Messenger, a cross-platform mobile messaging app which allows you to exchange messages without having to pay for SMS. WhatsApp Messenger is available for... Read more
Fantastical 3.8.2 - Create calendar even...
Fantastical is the Mac calendar you'll actually enjoy using. Creating an event with Fantastical is quick, easy, and fun: Open Fantastical with a single click or keystroke Type in your event details... Read more
iShowU Instant 1.4.14 - Full-featured sc...
iShowU Instant gives you real-time screen recording like you've never seen before! It is the fastest, most feature-filled real-time screen capture tool from shinywhitebox yet. All of the features you... Read more
Geekbench 6.2.0 - Measure processor and...
Geekbench provides a comprehensive set of benchmarks engineered to quickly and accurately measure processor and memory performance. Designed to make benchmarks easy to run and easy to understand,... Read more
Quicken 7.2.3 - Complete personal financ...
Quicken makes managing your money easier than ever. Whether paying bills, upgrading from Windows, enjoying more reliable downloads, or getting expert product help, Quicken's new and improved features... Read more
EtreCheckPro 6.8.2 - For troubleshooting...
EtreCheck is an app that displays the important details of your system configuration and allow you to copy that information to the Clipboard. It is meant to be used with Apple Support Communities to... Read more
iMazing 2.17.7 - Complete iOS device man...
iMazing is the world’s favourite iOS device manager for Mac and PC. Millions of users every year leverage its powerful capabilities to make the most of their personal or business iPhone and iPad.... Read more

Latest Forum Discussions

See All

‘Junkworld’ Is Out Now As This Week’s Ne...
Epic post-apocalyptic tower-defense experience Junkworld () from Ironhide Games is out now on Apple Arcade worldwide. We’ve been covering it for a while now, and even through its soft launches before, but it has returned as an Apple Arcade... | Read more »
Motorsport legends NASCAR announce an up...
NASCAR often gets a bad reputation outside of America, but there is a certain charm to it with its close side-by-side action and its focus on pure speed, but it never managed to really massively break out internationally. Now, there's a chance... | Read more »
Skullgirls Mobile Version 6.0 Update Rel...
I’ve been covering Marie’s upcoming release from Hidden Variable in Skullgirls Mobile (Free) for a while now across the announcement, gameplay | Read more »
Amanita Design Is Hosting a 20th Anniver...
Amanita Design is celebrating its 20th anniversary (wow I’m old!) with a massive discount across its catalogue on iOS, Android, and Steam for two weeks. The announcement mentions up to 85% off on the games, and it looks like the mobile games that... | Read more »
SwitchArcade Round-Up: ‘Operation Wolf R...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for September 21st, 2023. I got back from the Tokyo Game Show at 8 PM, got to the office here at 9:30 PM, and it is presently 11:30 PM. I’ve done what I can today, and I hope you enjoy... | Read more »
Massive “Dark Rebirth” Update Launches f...
It’s been a couple of months since we last checked in on Diablo Immortal and in that time the game has been doing what it’s been doing since its release in June of last year: Bringing out new seasons with new content and features. | Read more »
‘Samba De Amigo Party-To-Go’ Apple Arcad...
SEGA recently released Samba de Amigo: Party-To-Go () on Apple Arcade and Samba de Amigo: Party Central on Nintendo Switch worldwide as the first new entries in the series in ages. | Read more »
The “Clan of the Eagle” DLC Now Availabl...
Following the last paid DLC and free updates for the game, Playdigious just released a new DLC pack for Northgard ($5.99) on mobile. Today’s new DLC is the “Clan of the Eagle" pack that is available on both iOS and Android for $2.99. | Read more »
Let fly the birds of war as a new Clan d...
Name the most Norse bird you can think of, then give it a twist because Playdigious is introducing not the Raven clan, mostly because they already exist, but the Clan of the Eagle in Northgard’s latest DLC. If you find gathering resources a... | Read more »
Out Now: ‘Ghost Detective’, ‘Thunder Ray...
Each and every day new mobile games are hitting the App Store, and so each week we put together a big old list of all the best new releases of the past seven days. Back in the day the App Store would showcase the same games for a week, and then... | Read more »

Price Scanner via MacPrices.net

Apple AirPods 2 with USB-C now in stock and o...
Amazon has Apple’s 2023 AirPods Pro with USB-C now in stock and on sale for $199.99 including free shipping. Their price is $50 off MSRP, and it’s currently the lowest price available for new AirPods... Read more
New low prices: Apple’s 15″ M2 MacBook Airs w...
Amazon has 15″ MacBook Airs with M2 CPUs and 512GB of storage in stock and on sale for $1249 shipped. That’s $250 off Apple’s MSRP, and it’s the lowest price available for these M2-powered MacBook... Read more
New low price: Clearance 16″ Apple MacBook Pr...
B&H Photo has clearance 16″ M1 Max MacBook Pros, 10-core CPU/32-core GPU/1TB SSD/Space Gray or Silver, in stock today for $2399 including free 1-2 day delivery to most US addresses. Their price... Read more
Switch to Red Pocket Mobile and get a new iPh...
Red Pocket Mobile has new Apple iPhone 15 and 15 Pro models on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide service using all the major... Read more
Apple continues to offer a $350 discount on 2...
Apple has Studio Display models available in their Certified Refurbished store for up to $350 off MSRP. Each display comes with Apple’s one-year warranty, with new glass and a case, and ships free.... Read more
Apple’s 16-inch MacBook Pros with M2 Pro CPUs...
Amazon is offering a $250 discount on new Apple 16-inch M2 Pro MacBook Pros for a limited time. Their prices are currently the lowest available for these models from any Apple retailer: – 16″ MacBook... Read more
Closeout Sale: Apple Watch Ultra with Green A...
Adorama haș the Apple Watch Ultra with a Green Alpine Loop on clearance sale for $699 including free shipping. Their price is $100 off original MSRP, and it’s the lowest price we’ve seen for an Apple... Read more
Use this promo code at Verizon to take $150 o...
Verizon is offering a $150 discount on cellular-capable Apple Watch Series 9 and Ultra 2 models for a limited time. Use code WATCH150 at checkout to take advantage of this offer. The fine print: “Up... Read more
New low price: Apple’s 10th generation iPads...
B&H Photo has the 10th generation 64GB WiFi iPad (Blue and Silver colors) in stock and on sale for $379 for a limited time. B&H’s price is $70 off Apple’s MSRP, and it’s the lowest price... Read more
14″ M1 Pro MacBook Pros still available at Ap...
Apple continues to stock Certified Refurbished standard-configuration 14″ MacBook Pros with M1 Pro CPUs for as much as $570 off original MSRP, with models available starting at $1539. Each model... Read more

Jobs Board

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
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
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
Retail Key Holder- *Apple* Blossom Mall - Ba...
Retail Key Holder- APPLE BLOSSOM MALL Brand: Bath & Body Works Location: Winchester, VA, US Location Type: On-site Job ID: 03YM1 Job Area: Store: Sales and Support 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
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.