realtimeclock.pp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. program realtimeclock;
  2. {$mode objfpc}
  3. uses
  4. ctypes, nds9;
  5. var
  6. hours, seconds, minutes: integer;
  7. months: array [0..11] of pchar = ('January', 'February', 'March', 'April',
  8. 'May', 'June', 'July', 'August',
  9. 'September', 'October', 'November', 'December');
  10. unixTime: time_t;
  11. timeStruct: ptm;
  12. //function errno2: pcint; cdecl; external name 'errno';
  13. { $define errno := __errno()^}
  14. //draw a watch hands
  15. procedure DrawQuad(x, y, width, height: cfloat);
  16. begin
  17. glBegin(GL_QUADS);
  18. glVertex3f(x - width / 2, y, 0);
  19. glVertex3f(x + width / 2, y, 0);
  20. glVertex3f(x + width / 2, y + height, 0);
  21. glVertex3f(x - width / 2, y + height, 0);
  22. glEnd();
  23. end;
  24. begin
  25. //put 3D on top
  26. lcdMainOnTop();
  27. // Setup the Main screen for 3D
  28. videoSetMode(MODE_0_3D);
  29. // Reset the screen and setup the view
  30. glInit();
  31. // Set our viewport to be the same size as the screen
  32. glViewport(0,0,255,191);
  33. // Specify the Clear Color and Depth
  34. glClearColor(0,0,0,31);
  35. glClearDepth($7FFF);
  36. consoleDemoInit();
  37. //ds specific, several attributes can be set here
  38. glPolyFmt(POLY_ALPHA(31) or POLY_CULL_NONE);
  39. // Set the current matrix to be the model matrix
  40. glMatrixMode(GL_MODELVIEW);
  41. glLoadIdentity();
  42. glMatrixMode(GL_PROJECTION);
  43. glLoadIdentity();
  44. gluPerspective(70, 256.0 / 192.0, 0.1, 100);
  45. gluLookAt( 0.0, 0.0, 3.0, //camera possition
  46. 0.0, 0.0, 0.0, //look at
  47. 0.0, 1.0, 0.0); //up
  48. while true do
  49. begin
  50. unixTime := time(nil);
  51. timeStruct := gmtime(ptime_t(@unixTime));
  52. hours := timeStruct^.tm_hour;
  53. minutes := timeStruct^.tm_min;
  54. seconds := timeStruct^.tm_sec;
  55. //Push our original Matrix onto the stack (save state)
  56. glPushMatrix();
  57. //draw second hand
  58. glColor3f(0,0,1);
  59. glRotateZ(-seconds * 360 / 60);
  60. glTranslatef(0, 1.9, 0);
  61. DrawQuad(0, 0, 0.2, 0.2);
  62. // Pop our Matrix from the stack (restore state)
  63. glPopMatrix(1);
  64. //Push our original Matrix onto the stack (save state)
  65. glPushMatrix();
  66. //draw minute hand
  67. glColor3f(0, 1, 0);
  68. glRotateZ(-minutes * 360 / 60);
  69. DrawQuad(0, 0, 0.2, 2);
  70. // Pop our Matrix from the stack (restore state)
  71. glPopMatrix(1);
  72. //Push our original Matrix onto the stack (save state)
  73. glPushMatrix();
  74. //draw hourhand
  75. glColor3f(1, 0, 0);
  76. glRotateZ(-hours * 360 / 12);
  77. DrawQuad(0, 0, 0.3, 1.8);
  78. // Pop our Matrix from the stack (restore state)
  79. glPopMatrix(1);
  80. printf(#$1B'[2J%02i:%02i:%02i', hours, minutes, seconds);
  81. printf(#10'%s %i %i', months[timeStruct^.tm_mon], timeStruct^.tm_mday, timeStruct^.tm_year +1900);
  82. // flush to screen
  83. glFlush(0);
  84. swiWaitForVBlank();
  85. end;
  86. end.