TweetFollow Us on Twitter

TML Pascal Tricks
Volume Number:6
Issue Number:3
Column Tag:Pascal Procedures

TML Pascal Tricks

By Paul Diegenbach, Amsterdam, the Netherlands

XCMD - drawing in a Hypercard stack

The attractiveness of Hypercard is for a large part due to it graphical possibilities. Nice cards always mean nice graphics. The Hypertalk language allows drawing on the screen under program control. When we tear the tools menu off the menubar, we can actually see the actions as the tools flop from pen to brush to marquee and so on. Nice to watch, but oh so slow. Ever tried to draw a complicated graph in Hypercard? I tried it in an attempt to simulate the differentiation of an egg to all the different cells an embryo consists of. One of the theories implies the evaluation of some differential equations and then drawing a thousand points on the screen.

The idea was to let students be able to change the parameters in the equations and see the results in a user-friendly environment. The first step was to implement the equation solving in Hypertalk. Too slow, so an XCMD was quickly written. Drawing the results by Hypertalk: still to slow, so why not do the drawing in the XCMD as well? In a first result the drawing looks nice, but as soon as Hypercard updates the screen from its offscreen bitmap, the drawing disappears. The solution to this problem is to copy the drawing in the paste buffer (scrap) and paste it into Hypercard using the paste command from the menu. This combination works fine and quick. The skeleton for the XCMD is given below, in TML-Pascal, with a Fourier-demo in the drawing part to make it less dumb. In this demo we use parameters from Hypercard for the size of the rectangle and for the Fourier coefficients. Although not necessary, it is often handy to give the rectangle parameters. They can easily be estimated by having a temporary field of the same size and asking in the message window ‘put the size of card field tmp into msg’. The Hypercard stack with this demo implemented is on the source disk for this month.

To avoid flicker between the copy and paste, it is convenient to lock the screen before the XCMD is used. Users of other Pascals sometimes need to remove the ParamPtr parameter, that is the first parameter in every call to the glue routines:

{1}

UNIT PictToScrp;
(******************************************************      
This XCMD demodnstrates a method to plot and calculate in Pascal (perhaps 
with floating point processor enabled) and then copy and paste the picture 
in the card compile and link as follows (assuming Fourier is an existing 
stack):
  
  TMLPascal Fourier.p
or:  ( really fast on a mac II)
  TMLPascal Fourier.p  -MC68020 -MC68881 -d Elems881=true

  Link -w -t ‘STAK’ -c ‘WILD’ 
  -m FOURIER -rt XCMD=1282 -sg Fourier 
   Fourier.p.o 
   “{Libraries}”Runtime.o 
   “{Libraries}”Interface.o 
   “{TMLPLibraries}”TMLPasLib.o 
   “{Libraries}”HyperXLib.o 
   -o “Fourier”

 Hypercard call :
  set the lockscreen to true
  -- prevents flicker
  Fourier  330,14,480,319,10,7,5,3,4
  -- 330,14,480 is the area to draw into
  -- 10,7,5,3,4  are the fourier factors times 10
  set the lockscreen to false
 ******************************************************)

INTERFACE

  USES MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf, PasLibIntf, HyperXCMD;

  PROCEDURE Fourier(ParamPtr: XCMDPtr);

IMPLEMENTATION

  PROCEDURE PictScrap(ParamPtr: XCMDPtr); FORWARD;

  PROCEDURE Fourier(ParamPtr: XCMDPtr);
    BEGIN
      PictScrap(ParamPtr);
    END;

  PROCEDURE PictScrap(ParamPtr: XCMDPtr);
    VAR
      thePoint     : Point;
      hPicture     : PicHandle;
      rg           : rect;
      Piclength    : LONGINT;
      xoffset, yoffset: integer;
      str          : str255;
      xLeft, bot, top, xRight: LONGINT;
      one, two, three, four, five: LONGINT;
      x, y         : extended;
      z1, z2, z3, z4, z5: extended;

PROCEDURE MyDrawing;
 { all your drawing stuff goes here }
      VAR
        i            : integer;
        maxim, offset: extended;

      FUNCTION Sinus(x: extended): extended;
        BEGIN
          Sinus := sin(x) * z1 + sin(2 * x) * z2 + sin(3 * x) * z3 + 
sin(4 * x)  * z4 + sin(5 * x) * z5
 { the more, the better }
        END;

      BEGIN
        eraserect(rg);
        Framerect(rg); { optional }
        offset := top + (bot - top) / 2.0;
        z1 := one / 10.0;
        z2 := two / 10.0;
        z3 := three / 10.0;
        z4 := four / 10.0;
        z5 := five / 10.0;
        x := 0.02 {1 / 50} ;
        maxim := Sinus(x);
        FOR i := 2 TO 314 DO
          BEGIN
          IF i MOD 10 = 0 THEN
SendCardMessage(ParamPtr, ‘set cursor to busy’);
          x := i / 50;
          y := Sinus(x);
          IF maxim < y THEN
            maxim := y;
          END;

        x := 1 / 50;
        y := Sinus(x);
        maxim := 100 / maxim;
SendCardMessage(ParamPtr, ‘set cursor to busy’);
        moveto(xLeft + 1, round(y * maxim + offset));
        FOR i := 2 TO 314 DO
          BEGIN { for two pi }
          x := i / 50;
          y := Sinus(x);
lineto(i + xLeft + 1, round(y * maxim + offset))
          END;
