Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct tm *current_time;
- GLint hour, minute, second;
- int asegundos = 0;
- int aminutos = 0;
- int ahoras = 0;
- //.................................................... Variaveis
- GLfloat r, g, b; // cor RGB
- //-----------------------------------------------------------------------------------
- // Inicializacao
- //-----------------------------------------------------------------------------------
- void Inicializa(void)
- {
- glClearColor(0.0, 0.0, 0.0, 1.0); //.... Cor para apagar ecran (Preto)
- gluOrtho2D(-50, 50, -50, 50); //.... Definicao sistema coordenadas area de desenho
- glShadeModel(GL_SMOOTH); //.... Interpolacao de cor com base na cor dos vertices
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- r = 1; g = 0; b = 0; //.... Vermelho
- }
- //-----------------------------------------------------------------------------------
- //-------------------------------------------------------------------- INTERACCAO
- //-----------------------------------------------------------------------------------
- //¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨Função callback eventos teclado
- void Teclado(unsigned char key, int x, int y){
- switch (key) {
- case 'r':
- glRotatef(50, 0, 0, 1);
- case 27: //ESC
- exit(0);
- break;
- }
- }
- void DesenhaSegundos(){
- asegundos = asegundos + 6;
- glPushMatrix();
- glRotatef(-asegundos, 0, 0, 1);
- glBegin(GL_POLYGON);
- glVertex2f(0, 30.0f);
- glVertex2f(2.0f, 32.0f);
- glVertex2f(-2.0f, 32.0f);
- glEnd();
- glPopMatrix();
- }
- void DesenhaMinutos(){
- aminutos = aminutos + 6;
- glPushMatrix();
- glRotatef(-aminutos, 0, 0, 1);
- glBegin(GL_POLYGON);
- glVertex2f(0, 20.0f);
- glVertex2f(2.0f, 22.0f);
- glVertex2f(-2.0f, 22.0f);
- glEnd();
- glPopMatrix();
- }
- void DesenhaHoras(){
- ahoras = ahoras + 30;
- glPushMatrix();
- glRotatef(-ahoras, 0, 0, 1);
- glBegin(GL_POLYGON);
- glVertex2f(0, 10.0f);
- glVertex2f(2.0f, 12.0f);
- glVertex2f(-2.0f, 12.0f);
- glEnd();
- glPopMatrix();
- }
- void displayText(char *string, GLfloat x, GLfloat y){
- glRasterPos2f(x, y);
- while (*string){
- glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *string++);
- }
- }
- void DesenhaCirculo(){
- glBegin(GL_POLYGON);
- glVertex2f(-36, 0);
- glVertex2f(-25, 25);
- glVertex2f(0, 36);
- glVertex2f(25, 25);
- glVertex2f(36, 0);
- glVertex2f(25, -25);
- glVertex2f(0, -36);
- glVertex2f(-25, -25);
- glEnd();
- }
- //---------------------------------------- Função callback de desenho (principal)
- void Desenha(void)
- {
- time_t timer = time(0);
- current_time = localtime(&timer);
- hour = current_time->tm_hour;
- minute = current_time->tm_min;
- second = current_time->tm_sec;
- asegundos = second * 6 -6;
- aminutos = minute * 6 -6;
- ahoras = hour * 30 -30;
- if (hour > 12){
- ahoras = (hour-12) * 30 - 30;
- }
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(1, 0, 0);
- DesenhaCirculo();
- glColor3f(1, 1, 1);
- DesenhaSegundos();
- DesenhaMinutos();
- DesenhaHoras();
- char resultado[256];
- sprintf(resultado, "%i:%i:%i", hour, minute,second);
- displayText(resultado, -5, 0);
- glutSwapBuffers(); //.. actualiza ecran
- }
- void Timer(int t)
- {
- glutPostRedisplay();
- glutTimerFunc(1000, Timer, 1); //.. Espera msecDelay milisegundos
- }
- //-----------------------------------------------------------------------------------
- // MAIN
- //-----------------------------------------------------------------------------------
- int main(int argc, char** argv)
- {
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- glutInit(&argc, argv); //===1:Inicia janela
- glutInitDisplayMode(/*GLUT_DEPTH |*/GLUT_DOUBLE | GLUT_RGB); // :Single mode, RBG
- glutInitWindowSize(600, 500); // :dimensoes (pixeis)
- glutInitWindowPosition(200, 100); // :localizacao
- glutCreateWindow("Relógio");
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Inicializa(); //===2:Inicializacao estado/parametros
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //===3:Definicao callbaks
- glutDisplayFunc(DesenhaCirculo);
- glutDisplayFunc(Desenha); // :desenho
- glutTimerFunc(1000, Timer, 1); // :eventos timer
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- glutMainLoop(); //===4:Inicia processamento
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement