From 31c278813ef653426171a20f3aa624a981fb6faf Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Tue, 21 Oct 2008 13:39:47 +0000 Subject: [PATCH] Trying to get straight lines drawn with shift + click. Seems the value of the starting point gets cut to 8 bit somewhere but i can't see where that happens... and it works fine when drawing, but not in preview mode ! git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@294 416bcca6-2ee7-4201-b75f-2eb2f807beb1 --- divers.c | 3 ++- graph.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ graph.h | 1 + operatio.c | 15 +++++++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) diff --git a/divers.c b/divers.c index bc6ef10a..3ce67ef2 100644 --- a/divers.c +++ b/divers.c @@ -343,7 +343,8 @@ void Get_input(void) Afficher_curseur(); } - // Vidage de toute mise à jour qui serait encore en attente. + // Vidage de toute mise à jour de l'affichage à l'écran qui serait encore en attente. + // (c'est fait ici car on est sur que cette fonction est apellée partout ou on a besoin d'interragir avec l'utilisateur) Flush_update(); } diff --git a/graph.c b/graph.c index 77425d41..effa2839 100644 --- a/graph.c +++ b/graph.c @@ -4458,6 +4458,51 @@ void Tracer_ellipse_pleine(short Centre_X,short Centre_Y,short Rayon_horizontal, Mettre_Ecran_A_Jour(Centre_X-Rayon_horizontal,Centre_Y-Rayon_vertical,2*Rayon_horizontal+1,2*Rayon_vertical+1); } +/****************** +* TRACÉ DE LIGNES * +******************/ + +void Rectifier_coordonnees_a_45_degres(short AX, short AY, short* BX, short* BY) +// Modifie BX et BY pour que la ligne AXAY - BXBY soit +// - une droite horizontale +// - une droite verticale +// - une droite avec une pente de 45 degrés +{ + int dx, dy; + + DEBUG("ax",AX); + dx = (*BX)-AX; + dy = AY- *BY; // On prend l'opposée car à l'écran les Y sont positifs en bas, et en maths, positifs en haut + + if (dx==0) return; // On est en lockx et de toutes façons le X n'a pas bougé, on sort tout de suite pour éviter une méchante division par 0 + + float tan = dy/dx; + + if (tan <= 0.4142 && tan >= -0.4142) + { + // Cas 1 : Lock Y + *BY = AY; + DEBUG("horiz",dx); + } + else if ( tan > 0.4142 && tan < 2.4142) + { + // Cas 2 : dy=dx + DEBUG("PLOP",dx); + } + else if (tan < -0.4142 && tan >= -2.4142) + { + // Cas 8 : dy = -dx + DEBUG("PLiP",dx); + } + else + { + // Cas 3 : Lock X + *BX = AX; + DEBUG("vert",dx); + } + + return; +} // -- Tracer général d'une ligne ------------------------------------------ diff --git a/graph.h b/graph.h index 730901b6..c46abc7a 100644 --- a/graph.h +++ b/graph.h @@ -132,6 +132,7 @@ void Tracer_ellipse_vide_Preview (short Centre_X,short Centre_Y,short Rayon_hor void Effacer_ellipse_vide_Preview (short Centre_X,short Centre_Y,short Rayon_horizontal,short Rayon_vertical); void Tracer_ellipse_pleine (short Centre_X,short Centre_Y,short Rayon_horizontal,short Rayon_vertical,byte Couleur); +void Rectifier_coordonnees_a_45_degres(short AX, short AY, short* BX, short* BY); void Tracer_ligne_Definitif (short Debut_X,short Debut_Y,short Fin_X,short Fin_Y,byte Couleur); void Tracer_ligne_Preview (short Debut_X,short Debut_Y,short Fin_X,short Fin_Y,byte Couleur); void Tracer_ligne_Preview_xor(short Debut_X,short Debut_Y,short Fin_X,short Fin_Y,byte Couleur); diff --git a/operatio.c b/operatio.c index 0d3d36ca..78994db7 100644 --- a/operatio.c +++ b/operatio.c @@ -400,6 +400,8 @@ void Ligne_12_0(void) // Taille_Pile : 0 // // Souris effacée: Oui + +// Début du tracé d'une ligne (premier clic) { Initialiser_debut_operation(); Backup(); @@ -435,6 +437,8 @@ void Ligne_12_5(void) // Taille_Pile : 5 // // Souris effacée: Non + +// Poursuite du tracé d'une ligne (déplacement de la souris en gardant le curseur appuyé) { short Debut_X; short Debut_Y; @@ -444,8 +448,13 @@ void Ligne_12_5(void) Operation_POP(&Fin_Y); Operation_POP(&Fin_X); + + DEBUG("px ",Pinceau_X); if ((Pinceau_X!=Fin_X) || (Pinceau_Y!=Fin_Y)) { + // On corrige les coordonnées de la ligne si la touche shift est appuyée... + if(SDL_GetModState() & KMOD_SHIFT) + Rectifier_coordonnees_a_45_degres(Debut_X,Debut_Y,&Pinceau_X,&Pinceau_Y); Effacer_curseur(); Operation_POP(&Debut_Y); Operation_POP(&Debut_X); @@ -480,6 +489,8 @@ void Ligne_0_5(void) // Taille_Pile : 5 // // Souris effacée: Oui + +// Fin du tracé d'une ligne (relachage du bouton) { short Debut_X; short Debut_Y; @@ -493,6 +504,10 @@ void Ligne_0_5(void) Operation_POP(&Debut_X); Operation_POP(&Couleur); + // On corrige les coordonnées de la ligne si la touche shift est appuyée... + if(SDL_GetModState() & KMOD_SHIFT) + Rectifier_coordonnees_a_45_degres(Debut_X,Debut_Y,&Fin_X,&Fin_Y); + Pinceau_Forme=Pinceau_Forme_avant_operation; Pixel_figure_Preview (Debut_X,Debut_Y,Lit_pixel_dans_ecran_courant(Debut_X,Debut_Y));