home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Add-Ons / After Dark / Refractor 1.0 / Source / GraphicsModule_main.c next >
Encoding:
Text File  |  1995-11-05  |  3.9 KB  |  166 lines  |  [TEXT/CWIE]

  1. //////////
  2. //
  3. //    Refractor 1.0
  4. //    ©1995 Steven J. Bushell
  5. //
  6. //    This file is based on GraphicsModule_main.c, by Patrick Beard, Bruce Burkhalter
  7. //    and Colin Glassey.  It has been modified to check for the presence of a
  8. //    floating-point coprocessor, and the PowerPC system architecture.  If either of these
  9. //    is present, the appropriate code resource is loaded and used instead of the original
  10. //    68k code resource.
  11. //
  12. //    When the module is finished, the code resource is released from memory.
  13. //
  14. //////////
  15.  
  16. #include "GraphicsModule_Types.h"
  17. #include <A4Stuff.h>
  18.  
  19. pascal OSErr main (Handle *storage, RgnHandle blankRgn, short message, GMParamBlockPtr params);
  20. typedef pascal OSErr (* mainFuncType)(Handle *, RgnHandle, short, GMParamBlockPtr);
  21.  
  22. #if powerc
  23.  
  24.     enum
  25.     {
  26.         uppADModuleProcInfo = kPascalStackBased
  27.              | RESULT_SIZE(SIZE_CODE(sizeof(OSErr)))
  28.              | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(Handle *)))
  29.              | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(RgnHandle)))
  30.              | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(short)))
  31.              | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(GMParamBlockPtr)))
  32.     };
  33.     RoutineDescriptor    mainRD = BUILD_ROUTINE_DESCRIPTOR (uppADModuleProcInfo, main);
  34.     unsigned long __procinfo = uppADModuleProcInfo;
  35.  
  36. #elif !__68881__
  37.  
  38.     static long        type;
  39.     static Handle    code = nil;
  40.     static Boolean    isPowerPC = false;
  41.     
  42.     static Boolean    IsPowerPC (void)
  43.     {
  44.         long    result;
  45.         OSErr    err;
  46.     
  47.         err = Gestalt (gestaltSysArchitecture, &result);
  48.         if (err)
  49.             return false;
  50.         
  51.         return (result == gestaltPowerPC);
  52.     }
  53.     
  54.     static Boolean    hasFPU = false;
  55.     
  56.     static Boolean HasFPU (void)
  57.     {
  58.         long    result;
  59.         OSErr    err;
  60.     
  61.         err = Gestalt (gestaltFPUType, &result);
  62.         if (err)
  63.             return false;
  64.         
  65.         return (result != gestaltNoFPU);
  66.     }
  67.  
  68. #endif
  69.  
  70. //    Prototypes for essential functions.
  71. OSErr DoInitialize(Handle *storage, RgnHandle blankRgn, GMParamBlockPtr params);
  72. OSErr DoClose(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params);
  73. OSErr DoBlank(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params);
  74. OSErr DoDrawFrame(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params);
  75. OSErr DoSetUp(RgnHandle blankRgn, short message, GMParamBlockPtr params);
  76.  
  77. pascal OSErr 
  78. main (Handle *storage, RgnHandle blankRgn, short message, GMParamBlockPtr params)
  79. {
  80.     OSErr    err = noErr;
  81.  
  82.     EnterCodeResource ();
  83.  
  84. #if !powerc && !__MC68881__
  85.     //    If we're a Power Mac, and we've got the code resource, and we're already initialized,
  86.     //    we jump right to the native code resource.
  87.     //    Ditto for a mac with an FPU.
  88.     if ((isPowerPC || hasFPU) && code && (message != Initialize) && (message != Close))
  89.     {
  90.         err = ((mainFuncType)*code)(storage, blankRgn, message, params);
  91.         ExitCodeResource ();
  92.         return err;
  93.     } 
  94. #endif
  95.  
  96.     switch (message)
  97.     {
  98.         case Initialize:
  99. #if !powerc && !__MC68881__
  100.             hasFPU = HasFPU ();
  101.             isPowerPC = IsPowerPC();
  102.  
  103.             //    Get the correct module type for either a PowerPC or a 68k w/FPU.
  104.             type = isPowerPC ? 'Padm' : 'Fadm';
  105.  
  106.             if (isPowerPC || hasFPU)
  107.             {
  108.                 code = GetResource (type, 0);
  109.                 if (code)
  110.                 {
  111.                     HLock (code);
  112.                     err = ((mainFuncType)*code)(storage, blankRgn, message, params);
  113.                 }
  114.                 else
  115.                 {
  116.                     err = DoInitialize (storage, blankRgn, params);
  117.                 }
  118.             }
  119.             else
  120. #endif
  121.             err = DoInitialize (storage, blankRgn, params);
  122.             break;
  123.  
  124.         case Close:
  125. #if !powerc && !__MC68881__
  126.             if (code)
  127.             {
  128.                 ReleaseResource (code);
  129.                 code = nil;
  130.                 isPowerPC = false;
  131.                 hasFPU = false;
  132.             }
  133. #endif
  134.             err = DoClose (*storage, blankRgn, params);
  135.             break;
  136.  
  137.         case Blank:
  138.             err = DoBlank (*storage, blankRgn, params);
  139.             break;
  140.  
  141.         case DrawFrame:
  142.             err = DoDrawFrame (*storage, blankRgn, params);        
  143.             break;
  144.         
  145.         case ModuleSelected:
  146.             // err = DoSelected (*storage, blankRgn, params);
  147.             break;
  148.         case DoAbout:
  149.             // err = DoAbout (*storage, blankRgn, params);
  150.             break;
  151.  
  152.         default: // user may have hit a button
  153.             if(message >= ButtonMessage)
  154.             {
  155.                 // handle the button
  156.                 err = DoSetUp (blankRgn, message, params);
  157.             }
  158.  
  159.             break;
  160.     }
  161.  
  162.     ExitCodeResource ();
  163.  
  164.     return err;
  165. }
  166.