home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Contributed / SpriteWorld / SpriteWorld Files / Utils / SWFixed.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-06  |  2.0 KB  |  66 lines  |  [TEXT/CWIE]

  1. #ifndef __SWFIXED__
  2. #define __SWFIXED__
  3. //---------------------------------------------------------------------------------------
  4. //    SWFixed - fixed point routines for SpriteWorld
  5. //---------------------------------------------------------------------------------------
  6. #ifndef __MACTYPES__
  7. #include <MacTypes.h>
  8. #endif
  9. #ifndef __ERRORS__
  10. #include <Errors.h>
  11. #endif
  12. #ifndef __Memory__
  13. #include <Memory.h>
  14. #endif
  15.  
  16. typedef long SWFixed;
  17.  
  18.     // call this once before using trig functions (sin/cos)
  19. OSErr SWInitTrigTables(short numAngles);
  20.  
  21.     //    Conversion macros
  22. SWFixed SW_FLOAT2FIX(float x);
  23. float SW_FIX2FLOAT(SWFixed x);
  24.  
  25. SWFixed SW_INT2FIX(int x);
  26. int SW_FIX2INT(SWFixed x);
  27.  
  28.     //    Trigonometry macros
  29. SWFixed SW_SIN(int x);
  30. SWFixed SW_COS(int x);
  31.  
  32. //---------------------------------------------------------------------------------------
  33.  
  34. #define SW_FIX_SHIFT        8        // a bit low, to avoid user causing overflows
  35.  
  36. #define SW_FIX_MULT            (1L << SW_FIX_SHIFT)                
  37. #define SW_FIX_ROUND        (SW_FIX_MULT-1)                
  38.  
  39. #define SW_FLOAT2FIX(x)        ((x) * SW_FIX_MULT)
  40. #define SW_FIX2FLOAT(x)        ((x) * (1.0 / SW_FIX_MULT))
  41.  
  42. #define SW_INT2FIX(x)        ((long)(x) << SW_FIX_SHIFT)
  43. #define SW_FIX2INT(x)        ((x) >> SW_FIX_SHIFT)
  44.  
  45. //---------------------------------------------------------------------------------------
  46.  
  47. #define SW_NUMANGLES        360        // "normal" number of angles (degrees)
  48.  
  49. extern long                    gSWNumAngles;
  50. extern SWFixed                *gSWSinTable;    // gSWSinTable[gSWNumAngles]
  51. extern SWFixed                *gSWCosTable;    // gSWCosTable[gSWNumAngles]
  52.  
  53. #if SW_ASSERT_ON == 1
  54.  
  55. #define SW_SIN(x)            (gSWSinTable != NULL ? gSWSinTable[x % gSWNumAngles] : 0),    \
  56.                             SW_ASSERT(gSWSinTable != NULL && x >= 0 && x < gSWNumAngles)
  57. #define SW_COS(x)            (gSWCosTable != NULL ? gSWCosTable[x % gSWNumAngles] : 0),    \
  58.                             SW_ASSERT(gSWCosTable != NULL && x >= 0 && x < gSWNumAngles)
  59. #else
  60. #define SW_SIN(x)            (gSWSinTable[x])
  61. #define SW_COS(x)            (gSWCosTable[x])
  62. #endif
  63.  
  64. //---------------------------------------------------------------------------------------
  65. #endif /*__SWFIXED__*/
  66.