/* demo-06.c : demonstration de EZ-Draw * * Edouard.Thiel@lif.univ-mrs.fr - 21/06/2011 - version 1.2 * * Compilation sous Unix : * gcc -Wall demo-06.c ez-draw.c -o demo-06 -lX11 -lXext -L/usr/X11R6/lib * Compilation sous Windows : * gcc -Wall demo-06.c ez-draw.c -o demo-06.exe -lgdi32 * * This program is free software under the terms of the * GNU Lesser General Public License (LGPL) version 2.1. */ #include "ez-draw.h" #define SOM_MAX 100 int som_nb = 0, som_x[SOM_MAX], som_y[SOM_MAX]; void sommet_vider () { som_nb = 0; } void sommet_ajouter (int x, int y) { if (som_nb >= SOM_MAX) return; som_x[som_nb] = x; som_y[som_nb] = y; som_nb++; } void sommet_deplacer (int x, int y) { if (som_nb <= 0 || som_nb >= SOM_MAX) return; som_x[som_nb-1] = x; som_y[som_nb-1] = y; } void sommet_dessiner (Ez_window win) { int i; /* On dessine les sommets */ ez_set_color (ez_blue); for (i = 0; i < som_nb; i++) ez_draw_rectangle (win, som_x[i]-2, som_y[i]-2, som_x[i]+2, som_y[i]+2); /* On relie les sommets par des segments */ ez_set_color (ez_grey); for (i = 1; i < som_nb; i++) ez_draw_line (win, som_x[i-1], som_y[i-1], som_x[i], som_y[i]); } void win1_on_expose (Ez_event *ev) { ez_set_color (ez_black); ez_draw_text (ev->win, EZ_TL, 10, 10, "Cliquez et tirez la souris dans la fenetre pour dessiner.\n" "Tapez sur espace pour vider la fenetre, 'q' pour quitter."); sommet_dessiner (ev->win); } void win1_on_button_press (Ez_event *ev) { sommet_ajouter (ev->mx, ev->my); ez_send_expose (ev->win); } void win1_on_motion_notify (Ez_event *ev) { if (ev->mb == 0) return; /* pas de bouton enfonce' */ sommet_deplacer (ev->mx, ev->my); ez_send_expose (ev->win); } void win1_on_key_press (Ez_event *ev) { switch (ev->key_sym) { case XK_q : ez_quit (); break; case XK_space : sommet_vider (); ez_send_expose (ev->win); break; } } void win1_event (Ez_event *ev) { switch (ev->type) { case Expose : win1_on_expose (ev); break; case ButtonPress : win1_on_button_press (ev); break; case MotionNotify : win1_on_motion_notify (ev); break; case KeyPress : win1_on_key_press (ev); break; } } int main () { Ez_window win1; if (ez_init() < 0) exit(1); win1 = ez_window_create (400, 300, "Demo 06 : dessin a la souris", win1_event); /* On associe un double-buffer d'affichage pour eviter tout clignotement */ ez_window_dbuf (win1, 1); ez_main_loop (); exit(0); }