Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.67 KB | None | 0 0
  1. void CombineLayersIntoFirst(bool firstLayer[PictureWidth][PictureHeight], bool secondLayer[PictureWidth][PictureHeight])
  2. {
  3. for (int x = 0; x < PictureWidth; x++)
  4. {
  5. for (int y = 0; y < PictureHeight; y++)
  6. {
  7. if (firstLayer[x][y] || secondLayer[x][y])
  8. {
  9. firstLayer[x][y] = true;
  10. }
  11. }
  12. }
  13. }
  14.  
  15. //2
  16.  
  17. void ClearLayer(bool layer[PictureWidth][PictureHeight])
  18. {
  19. for (int x = 0; x < PictureWidth; x++)
  20. {
  21. for (int y = 0; y < PictureHeight; y++)
  22. {
  23. layer[x][y] = false;
  24. }
  25. }
  26. }
  27.  
  28. // 3
  29. void PrintClock(bool clockFrameLayer[PictureWidth][PictureHeight], bool clockNumbersLayer[PictureWidth][PictureHeight], bool hoursHandLayer[PictureWidth][PictureHeight], bool minutesHandLayer[PictureWidth][PictureHeight], bool secondsHandLayer[PictureWidth][PictureHeight], bool centerPointLayer[PictureWidth][PictureHeight])
  30. {
  31. //
  32. system("color 0F");
  33.  
  34. //
  35. for (int y = 0; y < PictureHeight; y++)
  36. {
  37. for (int x = 0; x < PictureWidth; x++)
  38. {
  39. //
  40. if (secondsHandLayer[x][y] || minutesHandLayer[x][y] || hoursHandLayer[x][y] || clockNumbersLayer[x][y] || clockFrameLayer[x][y] || centerPointLayer[x][y])
  41. {
  42. //
  43. if (centerPointLayer[x][y]) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x04); //
  44. else if (secondsHandLayer[x][y]) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0F); //
  45. else if (minutesHandLayer[x][y]) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x09); //
  46. else if (hoursHandLayer[x][y]) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x02); //
  47. else if (clockNumbersLayer[x][y]) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x08); //
  48. else if (clockFrameLayer[x][y]) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0F); //
  49.  
  50. cout << char(symbolChar) << char(symbolChar); //
  51. }
  52. else
  53. {
  54. cout << char(emptyChar) << char(emptyChar); //
  55. }
  56. }
  57.  
  58. //
  59. cout << endl;
  60. }
  61. }
  62.  
  63. // included libraries
  64. #include <iostream>
  65. #include <Windows.h>
  66. #include <cmath>
  67. #include <stdio.h>
  68. #include <ctime>
  69.  
  70. // Define Constants
  71. #define PI (22.0/7.0)
  72. #define PictureWidth 27
  73. #define PictureHeight 27
  74.  
  75. // Frames Variables
  76. const int targetedFPS = 1;
  77. const float idealDurationPerFrame = 1000.0f / targetedFPS;
  78. float nextFrameAfter = 1, duration = 0;
  79. clock_t startTime;
  80. unsigned int framesCounter = 0;
  81.  
  82. // Characters To Draw Circle
  83. char symbolChar = 219;
  84. char emptyChar = 32;
  85.  
  86. using namespace std;
  87.  
  88. //
  89. void ClearLayer(bool layer[PictureWidth][PictureHeight])
  90. {
  91. for (int x = 0; x < PictureWidth; x++)
  92. {
  93. for (int y = 0; y < PictureHeight; y++)
  94. {
  95. layer[x][y] = false;
  96. }
  97. }
  98. }
  99.  
  100. // Draw Circle
  101. void DrawCircle(bool layer[PictureWidth][PictureHeight])
  102. {
  103. layer[0][10] = true;
  104. layer[0][11] = true;
  105. layer[0][12] = true;
  106. layer[0][13] = true;
  107. layer[0][14] = true;
  108. layer[0][15] = true;
  109. layer[0][16] = true;
  110.  
  111. layer[1][8] = true;
  112. layer[1][9] = true;
  113. layer[1][17] = true;
  114. layer[1][18] = true;
  115.  
  116. layer[2][6] = true;
  117. layer[2][7] = true;
  118. layer[2][19] = true;
  119. layer[2][20] = true;
  120.  
  121. layer[3][5] = true;
  122. layer[3][21] = true;
  123.  
  124. layer[4][4] = true;
  125. layer[4][22] = true;
  126.  
  127. layer[5][3] = true;
  128. layer[5][23] = true;
  129.  
  130. layer[6][2] = true;
  131. layer[6][24] = true;
  132.  
  133. layer[7][2] = true;
  134. layer[7][24] = true;
  135.  
  136. layer[8][1] = true;
  137. layer[8][25] = true;
  138.  
  139. layer[9][1] = true;
  140. layer[9][25] = true;
  141.  
  142. layer[10][0] = true;
  143. layer[10][26] = true;
  144.  
  145. layer[11][0] = true;
  146. layer[11][26] = true;
  147.  
  148. layer[12][0] = true;
  149. layer[12][26] = true;
  150.  
  151. layer[13][0] = true;
  152. layer[13][26] = true;
  153.  
  154. layer[14][0] = true;
  155. layer[14][26] = true;
  156.  
  157. layer[15][0] = true;
  158. layer[15][26] = true;
  159.  
  160. layer[16][0] = true;
  161. layer[16][26] = true;
  162.  
  163. layer[17][1] = true;
  164. layer[17][25] = true;
  165.  
  166. layer[18][1] = true;
  167. layer[18][25] = true;
  168.  
  169. layer[19][2] = true;
  170. layer[19][24] = true;
  171.  
  172. layer[20][2] = true;
  173. layer[20][24] = true;
  174.  
  175. layer[21][3] = true;
  176. layer[21][23] = true;
  177.  
  178. layer[22][4] = true;
  179. layer[22][22] = true;
  180.  
  181. layer[23][5] = true;
  182. layer[23][21] = true;
  183.  
  184. layer[24][6] = true;
  185. layer[24][7] = true;
  186. layer[24][20] = true;
  187. layer[24][19] = true;
  188.  
  189. layer[25][8] = true;
  190. layer[25][9] = true;
  191. layer[25][18] = true;
  192. layer[25][17] = true;
  193.  
  194. layer[26][10] = true;
  195. layer[26][11] = true;
  196. layer[26][12] = true;
  197. layer[26][13] = true;
  198. layer[26][14] = true;
  199. layer[26][15] = true;
  200. layer[26][16] = true;
  201. }
  202.  
  203. // Draw Clock Numbers
  204. void DrawClockNumbers(bool layer[PictureWidth][PictureHeight])
  205. {
  206. //12
  207. layer[11][2] = true;
  208. layer[11][3] = true;
  209. layer[11][4] = true;
  210. layer[11][5] = true;
  211. layer[11][6] = true;
  212.  
  213. layer[13][2] = true;
  214. layer[14][2] = true;
  215. layer[15][2] = true;
  216. layer[15][3] = true;
  217. layer[13][4] = true;
  218. layer[14][4] = true;
  219. layer[15][4] = true;
  220. layer[13][5] = true;
  221. layer[13][6] = true;
  222. layer[14][6] = true;
  223. layer[15][6] = true;
  224.  
  225. //3
  226. layer[22][11] = true;
  227. layer[23][11] = true;
  228. layer[24][11] = true;
  229. layer[24][12] = true;
  230. layer[22][13] = true;
  231. layer[23][13] = true;
  232. layer[24][13] = true;
  233. layer[24][14] = true;
  234. layer[22][15] = true;
  235. layer[23][15] = true;
  236. layer[24][15] = true;
  237.  
  238. //6
  239. layer[12][20] = true;
  240. layer[13][20] = true;
  241. layer[14][20] = true;
  242. layer[12][21] = true;
  243. layer[12][22] = true;
  244. layer[13][22] = true;
  245. layer[14][22] = true;
  246. layer[12][23] = true;
  247. layer[14][23] = true;
  248. layer[12][24] = true;
  249. layer[13][24] = true;
  250. layer[14][24] = true;
  251.  
  252. //9
  253. layer[2][11] = true;
  254. layer[3][11] = true;
  255. layer[4][11] = true;
  256. layer[2][12] = true;
  257. layer[4][12] = true;
  258. layer[2][13] = true;
  259. layer[3][13] = true;
  260. layer[4][13] = true;
  261. layer[4][14] = true;
  262. layer[2][15] = true;
  263. layer[3][15] = true;
  264. layer[4][15] = true;
  265.  
  266. //dot as the following numbers (1, 2, 4, 5, 7, 8, 10, 11)
  267. //1
  268. layer[17][4] = true;
  269. layer[17][5] = true;
  270. layer[18][4] = true;
  271. layer[18][5] = true;
  272.  
  273. //2
  274. layer[21][7] = true;
  275. layer[21][8] = true;
  276. layer[22][7] = true;
  277. layer[22][8] = true;
  278.  
  279. //4
  280. layer[21][18] = true;
  281. layer[21][19] = true;
  282. layer[22][18] = true;
  283. layer[22][19] = true;
  284.  
  285. //5
  286. layer[17][21] = true;
  287. layer[17][22] = true;
  288. layer[18][21] = true;
  289. layer[18][22] = true;
  290.  
  291. //7
  292. layer[8][21] = true;
  293. layer[8][22] = true;
  294. layer[9][21] = true;
  295. layer[9][22] = true;
  296.  
  297. //8
  298. layer[4][18] = true;
  299. layer[4][19] = true;
  300. layer[5][18] = true;
  301. layer[5][19] = true;
  302.  
  303. //10
  304. layer[4][7] = true;
  305. layer[4][8] = true;
  306. layer[5][7] = true;
  307. layer[5][8] = true;
  308.  
  309. //11
  310. layer[8][4] = true;
  311. layer[8][5] = true;
  312. layer[9][4] = true;
  313. layer[9][5] = true;
  314. }
  315.  
  316. //
  317. void CombineLayersIntoFirst(bool firstLayer[PictureWidth][PictureHeight], bool secondLayer[PictureWidth][PictureHeight])
  318. {
  319. for (int x = 0; x < PictureWidth; x++)
  320. {
  321. for (int y = 0; y < PictureHeight; y++)
  322. {
  323. if (firstLayer[x][y] || secondLayer[x][y])
  324. {
  325. firstLayer[x][y] = true;
  326. }
  327. }
  328. }
  329. }
  330.  
  331. // Display Clock
  332. void PrintClock(bool clockFrameLayer[PictureWidth][PictureHeight], bool clockNumbersLayer[PictureWidth][PictureHeight], bool hoursHandLayer[PictureWidth][PictureHeight], bool minutesHandLayer[PictureWidth][PictureHeight], bool secondsHandLayer[PictureWidth][PictureHeight], bool centerPointLayer[PictureWidth][PictureHeight])
  333. {
  334. //
  335. system("color 0F");
  336.  
  337. //
  338. for (int y = 0; y < PictureHeight; y++)
  339. {
  340. for (int x = 0; x < PictureWidth; x++)
  341. {
  342. //
  343. if (secondsHandLayer[x][y] || minutesHandLayer[x][y] || hoursHandLayer[x][y] || clockNumbersLayer[x][y] || clockFrameLayer[x][y] || centerPointLayer[x][y])
  344. {
  345. //
  346. if (centerPointLayer[x][y]) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x04); //
  347. else if (secondsHandLayer[x][y]) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0F); //
  348. else if (minutesHandLayer[x][y]) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x09); //
  349. else if (hoursHandLayer[x][y]) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x02); //
  350. else if (clockNumbersLayer[x][y]) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x08); //
  351. else if (clockFrameLayer[x][y]) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0F); //
  352.  
  353. cout << char(symbolChar) << char(symbolChar); //
  354. }
  355. else
  356. {
  357. cout << char(emptyChar) << char(emptyChar); //
  358. }
  359. }
  360.  
  361. //
  362. cout << endl;
  363. }
  364. }
  365.  
  366. // Draw Arrows Of Clock
  367. void DrawClockHand(bool handLayer[PictureWidth][PictureHeight], float degree, float length)
  368. {
  369. //
  370. if (length > 1) return;
  371.  
  372. //
  373. double StartPointx = PictureWidth / 2.0;
  374. double StartPointy = PictureHeight / 2.0;
  375.  
  376. //
  377. double LineLengthx = StartPointx * length;
  378. double LineLengthy = StartPointy * length;
  379.  
  380. //
  381. double LineLength = sqrt(LineLengthx * LineLengthx + LineLengthy * LineLengthy);
  382.  
  383. //
  384. double deltaT = min(LineLengthx, LineLengthy) / max(LineLengthx, LineLengthy);
  385.  
  386. //
  387. int x, y;
  388. for (double t = 0; t <= LineLength; t += deltaT)
  389. {
  390. //
  391. x = int(StartPointx + t * cos((degree - 0.25) * 2.0 * PI));
  392. y = int(StartPointy + t * sin((degree - 0.25) * 2.0 * PI));
  393.  
  394. //
  395. handLayer[x][y] = true;
  396. }
  397. }
  398.  
  399. // Draw & Set Center Of Clock
  400. void DrawCenterPoint(bool layer[PictureWidth][PictureHeight])
  401. {
  402. layer[PictureWidth / 2][PictureHeight / 2] = true;
  403. }
  404.  
  405. // Convert Actual Time Ti Ticks
  406. float ConvertTimeToDegree(int time, int ticks, int timeFraction = 0, int timeFractionTicks = 1)
  407. {
  408. return float(time) / float(ticks) + float(timeFraction) / (float(timeFractionTicks) * float(ticks));
  409. }
  410.  
  411. // Display Clock In Actual Time
  412. void PrintDigitalClock(int seconds, int minutes, int hours)
  413. {
  414. cout << "Clock: " << hours << ":" << minutes << ":" << seconds << endl;
  415. }
  416.  
  417. // Display Frame Counter & Its Duration
  418. void PrintFramesLog()
  419. {
  420. cout << "frame #" << framesCounter << "; " << "frame duration: " << duration << " ms; " << "sleep period: " << (idealDurationPerFrame - duration) << " ms; running at: " << (1000.0f / (duration + max(0, (idealDurationPerFrame - duration)))) << " fps; next frame after: " << nextFrameAfter << "; " << endl;
  421. }
  422.  
  423. // get the time from the local system
  424. void UpdateClockToCurrentTime(int& hours, int& minutes, int& seconds)
  425. {
  426. time_t t = time(0); // get time now
  427. struct tm* now = new tm; // intialize a struc
  428. localtime_s(now, &t); // Converts a time_t time value to a tm structure, and corrects for the local time zone.
  429.  
  430. // store the time in the var.
  431. seconds = now->tm_sec;
  432. minutes = now->tm_min;
  433. hours = now->tm_hour;
  434.  
  435. //cout << (now->tm_year + 1900) << '-'
  436. // << (now->tm_mon + 1) << '-'
  437. // << now->tm_mday
  438. // << endl;
  439. }
  440.  
  441. int main()
  442. {
  443. // Function Call
  444. bool circleLayer[PictureWidth][PictureHeight];
  445. bool numbersLayer[PictureWidth][PictureHeight];
  446. bool secondsHandLayer[PictureWidth][PictureHeight];
  447. bool minutesHandLayer[PictureWidth][PictureHeight];
  448. bool hoursHandLayer[PictureWidth][PictureHeight];
  449. bool centerPointLayer[PictureWidth][PictureHeight];
  450.  
  451. // Variable Declaration
  452. int seconds = 0;
  453. int minutes = 0;
  454. int hours = 0;
  455.  
  456. // loop for ever
  457. while (true)
  458. {
  459. // get the time at the start
  460. startTime = clock();
  461.  
  462. // process the frame
  463. {
  464. //
  465. ClearLayer(circleLayer);
  466. ClearLayer(numbersLayer);
  467. ClearLayer(centerPointLayer);
  468. ClearLayer(secondsHandLayer);
  469. ClearLayer(minutesHandLayer);
  470. ClearLayer(hoursHandLayer);
  471.  
  472. // Update Clock To Current Time
  473. UpdateClockToCurrentTime(hours, minutes, seconds);
  474.  
  475. // Function Call To Draw Circle, Clock Numbers, Center Of Clock, Clock Arrows in seconds, minutes & Hours
  476. DrawCircle(circleLayer);
  477. DrawClockNumbers(numbersLayer);
  478. DrawCenterPoint(centerPointLayer);
  479. DrawClockHand(secondsHandLayer, ConvertTimeToDegree(seconds, 60), 0.5f);
  480. DrawClockHand(minutesHandLayer, ConvertTimeToDegree(minutes, 60, seconds, 60), 0.4f);
  481. DrawClockHand(hoursHandLayer, ConvertTimeToDegree(hours, 12, minutes, 60), 0.3f);
  482.  
  483. // Clear Screen
  484. system("cls");
  485.  
  486. // Display & Output Clock
  487. PrintClock(circleLayer, numbersLayer, hoursHandLayer, minutesHandLayer, secondsHandLayer, centerPointLayer);
  488.  
  489. // Function Call To Display Clock In Actual Time
  490. PrintDigitalClock(seconds, minutes, hours);
  491. }
  492.  
  493. // find duration of the processed frame
  494. duration = (clock() - startTime);
  495.  
  496. // find the next targeted frame
  497. nextFrameAfter = (duration / 1000.0f) + 1;
  498.  
  499. // increase the frame counter
  500. framesCounter += nextFrameAfter;
  501.  
  502. //
  503. PrintFramesLog();
  504.  
  505. // frame process duration finished too early based on the targeted frames per seconds so sleep
  506. while (duration < idealDurationPerFrame)
  507. {
  508. duration = (clock() - startTime);
  509. }
  510. }
  511.  
  512. // Exit Program
  513. system("pause");
  514. return 0;
  515. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement