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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
| // ###############################################
// UVSQ -- Licence UFR des Sciences
//
// L1S1 IN100 -- Fondements de l'Informatique 1
// L1S2 IN210 -- Fondements de l'Informatique 2
//
// Franck QUESSETTE -- Franck.Quessette@uvsq.fr
// Version de novembre 2009
//
// SOMMAIRE
//
// 0. AFFICHAGE DE TEXTE AVEC SDL_ttf ... ligne 30
// 1. VARIABLES, CONSTANTES ............. ligne 47
// 2. AFFICHAGE ......................... ligne 67
// 3. GESTION D'ÉVÉNEMENTS .............. ligne 152
// 4. DESSIN D'OBJETS ................... ligne 375
// 5. ÉCRITURE DE TEXTE ................. ligne 749
// 6. LECTURE D'ENTIER .................. ligne 862
// 7. GESTION DU TEMPS .................. ligne 879
// 8. VALEUR ALÉATOIRES ................. ligne 939
// 9. DIVERS ............................ ligne 957
// ################################################
// ##################################
// 0. AFFICHAGE DE TEXTE AVEC SDL_ttf
// ##################################
// Définit SDL_TTF_OK ou non
#include "police.h"
#ifdef SDL_TTF_OK
#include <SDL/SDL_ttf.h>
#define POLICE_NAME "verdana.ttf"
TTF_Font *police[256];
int verdana_ok = 0;
#endif
#include "graphics.h"
#include <SDL/SDL_ttf.h>
#include <SDL/SDL_image.h>
// ########################
// 1. VARIABLES, CONSTANTES
// ########################
// 1.1 La variable dans laquelle
// l'image finale est écrite
SDL_Surface * SDL_screen;
// 1.2 Pour ne pas oublier l'appel à init_graphics()
int __init_graphics_is_already_called = 0;
// 1.3 Si SDL_AFFICHE_AUTO vaut 1, l'affichage
// est automatiquement fait pour chaque objet
// Sinon il faut le fait à la main
int SDL_AFFICHE_AUTO = 1;
// 1.4 Les constantes de taille max de l'écran
#define MAX_WIDTH 2048
#define MAX_HEIGHT 1200
// ############
// 2. AFFICHAGE
// ############
// 2.1 Initialisation de la fenêtre sur laquelle on dessine
// W et H sont la largeur et la hauteur désirée.
// La variable globale SDL_screen est initialisée
void init_graphics(int W, int H)
{
// Initialisation d'une taille raisonnable
if ((W>10) && (W<MAX_WIDTH )) WIDTH = W; else WIDTH = 600;
if ((H>10) && (H<MAX_HEIGHT)) HEIGHT = H; else HEIGHT = 400;
// Initialisation de la SDL_surface
SDL_Init(SDL_INIT_VIDEO);
SDL_screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( SDL_screen == NULL )
{
fprintf(stderr, "Impossible de passer en %dx%d en 32 bits: %s\n", WIDTH, HEIGHT, SDL_GetError());
exit(1);
}
// Supprime le curseur de la souris dans la fenêtre
// SDL_ShowCursor(SDL_DISABLE);
// Autorise la prise en compte de répétition lors d'un appui
// long sur une touche
SDL_EnableKeyRepeat(1,0);
// Le titre de la fenêtre
SDL_WM_SetCaption("Jeu",NULL);
__init_graphics_is_already_called = 25;
printf("Fenetre de %d x %d\n",WIDTH,HEIGHT);
#ifdef SDL_TTF_OK
printf("SDL_ttf OK, ");
// Ouverture de la police
TTF_Init();
police[10] = TTF_OpenFont(POLICE_NAME, 10);
if (police[10]) {verdana_ok=1; printf("police %s OK : affichage graphique OK.\n",POLICE_NAME);}
else {verdana_ok = 0; printf("police %s absente : affichage dans la console.\n",POLICE_NAME);}
#else
printf("SDL_ttf absent : affichage dans la console.\n");
#endif
// Remplit la fenêtre de noir
fill_screen(blanc);
affiche_auto_on();
affiche_all();
}
// 2.2 Affichage automatique ou manuel
void affiche_auto_on () { SDL_AFFICHE_AUTO = 1; }
void affiche_auto_off() { SDL_AFFICHE_AUTO = 0; }
// Affiche tous les objets et vérifie que la fonction init_graphics
// a été appelée précédemment et affiche un message d'erreur sinon.
void affiche_all()
{
SDL_Event event;
if (SDL_PollEvent(&event)) if (event.type == SDL_QUIT) exit(0);
if (__init_graphics_is_already_called == 25) SDL_Flip(SDL_screen);
else {
init_graphics(380,80);
write_text("init_graphics() n'a pas été appelée.");
write_text("");
write_text(" Cliquer pour terminer.");
fprintf(stderr,"init_graphics() n'a pas été appelée.\n");
fprintf(stderr,"Cliquer pour terminer.\n");
wait_clic();
exit(1);
}
}
// La fonction synchro est la fonction historique
void synchro() { affiche_all(); }
// 2.3 Création de couleur
COULEUR couleur_RGB(int r, int g, int b)
{
COULEUR C;
return ((r%256)<<16) + ((g%256)<<8) + (b%256);
return C;
}
// #######################
// 3. GESTION D'ÉVÉNEMENTS
// #######################
// 3.1 Renvoie le ou les flèches appuyées
// sous forme d'un déplacement en
// x négatif = nombre d'appuis sur la flèche gauche
// x positif = nombre d'appuis sur la flèche droite
// y négatif = nombre d'appuis sur la flèche bas
// y positif = nombre d'appuis sur la flèche haut
// Instruction non bloquante, si aucune flèche n'a été
// appuyée les champs x et y vaudront 0.
POINT get_arrow()
{
POINT dep;
dep.x = dep.y = 0;
SDL_Event event;
while (SDL_PollEvent(&event))
{
/* Si l'utilisateur a demandé à fermer la fenêtre, on quitte */
if (event.type == SDL_QUIT) exit(0);
/* Si l'utilisateur a appuyé sur une touche */
if (event.type == SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE : exit(0);
case SDLK_LEFT : (dep.x) -= MINDEP; break;
case SDLK_RIGHT : (dep.x) += MINDEP; break;
case SDLK_UP : (dep.y) += MINDEP; break;
case SDLK_DOWN : (dep.y) -= MINDEP; break;
default : break;
}
}
}
return dep;
}
// 3.2 Renvoie déplacement de souris
// Meme sémantique que get_arrow()
// Instruction non bloquante, si la souris n'a pas bougé,
// les champs x et y vaudront 0.
POINT get_mouse()
{
POINT dep;
dep.x = dep.y = -1;
SDL_Event event;
while (SDL_PollEvent(&event))
{
/* Si l'utilisateur a demandé à fermer la fenêtre, on quitte */
if (event.type == SDL_QUIT) exit(0);
/* Si l'utilisateur a appuyé sur une touche */
if (event.type == SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE : exit(0);
default : break;
}
}
/* Si l'utilisateur a bougé la souris */
if (event.type == SDL_MOUSEMOTION)
{
dep.x = event.motion.x;
dep.y = event.motion.y;
}
}
return dep;
}
// 3.3 Attend que l'on tape une touche et quitte
// Instruction bloquante
void wait_escape()
{
int display = 1;
SDL_Event event;
POINT p;
p.x = WIDTH/2 - 190;
p.y = 25;
aff_pol("Appuyer sur une touche pour terminer",20,p,gris);
affiche_all();
while (SDL_WaitEvent(&event) && display)
{
/* Si l'utilisateur a demandé à fermer la fenêtre, on quitte */
if (event.type == SDL_QUIT) exit(0);
/* Si l'utilisateur a appuyé sur une touche */
if (event.type == SDL_KEYDOWN)
{
display=0; break;
}
}
/* Fermeture de la police */
#ifdef SDL_TTF_OK
int i;
if (verdana_ok) for (i=0;i<256;i++) if (police[i]) TTF_CloseFont(police[i]);
TTF_Quit();
#endif
SDL_Quit();
}
// 3.4 Attend que l'utilisateur clique sur le bouton gauche
// Renvoie les coordonnées du point cliqué
// Instruction bloquante
POINT wait_clic()
{
int encore = 1;
POINT P;
SDL_Event event;
P.x = 0;
P.y = 0;
#ifdef EN_LOCAL
// A ne mettre que si on est en local, sur les ordi des étudiants, c'est trop lent
#ifdef SDL_TTF_OK
POINT E,F;
char S[16];
E.x = WIDTH - 55; E.y = 15;
F.x = WIDTH; F.y = 0;
#endif
#endif
while (SDL_WaitEvent(&event) && encore)
{
/* Si l'utilisateur clique avec la souris */
if ((event.type == SDL_MOUSEBUTTONDOWN) && (event.button.button == SDL_BUTTON_LEFT))
{
encore=0;
P.x = event.button.x;
P.y = HEIGHT-event.button.y;
}
/* Si l'utilisateur déplace la souris */
if (event.type == SDL_MOUSEMOTION)
{
#ifdef EN_LOCAL
// A ne mettre que si on est en local, sur les ordi des étudiants, c'est trop lent
#ifdef SDL_TTF_OK
if (police[10])
{
draw_fill_rectangle(E,F,noir);
sprintf(S,"%4d %4d",event.motion.x,HEIGHT - event.motion.y);
aff_pol(S,10,E,gris);
affiche_all();
}
#endif
#endif
printf("%cEn attente de clic ... %4d %4d %c",13,event.motion.x,HEIGHT - event.motion.y,13);
fflush(stdout);
}
/* Si l'utilisateur a demandé à fermer la fenêtre, on quitte */
if (event.type == SDL_QUIT) exit(0);
}
#ifdef EN_LOCAL
// A ne mettre que si on est en local, sur les ordi des étudiants, c'est trop lent
#ifdef SDL_TTF_OK
aff_pol(S,10,E,noir);
//draw_fill_rectangle(E,F,jaune);
affiche_all();
#endif
#endif
printf("%cClic en %4d %4d \n",13,P.x,P.y);
return P;
}
// 3.5 Attend que l'on clique et renvoie dans button le bouton cliqué :
// *button vaut soit 'G' (pour Gauche), soit 'M' (pour milieu),
// soit 'D' (pour Droit) en fonction du bouton cliqué
// Instruction bloquante
POINT wait_clic_GMD(char *button)
{
int encore = 1;
POINT P;
SDL_Event event;
P.x = 0;
P.y = 0;
#ifdef SDL_TTF_OK
POINT E,F;
char S[16];
E.x = WIDTH - 55; E.y = 15;
F.x = WIDTH; F.y = 0;
#endif
affiche_all();
printf("Cliquer dans la fenêtre...");
while (SDL_WaitEvent(&event) && encore)
{
/* Si l'utilisateur a demandé à fermer la fenêtre, on quitte */
if (event.type == SDL_QUIT) exit(0);
/* Si l'utilisateur a cliqué avec la souris */
if ((event.type == SDL_MOUSEBUTTONDOWN))
{
#ifdef SDL_TTF_OK
if (!police[10])
{
draw_fill_rectangle(E,F,noir);
sprintf(S,"%4d %4d",event.motion.x,HEIGHT - event.motion.y);
aff_pol(S,10,E,gris);
affiche_all();
}
#endif
printf("%cCliquer dans la fenêtre ... %4d %4d %c",13,event.motion.x,HEIGHT - event.motion.y,13);
fflush(stdout);
encore=0;
P.x = event.button.x;
P.y = HEIGHT-event.button.y;
if (event.button.button == SDL_BUTTON_LEFT) *button = 'G';
if (event.button.button == SDL_BUTTON_MIDDLE) *button = 'M';
if (event.button.button == SDL_BUTTON_RIGHT) *button = 'D';
}
}
#ifdef SDL_TTF_OK
draw_fill_rectangle(E,F,noir);
#endif
printf("Clic '%c' en %4d %4d \n",*button,P.x,P.y);
return P;
}
// ##################
// 4. DESSIN D'OBJETS
// ##################
// 4.1 Remplissage de tout l'écran
void fill_screen(COULEUR color)
{
int i,j;
for (i=0;i<WIDTH;i++)
for (j=0;j<HEIGHT;j++) *((COULEUR *)SDL_screen->pixels + (HEIGHT-j-1) * WIDTH + i) = color;
if (SDL_AFFICHE_AUTO) affiche_all();
}
// 4.x.1 Fonction de clipping (vérification que le point est
// dans la fenêtre)
// Cette fonction n'est pas visible en dehors de ce fichier
int dans_ecran(int x, int y)
{
if (x<0) return 0;
if (x>=WIDTH) return 0;
if (y<0) return 0;
if (y>=HEIGHT) return 0;
return 1;
}
// 4.x.2 Macro qui permet d'ajouter un pixel à la SDL_surface
// Inverse l'ordonnée entre haut et bas
#define add_pix(x,y,color) if (dans_ecran((x),(y))) *((COULEUR *)SDL_screen->pixels + (HEIGHT-(y)-1) * WIDTH + (x)) = (color)
// 4.2 Affichage un pixel
void draw_pixel(POINT p, COULEUR color)
{
add_pix(p.x,p.y,color);
if (SDL_AFFICHE_AUTO) affiche_all();
}
// 4.3 Dessine un segment
void draw_line(POINT p1, POINT p2, COULEUR color)
{
int xmin, xmax;
int ymin, ymax;
int i,j;
float a,b,ii,jj;
if (p1.x < p2.x) {xmin=p1.x; xmax=p2.x;} else{xmin=p2.x; xmax=p1.x;}
if (p1.y < p2.y) {ymin=p1.y; ymax=p2.y;} else{ymin=p2.y; ymax=p1.y;}
if (xmin==xmax) for (j=ymin;j<=ymax;j++) add_pix(xmin,j,color);
if (ymin==ymax) for (i=xmin;i<=xmax;i++) add_pix(i,ymin,color);
// La variation la plus grande est en x
if ((xmax-xmin >= ymax-ymin) && (ymax-ymin>0))
{
a = (float)(p1.y-p2.y) / ((float)(p1.x-p2.x));
b = p1.y - a*p1.x;
for (i=xmin;i<=xmax;i++)
{
jj = a*i+b;
j = jj;
if (((jj-j) > 0.5) && (j < HEIGHT-1)) j++;
add_pix(i,j,color);
}
}
// La variation la plus grande est en y
if ((ymax-ymin > xmax-xmin) && (xmax-xmin>0))
{
a = (float)(p1.y-p2.y) / ((float)(p1.x-p2.x));
b = p1.y - a*p1.x;
for (j=ymin;j<=ymax;j++)
{
ii = (j-b)/a;
i = ii;
if (((ii-i) > 0.5) && (i < WIDTH-1)) i++;
add_pix(i,j,color);
}
}
if (SDL_AFFICHE_AUTO) affiche_all();
}
// 4.4 Dessine un rectangle non rempli
// Les deux points sont deux points quelconques
// non adjacents du rectangle
void draw_rectangle(POINT p1, POINT p2, COULEUR color)
{
int xmin, xmax;
int ymin, ymax;
int i,j;
if (p1.x < p2.x) {xmin=p1.x; xmax=p2.x;} else{xmin=p2.x; xmax=p1.x;}
if (p1.y < p2.y) {ymin=p1.y; ymax=p2.y;} else{ymin=p2.y; ymax=p1.y;}
for (i=xmin;i<=xmax;i++) add_pix(i,ymin,color);
for (i=xmin;i<=xmax;i++) add_pix(i,ymax,color);
for (j=ymin;j<=ymax;j++) add_pix(xmin,j,color);
for (j=ymin;j<=ymax;j++) add_pix(xmax,j,color);
if (SDL_AFFICHE_AUTO) affiche_all();
}
// 4.5 Dessine un rectangle rempli
// Les deux points sont deux points quelconques
// non adjacents du rectangle
void draw_fill_rectangle(POINT p1, POINT p2, COULEUR color)
{
int xmin, xmax;
int ymin, ymax;
int i,j;
if (p1.x < p2.x) {xmin=p1.x; xmax=p2.x;} else{xmin=p2.x; xmax=p1.x;}
if (p1.y < p2.y) {ymin=p1.y; ymax=p2.y;} else{ymin=p2.y; ymax=p1.y;}
for (i=xmin;i<=xmax;i++) for (j=ymin;j<=ymax;j++) add_pix(i,j,color);
if (SDL_AFFICHE_AUTO) affiche_all();
}
// 4.6 Dessine un cercle non rempli
void draw_circle(POINT centre, int rayon, COULEUR color)
{
POINT min, max;
int i,j;
float dx, dy, rr;
min.x = centre.x - rayon; max.x = centre.x + rayon;
min.y = centre.y - rayon; max.y = centre.y + rayon;
rr = rayon*rayon;
for (i=min.x;i<=max.x;i++)
{
dx = i - centre.x;
dy = sqrt(rr - dx*dx);
j = centre.y + dy;
add_pix(i,j,color);
j = centre.y - dy;
add_pix(i,j,color);
}
for (j=min.y;j<=max.y;j++)
{
dy = j - centre.y;
dx = sqrt(rr - dy*dy);
i = centre.x + dx;
add_pix(i,j,color);
i = centre.x - dx;
add_pix(i,j,color);
}
if (SDL_AFFICHE_AUTO) affiche_all();
}
// 4.7 Dessine un cercle rempli
void draw_fill_circle(POINT centre, int rayon, COULEUR color)
{
POINT min, max;
int i,j;
float dx, dy, rr;
min.x = centre.x - rayon; max.x = centre.x + rayon;
min.y = centre.y - rayon; max.y = centre.y + rayon;
rr = rayon*rayon;
for (i=min.x;i<=max.x;i++)
{
dx = i - centre.x;
for (j=min.y;j<=max.y;j++)
{
dy = j - centre.y;
if (dx*dx + dy*dy <= rr) add_pix(i,j,color);
}
}
if (SDL_AFFICHE_AUTO) affiche_all();
}
// 4.8.1 Dessine un quart de cercle en haut à droite
void draw_circle_HD(POINT centre, int rayon, COULEUR color)
{
POINT min, max;
int i,j;
float dx, dy, rr;
min.x = centre.x; max.x = centre.x + rayon;
min.y = centre.y; max.y = centre.y + rayon;
rr = rayon*rayon;
for (i=min.x;i<=max.x;i++)
{
dx = i - centre.x;
dy = sqrt(rr - dx*dx);
j = centre.y + dy;
add_pix(i,j,color);
}
for (j=min.y;j<=max.y;j++)
{
dy = j - centre.y;
dx = sqrt(rr - dy*dy);
i = centre.x + dx;
add_pix(i,j,color);
}
if (SDL_AFFICHE_AUTO) affiche_all();
}
// 4.8.2 Dessine un quart de cercle en bas à droite
void draw_circle_BD(POINT centre, int rayon, COULEUR color)
{
POINT min, max;
int i,j;
float dx, dy, rr;
min.x = centre.x; max.x = centre.x + rayon;
min.y = centre.y - rayon; max.y = centre.y;
rr = rayon*rayon;
for (i=min.x;i<=max.x;i++)
{
dx = i - centre.x;
dy = sqrt(rr - dx*dx);
j = centre.y - dy;
add_pix(i,j,color);
}
for (j=min.y;j<=max.y;j++)
{
dy = j - centre.y;
dx = sqrt(rr - dy*dy);
i = centre.x + dx;
add_pix(i,j,color);
}
if (SDL_AFFICHE_AUTO) affiche_all();
}
// 4.8.3 Dessine un quart de cercle en haut à gauche
void draw_circle_HG(POINT centre, int rayon, COULEUR color)
{
POINT min, max;
int i,j;
float dx, dy, rr;
min.x = centre.x - rayon; max.x = centre.x;
min.y = centre.y; max.y = centre.y + rayon;
rr = rayon*rayon;
for (i=min.x;i<=max.x;i++)
{
dx = i - centre.x;
dy = sqrt(rr - dx*dx);
j = centre.y + dy;
add_pix(i,j,color);
}
for (j=min.y;j<=max.y;j++)
{
dy = j - centre.y;
dx = sqrt(rr - dy*dy);
i = centre.x - dx;
add_pix(i,j,color);
}
if (SDL_AFFICHE_AUTO) affiche_all();
}
// 4.8.4 Dessine un quart de cercle en bas à gauche
void draw_circle_BG(POINT centre, int rayon, COULEUR color)
{
POINT min, max;
int i,j;
float dx, dy, rr;
min.x = centre.x - rayon; max.x = centre.x;
min.y = centre.y - rayon; max.y = centre.y;
rr = rayon*rayon;
for (i=min.x;i<=max.x;i++)
{
dx = i - centre.x;
dy = sqrt(rr - dx*dx);
j = centre.y - dy;
add_pix(i,j,color);
}
for (j=min.y;j<=max.y;j++)
{
dy = j - centre.y;
dx = sqrt(rr - dy*dy);
i = centre.x - dx;
add_pix(i,j,color);
}
if (SDL_AFFICHE_AUTO) affiche_all();
}
// 4.9 Dessine une ellipse remplie
// Les arguments F1 et F2 sont les focales et r est
// la somme des distances à chacun des points focaux
void draw_fill_ellipse(POINT F1, POINT F2, int r, COULEUR color)
{
int i,j;
int dx, fx;
int dy, fy;
float d, d1, d2;
d = (F1.x-F2.x)*(F1.x-F2.x) + (F1.y-F2.y)*(F1.y-F2.y);
d = sqrt(d);
if (F1.x<F2.x) {dx = F1.x - d - r; fx = F2.x + d + r;} else {dx = F2.x - d - r; fx = F1.x + d + r;}
if (F1.y<F2.y) {dy = F1.y - d - r; fy = F2.y + d + r;} else {dy = F2.y - d - r; fy = F1.y + d + r;}
for (i=dx;i<=fx;i++)
for (j=dy;j<=fy;j++)
if (dans_ecran(i,j))
{
d1 = (i-F1.x)*(i-F1.x) + (j-F1.y)*(j-F1.y);
d1 = sqrt(d1);
d2 = (i-F2.x)*(i-F2.x) + (j-F2.y)*(j-F2.y);
d2 = sqrt(d2);
if (d1+d2 < d+r) add_pix(i,j,color);
}
if (SDL_AFFICHE_AUTO) affiche_all();
}
// 4.10 Dessine un triangle
void draw_triangle(POINT p1, POINT p2, POINT p3, COULEUR color)
{
draw_line(p1,p2,color);
draw_line(p2,p3,color);
draw_line(p3,p1,color);
}
// 4.11 Dessine un triangle rempli
// Fonction annexe qui calcule le min de 3 valeurs
int min3(int a, int b, int c)
{
if ( (a<b) && (a<c) ) return a;
if ( (b<a) && (b<c) ) return b;
return c;
}
// Fonction annexe qui calcule le max de 3 valeurs
int max3(int a, int b, int c)
{
if ( (a>b) && (a>c) ) return a;
if ( (b>a) && (b>c) ) return b;
return c;
}
void draw_fill_triangle(POINT p1, POINT p2, POINT p3, COULEUR color)
{
float a12, b12, a23, b23, a31, b31;
float s1, s2, s3;
// La droite passant par les point pi et pj
// a pour équation : y = aij x + bij
a12 = (p1.y-p2.y)/(float)(p1.x-p2.x); b12 = p1.y - a12*p1.x;
a23 = (p2.y-p3.y)/(float)(p2.x-p3.x); b23 = p2.y - a23*p2.x;
a31 = (p3.y-p1.y)/(float)(p3.x-p1.x); b31 = p3.y - a31*p3.x;
// Le signe de sk détermine de quel coté pk est de la droite [pi,pj]
s3 = p3.y - (a12*p3.x + b12);
s1 = p1.y - (a23*p1.x + b23);
s2 = p2.y - (a31*p2.x + b31);
int minx, maxx, miny, maxy;
minx = min3(p1.x,p2.x,p3.x); maxx = max3(p1.x,p2.x,p3.x);
miny = min3(p1.y,p2.y,p3.y); maxy = max3(p1.y,p2.y,p3.y);
int i,j;
int ok;
for (i=minx;i<maxx;i++)
for (j=miny;j<maxy;j++)
{
ok = 1;
// On vérifie que le point (i,j) est du bon coté
// des 3 droites du triangle
if (s3 * (j - (a12*i + b12)) < 0) ok = 0;
if (s1 * (j - (a23*i + b23)) < 0) ok = 0;
if (s2 * (j - (a31*i + b31)) < 0) ok = 0;
if (ok) add_pix(i,j,color);
}
}
// ####################
// 5. ÉCRITURE DE TEXTE
// ####################
// 5.1 Affiche du texte avec
// Le texte est passé dans l'argument "a_ecrire"
// la police est celle définie par la constante POLICE_NAME
// dans graphics.c
// la taille est passée en argument
// l'argument p de type POINT est le point en haut à gauche
// à partir duquel le texte s'affiche
// la COULEUR C passée en argument est la couleur d'affichage
// Nécessite l'installation de SDL_ttf.h
void aff_pol(char *a_ecrire, int taille, POINT p, COULEUR C)
{
#ifdef SDL_TTF_OK
int i;
SDL_Color color;
SDL_Surface *texte = NULL;
SDL_Rect position;
static int premiere_fois = 1;
static TTF_Font *police[256];
TTF_Font *pol;
// Initialisation de la police (n'est fait qu'une seule fois pour les tailles < 256)
if (premiere_fois) { TTF_Init(); for (i=0;i<256;i++) police[i] = NULL; premiere_fois = 0;}
if (taille>=256) pol = TTF_OpenFont(POLICE_NAME, taille);
else {
if (police[taille]==NULL) police[taille] = TTF_OpenFont(POLICE_NAME, taille);
pol = police[taille];
}
SDL_GetRGB(C,SDL_screen->format,&(color.r),&(color.g),&(color.b));
/* Ecriture du texte dans la SDL_Surface "texte" en mode Blended (optimal) */
if (pol) texte = TTF_RenderText_Blended(pol, a_ecrire, color); else texte = NULL;
if (texte) {
position.x = p.x;
position.y = HEIGHT - p.y;
SDL_BlitSurface(texte, NULL, SDL_screen, &position); /* Blit du texte par-dessus */
if (SDL_AFFICHE_AUTO) affiche_all();
SDL_FreeSurface(texte);
}
else printf("%s\n",a_ecrire);
/*
if (SDL_AFFICHE_AUTO) affiche_all();
if (police) TTF_CloseFont(police);
TTF_Quit();
if (texte) SDL_FreeSurface(texte);
*/
#else
taille = 0; p.x = p.y = 0; C = 0;
printf("%s\n",a_ecrire);
#endif
}
// 5.2 Affiche un entier
// Meme sémantique que aff_pol()
void aff_int(int n, int taille, POINT p, COULEUR C)
{
char s[32];
sprintf(s,"%d",n);
aff_pol(s,taille,p,C);
}
// 5.3 Affiche dans la fenêtre graphique comme dans une fenêtre
// shell mais sans déroulement. Commence en haut et
// se termine en bas.
void write_text(char *a_ecrire)
{
#ifdef SDL_TTF_OK
static char s[256];
static int fin = 0;
static int premier = 1;
static POINT position;
if (premier) {position.x = 10; position.y = HEIGHT; premier = 0;}
if (verdana_ok)
{
// printf("verd\n");
if (a_ecrire)
{
if (fin+strlen(a_ecrire) > 256) {fprintf(stderr,"Chaine de caractère trop longue\n"); return;}
strcat(s+fin,a_ecrire);
// printf("a_ecr : #%s#\n",s);
aff_pol(s,20,position,blanc);
fin += strlen(a_ecrire);
s[fin] = '\0';
}
else
{
position.y -= 20;
fin = 0;
s[fin] = '\0';
}
}
else { if (a_ecrire) {printf("%s",a_ecrire); fflush(stdout);} else printf("\n"); }
#else
if (a_ecrire) {printf("%s",a_ecrire); fflush(stdout);}
else printf("\n");
#endif
}
void write_int(int n)
{
char s[32];
sprintf(s,"%d",n);
write_text(s);
}
void write_bool(BOOL b) { if (b) write_text("True"); else write_text("False"); }
void writeln() { write_text(NULL); }
// ###################
// 6. LECTURE D'ENTIER
// ###################
// 6.1 Renvoie l'entier tapé au clavier.
// Cette fonction est bloquante
int lire_entier_clavier()
{
int n;
int ret;
printf("Donner un entier : ");
fflush(stdout);
ret = scanf("%d",&n);
return n;
}
// ###################
// 7. GESTION DU TEMPS
// ###################
// 7.1 Chronomètre élémentaire, précis à la micro seconde.
// Fonction non visible en dehors de ce fichier
float chrono(int action)
{
static double ch = 0.0;
double current;
float delta;
struct timeval tv;
gettimeofday(&tv,NULL);
current = tv.tv_sec + (float)(tv.tv_usec)*1e-6;
if (action == 0) ch = current;
delta = (float)(current-ch);
return delta;
}
// Déclenchement du chrono
// Le remet à zéro s'il était déjà lancé
void chrono_start() { chrono(0); }
float chrono_val() { return chrono(1); }
// 7.2 Attend le nombre de millisecondes passé en argument
void attendre(int millisecondes) { SDL_Delay(millisecondes); }
// 7.3.1 Renvoie l'heure de l'heure courante
int heure()
{
struct tm *stm;
struct timeval tv;
gettimeofday(&tv,NULL);
stm = localtime((time_t *)&(tv.tv_sec));
return stm->tm_hour;
}
// 7.3.2 Renvoie le nombre de minutes de l'heure courante
int minute()
{
struct tm *stm;
struct timeval tv;
gettimeofday(&tv,NULL);
stm = localtime((time_t *)&(tv.tv_sec));
return stm->tm_min;
}
// 7.3.3 Renvoie le nombre de secondes de l'heure courante
int seconde()
{
struct tm *stm;
struct timeval tv;
gettimeofday(&tv,NULL);
stm = localtime((time_t *)&(tv.tv_sec));
return stm->tm_sec;
}
// ####################
// 8. VALEUR ALÉATOIRES
// ####################
// 8.1 Renvoie un float dans l'intervalle [0;1[
float alea_float()
{
static int init_alea = 1;
if (init_alea) srand(time(NULL));
init_alea = 0;
return (float)rand() / (float)RAND_MAX;
}
// 8.2 Renvoie un int dans l'intervalle [0..N[
// soit N valeurs différentes de 0 à N-1
int alea_int(int N) { return (int)(N*alea_float()); }
// #########
// 9. DIVERS
// #########
// 9.1 Renvoie la distance entre deux points
int distance(POINT P1, POINT P2)
{
int d;
d = (P1.x-P2.x)*(P1.x-P2.x) + (P1.y-P2.y)*(P1.y-P2.y);
return (int)(sqrt(d));
} |
Partager