shapes_digital_clock.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - digital clock
  4. *
  5. * Example complexity rating: [★★★★] 4/4
  6. *
  7. * Example originally created with raylib 5.5, last time updated with raylib 5.6
  8. *
  9. * Example contributed by Hamza RAHAL (@hmz-rhl) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2025 Hamza RAHAL (@hmz-rhl) and Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #define _CRT_SECURE_NO_WARNINGS // Disable some Visual Studio warnings
  18. #include "raylib.h"
  19. #include <math.h> // Required for: cosf(), sinf()
  20. #include <time.h> // Required for: time(), localtime()
  21. #define CLOCK_ANALOG 0
  22. #define CLOCK_DIGITAL 1
  23. //----------------------------------------------------------------------------------
  24. // Types and Structures Definition
  25. //----------------------------------------------------------------------------------
  26. // Clock hand type
  27. typedef struct {
  28. int value; // Time value
  29. // Visual elements
  30. float angle; // Hand angle
  31. int length; // Hand length
  32. int thickness; // Hand thickness
  33. Color color; // Hand color
  34. } ClockHand;
  35. // Clock hands
  36. typedef struct {
  37. ClockHand second; // Clock hand for seconds
  38. ClockHand minute; // Clock hand for minutes
  39. ClockHand hour; // Clock hand for hours
  40. } Clock;
  41. //----------------------------------------------------------------------------------
  42. // Module Functions Declaration
  43. //----------------------------------------------------------------------------------
  44. static void UpdateClock(Clock *clock); // Update clock time
  45. static void DrawClockAnalog(Clock clock, Vector2 position); // Draw analog clock at desired center position
  46. static void DrawClockDigital(Clock clock, Vector2 position); // Draw digital clock at desired position
  47. static void DrawDisplayValue(Vector2 position, int value, Color colorOn, Color colorOff);
  48. static void Draw7SDisplay(Vector2 position, char segments, Color colorOn, Color colorOff);
  49. static void DrawDisplaySegment(Vector2 center, int length, int thick, bool vertical, Color color);
  50. //------------------------------------------------------------------------------------
  51. // Program main entry point
  52. //------------------------------------------------------------------------------------
  53. int main(void)
  54. {
  55. // Initialization
  56. //--------------------------------------------------------------------------------------
  57. const int screenWidth = 800;
  58. const int screenHeight = 450;
  59. SetConfigFlags(FLAG_MSAA_4X_HINT);
  60. InitWindow(screenWidth, screenHeight, "raylib [shapes] example - digital clock");
  61. int clockMode = CLOCK_DIGITAL;
  62. // Initialize clock
  63. // NOTE: Includes visual info for anlaog clock
  64. Clock clock = {
  65. .second.angle = 45,
  66. .second.length = 140,
  67. .second.thickness = 3,
  68. .second.color = MAROON,
  69. .minute.angle = 10,
  70. .minute.length = 130,
  71. .minute.thickness = 7,
  72. .minute.color = DARKGRAY,
  73. .hour.angle = 0,
  74. .hour.length = 100,
  75. .hour.thickness = 7,
  76. .hour.color = BLACK,
  77. };
  78. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  79. //--------------------------------------------------------------------------------------
  80. // Main game loop
  81. while (!WindowShouldClose()) // Detect window close button or ESC key
  82. {
  83. // Update
  84. //----------------------------------------------------------------------------------
  85. if (IsKeyPressed(KEY_SPACE))
  86. {
  87. // Toggle clock mode
  88. if (clockMode == CLOCK_DIGITAL) clockMode = CLOCK_ANALOG;
  89. else if (clockMode == CLOCK_ANALOG) clockMode = CLOCK_DIGITAL;
  90. }
  91. UpdateClock(&clock); // Update clock required data: value and angle
  92. //----------------------------------------------------------------------------------
  93. // Draw
  94. //----------------------------------------------------------------------------------
  95. BeginDrawing();
  96. ClearBackground(RAYWHITE);
  97. // Draw clock in selected mode
  98. if (clockMode == CLOCK_ANALOG) DrawClockAnalog(clock, (Vector2){ 400, 240 });
  99. else if (clockMode == CLOCK_DIGITAL)
  100. {
  101. DrawClockDigital(clock, (Vector2){ 30, 60 });
  102. // Draw clock using default raylib font
  103. // Get pointer to formated clock time string
  104. // WARNING: Pointing to an internal static string that is reused between TextFormat() calls
  105. const char *clockTime = TextFormat("%02i:%02i:%02i", clock.hour.value, clock.minute.value, clock.second.value);
  106. DrawText(clockTime, GetScreenWidth()/2 - MeasureText(clockTime, 150)/2, 300, 150, BLACK);
  107. }
  108. DrawText(TextFormat("Press [SPACE] to switch clock mode: %s",
  109. (clockMode == CLOCK_DIGITAL)? "DIGITAL CLOCK" : "ANALOGUE CLOCK"), 10, 10, 20, DARKGRAY);
  110. EndDrawing();
  111. //----------------------------------------------------------------------------------
  112. }
  113. // De-Initialization
  114. //--------------------------------------------------------------------------------------
  115. CloseWindow(); // Close window and OpenGL context
  116. //--------------------------------------------------------------------------------------
  117. return 0;
  118. }
  119. //----------------------------------------------------------------------------------
  120. // Module Functions Definition
  121. //----------------------------------------------------------------------------------
  122. // Update clock time
  123. static void UpdateClock(Clock *clock)
  124. {
  125. time_t rawtime;
  126. struct tm *timeinfo;
  127. time(&rawtime);
  128. timeinfo = localtime(&rawtime);
  129. // Updating time data
  130. clock->second.value = timeinfo->tm_sec;
  131. clock->minute.value = timeinfo->tm_min;
  132. clock->hour.value = timeinfo->tm_hour;
  133. clock->hour.angle = (timeinfo->tm_hour%12)*180.0f/6.0f;
  134. clock->hour.angle += (timeinfo->tm_min%60)*30/60.0f;
  135. clock->hour.angle -= 90;
  136. clock->minute.angle = (timeinfo->tm_min%60)*6.0f;
  137. clock->minute.angle += (timeinfo->tm_sec%60)*6/60.0f;
  138. clock->minute.angle -= 90;
  139. clock->second.angle = (timeinfo->tm_sec%60)*6.0f;
  140. clock->second.angle -= 90;
  141. }
  142. // Draw analog clock
  143. // Parameter: position, refers to center position
  144. static void DrawClockAnalog(Clock clock, Vector2 position)
  145. {
  146. // Draw clock base
  147. DrawCircleV(position, clock.second.length + 40.0f, LIGHTGRAY);
  148. DrawCircleV(position, 12.0f, GRAY);
  149. // Draw clock minutes/seconds lines
  150. for (int i = 0; i < 60; i++)
  151. {
  152. DrawLineEx((Vector2){ position.x + (clock.second.length + ((i%5)? 10 : 6))*cosf((6.0f*i - 90.0f)*DEG2RAD),
  153. position.y + (clock.second.length + ((i%5)? 10 : 6))*sinf((6.0f*i - 90.0f)*DEG2RAD) },
  154. (Vector2){ position.x + (clock.second.length + 20)*cosf((6.0f*i - 90.0f)*DEG2RAD),
  155. position.y + (clock.second.length + 20)*sinf((6.0f*i - 90.0f)*DEG2RAD) }, ((i%5)? 1.0f : 3.0f), DARKGRAY);
  156. // Draw seconds numbers
  157. //DrawText(TextFormat("%02i", i), centerPosition.x + (clock.second.length + 50)*cosf((6.0f*i - 90.0f)*DEG2RAD) - 10/2,
  158. // centerPosition.y + (clock.second.length + 50)*sinf((6.0f*i - 90.0f)*DEG2RAD) - 10/2, 10, GRAY);
  159. }
  160. // Draw hand seconds
  161. DrawRectanglePro((Rectangle){ position.x, position.y, (float)clock.second.length, (float)clock.second.thickness },
  162. (Vector2){ 0.0f, clock.second.thickness/2.0f }, clock.second.angle, clock.second.color);
  163. // Draw hand minutes
  164. DrawRectanglePro((Rectangle){ position.x, position.y, (float)clock.minute.length, (float)clock.minute.thickness },
  165. (Vector2){ 0.0f, clock.minute.thickness/2.0f }, clock.minute.angle, clock.minute.color);
  166. // Draw hand hours
  167. DrawRectanglePro((Rectangle){ position.x, position.y, (float)clock.hour.length, (float)clock.hour.thickness },
  168. (Vector2){ 0.0f, clock.hour.thickness/2.0f }, clock.hour.angle, clock.hour.color);
  169. }
  170. // Draw digital clock
  171. // PARAM: position, refers to top-left corner
  172. static void DrawClockDigital(Clock clock, Vector2 position)
  173. {
  174. // Draw clock using custom 7-segments display (made of shapes)
  175. DrawDisplayValue((Vector2){ position.x, position.y }, clock.hour.value/10, RED, Fade(LIGHTGRAY, 0.3f));
  176. DrawDisplayValue((Vector2){ position.x + 120, position.y }, clock.hour.value%10, RED, Fade(LIGHTGRAY, 0.3f));
  177. DrawCircle((int)position.x + 240, (int)position.y + 70, 12, (clock.second.value%2)? RED : Fade(LIGHTGRAY, 0.3f));
  178. DrawCircle((int)position.x + 240, (int)position.y + 150, 12, (clock.second.value%2)? RED : Fade(LIGHTGRAY, 0.3f));
  179. DrawDisplayValue((Vector2){ position.x + 260, position.y }, clock.minute.value/10, RED, Fade(LIGHTGRAY, 0.3f));
  180. DrawDisplayValue((Vector2){ position.x + 380, position.y }, clock.minute.value%10, RED, Fade(LIGHTGRAY, 0.3f));
  181. DrawCircle((int)position.x + 500, (int)position.y + 70, 12, (clock.second.value%2)? RED : Fade(LIGHTGRAY, 0.3f));
  182. DrawCircle((int)position.x + 500, (int)position.y + 150, 12, (clock.second.value%2)? RED : Fade(LIGHTGRAY, 0.3f));
  183. DrawDisplayValue((Vector2){ position.x + 520, position.y }, clock.second.value/10, RED, Fade(LIGHTGRAY, 0.3f));
  184. DrawDisplayValue((Vector2){ position.x + 640, position.y }, clock.second.value%10, RED, Fade(LIGHTGRAY, 0.3f));
  185. }
  186. // Draw 7-segment display with value
  187. static void DrawDisplayValue(Vector2 position, int value, Color colorOn, Color colorOff)
  188. {
  189. switch (value)
  190. {
  191. case 0: Draw7SDisplay(position, 0b00111111, colorOn, colorOff); break;
  192. case 1: Draw7SDisplay(position, 0b00000110, colorOn, colorOff); break;
  193. case 2: Draw7SDisplay(position, 0b01011011, colorOn, colorOff); break;
  194. case 3: Draw7SDisplay(position, 0b01001111, colorOn, colorOff); break;
  195. case 4: Draw7SDisplay(position, 0b01100110, colorOn, colorOff); break;
  196. case 5: Draw7SDisplay(position, 0b01101101, colorOn, colorOff); break;
  197. case 6: Draw7SDisplay(position, 0b01111101, colorOn, colorOff); break;
  198. case 7: Draw7SDisplay(position, 0b00000111, colorOn, colorOff); break;
  199. case 8: Draw7SDisplay(position, 0b01111111, colorOn, colorOff); break;
  200. case 9: Draw7SDisplay(position, 0b01101111, colorOn, colorOff); break;
  201. default: break;
  202. }
  203. }
  204. // Draw seven segments display
  205. // Parameter: position, refers to top-left corner of display
  206. // Parameter: segments, defines in binary the segments to be activated
  207. static void Draw7SDisplay(Vector2 position, char segments, Color colorOn, Color colorOff)
  208. {
  209. int segmentLen = 60;
  210. int segmentThick = 20;
  211. float offsetYAdjust = segmentThick*0.3f; // HACK: Adjust gap space between segment limits
  212. // Segment A
  213. DrawDisplaySegment((Vector2){ position.x + segmentThick + segmentLen/2.0f, position.y + segmentThick },
  214. segmentLen, segmentThick, false, (segments & 0b00000001)? colorOn : colorOff);
  215. // Segment B
  216. DrawDisplaySegment((Vector2){ position.x + segmentThick + segmentLen + segmentThick/2.0f, position.y + 2*segmentThick + segmentLen/2.0f - offsetYAdjust },
  217. segmentLen, segmentThick, true, (segments & 0b00000010)? colorOn : colorOff);
  218. // Segment C
  219. DrawDisplaySegment((Vector2){ position.x + segmentThick + segmentLen + segmentThick/2.0f, position.y + 4*segmentThick + segmentLen + segmentLen/2.0f - 3*offsetYAdjust },
  220. segmentLen, segmentThick, true, (segments & 0b00000100)? colorOn : colorOff);
  221. // Segment D
  222. DrawDisplaySegment((Vector2){ position.x + segmentThick + segmentLen/2.0f, position.y + 5*segmentThick + 2*segmentLen - 4*offsetYAdjust },
  223. segmentLen, segmentThick, false, (segments & 0b00001000)? colorOn : colorOff);
  224. // Segment E
  225. DrawDisplaySegment((Vector2){ position.x + segmentThick/2.0f, position.y + 4*segmentThick + segmentLen + segmentLen/2.0f - 3*offsetYAdjust },
  226. segmentLen, segmentThick, true, (segments & 0b00010000)? colorOn : colorOff);
  227. // Segment F
  228. DrawDisplaySegment((Vector2){ position.x + segmentThick/2.0f, position.y + 2*segmentThick + segmentLen/2.0f - offsetYAdjust },
  229. segmentLen, segmentThick, true, (segments & 0b00100000)? colorOn : colorOff);
  230. // Segment G
  231. DrawDisplaySegment((Vector2){ position.x + segmentThick + segmentLen/2.0f, position.y + 3*segmentThick + segmentLen - 2*offsetYAdjust },
  232. segmentLen, segmentThick, false, (segments & 0b01000000)? colorOn : colorOff);
  233. }
  234. // Draw one 7-segment display segment, horizontal or vertical
  235. static void DrawDisplaySegment(Vector2 center, int length, int thick, bool vertical, Color color)
  236. {
  237. if (!vertical)
  238. {
  239. // Horizontal segment points
  240. /*
  241. 3___________________________5
  242. / \
  243. /1 x 6\
  244. \ /
  245. \2___________________________4/
  246. */
  247. Vector2 segmentPointsH[6] = {
  248. (Vector2){ center.x - length/2.0f - thick/2.0f, center.y }, // Point 1
  249. (Vector2){ center.x - length/2.0f, center.y + thick/2.0f }, // Point 2
  250. (Vector2){ center.x - length/2.0f, center.y - thick/2.0f }, // Point 3
  251. (Vector2){ center.x + length/2.0f, center.y + thick/2.0f }, // Point 4
  252. (Vector2){ center.x + length/2.0f, center.y - thick/2.0f }, // Point 5
  253. (Vector2){ center.x + length/2.0f + thick/2.0f, center.y }, // Point 6
  254. };
  255. DrawTriangleStrip(segmentPointsH, 6, color);
  256. }
  257. else
  258. {
  259. // Vertical segment points
  260. Vector2 segmentPointsV[6] = {
  261. (Vector2){ center.x, center.y - length/2.0f - thick/2.0f }, // Point 1
  262. (Vector2){ center.x - thick/2.0f, center.y - length/2.0f }, // Point 2
  263. (Vector2){ center.x + thick/2.0f, center.y - length/2.0f }, // Point 3
  264. (Vector2){ center.x - thick/2.0f, center.y + length/2.0f }, // Point 4
  265. (Vector2){ center.x + thick/2.0f, center.y + length/2.0f }, // Point 5
  266. (Vector2){ center.x, center.y + length/2 + thick/2.0f }, // Point 6
  267. };
  268. DrawTriangleStrip(segmentPointsV, 6, color);
  269. }
  270. }