home *** CD-ROM | disk | FTP | other *** search
/ Dream 41 / Amiga_Dream_41.iso / Amiga / Pro / 3d / ICoons1_0.lzh / icoons / source / timer.c < prev    next >
C/C++ Source or Header  |  1992-10-30  |  5KB  |  144 lines

  1. /* :ts=8 */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <math.h>
  6.  
  7. #include "general.h"
  8. #include "globals.h"
  9. #include "intui.h"
  10. #include "timer.h"
  11.  
  12. static Boolean_T    Timer_Enabled     = FALSE;
  13. static Boolean_T    Timer_Expired     = FALSE;
  14. static Boolean_T    Initialize_Time = TRUE;
  15.  
  16. static ULONG        Stop_Seconds    = 0;
  17. static ULONG        Stop_Micros    = 0;
  18.  
  19. static
  20. void Enable_IntuiTicks(Boolean_T On)
  21. /************************************************************************/
  22. /*                                                                      */
  23. /* Enable or disable generation of IDCMP_INTUITICKS messages.        */
  24. /*                                                                      */
  25. /************************************************************************/
  26. {
  27.     long    Status;
  28.     ULONG     IDCMP_Flags;
  29.  
  30.  
  31.     IDCMP_Flags = Windows[W_Main].Window->IDCMPFlags;
  32.  
  33.     if (On) IDCMP_Flags |= IDCMP_INTUITICKS;
  34.     else    IDCMP_Flags &= ~IDCMP_INTUITICKS;
  35.  
  36.     if (ModifyIDCMP(Windows[W_Main].Window, IDCMP_Flags) == NULL) {
  37.  
  38.         Display_Error_Message("Couldn't modify IDCMP port\n");
  39.     CloseStuff();
  40.     exit(3);
  41.  
  42.     } /* if */
  43.  
  44. } /* Enable_IntuiTicks */
  45.  
  46. Boolean_T Handle_IntuiTick_Message(struct IntuiMessage *Msg)
  47. /************************************************************************/
  48. /*                                                                      */
  49. /* This function should be called on receipt of an INTUITICK message.    */
  50. /*                                                                      */
  51. /* It will do the following:                        */
  52. /*                                                                      */
  53. /* 1: If the timer isn't enabled, then disable intuiticks (we shouldn't    */
  54. /*    get these if the timer isn't enabled).                */
  55. /*                                                                      */
  56. /* 2: If 'Initialize_Time' is TRUE, then the timer has been started,    */
  57. /*    but we still need to compute stop time for the timer, so do this    */
  58. /*    and return.                            */
  59. /*                                                                      */
  60. /* 3: Otherwise, check if the timer has expired. In this case disable    */
  61. /*    IntuiTicks, set 'Timer_Expired' to TRUE, and return TRUE.        */
  62. /*                                                                      */
  63. /************************************************************************/
  64. {
  65.     if (!Timer_Enabled) {
  66.         Enable_IntuiTicks(FALSE);
  67.     return(FALSE);
  68.     }
  69.  
  70.     if (Initialize_Time) {
  71.  
  72.     /* Compute stop time based on current time from message */
  73.  
  74.         Stop_Micros += Msg->Micros;
  75.         Stop_Seconds += Msg->Seconds;
  76.  
  77.         if (Stop_Micros >= 1000000) {
  78.  
  79.         Stop_Micros -= 1000000;
  80.         Stop_Seconds += 1;
  81.  
  82.         } /* if */
  83.     Initialize_Time = FALSE;
  84.     return(FALSE);
  85.     }
  86.     
  87.     if (Msg->Seconds > Stop_Seconds ||
  88.     (Msg->Seconds == Stop_Seconds && Msg->Micros > Stop_Micros)) {
  89.  
  90.     /* Timer has expired.            */    
  91.  
  92.     Timer_Expired = TRUE;
  93.         Timer_Enabled = FALSE;
  94.         Enable_IntuiTicks(FALSE);
  95.  
  96.     return(TRUE);
  97.  
  98.     }  /* if */
  99.  
  100.     return(FALSE);
  101.  
  102. } /* Handle_IntuiTick_Message */
  103.  
  104. void Start_Timer(unsigned long Seconds, unsigned long Micros)
  105. /************************************************************************/
  106. /*                                                                      */
  107. /* Start the timer.                            */
  108. /*                                                                      */
  109. /************************************************************************/
  110. {
  111.     Stop_Micros  = Micros;
  112.     Stop_Seconds = Seconds;
  113.  
  114.     if (!Timer_Enabled) Enable_IntuiTicks(TRUE);
  115.     Timer_Enabled   = TRUE;
  116.     Initialize_Time = TRUE;    /* Compute stop time at first tick */
  117.     Timer_Expired   = FALSE;
  118.  
  119. } /* Start_Timer */
  120.  
  121. void Stop_Timer()
  122. /************************************************************************/
  123. /*                                                                      */
  124. /* Stop the timer.                            */
  125. /*                                                                      */
  126. /************************************************************************/
  127. {
  128.     if (Timer_Enabled) Enable_IntuiTicks(FALSE);
  129.     Timer_Enabled = FALSE;
  130.     Timer_Expired = FALSE;
  131.  
  132. } /* Stop_Timer */
  133.  
  134. Boolean_T Check_Timer()
  135. /************************************************************************/
  136. /*                                                                      */
  137. /* Return TRUE if timer has expired.                     */
  138. /*                                                                      */
  139. /************************************************************************/
  140. {
  141.     return(Timer_Expired);
  142.  
  143. } /* Check_Timer */
  144.