Advertisement
ReyeMe

TD - Time

Jan 20th, 2020
162
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -------------------------------------------------------------
  2. time.h
  3. -------------------------------------------------------------
  4.  
  5. #ifndef __TIME_TRAINS__
  6. #define __TIME_TRAINS__
  7.  
  8. /**
  9.  * Maximum length of seconds or minutes
  10.  */
  11. #define TIME_LENGTH 60
  12.  
  13. /**
  14.  * Maximum length of hours in day
  15.  */
  16. #define TIME_LENGTH_DAY 24
  17.  
  18. /**
  19.  * Contains information about current time
  20.  */
  21. typedef struct
  22. {
  23.     /**
  24.      *  Number of hours
  25.      */
  26.     unsigned char Hours;
  27.  
  28.     /**
  29.      * Number of minutes
  30.      */
  31.     unsigned char Minutes;
  32.  
  33.     /**
  34.      * Number of seconds
  35.      */
  36.     unsigned char Seconds;
  37. } GameTime;
  38.  
  39. /**
  40.  * Current time
  41.  */
  42. static GameTime *time_current;
  43.  
  44. /**
  45.  * Reset time back to zero
  46.  * @param Item to reset
  47.  */
  48. void time_Reset(GameTime *time);
  49.  
  50. /**
  51.  * Move time forward by 1 second
  52.  * @param Item to update
  53.  */
  54. void time_UpdateSeconds(GameTime *time);
  55.  
  56. #endif
  57.  
  58. -------------------------------------------------------
  59. time.c
  60. -------------------------------------------------------
  61.  
  62. #include "../include/time.h"
  63.  
  64. /**
  65.  * Reset time back to zero
  66.  */
  67. void time_Reset(GameTime *time)
  68. {
  69.     time->Hours = 0;
  70.     time->Minutes = 0;
  71.     time->Seconds = 0;
  72. }
  73.  
  74. /**
  75.  * Move time forward by 1 second
  76.  */
  77. void time_UpdateSeconds(GameTime *time)
  78. {
  79.     if (++time->Seconds >= TIME_LENGTH)
  80.     {
  81.         time->Seconds = 0;
  82.  
  83.         if (++time->Minutes >= TIME_LENGTH)
  84.         {
  85.             time->Minutes = 0;
  86.            
  87.             if (++time->Hours >= TIME_LENGTH_DAY)
  88.             {
  89.                 time->Hours = 0;
  90.             }
  91.         }
  92.     }
  93. }
Advertisement
RAW Paste Data Copied
Advertisement