1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
| // Prototypes
void drawLine(SDL_Surface * ,int ,int ,int ,int ,Uint32 );
void dot(SDL_Surface * ,int ,int ,Uint32 );
// Dessin du grappin
// PosEcran = Position du coin en haut à gauche de l'écran par rapport au monde
// TailleEcran = Taille de l'écran
// Perso = position du premier point du grappin par rapport au monde
// Grappin = position du deuxième point du grappin par rapport au monde
// decal_x = position de l'écran par rapport à l'écran
void drawGrappin( vecteur2D vPosEcran, vecteur2D vTailleEcran ,
f_vecteur2D vPerso, f_vecteur2D vGrappin ,
int decal_x )
{
// Les deux extrémités de la partie à dessiner
f_vecteur2D point[2];
// A et B pour l'équation réduite, inter pour les points d'intersections
double a,b,inter;
// Limites de dessin : ce sont des segments et pas des droites
double x_limit[2];
if(point[0].x<point[1].x)
{
x_limit[0]=point[0].x;
x_limit[1]=point[1].x;
}
else
{
x_limit[0]=point[1].x;
x_limit[1]=point[0].x;
}
int etat=0;
// Etats :
// 0 : on a trouvé aucun point
// 1 : on a trouvé un point
// 2 : on a trouvé les deux points
// ----------------------------------------------------------------------------------------------------
// ---------- Dans l'écran ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
// Si le perso est dans l'écran
if( (vPerso.x>vPosEcran.x) && (vPerso.y>vPosEcran.y)
&& (vPerso.x<vPosEcran.x+vTailleEcran.x)
&& (vPerso.y<vPosEcran.y+vTailleEcran.y) )
{
point[etat].x=vPerso.x;
point[etat].y=vPerso.y;
etat+=1;
}
// Si le grappin est dans l'écran
if( (vGrappin.x>vPosEcran.x) && (vGrappin.y>vPosEcran.y)
&& (vGrappin.x<vPosEcran.x+vTailleEcran.x)
&& (vGrappin.y<vPosEcran.y+vTailleEcran.y) )
{
point[etat].x=vGrappin.x;
point[etat].y=vGrappin.y;
etat+=1;
}
// ----------------------------------------------------------------------------------------------------
// ---------- Hors de l'écran -------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
if(etat<2)
{
// On calcule l'équation de la droite
// y = a * X + b
a = (vGrappin.y-vPerso.y)/(vGrappin.x-vPerso.x);
b = vPerso.y-vPerso.x*a;
// Si on n'a pas encore les deux points
if(etat<2)
{
// Si le segment passe par la gauche de l'écran
if( (x_limit[0]<vPosEcran.x) && (vPosEcran.x<x_limit[1]) )
{
// On trouve l'intersection avec la gauche du rectangle de l'écran
inter = a * vPosEcran.x + b;
// Si c'est dans le segment, alors on a trouvé l'un des points
if( (inter > vPosEcran.y) && (inter < vPosEcran.y+vTailleEcran.y) )
{
point[0].x = vPosEcran.x;
point[0].y = inter;
etat+=1;
}
}
}
// Si on n'a pas encore les deux points
if(etat<2)
{
// Si le segment passe par la gauche de l'écran
if( (x_limit[0]<(vPosEcran.x+vTailleEcran.x)) && ((vPosEcran.x+vTailleEcran.x)<x_limit[1]) )
{
// On cherche l'intersection avec la droite de l'écran
inter = a * (vPosEcran.x + vTailleEcran.x) + b;
// Si c'est dans le segment, on a trouvé l'un des points
if( (inter > vPosEcran.y) && (inter < vPosEcran.y+vTailleEcran.y) )
{
point[etat].x = vPosEcran.x + vTailleEcran.x;
point[etat].y = inter;
etat+=1;
}
}
}
// Si on n'a pas encore les deux points
if(etat<2)
{
// On cherche l'intersection avec le haut de l'écran
inter = (vPosEcran.y - b) / a;
// Si cette intersection ne dépasse pas les limites
if( (x_limit[0]<inter) && (inter<x_limit[1]) )
{
// Si c'est dans le segment, on a trouvé l'un des points
if( (inter > vPosEcran.x) && (inter < vPosEcran.x+vTailleEcran.x) )
{
point[etat].x = inter;
point[etat].y = vPosEcran.y;
etat+=1;
}
}
}
// Si on n'a pas encore les deux points
if(etat<2)
{
// On cherche l'intersection avec le bas de l'écran
inter = (vPosEcran.y + vTailleEcran.y - b) / a;
// Si cette intersection ne dépasse pas les limites
if( (x_limit[0]<inter) && (inter<x_limit[1]) )
{
// Si c'est dans le segment, on a trouvé l'un des points
if( (inter > vPosEcran.x) && (inter < vPosEcran.x+vTailleEcran.x) )
{
point[etat].x = inter;
point[etat].y = vPosEcran.y + vTailleEcran.y;
etat+=1;
}
}
}
}
// Si on a les deux points, on peut dessiner
if(etat==2)
{
// Dessin
// On enlève les coordonnées du coin de l'écran,
// et on rajoute le décalage
drawLine(sScreen,
(int)point[0].x+decal_x-vPosEcran.x,
(int)point[0].y-vPosEcran.y,
(int)point[1].x+decal_x-vPosEcran.x,
(int)point[1].y-vPosEcran.y,
SDL_MapRGB(sScreen->format,255,0,0)
);
}
else
cout<<"\nBug ! Etat = "<<etat;
}
// Fonction qui dessine un pixel
void dot(SDL_Surface * surf,int x,int y,Uint32 color)
{
SDL_Rect rect;
rect.x=x;
rect.y=y;
rect.w=1;
rect.h=1;
SDL_FillRect(surf,&rect,color);
}
// Fonction qui retourne max(abs(a),abs(b))
int abs_max(int a,int b)
{
if(a<0)
a=-a;
if(b<0)
b=-b;
return (a<b)?b:a;
}
// Fonction pour tracer une ligne droite
void drawLine(SDL_Surface * surf,int x1,int y1,int x2,int y2,Uint32 color)
{
int div,i;
// Pos contient la position actuelle sur la ligne
// Move contient le vecteur qui est ajouté à pos pour passer au pixel suivant
f_vecteur2D pos,move;
// Position de départ
pos.x=x1;
pos.y=y1;
// Déplacement maxi
move.x=x2-x1;
move.y=y2-y1;
// On divise ce déplacement par la plus grande différence de coordonnées
div = abs_max(x2-x1,y2-y1);
move.x=move.x/div;
move.y=move.y/div;
// Maintenant, on boucle DIV fois
for(i=0;i<div;i+=1)
{
// Point
dot(surf,(int)pos.x,(int)pos.y,color);
// On avance
pos.x+=move.x;
pos.y+=move.y;
}
} |
Partager