Ingemar's guide to Think Pascal 4.5d4 Free Mac development usingThink Pascal 4.5written byIngemarRagnemalm.News: (May 2000)There is a patch to make Think Pascal 4.5 compatible with newerversions of MacOS! And this time it works! See"How do I get it" below.Contents: Introduction What is Think Pascal? How do I get it? Setting it up Getting started Having fun with the debugger Doh! Why doesn't my program work? Learning the Pascal language Using Think 4.0.2 demos with 4.5d4 Using Sprite Animation Toolkit from 4.5d4 OK, so what's so great with the debugger? Acknowledgements and final wordsIntroductionThis page is written in order to help you get started with ThinkPascal 4.5, the newest version of Think Pascal, freely downloadableoff the net. The fact that you can download the complete compiler forfree means that we have a complete free development system forthe Mac!Note, however, that it is provided as an unofficial update.You can get no support whatsoever for it from Symantec! Version 4.5d4was made by Rich Siegel (of BBEdit fame), who was in the pastresponsible for the official updates of Think Pascal. We should bethankful to both Rich for making the update and for Symantec forproviding it. Both make no money from it. Let's make a note in the"good reputation" column!About the authorI, Ingemar Ragnemalm, have no official status with Symantec andThink Pascal, but am basically just a long-time satisfied user. Ihave developed large amounts of code with Think Pascal, and I believethat I know both Mac programming and Think Pascal pretty well. Still,I don't claim that I know everything about either of them.I started programming the Mac with Borland's Turbo Pascal, andswitched to Think C since that's what everybody recommended, and Ineeded something a bit more professional than Turbo, something thatcould handle more than one source file at a time.One day, a friend had an old Pascal program that he wanted to runon the Mac. For some reason, I tried Think Pascal when doing it.Actually, I think the reason was that TransSkel was made for Thinkrather than Turbo. And, I was astonished to see how easy it was!Think Pascal nailed my errors far quicker than any of the othersystems I knew, and I used the debugger efficiently almost withoutbeing aware of it. I gave it a few more shots, and was hooked. Iconverted the C code I was working on to Pascal, and saw howdevelopment speed was multiplied. Today, that old code is known asSpriteAnimation Toolkit.I was amazed, and I still am. Part of it was that Pascal is a moreforgiving language than C, and also nicer to use with the Mactoolbox, but an equal part was the qualities of Think Pascal as adevelopment system.What is Think Pascal?Think Pascal is an excellent development system for the Macintosh.It was released by Think Technologies in 1986 as Lightspeed Pascal.Think Technologies was bought by Symantec, and the name was changedto Think Pascal. The last official update came 1992, and the productwas officially discontinued in 1997.Lightspeed Pascal was a fantastic development system,significantly better than its cousin Lightspeed C (Think C). It issurprising that it didn't get more attention. There are some reasons,one that Pascal is often considered to be "just" an educationallanguage, an impression that perhaps was exaggerated by its closerelation to MacPascal, an interpreter that definitely is only foreducational use.But, Lightspeed/Think Pascal is so much more! It is a good compiler, generating good, fast 68k code. The editor auto-formats the code, which keeps it clean and readable, saving you the time to do it. It has a fantastic debugger with powerful features that no other development system has. Learn the debugger, it is fantastic! (See further below.)It has some drawbacks, namely: You can't turn off auto-formatting. It can not generate PPC native code, only 68k code. Careless port-setting can create problems that will make the program break when compiled to disk, but work in the debugger. You can not compile C code, assembler or any other language.All these problems have their solutions: You edit non-Pascal files with a separate text editor, like BBEdit or SimpleText. You develop your program in Think Pascal, and when you need a PPC native version, you make the necessary changes to make it compile in CodeWarrior too. Don't switch to CodeWarrior, just make your code work with both and have the best of both worlds! The debugger vs stand-alone problem never, absolutely never happens to me, so it seems like it is a beginner problem. I believe the problem is port setting, but I'll try to add more info when I can. (See further the section Doh! Why doesn't my program work?.) If you need to link C code with your Think Pascal program, you need to compile it to a library with Think C.How do I get it?DownloadThink Pascal 4.5d4 from Symantec.Downloada better set of interfaces from my archive.Downloadthe Think Pascal 4.5d4->4.5a1 patch. This is necessary to useThink Pascal on MacOS verisons 8.5 and up, and adds some otherimprovements as well.No, you can't use the interface files that come with the compiler.They are buggy, and also tedious to use, with lots of unnecessary"uses". The modified version may not be perfect, but it works a lotbetter.Setting it upDecompress THINK Pascal 4.5d4.sit, Ingemar's UPI for TP4.5d4.cpt and Pascal 4.5a1 Update.sit by dropping them onStuffit Expander. (Keep the archives in case you want to go back.)The result is three folders, one named THINK Pascal 4.5d4, onenamed THINK Pascal 4.5d4 Folder and a third named PascalUpdate. The first contains my modified UPI,the other Think Pascaland the original UPI, with subfolders named THINK Pascal 4.5d4and THINKPas Univ Hdr 2.1, and the third is the update.Now we must clean up this mess.Move the contents of THINK Pascal 4.5d4 (my modifiedinterfaces) into THINK Pascal 4.5d4 Folder:THINK Pascal 4.5d4- that is, move my interfaces to the folder where the compiler islocated. Trash the empty THINK Pascal 4.5d4 folder.Open THINKPas Univ Hdr 2.1. Move Runtime.lib and the folderLibraries into the folder where the compiler is. Take THINK Pascal4.5d4 out of THINK Pascal 4.5d4 Folder, and trash THINK Pascal4.5d4 Folder (now containing only THINKPas Univ Hdr 2.1).Move Interface.Lib from the update frolder to the Think Pascal 4.5folder. Trash Interface.o. (There was another Interface.Lib in theUniv Hdr folder, but that should be trashed by now.)Run THINK Pascal 4.5d4 -> 4.5a1 to update the ThinkPascal 4.5 application.To clean up the Think Pascal 4.5 folder, you may want to put awaythe doc files. If you kept the archives, why not trash them? There issome useful information in them, though.You should now have a folder with the following files and folders: Interface.Lib (was Interface.o when you got it, but you have renamed it) Interfaces (contains my modified files) Libraries (from the Univ Hdr folder) Runtime.Lib (ditto) THINK Pascal 4.5 (that is, TP 4.5a1)The resulting folder should something like this: Rename the folder to THINK Pascal 4.5a1, just to make it tell thetruth. OK, now you have a complete development system ready to use!You need two more things to make it complete: you need aresource editor and a reference to the Mac toolbox!ResEditis the only choice for resource editor if you have an extremely lowbudget. Resorceror may be of interest the day your budget is better.Finally, how about the documentation to the Mac toolbox? InsideMacintosh is the original text on the subject. If you haven't done soalready, I suggest you get a copy of Apple's Inside Macintoshdocumentation. You candownloadit from Apple. There is a lot of documentation so I suggest lookingat the Overview first to give you an idea of what other documentationvolumes might be worthwhile for your needs.An alternative that is easier to use is Think Reference, which wasmade by Symantec but is published by MacTech these days. Commercial,a bit too C-centric, but easy to use.An old shareware solution isInside MacDA. It only covers up to Inside Mac 4, so it is older than ColorQuickDraw, but still small, fast and pretty useful as a reference tothe most important parts. A more modern solution that you can alsoget off the net isObiWanby Peter Lewis. It is shareware, $10.Getting startedThink Pascal is a fairly intuitive development system, where youcan learn a lot by just trial and error. Open dialogs, changesettings, try menu commands, and not least, run and modify demoprograms!Now, how about starting out with the simplest program of all,"Hello world". Do the following:Create a new project. Check "instant project" if you want a mainprogram file created for you. Replace the line{ Insert your program code here }by ShowText; WriteLn('Hello world!');Hit cmd-R (Run) to compile and run. No problem!Now, how about the "real Mac" version of Hello world? It islonger, but not too bad: program Test; {Variables:} var w: WindowPtr; {A window to draw in} r: Rect; {The bounding box of the window} begin {Create the window} SetRect(r, 50, 50, 200, 100); w := NewWindow(nil, r, '', true, plainDBox, WindowPtr(-1), false, 0); {Make it the current drawing port} SetPort(w); {Draw a string!} MoveTo(5, 20); DrawString('Hello world!'); {Wait for a mouse-click, then stop.} while not Button do ; end.All the parameters to NewWindow are a bit confusing, but you willusually read windows from resources, which is easier.I have no intention of providing a complete manual here, but hereare some notes on things that are not entirely intuitive: You must have a project file to compile anything. You can't just open a source file and compile it. The project file describes what source and library files the compiler should use. All files that should be used must be added to the project file/window. The four letters DNVR listed with each file in the project window enable/disable the following features: D: Debug, lets you step, set breakpoints etc in the unit. N: Macsbug names. V: Overflow checking. R: Range checking. Disable DVR for maximum speed, enable them for maximum safety. I believe N only affects the size. Debugging with range and overflow checking on can be a lot easier than without them! Use the procedures ShowDrawing and ShowText to show the Text and Drawing windows from the program. You don't use Drawing and Text in "real" programs, but they are quite useful for quick test hacks. The "Run options" dialog is very important! There, you can select a resource file to use, and to copy into the application when building to disk, and there you set the memory partition and stack size. Note that the memory partition when building to disk is not set there, but by a "SIZE" resource.Having fun with the debuggerHave you tried the debugger yet? You don't really need to know allof it to start working, but there is a lot of potential there, notall of it obvious. It consists of at least four parts: the editorwindows, Lightsbug, Instant and Observe.The Lightsbug window may seem a bit cluttered. The most usefulpart is the variable list, but the other parts are worth exploring.You can click-and-drag variables to the icons down the left side toaccess some special features.The top three icons swich between variable view, register view andheap view. You will mostly use the variable view, although the othertwo are in no way useless! Then follows these: Magnifying glass, Filecabinet, Eye Folder, Pencil icon, Groucho icon and Trash icon. Allare explained if you click on them!The bottom part of the window shows the memory as raw hex numbers.That is occasionally useful, but when you don't use it, you can hideit by dragging the separating line downwards.Don't forget the other parts of the debugger! Lightsbug is justone of them. The innocent-looking Observe and Instant are extremelypowerful and useful, and the editor windows, where you single-stepand set breakpoints, is perhaps the most vital one.Note, however, that power should be used with a little care. Becareful with putting expressions in Observe that use pointers orhandles that may become invalid, or expressions that allocate memory!Here are some specific non-obvious debugger features: You must check "stops in" to be able to set breakpoints. You set breakpoints by clicking "stop signs" in the left side of the editor window. When debugging, the little icon in the upper right corner can be clicked to stop execution at any time. Command-shift-period has the same effect. You can get more than one Lightsbug window! Try cmd-shift-L! Observe will re-evaluate all expressions typed in it if you select Observe from the Debug menu. The contents of Observe and Instant isn't saved when you quit, but if you have much and interesting contents in it, you can save manually from the file menu. The same holds for Instant.Doh! Why doesn't my program work?There are a few pitfalls, problems that you can run into after awhile. You sometimes get error messages about "segment too large". If you do, click the pyramid icon in the project window to switch to "segment view". There, you can drag files between segments. Generally speaking, try to keep you segments below 32k! The debugging gear in Think Pascal is not interrupt-safe, so if you have any VBL tasks, Time Manager tasks, Device Manager or file system callback routines, put a {$Push} {$D-} before the routine's definition, and a {$Pop} after it. It's pretty much impossible to debug Drag Manager tracking routines inside of Think Pascal, because of the games that the Drag Manager plays. (Don't bother trying; your machine will lock up solidly.) You'll need to debug them using Macsbug. You can debug Drag Manager receive handlers, however. It's tricky to debug Apple Event code inside of Think Pascal, because when you send Think Pascal to the background, the user program gets suspended and won't respond to Apple Events. However, if you can assemble a test harness so that your program sends Apple Events to itself, or your test harness can send events to your program from another machine, Think Pascal will pass any events that it receives on to the running program.There is one infamous problem that you should be aware of: Whenyou build a program to disk, and it doesn't work. This can leave abeginner rather stumped and frustrated. Well, don't be. The likelyproblems are the following: Careless port-setting. Inside Think Pascal, the port is set to "Drawing" by default. When running stand-alone, that default is no longer valid, so you must make sure that you SetPort to some valid window. Think Pascal initializes the Toolbox automatically. If you do that yourself too, you may get problems. You should either disable Think's initialization by {$I-}, or let Think do the initializations for you. If you give your program plenty of memory in Run options..., but don't include a SIZE resource with some comparable memory setting, then your program may run out of memory when running stand-alone. Finally, there is one problem that all development system has. If you write outside arrays, or follow invalid pointers, you will get unpredictable errors, errors that will change whenever your program changes. Pascal's range checking can help you find some problems, but invalid pointers, you must look out for yourself. Check if NewPtr/NewHandle/GetResource and similar calls return nil before using the result. When disposing data, set the disposed pointer/handle to nil.Finally, if the compiler is crashing on you for no apparentreason: Are you using MacOS 8.5 or newer? Are you still using4.5d4 (or older) or have you applied the update mentioned above? Ifnot, do it!Learning the Pascal languageI will not teach you Pascal here. To learn the language, you canjust buy a textbook. There is also some resources on the net. Apretty nice, although not Mac-specific, on-line course is availablehere. Note thatthere are some bugs in the links, with mixing "htm" and "htlm"extensions incorrectly, but you will figure it out. There is also alink to another Pascal course atPascal Central.Apart from learning the Pascal language, Mac programming requiresome specific knowledge. Reference material like Inside Mac, ThinkReference or ObiWan holds the information, but they don't teach Macprogramming. But, there is a way, and it is free: the bookMacintoshPascal. That book, and its demos, deal with CodeWarrior, but thedifferences are not too big.Using Think 4.0.2 demos with 4.5a1Now you should go out and get some demos! Here are some places toget them:Mysource-codearchive (also byftp).Pascal Central, trulyworth the name with links to most net resources of interest for aPascal programmer. In particular, check itssource-codesection.PeterLewis has made a large bunch of reusable modules available. (Allnewer code there is for CodeWarrior, but that isn't too hard toadapt.)Water's EdgeSoftware has the Tools Plus library, which supports Think Pascal.LeoPossajennikov has made a number of small freeware games availablewith Think Pascal source-code.Want to play music in your TP application? TryZerius SoundSystem by Cody DW Jones, the only MOD player that comes withPascal interfaces in the standard package &endash; and it is also thebest MOD playing lib I've found!Almost any Pascal source for the Mac is of interest for you. Thereis one little problem though: there are almost no demos that arewritten for TP 4.5a1! You will find demos for TP 4.0.2, and forCodeWarrior, and for both, and perhaps some for Turbo Pascal for theMac or MPW Pascal. You will need to make some changes to make itwork.It will be easiest with TP 4.0.2 demos. Let's get a simple one andmake the necessary changes!A very simple animation demo I wrote isMicroAnimationDemo. Itdemonstrates sprite animation, and is about as simple as it gets. Thelink above is to a version that works with TP 4.0.2 and CodeWarrior.To see how I adapted it to TP 4.5a1, clickhere.Some demos will work with no changes at all. Such demos includeInfo,OneTest,Onthe edge andMemoryGame.Generally speaking, most code that doesn't require any interfaceunits (except the auto-included ones) will run without changes.The demo KbdLts required a few changes. Types.p had to be added tothe project and to the "uses". Some changes in DeskBus.p also messedit up a little, so I had to hack that.When I modified the interfaces, there was one problem that Icouldn't solve to 100%, and that was the changed defintion of"Pattern". Painted Black is a program that required some changes toreflect that problem.Here are some modified demos:MicroAnimationDemoKbdLtsPaintedBlackUsing Sprite Animation Toolkit from4.5a1Sprite Animation Toolkit, my game making library, is a packagethat should be of interest for hobby game programmers, especiallyusing Think Pascal. The standard distribution supports only TP 4.0.2,and most demos require some changes to work with TP 4.5a1. In orderto make it easier for 4.5a1 users to get started, I have put togethera package with SAT 2.5.0 and a couple of demos, all tested with TP4.5d4 with the modified interfaces. (It is not yet tested with4.5a1.)Download!OK, so what's so great with thedebugger?Anyone who speaks for Think Pascal is bound to mention thedebugger in more or less enthusiastic terms.So, what about it? Isn't it just another source-level debugger?No, it isn't. Like all other source-level debuggers, you can setbreakpoints, single-step though the code, inspect variables, andevaluate simple expressions... but that's where the competitors stop.Here's a quick comparison of features:Breakpoints and single-stepping:Think Pascal: Everything is done in the editor windows. This meansno unnecessary windows to clutter your screen, and you can set thebreakpoints before you compile and run!Think C and CodeWarrior up to CW Pro 2: You must first compile andrun. Then the debugger stops, and you can then navigate to the placesyou want breakpoints. Extra windows, harder to navigate, andcompile-and-debug takes much longer!CW Pro 3 is better than older CW in this respect. You setbreakpoints in the editor windows, but you still single-step in aseparate debugger window. Well, it is getting there.Viewing variables:Think Pascal: All variables accessible in the current context arelisted in the Lightsbug window(s).Think C: Old versions: no variable list available. In Symantec C++v8, it finally got variable lists. I don't know if those give thefull set of variables or just a subset.CodeWarrior: Only a subset of the accessible variables are shown.All three can change the value of shown variables.Expressions:Think Pascal: Any expression that is valid in the code isevaluated, including constants and function calls. (This is theObserve window.)Think C: Fairly complex expressions can be evaluated. Functioncalls can be evaluated, but the feature is off by default.CodeWarrior: Only very simple expressions can be evaluated.Constants and function calls are not allowed. On the positive side,expressions can be used for conditional breakpoints, but that wouldonly be useful if the expressions were not so limited.Type-and-execute ("Instant"):Think Pascal: Any code that is valid in the code can be typed inand executed while the program is halted.Think C and CodeWarrior: Don't have it and will never get it.Well, that's what I think is the most important differences. ThinkPascal wins every comparison with a wide margin! (Anyone whodisagrees, please tell me so, but not without solid arguments.) Ifonly the C users realized what they are missing, perhaps we would seeas good debuggers in the future, but as it is now, Think is the topof the line. Spread the word and the future might get brighter.If nothing else, take a little time learning what you can do withit. It is well worth it.Acknowledgements and final wordsThanks to Rich Siegel, Gale Paeper and all others who havesuggested additions and corrections! Some parts of the text is quotesfrom you. And thanks to Symantec for making this possible.I'm sure CodeWarrior fans aren't too amused by the comparison inthe previous section. Let me stress that I am notanti-CodeWarrior in any way. I have it, use it, like much of it, butkeep wishing for a better debugger for it. As I've said before, thebest development system I've been able to come up with is acombination of Think Pascal and CodeWarrior. With TP 4.5a1, it iseven easier than before to use the same code with both compilers!I belive that this page will be a positive thing for all involved: Beginners get a zero-budget, easy to use, powerful system to start with, using a language that is close to english, and with decent amounts of source-code on the net to learn from and work from. Symantec and Bare-Bones Software (that's Rich) get good publicity. (Now don't forget that About box, people!) Metrowerks gets more customers in the long run, since the TP users will want to compile PPC native code sooner or later. Current Mac developers might get better debuggers some day, since TP users have higher expectations on source-level debuggers, which could raise the demand for improvements. The Mac gets more developers, thereby the Mac users get more programs, and Apple gets more customers. The Wintel PC gets more competition, thereby more of the progress it still needs. The world gets... oh, never mind.But... what do I get? Well, I don't know. A higher phone bill, Iguess.Copyright ©1998 Ingemar Ragnemalm.Updated: 5-June-2000 |
|