home *** CD-ROM | disk | FTP | other *** search
/ Teach Yourself Game Programming in 21 Days / TYGAMES_R.ISO / source / day_08 / graph8.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-20  |  2.3 KB  |  88 lines

  1.  
  2. /// I N C L U D E S ///////////////////////////////////////////////////////////
  3.  
  4. #include <math.h>
  5. #include <stdio.h>
  6. #include "graph8.h"
  7.  
  8. // F U N C T I O N S //////////////////////////////////////////////////////////
  9.  
  10. fixed Assign_Integer(long integer)
  11. {
  12. // this function assigns a integer to a fixed point type by shifting
  13.  
  14. return((fixed)integer << FP_SHIFT);
  15.  
  16. } // end Assign_Integer
  17.  
  18. ///////////////////////////////////////////////////////////////////////////////
  19.  
  20. fixed Assign_Float(float number)
  21. {
  22. // this function assigns a floating point number to a fixed point type
  23. // by multiplication since it makes no sense to shift a floating point data type
  24.  
  25. return((fixed)(number * FP_SHIFT_2N));
  26.  
  27. } // end Assign_Float
  28.  
  29. ///////////////////////////////////////////////////////////////////////////////
  30.  
  31. fixed Mul_Fixed(fixed f1,fixed f2)
  32. {
  33. // this function mulitplies two fixed point numbers and returns the result
  34. // notice how the final result is shifted back
  35.  
  36. return((f1*f2) >> FP_SHIFT);
  37.  
  38. } // end Mul_Fixed
  39.  
  40. ///////////////////////////////////////////////////////////////////////////////
  41.  
  42. fixed Div_Fixed(fixed f1,fixed f2)
  43. {
  44. // this function divvides two fixed point numbers and returns the result
  45. // notice how the divedend is pre-shifted before the division
  46.  
  47. return((f1<<FP_SHIFT)/f2);
  48.  
  49. } // end Div_Fixed
  50.  
  51. ///////////////////////////////////////////////////////////////////////////////
  52.  
  53. fixed Add_Fixed(fixed f1,fixed f2)
  54. {
  55.  
  56. // this function adds two fixed point numbers and returns the result
  57. // notice how no shifting is necessary
  58.  
  59. return(f1+f2);
  60.  
  61. } // end Add_Fixed
  62.  
  63. ///////////////////////////////////////////////////////////////////////////////
  64.  
  65. fixed Sub_Fixed(fixed f1,fixed f2)
  66. {
  67.  
  68. // this function subtracts two fixed point numbers and returns the result
  69. // notice how no shifting is necessary
  70.  
  71. return(f1-f2);
  72.  
  73. } // end Sub_Fixed
  74.  
  75. ///////////////////////////////////////////////////////////////////////////////
  76.  
  77. void Print_Fixed(fixed f1)
  78. {
  79. // this function prints out a fixed point number, it does this by
  80. // extracting the portion to the left of the imaginary decimal and
  81. // extracting the portion to the right of the imaginary decimal point
  82.  
  83. printf("%ld.%ld",f1 >> FP_SHIFT, 100*(unsigned long)(f1 & 0x00ff)/FP_SHIFT_2N);
  84.  
  85. } // end Print_Fixed
  86.  
  87.  
  88.