home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / SDKs / Third Party SDKs / ATI RAVE SDK / Samples / QD3D Tests / CommonSample / MyErrorHandler.c < prev    next >
Encoding:
Text File  |  1998-06-09  |  14.8 KB  |  369 lines  |  [TEXT/MPCC]

  1. // Quickdraw 3D sample code
  2. //
  3. // Nick Thompson, AppleLink: DEVSUPPORT (devsupport@applelink.apple.com)
  4. //
  5. // ©1994-5 Apple Computer Inc., All Rights Reserved
  6.  
  7. #include <Processes.h>
  8. #include <Dialogs.h>
  9. #include <Memory.h>
  10. #include <SegLoad.h>
  11. #include <TextUtils.h>
  12.  
  13. #include "QD3D.h"
  14. #include "QD3DErrors.h"
  15.  
  16. #include "MyErrorHandler.h"
  17.  
  18.  
  19. //---------------------------------------------------------------------------
  20.  
  21. const int kErrorHandlerAlertID = 21032 ;            // ID of the alert resource
  22.  
  23. //---------------------------------------------------------------------------
  24.  
  25. // THIS ENUM MUST MATCH THE STRUCT BELOW
  26. enum {
  27.     kWarningString,
  28.     kErrorString,
  29.     kFirstErrorString,
  30.     kLastErrorString,
  31.     kSpaceString,
  32.     kOpenParen,
  33.     kCloseParen,
  34.     kUnknownErrorString
  35. } ;
  36.  
  37.  
  38. // use these to construct intelligable messages
  39. static struct {
  40.     Str255        errorString ;
  41. } stringTable[] = {
  42.     "\pQuickDraw 3D warning.\r\r", 
  43.     "\pQuickDraw 3D Error.\r\r",
  44.     "\p\rFirst Error: ",
  45.     "\p\rLast Error: "
  46.     "\p ",
  47.     "\p(",
  48.     "\p)",
  49.     "\p\rUnknown Message"
  50. } ;
  51.  
  52.  
  53. //---------------------------------------------------------------------------
  54.  
  55.  
  56. /*
  57.     To generate these tables, copy/paste the error codes from QD3DErrors.h
  58.     and use this regexp:
  59.     
  60.     Find:        kQ3Error([0-9A-z]*),
  61.     Replace:    kQ3Error\1, "\\pkQ3Error\1",
  62.     
  63.     Find:        kQ3Warning([0-9A-z]*),
  64.     Replace:    kQ3Warning\1, "\\pkQ3Warning\1",
  65.     
  66.     Find:        kQ3Notice([0-9A-z]*),
  67.     Replace:    kQ3Notice\1, "\\pkQ3Notice\1",
  68.     
  69. */
  70.  
  71.  
  72. // define a table for all of the QuickDraw 3D error codes
  73. struct {
  74.     TQ3Warning    theWarning ;
  75.     Str255        theString ;
  76. } warningTable[] = {
  77.     /* General System */
  78.     kQ3WarningInternalException, "\pkQ3WarningInternalException",    
  79.     /* Object Warnings */
  80.     kQ3WarningNoObjectSupportForDuplicateMethod, "\pkQ3WarningNoObjectSupportForDuplicateMethod",
  81.     kQ3WarningNoObjectSupportForDrawMethod, "\pkQ3WarningNoObjectSupportForDrawMethod",
  82.     kQ3WarningNoObjectSupportForWriteMethod, "\pkQ3WarningNoObjectSupportForWriteMethod",
  83.     kQ3WarningNoObjectSupportForReadMethod, "\pkQ3WarningNoObjectSupportForReadMethod",
  84.     kQ3WarningUnknownElementType, "\pkQ3WarningUnknownElementType",
  85.     kQ3WarningTypeAndMethodAlreadyDefined, "\pkQ3WarningTypeAndMethodAlreadyDefined",
  86.     kQ3WarningTypeIsOutOfRange, "\pkQ3WarningTypeIsOutOfRange",
  87.     kQ3WarningTypeHasNotBeenRegistered, "\pkQ3WarningTypeHasNotBeenRegistered",
  88.     /* Parameter Warnings */
  89.     kQ3WarningVector3DNotUnitLength, "\pkQ3WarningVector3DNotUnitLength",
  90.     /* IO Warnings */
  91.     kQ3WarningInvalidSubObjectForObject, "\pkQ3WarningInvalidSubObjectForObject",
  92.     kQ3WarningInvalidHexString, "\pkQ3WarningInvalidHexString",
  93.     kQ3WarningUnknownObject, "\pkQ3WarningUnknownObject",
  94.     kQ3WarningInvalidMetafileObject, "\pkQ3WarningInvalidMetafileObject",
  95.     kQ3WarningUnmatchedBeginGroup, "\pkQ3WarningUnmatchedBeginGroup",
  96.     kQ3WarningUnmatchedEndGroup, "\pkQ3WarningUnmatchedEndGroup",
  97.     kQ3WarningInvalidTableOfContents, "\pkQ3WarningInvalidTableOfContents",
  98.     kQ3WarningUnresolvableReference, "\pkQ3WarningUnresolvableReference",
  99.     kQ3WarningNoAttachMethod, "\pkQ3WarningNoAttachMethod",
  100.     kQ3WarningInconsistentData, "\pkQ3WarningInconsistentData",
  101.     kQ3WarningReadLessThanSize, "\pkQ3WarningReadLessThanSize",
  102.     kQ3WarningFilePointerResolutionFailed, "\pkQ3WarningFilePointerResolutionFailed",
  103.     kQ3WarningFilePointerRedefined, "\pkQ3WarningFilePointerRedefined",
  104.     kQ3WarningStringExceedsMaximumLength, "\pkQ3WarningStringExceedsMaximumLength",
  105.     /* Memory Warnings */
  106.     kQ3WarningLowMemory, "\pkQ3WarningLowMemory",
  107.     kQ3WarningPossibleMemoryLeak, "\pkQ3WarningPossibleMemoryLeak",
  108.     /* View Warnings */
  109.     kQ3WarningViewTraversalInProgress, "\pkQ3WarningViewTraversalInProgress",
  110.     kQ3WarningNonInvertibleMatrix, "\pkQ3WarningNonInvertibleMatrix",
  111.     /* Quaternion Warning */
  112.     kQ3WarningQuaternionEntriesAreZero, "\pkQ3WarningQuaternionEntriesAreZero",
  113.     /* Renderer Warning */
  114.     kQ3WarningFunctionalityNotSupported, "\pkQ3WarningFunctionalityNotSupported",
  115.     /* DrawContext Warning */
  116.     kQ3WarningInvalidPaneDimensions, "\pkQ3WarningInvalidPaneDimensions",
  117.     /* Pick Warning */
  118.     kQ3WarningPickParamOutside, "\pkQ3WarningPickParamOutside",
  119.     /* Scale Warnings */
  120.     kQ3WarningScaleEntriesAllZero, "\pkQ3WarningScaleEntriesAllZero",
  121.     kQ3WarningScaleContainsNegativeEntries, "\pkQ3WarningScaleContainsNegativeEntries",
  122.     /* Generic Warnings */
  123.     kQ3WarningParameterOutOfRange, "\pkQ3WarningParameterOutOfRange",
  124. } ;
  125.  
  126.  
  127. struct {
  128.     TQ3Error    theError ;
  129.     Str255        theString ;
  130. } errorTable[] = {
  131.     /* Fatal Errors */
  132.     kQ3ErrorInternalError, "\pkQ3ErrorInternalError",
  133.     kQ3ErrorNoRecovery, "\pkQ3ErrorNoRecovery",                    /* TODO: This is not used correctly */
  134.     kQ3ErrorLastFatalError, "\pkQ3ErrorLastFatalError",
  135.     /* System Errors */
  136.     kQ3ErrorNotInitialized, "\pkQ3ErrorNotInitialized",
  137.     kQ3ErrorAlreadyInitialized, "\pkQ3ErrorAlreadyInitialized",
  138.     kQ3ErrorUnimplemented, "\pkQ3ErrorUnimplemented",
  139.     kQ3ErrorRegistrationFailed, "\pkQ3ErrorRegistrationFailed",
  140.     /* OS Errors */
  141.     kQ3ErrorUnixError, "\pkQ3ErrorUnixError",
  142.     kQ3ErrorMacintoshError, "\pkQ3ErrorMacintoshError",
  143.     kQ3ErrorX11Error, "\pkQ3ErrorX11Error",
  144.     /* Memory Errors */
  145.     kQ3ErrorMemoryLeak, "\pkQ3ErrorMemoryLeak",
  146.     kQ3ErrorOutOfMemory, "\pkQ3ErrorOutOfMemory",
  147.     /* Parameter errors */
  148.     kQ3ErrorNULLParameter, "\pkQ3ErrorNULLParameter",
  149.     kQ3ErrorParameterOutOfRange, "\pkQ3ErrorParameterOutOfRange",
  150.     kQ3ErrorInvalidParameter, "\pkQ3ErrorInvalidParameter",            /* TODO: Make this more specific */
  151.     kQ3ErrorInvalidData, "\pkQ3ErrorInvalidData",                /* TODO: Is this used? */
  152.     kQ3ErrorAcceleratorAlreadySet, "\pkQ3ErrorAcceleratorAlreadySet",        /* TODO: Is this used? */
  153.     kQ3ErrorVector3DNotUnitLength, "\pkQ3ErrorVector3DNotUnitLength",
  154.     kQ3ErrorVector3DZeroLength, "\pkQ3ErrorVector3DZeroLength",
  155.     /* Object Errors */
  156.     kQ3ErrorInvalidObject, "\pkQ3ErrorInvalidObject",
  157.     kQ3ErrorInvalidObjectClass, "\pkQ3ErrorInvalidObjectClass",
  158.     kQ3ErrorInvalidObjectType, "\pkQ3ErrorInvalidObjectType",
  159.     kQ3ErrorInvalidObjectName, "\pkQ3ErrorInvalidObjectName",
  160.     kQ3ErrorObjectClassInUse, "\pkQ3ErrorObjectClassInUse",            
  161.     kQ3ErrorAccessRestricted, "\pkQ3ErrorAccessRestricted",
  162.     kQ3ErrorMetaHandlerRequired, "\pkQ3ErrorMetaHandlerRequired",
  163.     kQ3ErrorNeedRequiredMethods, "\pkQ3ErrorNeedRequiredMethods",
  164.     kQ3ErrorNoSubClassType, "\pkQ3ErrorNoSubClassType",
  165.     kQ3ErrorUnknownElementType, "\pkQ3ErrorUnknownElementType",
  166.     kQ3ErrorNotSupported, "\pkQ3ErrorNotSupported",
  167.     /* Extension Errors */
  168.     kQ3ErrorNoExtensionsFolder, "\pkQ3ErrorNoExtensionsFolder",
  169.     kQ3ErrorExtensionError, "\pkQ3ErrorExtensionError",
  170.     kQ3ErrorPrivateExtensionError, "\pkQ3ErrorPrivateExtensionError",
  171.     /* Geometry Errors */
  172.     kQ3ErrorDegenerateGeometry, "\pkQ3ErrorDegenerateGeometry",
  173.     kQ3ErrorGeometryInsufficientNumberOfPoints, "\pkQ3ErrorGeometryInsufficientNumberOfPoints",
  174.     /* IO Errors */
  175.     kQ3ErrorNoStorageSetForFile, "\pkQ3ErrorNoStorageSetForFile",
  176.     kQ3ErrorEndOfFile, "\pkQ3ErrorEndOfFile",
  177.     kQ3ErrorFileCancelled, "\pkQ3ErrorFileCancelled",
  178.     kQ3ErrorInvalidMetafile, "\pkQ3ErrorInvalidMetafile",
  179.      kQ3ErrorInvalidMetafilePrimitive, "\pkQ3ErrorInvalidMetafilePrimitive",
  180.      kQ3ErrorInvalidMetafileLabel, "\pkQ3ErrorInvalidMetafileLabel",
  181.      kQ3ErrorInvalidMetafileObject, "\pkQ3ErrorInvalidMetafileObject",
  182.      kQ3ErrorInvalidMetafileSubObject, "\pkQ3ErrorInvalidMetafileSubObject",
  183.     kQ3ErrorInvalidSubObjectForObject, "\pkQ3ErrorInvalidSubObjectForObject",
  184.     kQ3ErrorUnresolvableReference, "\pkQ3ErrorUnresolvableReference",
  185.     kQ3ErrorUnknownObject, "\pkQ3ErrorUnknownObject",
  186.     kQ3ErrorStorageInUse, "\pkQ3ErrorStorageInUse",
  187.     kQ3ErrorStorageAlreadyOpen, "\pkQ3ErrorStorageAlreadyOpen",
  188.     kQ3ErrorStorageNotOpen, "\pkQ3ErrorStorageNotOpen",
  189.     kQ3ErrorStorageIsOpen, "\pkQ3ErrorStorageIsOpen",
  190.     kQ3ErrorFileAlreadyOpen, "\pkQ3ErrorFileAlreadyOpen",
  191.     kQ3ErrorFileNotOpen, "\pkQ3ErrorFileNotOpen",
  192.     kQ3ErrorFileIsOpen, "\pkQ3ErrorFileIsOpen",
  193.     kQ3ErrorBeginWriteAlreadyCalled, "\pkQ3ErrorBeginWriteAlreadyCalled",
  194.     kQ3ErrorBeginWriteNotCalled, "\pkQ3ErrorBeginWriteNotCalled",
  195.     kQ3ErrorEndWriteNotCalled, "\pkQ3ErrorEndWriteNotCalled",
  196.     kQ3ErrorReadStateInactive, "\pkQ3ErrorReadStateInactive",
  197.     kQ3ErrorStateUnavailable, "\pkQ3ErrorStateUnavailable",
  198.     kQ3ErrorWriteStateInactive, "\pkQ3ErrorWriteStateInactive",
  199.     kQ3ErrorSizeNotLongAligned, "\pkQ3ErrorSizeNotLongAligned",
  200.     kQ3ErrorFileModeRestriction, "\pkQ3ErrorFileModeRestriction",
  201.     kQ3ErrorInvalidHexString, "\pkQ3ErrorInvalidHexString",
  202.     kQ3ErrorWroteMoreThanSize, "\pkQ3ErrorWroteMoreThanSize",
  203.     kQ3ErrorWroteLessThanSize, "\pkQ3ErrorWroteLessThanSize",
  204.     kQ3ErrorReadLessThanSize, "\pkQ3ErrorReadLessThanSize",
  205.     kQ3ErrorReadMoreThanSize, "\pkQ3ErrorReadMoreThanSize",
  206.     kQ3ErrorNoBeginGroup, "\pkQ3ErrorNoBeginGroup",
  207.     kQ3ErrorSizeMismatch, "\pkQ3ErrorSizeMismatch",
  208.     kQ3ErrorStringExceedsMaximumLength, "\pkQ3ErrorStringExceedsMaximumLength",
  209.     kQ3ErrorValueExceedsMaximumSize, "\pkQ3ErrorValueExceedsMaximumSize",
  210.     kQ3ErrorNonUniqueLabel, "\pkQ3ErrorNonUniqueLabel",
  211.     kQ3ErrorEndOfContainer, "\pkQ3ErrorEndOfContainer",
  212.     kQ3ErrorUnmatchedEndGroup, "\pkQ3ErrorUnmatchedEndGroup",
  213.     kQ3ErrorFileVersionExists, "\pkQ3ErrorFileVersionExists",
  214.     /* View errors */
  215.     kQ3ErrorViewNotStarted, "\pkQ3ErrorViewNotStarted",
  216.     kQ3ErrorViewIsStarted, "\pkQ3ErrorViewIsStarted",
  217.     kQ3ErrorRendererNotSet, "\pkQ3ErrorRendererNotSet",
  218.     kQ3ErrorRenderingIsActive, "\pkQ3ErrorRenderingIsActive",
  219.     kQ3ErrorImmediateModeUnderflow, "\pkQ3ErrorImmediateModeUnderflow",
  220.     kQ3ErrorDisplayNotSet, "\pkQ3ErrorDisplayNotSet",
  221.     kQ3ErrorCameraNotSet, "\pkQ3ErrorCameraNotSet",
  222.     kQ3ErrorDrawContextNotSet, "\pkQ3ErrorDrawContextNotSet",
  223.     kQ3ErrorNonInvertibleMatrix, "\pkQ3ErrorNonInvertibleMatrix",
  224.     kQ3ErrorRenderingNotStarted, "\pkQ3ErrorRenderingNotStarted",
  225.     kQ3ErrorPickingNotStarted, "\pkQ3ErrorPickingNotStarted",
  226.     kQ3ErrorBoundsNotStarted, "\pkQ3ErrorBoundsNotStarted",
  227.     kQ3ErrorDataNotAvailable, "\pkQ3ErrorDataNotAvailable",
  228.     kQ3ErrorNothingToPop, "\pkQ3ErrorNothingToPop",
  229.     /* Renderer Errors */
  230.     kQ3ErrorUnknownStudioType, "\pkQ3ErrorUnknownStudioType",            /* TODO Name Change */
  231.     kQ3ErrorAlreadyRendering, "\pkQ3ErrorAlreadyRendering",
  232.     kQ3ErrorStartGroupRange, "\pkQ3ErrorStartGroupRange",
  233.     kQ3ErrorUnsupportedGeometryType, "\pkQ3ErrorUnsupportedGeometryType",
  234.     kQ3ErrorInvalidGeometryType, "\pkQ3ErrorInvalidGeometryType",
  235.     kQ3ErrorUnsupportedFunctionality, "\pkQ3ErrorUnsupportedFunctionality",
  236.     /* Group Errors */
  237.     kQ3ErrorInvalidPositionForGroup, "\pkQ3ErrorInvalidPositionForGroup",
  238.     kQ3ErrorInvalidObjectForGroup, "\pkQ3ErrorInvalidObjectForGroup",
  239.     kQ3ErrorInvalidObjectForPosition, "\pkQ3ErrorInvalidObjectForPosition",
  240.     /* Transform Errors */
  241.     kQ3ErrorScaleOfZero, "\pkQ3ErrorScaleOfZero",                /* Transform errors? */
  242.     /* String Errors */
  243.     kQ3ErrorBadStringType, "\pkQ3ErrorBadStringType",                /* What is this? */
  244.     /* Attribute Errors */
  245.     kQ3ErrorAttributeNotContained, "\pkQ3ErrorAttributeNotContained",        /* What is this? */
  246.     kQ3ErrorAttributeInvalidType, "\pkQ3ErrorAttributeInvalidType",        /* What is this? */
  247.     /* Camera Errors */
  248.     kQ3ErrorInvalidCameraValues, "\pkQ3ErrorInvalidCameraValues",        /* Redundant with kQ3ErrorParameterOutOfRange */
  249.     /* DrawContext Errors */
  250.     kQ3ErrorBadDrawContextType, "\pkQ3ErrorBadDrawContextType",
  251.     kQ3ErrorBadDrawContextFlag, "\pkQ3ErrorBadDrawContextFlag",
  252.     kQ3ErrorBadDrawContext, "\pkQ3ErrorBadDrawContext",
  253.     kQ3ErrorUnsupportedPixelDepth, "\pkQ3ErrorUnsupportedPixelDepth"
  254. } ;
  255.  
  256.  
  257.  
  258.  
  259. static void ConcatenateString( Str255 firstString, Str255 addThis ) ;
  260. static void CopyString( Str255 firstString, Str255 fromThis ) ;
  261. static void PresentErrMsgInDialog( Str255 theError ) ;
  262.  
  263.  
  264. //-------------------------------------------------------------------------------------------
  265. // MyErrorHandler - handle warnings from QuickDraw 3d
  266. void MyErrorHandler( TQ3Error sticky, TQ3Error latest, long refCon )
  267. {
  268.     Str255     myErrorStr ;
  269.     Str255    theWholeString ;
  270.  
  271.     short    stickyIndex = sticky - kQ3ErrorInternalError ;
  272.     short    latestIndex = latest - kQ3ErrorInternalError ;
  273.     
  274.     // if the error is out of range, then just tell us that something
  275.     // "unknown" happened. Otherwise look up the error from the table.
  276.     if( sticky != kQ3ErrorNone && ( sticky < kQ3ErrorInternalError || sticky > kQ3ErrorUnsupportedPixelDepth )) {
  277.         CopyString(    theWholeString, stringTable[kErrorString].errorString  ) ;
  278.         ConcatenateString( theWholeString, stringTable[ kUnknownErrorString ].errorString ) ;
  279.     }
  280.     else {
  281.         
  282.         
  283.         CopyString(    theWholeString, stringTable[ kErrorString ].errorString  ) ;
  284.         
  285.         ConcatenateString( theWholeString, stringTable[ kFirstErrorString ].errorString ) ;
  286.         CopyString(    myErrorStr, errorTable[ stickyIndex ].theString) ;
  287.         ConcatenateString( theWholeString, myErrorStr) ;
  288.         
  289.         ConcatenateString( theWholeString, stringTable[ kLastErrorString ].errorString ) ;
  290.         CopyString(    myErrorStr, errorTable[ stickyIndex ].theString) ;
  291.         ConcatenateString( theWholeString, myErrorStr) ;
  292.         
  293.         
  294.         // and display the error string 
  295.         PresentErrMsgInDialog( theWholeString ) ;        
  296.     }
  297.     
  298.     // clear down the error
  299.     latest = Q3Error_Get( &sticky );
  300.     
  301. }
  302.  
  303.  
  304. //-------------------------------------------------------------------------------------------
  305. // MyErrorHandler - handle warnings from QuickDraw 3d
  306. void MyWarningHandler( TQ3Warning     sticky, TQ3Warning     latest, long refCon )
  307. {
  308.     Str255     myWarningStr ;
  309.     Str255    theWholeString ;
  310.  
  311.     short    stickyIndex = sticky - kQ3WarningInternalException ;
  312.     short    latestIndex = latest - kQ3WarningInternalException ;
  313.     
  314.     // if the error is out of range, then just tell us that something
  315.     // "unknown" happened. Otherwise look up the error from the table.
  316.     if( sticky != kQ3ErrorNone && ( sticky < kQ3WarningInternalException || sticky > kQ3WarningParameterOutOfRange )) {
  317.     
  318.         CopyString(    theWholeString, stringTable[ kWarningString ].errorString  ) ;
  319.         ConcatenateString( theWholeString, stringTable[ kUnknownErrorString ].errorString ) ;
  320.         
  321.     }
  322.     else {
  323.         
  324.         CopyString(    theWholeString, stringTable[ kWarningString ].errorString  ) ;
  325.         
  326.         ConcatenateString( theWholeString, stringTable[ kFirstErrorString ].errorString ) ;
  327.         CopyString(    myWarningStr, warningTable[ stickyIndex ].theString) ;
  328.         ConcatenateString( theWholeString, myWarningStr) ;
  329.         
  330.         ConcatenateString( theWholeString, stringTable[ kLastErrorString ].errorString ) ;
  331.         CopyString(    myWarningStr, warningTable[ stickyIndex ].theString) ;
  332.         ConcatenateString( theWholeString, myWarningStr) ;
  333.         
  334.         // and display the error string 
  335.         PresentErrMsgInDialog( theWholeString ) ;        
  336.     }
  337.     
  338.     // clear down the error
  339.     latest = Q3Warning_Get( &sticky );
  340.     
  341.  
  342. }
  343.  
  344. //------------------------------------------------------------------------
  345. static void ConcatenateString( Str255 firstString, Str255 addThis )
  346. {
  347.     BlockMove( &addThis[1], &firstString[firstString[0]+1], addThis[0] ) ;
  348.     firstString[0] += addThis[0] ;
  349. }
  350.  
  351.  
  352. //------------------------------------------------------------------------
  353. static void CopyString( Str255 firstString, Str255 fromThis )
  354. {
  355.     BlockMove( &fromThis[1], &firstString[1], fromThis[0] ) ;
  356.     firstString[0] = fromThis[0] ;
  357. }
  358.  
  359. //------------------------------------------------------------------------
  360.  
  361. static void PresentErrMsgInDialog( Str255 theError )
  362. {    
  363.     short    itemHit ;
  364.     ParamText( theError, 0L,  0L,  0L ) ;
  365.     itemHit = StopAlert( kErrorHandlerAlertID, nil ) ;
  366.     
  367.     if( itemHit != ok ) 
  368.         ExitToShell() ;
  369. }