#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <graph.h>

typedef struct bouton{
  int xmin;
  int xmax;
  int ymin;
  int ymax;
}bouton;


void creerBouton(bouton** boutons){
  int numBouton, x, y;
  for (numBouton=0, y=100; y<300; y+=150, numBouton++){
    for (x=100; x<300; x+=150){
      boutons[numBouton]->xmin=x;
      boutons[numBouton]->xmax=x+100;
      boutons[numBouton]->ymin=y;
      boutons[numBouton]->ymax=y+100;
    }
  }
}

int detectClic(bouton* boutons, long int* instant, int choix){
  int numBouton;
  while(1){
    while (SourisCliquee()==0){
      if (Microsecondes>*instant && choix >= 0 && choix <= 3){
	afficheBouton(boutons, choix, 0, instant);
      }
    }
    for (numBouton=0;numBouton<4; numBouton++){
      if (_X<boutons[numBouton].xmax && _X>boutons[numBouton].xmin && _Y<boutons[numBouton].ymax && _Y>boutons[numBouton].ymin){
	return numBouton;
      }
    }
  }
}

void afficheBouton(bouton* boutons, int numBouton, int etat, long int* instant){
  if (Microsecondes()>*instant){
    char chemin[21]="images/bouton?-?.png";
    chemin[13]= (char) numBouton+48;
    chemin[13]= etat;
    ChargerImage(chemin,boutons[numBouton].xmin,boutons[numBouton].ymin,0,0,100,100);
    *instant = Microsecondes() + 500000;
    return 1;
  }
  return 0;
}
