/* demo8b.c : demonstration de EZ-Draw * * Edouard.Thiel@lif.univ-mrs.fr - 24/03/2009 - version 1.0 * * Compilation sous Unix : * gcc -Wall demo8b.c ez-draw.c -o demo8b -L/usr/X11R6/lib -lX11 -lXext -lm * Compilation sous Windows : * gcc -Wall demo8b.c ez-draw.c -o demo8b.exe -lgdi32 -lm * * This program is free software under the terms of the * GNU Lesser General Public License (LGPL) version 2.1. */ #include "ez-draw.h" #include #ifndef M_PI #define M_PI 3.14159265358979323846 #endif /* Fenetre 1 : une horloge tourne les aiguilles */ int cpt1 = 0, etat1 = 1, win1_w = 250, win1_h = 250, delay1 = 30; void dessin_aiguille (Window win, unsigned long color, double angle, int xc, int yc, int d1, int d2, int ra) { double a = (angle-90) * M_PI / 180, c = cos (a), s = sin (a), x1 = xc + c*d1, y1 = yc + s*d1, x2 = xc + c*d2, y2 = yc + s*d2, x3 = x1 - s*ra, y3 = y1 + c*ra, x4 = x1 + s*ra, y4 = y1 - c*ra, x5 = x2 - s*ra, y5 = y2 + c*ra, x6 = x2 + s*ra, y6 = y2 - c*ra; ez_set_color (color); ez_draw_line (win, x3, y3, x5, y5); ez_draw_line (win, x5, y5, x6, y6); ez_draw_line (win, x6, y6, x4, y4); ez_draw_line (win, x4, y4, x3, y3); } void win1_redessiner (Window win) { double h = cpt1 / 60.0, m = cpt1 % 60; ez_set_color (ez_magenta); ez_set_nfont (2); ez_draw_text (win, EZ_TC, win1_w/2, 6, "%02d:%02d", (int) h, (int) m); dessin_aiguille (win, ez_red, h*30, win1_w/2, win1_h/2, 20, 70, 10); dessin_aiguille (win, ez_blue, m* 6, win1_w/2, win1_h/2, 15, 92, 6); ez_set_color (ez_black); ez_set_nfont (0); ez_draw_text (win, EZ_BL, 8, win1_h-8, "espace : pause q : quitter"); } void win1_event (Ez_event *ev) /* Appele'e a chaque evenement sur win1 */ { switch (ev->type) { case Expose : /* Il faut tout redessiner */ win1_redessiner (ev->win); break; case TimerNotify : /* Le timer est arrive' a echance */ cpt1 = (cpt1 + 1) % 720; ez_send_expose (ev->win); ez_start_timer (ev->win, delay1); /* On re'arme le timer */ break; case KeyPress : /* Une touche a ete pressee */ switch (ev->key_sym) { case XK_q : ez_quit (); break; case XK_space : if (etat1) { etat1 = 0; ez_start_timer (ev->win, -1); } else { etat1 = 1; ez_start_timer (ev->win, delay1); } break; } break; } } /* Fenetre 2 : une balle rebondit sur une raquette */ int ba_x = 100, ba_y = 100, ba_dx = 4, ba_dy = 3, win2_w = 280, win2_h = 180, delay2 = 20; void avancer_balle (int x1, int y1, int x2, int y2) { ba_x += ba_dx; ba_y += ba_dy; if (ba_x <= x1) { ba_x = x1; ba_dx = -ba_dx; } else if (ba_x >= x2) { ba_x = x2; ba_dx = -ba_dx; } if (ba_y <= y1) { ba_y = y1; ba_dy = -ba_dy; } else if (ba_y >= y2) { ba_y = y2; ba_dy = -ba_dy; } } void win2_redessiner (Window win) { ez_set_color (ez_black); ez_draw_circle (win, ba_x-10, ba_y-10, ba_x+10, ba_y+10); ez_set_color (ez_blue); ez_fill_rectangle (win, ba_x-25, win2_h-8, ba_x+25, win2_h-3); } void win2_event (Ez_event *ev) /* Appele'e a chaque evenement sur win2 */ { switch (ev->type) { case Expose : /* Il faut tout redessiner */ win2_redessiner (ev->win); break; case TimerNotify : /* Le timer est arrive' a echance */ avancer_balle (10, 10, win2_w-10, win2_h-19); ez_send_expose (ev->win); ez_start_timer (ev->win, delay2); /* On re'arme le timer */ break; case ConfigureNotify : /* On recoit la nouvelle taille de la fenetre */ win2_w = ev->width; win2_h = ev->height; break; } } int main () { Window win1, win2; if (ez_init() < 0) exit(1); win1 = ez_window_create (win1_w, win1_h, "Demo 8b : horloge", win1_event); win2 = ez_window_create (win2_w, win2_h, "Demo 8b : rebonds", win2_event); /* On associe un double-buffer d'affichage pour eviter tout clignotement */ ez_window_dbuf (win1, 1); ez_window_dbuf (win2, 1); /* Provoque l'envoie d'un evenement TimerNotify dans delay millisecondes */ ez_start_timer (win1, delay1); ez_start_timer (win2, delay2); ez_main_loop (); exit(0); }