home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / WackyWindows / INIT / main.c next >
Encoding:
C/C++ Source or Header  |  1998-06-19  |  5.9 KB  |  220 lines  |  [TEXT/CWIE]

  1. // ******************************************************************
  2. //    program written by 
  3. //                         Paul Baxter
  4. //                         MacHack'98
  5. //
  6. // ******************************************************************
  7.  
  8. #include <A4Stuff.h>
  9. #include <stdlib.h>
  10. #include "ShowInit.h"
  11. #include "patch.h"
  12.  
  13. //#include "debug.h"
  14.  
  15. #define kIconID                    5000            // Install Icon
  16. #define kNoInstallIconID        5001            // Can't Install Icon
  17.  
  18. #define kSndPlayer68K            'SD68'            // 68K PlaySound Resource type
  19. #define kSndPlayerPPC            'SDPP'            // PPC PlaySound Resource type
  20. #define kShuffle                10000            // Number of times randomize sound play order
  21.  
  22. OSErr InstallSound(void);                        // Installs sounds and sound function
  23. OSErr InstallPatches(void);                        // Installs the patches
  24. short myRandom(short min, short max );            // Random function that takes a min and max
  25.  
  26. PlaySoundUPP gSoundPlayer68K = nil;                // 68K PlaySound Function
  27. PlaySoundUPP gSoundPlayerPPC = nil;                // PPC PlaySound Function
  28. short gNumSounds = 0;                            // Number of sounds
  29. short *gSoundIndex = nil;                        // array holding sound play order
  30. SndListHandle* gMySounds = nil;                    // array of sounds
  31. short gOurResFile;                                // resfile of the INIT
  32.  
  33. // * ******************************************************************************
  34. // *     main
  35. // *             Entry Point for the extension
  36. // * ******************************************************************************
  37. void main(void)
  38. {
  39.     THz theZone;
  40.     OSErr err;
  41.  
  42.     EnterCodeResource();
  43.  
  44.     gOurResFile = CurResFile();
  45.     err = noErr;
  46.  
  47.     theZone = GetZone();
  48.     SetZone(SystemZone());
  49.  
  50.     if (!err)
  51.         err = InstallSound();
  52.  
  53.  //    DEBUG("InstallSound return %d", err);
  54.  
  55.     if (!err)
  56.         err = InstallPatches();
  57.  
  58. //     DEBUG("InstallPatches return %d", err);
  59.     if (!err)
  60.         ShowINIT(kIconID, -1);
  61.     else
  62.         ShowINIT(kNoInstallIconID, -1);
  63.  
  64. //    DEBUG("Exit INIT");
  65.  
  66.     SetZone(theZone);
  67.     ExitCodeResource();
  68. }
  69.  
  70. // * ******************************************************************************
  71. // *     InstallSound
  72. // *             Load sounds and sound player functions
  73. //                 InstallSound Must be called before InstallPatches
  74. // * ******************************************************************************
  75. OSErr InstallSound(void)
  76. {
  77.     Handle theHandle;
  78.     SoundInstallerPtr soundInstallCode;
  79.     ResType theType;
  80.     long result;
  81.     short randcount, index, snd1, snd2, temp;
  82.     OSErr err;
  83.  
  84.     // DEBUG("Enter InstallSound");
  85.  
  86.     gNumSounds =  Count1Resources('snd ');
  87.     gSoundIndex = (short*)NewPtrSys(gNumSounds  * sizeof(short));
  88.     if (gSoundIndex) {
  89.         gMySounds = (SndListHandle* )NewPtrSys(gNumSounds * sizeof(SndListHandle));
  90.         if (gMySounds) {
  91.             for (index = 0; index < gNumSounds; index++) {
  92.                 gSoundIndex[index] = index;
  93.                 UseResFile(gOurResFile);
  94.                 gMySounds[index] = (SndListHandle) Get1IndResource('snd ', index + 1);
  95.                 if (gMySounds[index]) {
  96.                     HLockHi((Handle)gMySounds[index]);
  97.                     DetachResource((Handle)gMySounds[index]);
  98.                 }
  99.             }
  100.  
  101.             // lets play the sounds in a random order
  102.             for (randcount = 0; randcount < kShuffle; randcount++) {
  103.                 snd1 = myRandom(0, gNumSounds);
  104.                 snd2 = myRandom(0, gNumSounds);
  105.  
  106.                 temp = gSoundIndex[snd1];
  107.                 gSoundIndex[snd1] = gSoundIndex[snd2];
  108.                 gSoundIndex[snd2] = temp;
  109.             }
  110.         }
  111.     }
  112.  
  113.     err =  Gestalt(gestaltSysArchitecture, &result);
  114.     if ((!err) && (result == gestaltPowerPC)) {
  115.         theType = kSndPlayerPPC;
  116.         UseResFile(gOurResFile);
  117.         theHandle = Get1IndResource(theType, 1);
  118.         if (theHandle) {
  119.             HLockHi(theHandle);
  120.             DetachResource(theHandle);
  121.             soundInstallCode = NEW_SOUND_INSTALLER_PROC(*theHandle);
  122.             if (soundInstallCode) {
  123.                 gSoundPlayerPPC = CALL_SOUND_INSTALLER_PROC(soundInstallCode, gNumSounds, gSoundIndex, gMySounds);
  124.             }
  125.         }
  126.     }
  127.  
  128.     theType = kSndPlayer68K;
  129.     UseResFile(gOurResFile);
  130.     theHandle = Get1IndResource(theType, 1);
  131.     if (theHandle) {
  132.         HLockHi(theHandle);
  133.         DetachResource(theHandle);
  134.         soundInstallCode = NEW_68K_SOUND_INSTALLER_PROC(*theHandle);
  135.         if (soundInstallCode) {
  136.             gSoundPlayer68K = CALL_SOUND_INSTALLER_PROC(soundInstallCode, gNumSounds, gSoundIndex, gMySounds);
  137.         }
  138.     }
  139.  
  140.     // DEBUG("Exit InstallSound");
  141.  
  142.     if (gSoundPlayer68K || gSoundPlayerPPC) {
  143.         return noErr;
  144.     }
  145.     return -1;
  146. }
  147.  
  148. // * ******************************************************************************
  149. // *     InstallPatches
  150. // *             Loads the patches
  151. //                 InstallSound Must be called before InstallPatches
  152. // * ******************************************************************************
  153. OSErr InstallPatches()
  154. {
  155.     Handle theHandle;
  156.     PatchInstallerUPP patchInstallCode;
  157.     ProcPtr theProc;
  158.     Str255 rName;
  159.     ResType    theType;
  160.     long result;
  161.     short count, index, rID;
  162.     OSErr err;
  163.  
  164.     // DEBUG("Enter InstallPatches");
  165.  
  166.     err =  Gestalt(gestaltSysArchitecture, &result);
  167.     if ((!err) && (result == gestaltPowerPC))
  168.         theType = kPatchTypePPC;
  169.     else 
  170.         theType = kPatchType68K;
  171.  
  172.     UseResFile(gOurResFile);
  173.     count = Count1Resources(theType);
  174.     if (count == 0 && theType == kPatchTypePPC) {
  175.         theType = kPatchType68K;
  176.         UseResFile(gOurResFile);
  177.         count = Count1Resources(theType);
  178.     }
  179.     for (index = 1; index <= count; index++) {
  180.         UseResFile(gOurResFile);
  181.         theHandle = Get1IndResource(theType, index);
  182.         if (theHandle) {
  183.             GetResInfo(theHandle, &rID, &theType, rName);
  184.             HLockHi(theHandle);
  185.             DetachResource(theHandle);
  186.             patchInstallCode = nil;
  187. #ifdef powerc
  188.             if (theType == kPatchTypePPC)
  189.                 patchInstallCode = NEW_PATCH_INSTALLER_PROC(*theHandle);
  190. #endif
  191.             if (!patchInstallCode)
  192.                 patchInstallCode = NEW_68K_PATCH_INSTALLER_PROC(*theHandle);
  193.  
  194.             if (patchInstallCode) {
  195.                 UseResFile(gOurResFile);
  196.                 theProc = CALL_PATCH_INSTALLER_PROC(patchInstallCode, gSoundPlayer68K, gSoundPlayerPPC, rID, true);
  197.             }
  198.         }
  199.     }
  200.     UseResFile(gOurResFile);
  201.  
  202.     // DEBUG("Exit InstallPatches");
  203.  
  204.     return noErr;
  205. }
  206.  
  207. // * ******************************************************************************
  208. // *     myRandom
  209. // *             generate random numbers with a min and max
  210. // * ******************************************************************************
  211. short myRandom(short min, short max )
  212. {
  213.     unsigned    short rnd;
  214.     long        range, t;
  215.  
  216.     rnd = rand();
  217.     range = max - min;
  218.     t = (rnd * range) / RAND_MAX;
  219.     return( t+min );
  220. }