Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
3.7 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: util.cxx
  3. *
  4. * Misc. utility functions
  5. *
  6. * Copyright (c) 1996 Microsoft Corporation
  7. *
  8. \**************************************************************************/
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <windows.h>
  13. #include <sys/timeb.h>
  14. #include <GL/gl.h>
  15. #include <sys/types.h>
  16. #include <sys/timeb.h>
  17. #include <time.h>
  18. #include <math.h>
  19. #include "ssintrnl.hxx"
  20. #include "util.hxx"
  21. /******************************Public*Routine******************************\
  22. * SS_TIME class
  23. *
  24. \**************************************************************************/
  25. void
  26. SS_TIME::Update()
  27. {
  28. struct _timeb time;
  29. _ftime( &time );
  30. seconds = time.time + time.millitm/1000.0;
  31. }
  32. void
  33. SS_TIME::Zero()
  34. {
  35. seconds = 0.0;
  36. }
  37. double
  38. SS_TIME::Seconds()
  39. {
  40. return seconds;
  41. }
  42. SS_TIME
  43. SS_TIME::operator+( SS_TIME addTime )
  44. {
  45. return( *this += addTime );
  46. }
  47. SS_TIME
  48. SS_TIME::operator-( SS_TIME subTime )
  49. {
  50. return( *this -= subTime );
  51. }
  52. SS_TIME
  53. SS_TIME::operator+=( SS_TIME addTime )
  54. {
  55. seconds += addTime.seconds;
  56. return *this;
  57. }
  58. SS_TIME
  59. SS_TIME::operator-=( SS_TIME subTime )
  60. {
  61. seconds -= subTime.seconds;
  62. return *this;
  63. }
  64. /******************************Public*Routine******************************\
  65. * SS_TIMER class
  66. *
  67. \**************************************************************************/
  68. void
  69. SS_TIMER::Start()
  70. {
  71. startTime.Update();
  72. }
  73. SS_TIME
  74. SS_TIMER::Stop()
  75. {
  76. elapsed.Update();
  77. return elapsed - startTime;
  78. }
  79. void
  80. SS_TIMER::Reset()
  81. {
  82. elapsed.Zero();
  83. }
  84. SS_TIME
  85. SS_TIMER::ElapsedTime()
  86. {
  87. elapsed.Update();
  88. return elapsed - startTime;
  89. }
  90. /******************************Public*Routine******************************\
  91. * ss_Rand
  92. *
  93. * Generates integer random number 0..(max-1)
  94. *
  95. \**************************************************************************/
  96. int ss_iRand( int max )
  97. {
  98. return (int) ( max * ( ((float)rand()) / ((float)(RAND_MAX+1)) ) );
  99. }
  100. /******************************Public*Routine******************************\
  101. * ss_Rand2
  102. *
  103. * Generates integer random number min..max
  104. *
  105. \**************************************************************************/
  106. int ss_iRand2( int min, int max )
  107. {
  108. if( min == max )
  109. return min;
  110. else if( max < min ) {
  111. int temp = min;
  112. min = max;
  113. max = temp;
  114. }
  115. return min + (int) ( (max-min+1) * ( ((float)rand()) / ((float)(RAND_MAX+1)) ) );
  116. }
  117. /******************************Public*Routine******************************\
  118. * ss_fRand
  119. *
  120. * Generates float random number min...max
  121. *
  122. \**************************************************************************/
  123. FLOAT ss_fRand( FLOAT min, FLOAT max )
  124. {
  125. FLOAT diff;
  126. diff = max - min;
  127. return min + ( diff * ( ((float)rand()) / ((float)(RAND_MAX)) ) );
  128. }
  129. /******************************Public*Routine******************************\
  130. * ss_RandInit
  131. *
  132. * Initializes the randomizer
  133. *
  134. \**************************************************************************/
  135. void ss_RandInit( void )
  136. {
  137. struct _timeb time;
  138. _ftime( &time );
  139. srand( time.millitm );
  140. for( int i = 0; i < 10; i ++ )
  141. rand();
  142. }
  143. #if DBG
  144. /******************************Public*Routine******************************\
  145. * DbgPrint
  146. *
  147. * Formatted string output to the debugger.
  148. *
  149. * History:
  150. * 26-Jan-1996 -by- Gilman Wong [gilmanw]
  151. * Wrote it.
  152. \**************************************************************************/
  153. ULONG
  154. DbgPrint(PCH DebugMessage, ...)
  155. {
  156. va_list ap;
  157. char buffer[256];
  158. va_start(ap, DebugMessage);
  159. vsprintf(buffer, DebugMessage, ap);
  160. OutputDebugStringA(buffer);
  161. va_end(ap);
  162. return(0);
  163. }
  164. #endif