Advertisement
Guest User

RELOGIO

a guest
May 27th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.31 KB | None | 0 0
  1. struct tm *current_time;
  2. GLint hour, minute, second;
  3. int asegundos = 0;
  4. int aminutos = 0;
  5. int ahoras = 0;
  6.  
  7. //.................................................... Variaveis
  8. GLfloat r, g, b;                                    // cor RGB
  9.  
  10. //-----------------------------------------------------------------------------------
  11. //                                                                      Inicializacao
  12. //-----------------------------------------------------------------------------------
  13. void Inicializa(void)
  14. {
  15.     glClearColor(0.0, 0.0, 0.0, 1.0);   //....  Cor para apagar ecran (Preto)
  16.     gluOrtho2D(-50, 50, -50, 50);       //....  Definicao sistema coordenadas area de desenho
  17.     glShadeModel(GL_SMOOTH);                //....  Interpolacao de cor com base na cor dos vertices
  18.  
  19.     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  20.     r = 1; g = 0; b = 0;                        //....  Vermelho
  21. }
  22.  
  23. //-----------------------------------------------------------------------------------
  24. //-------------------------------------------------------------------- INTERACCAO
  25. //-----------------------------------------------------------------------------------
  26. //¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨Função callback eventos teclado
  27. void Teclado(unsigned char key, int x, int y){
  28.  
  29.     switch (key) {
  30.     case 'r':
  31.  
  32.         glRotatef(50, 0, 0, 1);
  33.  
  34.     case 27:                    //ESC
  35.         exit(0);
  36.         break;
  37.     }
  38. }
  39.  
  40.  
  41.  
  42. void DesenhaSegundos(){
  43.  
  44.     asegundos = asegundos + 6;
  45.  
  46.     glPushMatrix();
  47.  
  48.     glRotatef(-asegundos, 0, 0, 1);
  49.  
  50.     glBegin(GL_POLYGON);
  51.  
  52.     glVertex2f(0, 30.0f);
  53.     glVertex2f(2.0f, 32.0f);
  54.     glVertex2f(-2.0f, 32.0f);
  55.  
  56.     glEnd();
  57.  
  58.     glPopMatrix();
  59. }
  60.  
  61.  
  62. void DesenhaMinutos(){
  63.  
  64.     aminutos = aminutos + 6;
  65.  
  66.     glPushMatrix();
  67.     glRotatef(-aminutos, 0, 0, 1);
  68.  
  69.     glBegin(GL_POLYGON);
  70.  
  71.     glVertex2f(0, 20.0f);
  72.     glVertex2f(2.0f, 22.0f);
  73.     glVertex2f(-2.0f, 22.0f);
  74.  
  75.     glEnd();
  76.  
  77.     glPopMatrix();
  78. }
  79.  
  80. void DesenhaHoras(){
  81.  
  82.     ahoras = ahoras + 30;
  83.  
  84.     glPushMatrix();
  85.     glRotatef(-ahoras, 0, 0, 1);
  86.  
  87.     glBegin(GL_POLYGON);
  88.  
  89.     glVertex2f(0, 10.0f);
  90.     glVertex2f(2.0f, 12.0f);
  91.     glVertex2f(-2.0f, 12.0f);
  92.  
  93.     glEnd();
  94.  
  95.     glPopMatrix();
  96. }
  97.  
  98.  
  99. void displayText(char *string, GLfloat x, GLfloat y){
  100.  
  101.     glRasterPos2f(x, y);
  102.     while (*string){
  103.         glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *string++);
  104.     }
  105.  
  106. }
  107.  
  108. void DesenhaCirculo(){
  109.  
  110.     glBegin(GL_POLYGON);
  111.  
  112.     glVertex2f(-36, 0);
  113.     glVertex2f(-25, 25);
  114.     glVertex2f(0, 36);
  115.     glVertex2f(25, 25);
  116.     glVertex2f(36, 0);
  117.     glVertex2f(25, -25);
  118.     glVertex2f(0, -36);
  119.     glVertex2f(-25, -25);
  120.  
  121.     glEnd();
  122. }
  123.  
  124.  
  125. //---------------------------------------- Função callback de desenho (principal)
  126. void Desenha(void)
  127. {
  128.  
  129.     time_t timer = time(0);
  130.     current_time = localtime(&timer);
  131.     hour = current_time->tm_hour;
  132.     minute = current_time->tm_min;
  133.     second = current_time->tm_sec;
  134.  
  135.     asegundos = second * 6 -6;
  136.     aminutos = minute * 6 -6;
  137.     ahoras = hour * 30 -30;
  138.     if (hour > 12){
  139.         ahoras = (hour-12) * 30 - 30;
  140.     }
  141.  
  142.     glClear(GL_COLOR_BUFFER_BIT);
  143.     glColor3f(1, 0, 0);
  144.     DesenhaCirculo();
  145.     glColor3f(1, 1, 1);
  146.     DesenhaSegundos();
  147.     DesenhaMinutos();
  148.     DesenhaHoras();
  149.  
  150.     char resultado[256];
  151.  
  152.     sprintf(resultado, "%i:%i:%i", hour, minute,second);
  153.  
  154.  
  155.     displayText(resultado, -5, 0);
  156.  
  157.     glutSwapBuffers();                      //.. actualiza ecran
  158.  
  159. }
  160.  
  161. void Timer(int t)
  162. {
  163.     glutPostRedisplay();
  164.     glutTimerFunc(1000, Timer, 1); //.. Espera msecDelay milisegundos
  165. }
  166.  
  167. //-----------------------------------------------------------------------------------
  168. //                                                                               MAIN
  169. //-----------------------------------------------------------------------------------
  170. int main(int argc, char** argv)
  171. {
  172.     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  173.     glutInit(&argc, argv);                          //===1:Inicia janela
  174.     glutInitDisplayMode(/*GLUT_DEPTH |*/GLUT_DOUBLE | GLUT_RGB);   //       :Single mode, RBG
  175.     glutInitWindowSize(600, 500);                   //      :dimensoes (pixeis)
  176.     glutInitWindowPosition(200, 100);               //      :localizacao
  177.     glutCreateWindow("Relógio");
  178.  
  179.     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  180.     Inicializa();                                   //===2:Inicializacao estado/parametros
  181.  
  182.     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  183.     //===3:Definicao callbaks
  184.     glutDisplayFunc(DesenhaCirculo);
  185.     glutDisplayFunc(Desenha);                       //      :desenho
  186.     glutTimerFunc(1000, Timer, 1);                  //      :eventos timer
  187.  
  188.  
  189.     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  190.     glutMainLoop();                             //===4:Inicia processamento
  191.     return 1;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement