home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-07-16 | 5.4 KB | 200 lines | [TEXT/CWIE] |
- // ===========================================================================
- // MyUtils.cp ©1995-97 Timo Eloranta All rights reserved.
- // ===========================================================================
- // MyUtils.h (press Command-Tab to open the associated header)
-
- #include "MyUtils.h"
- #include <LMenuBar.h>
- #include <Sound.h> // Universal header - SndPlay() etc.
- #include "GraphGA_PaneIDs.h"
-
- // ---------------------------------------------------------------------------
- // • FastRandom
- //
- // Called by: main
- // CGraphDrawing::EdgeMutation
- // CGraphDrawing::ThreeConnectedNodes
- // CPopulation::DoMutate
- // CPopulation::DoCrossover
- // RangedRdm, RandomBool
- // ---------------------------------------------------------------------------
- // From: Andrew Welch <andrew@AmbrosiaSW.com>
- // Newsgroups: comp.sys.mac.programmer.help,alt.sources.mac
- // Subject: FastRandom() source
- // Date: Tue, 13 Feb 1996 18:54:00 -0500
- // Organization: Ambrosia Software, Inc.
- //
- // While converting an application to be PowerPC native, I noticed that an
- // A-Trap it made heavy use of (_Random) was not yet native. So I did a quick
- // and dirty translation of the _Random 68K assembler code into C.
- //
- // It's pretty self-explanitory -- call FastRandom(0) to initialize the random
- // number seed, or pass any positive value in the range parameter to generate
- // a psuedo-random number between 0..range - 1
- //
- // Enjoy... and remember, you get what you pay for. :)
-
- short
- FastRandom( short range )
- {
- short result;
- static unsigned long randomSeed;
- register unsigned long calc;
- register unsigned long regD0;
- register unsigned long regD1;
- register unsigned long regD2;
-
- if (range == 0)
- randomSeed = LMGetTicks() + LMGetTime();
- else
- {
- calc = randomSeed;
- regD0 = 0x41A7;
- regD2 = regD0;
-
- regD0 *= calc & 0x0000FFFF;
- regD1 = regD0;
-
- regD1 = regD1 >> 16;
-
- regD2 *= calc >> 16;
- regD2 += regD1;
- regD1 = regD2;
- regD1 += regD1;
-
- regD1 = regD1 >> 16;
-
- regD0 &= 0x0000FFFF;
- regD0 -= 0x7FFFFFFF;
-
- regD2 &= 0x00007FFF;
- regD2 = (regD2 << 16) + (regD2 >> 16);
-
- regD2 += regD1;
- regD0 += regD2;
-
- if (regD0 < 0)
- regD0 += 0x7FFFFFFF;
-
- randomSeed = regD0;
-
- if ((regD0 & 0x0000FFFF) == 0x8000)
- regD0 &= 0xFFFF0000;
-
- // -- Now that we have our pseudo random number, pin it to the range we want
-
- regD1 = range;
- regD1 *= (regD0 & 0x0000FFFF);
- regD1 = (regD1 >> 16);
-
- result = regD1;
- }
-
- return result;
- }
-
- // ---------------------------------------------------------------------------
- // • PlayOneSnd
- //
- // Called by: CGraphGADoc::SpendTime
- // ---------------------------------------------------------------------------
- // Play a single sound from a snd resource. If the Boolean inAsync
- // parameter is true, the sound is played asynchronously.
-
- pascal OSErr
- PlayOneSnd ( ResIDT inSoundID, Boolean inAsync)
- {
- OSErr theOE = noErr;
-
- static SndListHandle sPlayingSound;
- static SndChannelPtr sCurChannel;
-
- if ( sCurChannel ) {
- if (!( theOE = ::SndDisposeChannel( sCurChannel, true )))
- sCurChannel = nil;
- }
-
- if ( sPlayingSound ) {
- OSErr theOE2;
- ::ReleaseResource( (Handle) sPlayingSound );
- if ( !( theOE2 = ::ResError() ))
- sPlayingSound = nil;
- else if ( !theOE )
- theOE = theOE2;
- }
-
- if ( !theOE ) {
- sPlayingSound = (SndListHandle) ::GetResource('snd ', inSoundID );
-
- if ( !( theOE = ::ResError() ))
- if ( !sPlayingSound )
- theOE = resNotFound;
- else {
- ::HLockHi( (Handle) sPlayingSound );
-
- if ( !inAsync ) {
- if (!(theOE = ::SndPlay( nil, sPlayingSound, false ))) {
- ::ReleaseResource( (Handle) sPlayingSound );
- if ( !theOE )
- theOE = ::ResError ( );
- if ( !theOE )
- sPlayingSound = nil;
- }
- }
- else if (!(theOE = ::SndNewChannel( &sCurChannel,
- sampledSynth, initMono, nil)))
-
- theOE = ::SndPlay( sCurChannel, sPlayingSound, true );
- }
- }
- return theOE;
- }
-
- // ---------------------------------------------------------------------------
- // • ToggleMenuItem
- //
- // Called by: CGraphGADoc::PrepareToIterate
- // CGraphGADoc::ObeyCommand
- // CGraphPane::CGraphPane
- // CGraphPane::ObeyCommand
- // ---------------------------------------------------------------------------
- // An utility routine to toggle texts of menu items.
-
- void
- ToggleMenuItem(
- CommandT inCommand,
- ResIDT inStringID )
- {
- ResIDT menuID;
- MenuHandle menuH;
- Int16 menuItem;
- LMenuBar *theMenuBar = LMenuBar::GetCurrentMenuBar();
- Str255 theString;
-
- theMenuBar -> FindMenuItem( inCommand, menuID, menuH, menuItem );
-
- if (menuItem != 0) {
- ::GetIndString( theString, STRx_Menus, inStringID );
- ::SetMenuItemText( menuH, menuItem, theString );
- }
- }
-
- // ---------------------------------------------------------------------------
- // • MyPtInRect
- //
- // Called by: CGraphNodes::LargeContMutate
- // CGraphNodes::RectCrossover
- // ---------------------------------------------------------------------------
-
- Boolean
- MyPtInRect( Int16 inX, Int16 inY, const Rect *inRect )
- {
- Point thePt;
- Rect theRect = *inRect;
-
- SetPt( &thePt, inX, inY);
- ++theRect.right; ++theRect.bottom;
-
- return ::PtInRect( thePt, &theRect);
- }
-