SendCardMessage(ParamPtr, ‘set cursor to busy’);
        penpat(ltgray);
        moveto(xLeft + 1, round(offset));
        lineto(xRight - 2, round(offset));
        pennormal
      END;

    FUNCTION GetParameter(n: integer): LONGINT;
      VAR
        tmp          : LONGINT;
      BEGIN
        ZeroToPas(ParamPtr, ParamPtr^.params[n]^, str);
        stringToNum(str, tmp);
        GetParameter := tmp
      END;

    BEGIN
      IF ParamPtr^.paramCount <> 9 THEN
        SendCardMessage(ParamPtr, ‘put “wrong number of parameters” into 
msg’)
{ I think a more convenient way of error report than by ParamPtr^.ReturnValue 
}
      ELSE
        BEGIN
{get the drawing rectangle coordinates from hypercard }
        xLeft := GetParameter(1);
        top := GetParameter(2);
        xRight := GetParameter(3);
        bot := GetParameter(4);

{ In this example : get Fourier coëfficients from hypercard }
        one := GetParameter(5);
        two := GetParameter(6);
        three := GetParameter(7);
        four := GetParameter(8);
        five := GetParameter(9);
        setrect(rg, xLeft, top, xRight, bot);
        xoffset := xLeft + 5;
        yoffset := top + 5;
        cliprect(rg);
        hPicture := OpenPicture(rg);
        Showpen;
        MyDrawing; { here all your drawing }
        closepicture;
        Piclength := GethandleSize(handle(hPicture));
        IF ZeroScrap + putscrap(Piclength, ‘PICT’, ptr(hPicture^)) = 
NoErr THEN
     ParamPtr^.ReturnValue := PasToZero(ParamPtr, ‘’)
        ELSE
          ParamPtr^.ReturnValue := PasToZero(ParamPtr, ‘something wrong’);
SendCardMessage(ParamPtr, ‘choose select tool’);
SendCardMessage(ParamPtr, ‘domenu “Paste Picture”’);
SendCardMessage(ParamPtr, ‘choose browse tool’);
        END
    END;
END.

Small and Dirty “Write Always”

In the debugging of large programs we were accustomed to put a lot of ‘writeln’s’ in the source text to show what was happening. Although a workable solution on mainframes and mini’s, that’s not so easy in the Macintosh environment, there is not always a window available to write in. The following small subroutine (TML Pascal version) does the trick with some disadvantage: it overwrite all its lines on top of each other at the bottom of the screen in some kind of status line (bit like Online companion), but, sorry, the line is not automatically removed after leaving the program. Dirty isn’t it, so surely for debugging only. An ‘invalrect’ after each multifinder event and at the end of the program is sometimes a solution.

In the program source you write something like :

--2

 if dbg then DQText(‘now in procedure blahblah’)

(The clever one’s can write a MPW-script to put it after the first begin in every procedure or function) Dbg is used as a constant (or in a compiler directive).

The procedure (as a Pascal unit) looks like :

{3}

UNIT DQTextProcedure;
  INTERFACE
    USES
MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf, QDaccess;
 
{ Tml pascals QDaccess defines:
 TYPE  QDGlobalsPtr = ^QDGlobalsRec;
    QDGlobalsRec =
      record
        randSeed   :  LongInt;
        screenBits :  BitMap;
        arrow      :  Cursor;
        dkGray     :  Pattern;
        ltGray     :  Pattern;
        gray       :  Pattern;
        black      :  Pattern;
        white      :  Pattern;
        ThePort    :  GrafPtr;
      end;
function QDGlobals: QDGlobalsPtr;external;}

PROCEDURE DQText(Message: Str255);
  IMPLEMENTATION
PROCEDURE DQText(Message: Str255);
VAR
  SavedPort    : GrafPtr;
  LocalPort    : GrafPort;
  TheRect      : Rect;
BEGIN
  WITH QDGlobals^ DO
    BEGIN
      GetPort(SavedPort);
      OpenPort(@LocalPort);
      SetPort(@LocalPort);
      WITH ScreenBits.Bounds DO
 { this is QDGlobals^ s ScreenBits ! }
SetRect(TheRect, Left, Bottom-20, Right, Bottom);
      FillRect(TheRect, White);
      MoveTo(TheRect.Left, TheRect.Top);
      LineTo(TheRect.Right, TheRect.Top);
      MoveTo(TheRect.Left+7, TheRect.Bottom-7);
      DrawString(Message);
      ClosePort(@LocalPort);
      SetPort(SavedPort);
   END;
END;
END.

PROGRAM tstDQtext;
  { test the dqtext procedure : }
  USES MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf,
    PaslibIntf, QDaccess, DQtext;
  (*
  link as :
    Link  -t ‘APPL’ -c ‘????’ 
      DQtext.p.o 
      tstDQtext.p.o 
      “{Libraries}”Runtime.o 
      “{Libraries}”Interface.o 
      “{TMLPLibraries}”TMLPasLib.o 
      -o tstDQtext
  *)
  BEGIN
    textbook(NIL); { TML pascal simple window }
    writeln(‘ see bottom line’);
    writeln(‘ click mouse button’);
    PLFlush(output);
    DQtext(‘before’);
    REPEAT
    UNTIL button;
    DQtext(‘after mouse button’);
    writeln(‘ type return to end program’);
    readln
  END.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom 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
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.