# include </usr/X11R6/include/X11/Xlib.h>
# include <stdio.h>
# include <stdlib.h>
# include <ncurses.h>

Display *d;
Window w;   
GC gc;      //définit la variable "Contexte Graphique"
int i,x,y,haut,larg;
long alea = 1;

// création d'une fenêtre
main () 
{
//déclaration des fonctions appelées ...
  int DRAW(void);

  //dimensions de la fenêtre
  larg = 300;
  haut = 300;
  //vérifiaction de la présence dun serveur X
  if ((d = XOpenDisplay("")) == NULL)
  {
    fprintf(stderr,"Impossible de contacter le serveur\n"); 
    exit(1); 
  }
  //création de la fenêtre w
      w = XCreateSimpleWindow(d, RootWindow(d, DefaultScreen(d)),
			0, 0, larg, haut,
			2, WhitePixel(d, DefaultScreen(d)),
			BlackPixel(d, DefaultScreen(d)));
      XStoreName (d, w, "ma fenetre");
      // Selectionne l'Event Mask pour cette fenêtre
      //active le bit StructureNotifyMask dans l'Event Mask de la fenêtre w
      XSelectInput (d, w, StructureNotifyMask);
      //mapage de la fenêtre, pour la rendre visible
      XMapWindow (d, w);
      //création d'un contexte graphique
      gc = DefaultGC(d, DefaultScreen(d));
      //définition des couleurs
      int blackColor = BlackPixel(d, DefaultScreen(d));
      int whiteColor = WhitePixel(d, DefaultScreen(d));
      //établissement de la couleur du premier plan.
      XSetForeground(d, gc, whiteColor);

// déclaration d'un Xevent pour rendre la fenêtre active
      for (;;)
	{
	  XEvent ev; 
	  XNextEvent(d, &ev); 
	  if (ev.type == MapNotify)
	    break;
	}
      for (;;)
	{
	  for (i=10;i<(larg/4);i++)
	    {
	      for (x=0;x<((larg/i)+i);x++)
		{
		  for (y=0;y<((haut/i)+i);y++)
		    {
			DRAW();
		    }
		}
	      XFlush(d);
	      napms(10);
	      XClearWindow(d,w);
	      //XClearArea(d,w,0,0,larg,haut,0);
	    }
	  for (i=(larg/4);i>10;i--)
	    {
	      for (x=0;x<((larg/i)+i);x++)
		{
		  for (y=0;y<((haut/i)+i);y++)
		    {
		      DRAW();
		    }
		}
	      XFlush(d);
	      napms(10);
	      XClearWindow(d,w);
	      //XClearArea(d,w,0,0,larg,haut,0);
	    }
	} 


}

//  définition des fonctions.
int DRAW(void)
{
  long RAND(void);
  long taille;
  taille = RAND();
  XDrawRectangle(d, w, gc,((x*i)-(taille/2)),((y*i)+(i/2)-(taille/2)),taille,taille);
}

long RAND(void)
{
  const long a=16807, m=2147483647L, q=127773L, r=2836 ;
  ldiv_t x;
  long test;

  x = ldiv(alea, q) ;
  test = a * x.rem - r*x.quot ;
    if (test >= 0)
      alea = test ;
    else
      alea = test + m ;
    return (alea/40000000) ;
}

