Fonctions graphiques



next up previous contents index
Next: Syntaxe BNF de Up: Quelques éléments de Previous: Entrées-Sorties

Fonctions graphiques

       

Les fonctions sont les mêmes qu'en Pascal. Il existe toutefois une différence importante: l'environnement graphique n'est pas défini par défaut dans l'environnement ThinkC. Il faut donc l'initialiser par la procédure InitQuickDraw() gif et inclure le fichier include <MacLib.proto.h> de Philippe Chassignet. Le plus simple est de se fournir le projet VoidC qui contient ce prélude et la bibliothèque d'interface graphique.

typedef int  ShortInt;

typedef struct {
    ShortInt  v, h;
} Point;
	
typedef struct {
    ShortInt  top, left, bottom, right;
} Rect;

Et les fonctions correspondantes (voir page gif)

void SetRect (Rect *, ShortInt, ShortInt, ShortInt, ShortInt);
void UnionRect (Rect *, Rect *, Rect *);
void FrameRect (Rect *);
void PaintRect (Rect *);
void EraseRect (Rect *);
void InvertRect (Rect *);
void FrameOval (Rect *);
void PaintOval (Rect *);
void EraseOval (Rect *);
void InvertOval (Rect *);
typedef int Byte;;
typedef Byte Pattern [8];;
void FillRect (Rect *, Pattern);
void FillOval (Rect *, Pattern);
void FrameArc (Rect *, ShortInt, ShortInt);
void PaintArc (Rect *, ShortInt, ShortInt);
void EraseArc (Rect *, ShortInt, ShortInt);
void InvertArc (Rect *, ShortInt, ShortInt);
int Button (void);
void GetMouse (Point *);

Toutes ces définitions sont aussi sur Vax dans les fichiers include <MacLib.h> sur Mac et dans <MacLib.h> et <MacLib.proto.h> dans le directory /usr/local/tgix/c. Le programme de pong, écrit en Pascal dans le chapître précédent, devient instantanément le programme C suivant.

#include "MacLib.proto.h"

#define  C    5               /* Le rayon de la balle */
#define  X0   5
#define  X1   250
#define  Y0   5
#define  Y1   180

void GetXY (int *xp, int *yp)
{
#define   N   2
    Rect    r;
    Point   p;
    int x, y;

    while (!Button())         /* - */
        ;
    while (Button())          /* - */
        ;
    GetMouse(&p);             /* - */
    x = p.h;
    y = p.v;
    SetRect(&r, x - N, y - N, x + N, y + N);
    PaintOval(&r);            /* On affiche le point pour signifier la lecture */
    *xp = x; *yp = y;
}
 
int main()
{
    int   x, y, dx, dy;
    Rect  r, s;
    int  i;

    InitQuickDraw();            /* Initialisation du graphique */
    SetRect(&s, 50, 50, X1 + 100, Y1 + 100);
    SetDrawingRect(&s);
    ShowDrawing();
    SetRect(&s, X0, Y0, X1, Y1);
    FrameRect(&s);              /* Le rectangle de jeu */
    GetXY(&x, &y);              /* - */
    dx = 1;                     /* La vitesse initiale */
    dy = 1;                     /* de la balle */
    for (;;) {
         SetRect(&r, x - C, y - C, x + C, y + C);
         PaintOval(&r);          /* On dessine la balle en $x,y$ */
         x = x + dx;
         if (x - C <= X0 + 1 || x + C >= X1 - 1) 
             dx = -dx;
         y = y + dy;
         if (y - C <= Y0 + 1 || y + C >= Y1 - 1) 
             dy = -dy;
         for (i = 1; i <= 2500; ++i)
             ;                   /* On temporise */
         InvertOval(&r);         /* On efface la balle */
    }
}