From fb823ae266398f470cce682516545fd57c2b5fd6 Mon Sep 17 00:00:00 2001 From: Felix-Vimalaratnam Date: Fri, 1 Dec 2023 16:44:23 +0100 Subject: [PATCH] modification timer --- main.c | 80 ++++++++++++++++++++++++++-------------------------------- 1 file changed, 36 insertions(+), 44 deletions(-) diff --git a/main.c b/main.c index e73cf7f..51d401e 100644 --- a/main.c +++ b/main.c @@ -2,7 +2,6 @@ #include #include #include -#define CYCLE 10000L #define LARGEUR_FENETRE 1050 @@ -11,6 +10,38 @@ #define NBR_COLONNE 60 #define NBR_LIGNE 40 +void ConvertirMicrosecondes(unsigned long microsecondes, int *minutes, int *secondes) { + *secondes = (microsecondes % 60000000) / 1000000; + *minutes = *secondes % 60; + *secondes = *secondes - *minutes * 60; +} + +void AttendreMillisecondes(unsigned long millisecondes) { + unsigned long debut = Microsecondes(); + unsigned long attente = millisecondes * 1000; + while(Microsecondes() - debut < attente){ + } +} + +void timer(){ + const unsigned long CYCLE = 1000000L; + + unsigned long suivant = Microsecondes() + CYCLE; + + while (1) { + if (Microsecondes() > suivant) { + + int minutes, secondes; + ConvertirMicrosecondes(Microsecondes(), &minutes, &secondes); + + printf("Temps écoulé : %02d:%02d\n", minutes, secondes); + EcrireTexte(40*TAILLE_CASE,2*TAILLE_CASE,minutes,2); + EcrireTexte(40*TAILLE_CASE,5*TAILLE_CASE,secondes,2); + suivant = Microsecondes() + CYCLE; + } + + } +} typedef struct { int x; @@ -27,9 +58,6 @@ void afficherSerpent(Segment serpent[], int taille) { for (i = 0; i < 10; i++) { serpent[i].x = LARGEUR_FENETRE / 2 + i * TAILLE_CASE; serpent[i].y = HAUTEUR_FENETRE / 2; - } - - for (i = 0; i < taille; i++) { RemplirRectangle(serpent[i].x , serpent[i].y, TAILLE_CASE, TAILLE_CASE); } } @@ -104,29 +132,13 @@ void deplacerSerpent(int longueurSerpent, Segment serpent[], int direction){ ChoisirCouleurDessin(s); RemplirRectangle(serpent[i].x, serpent[i].y, TAILLE_CASE, TAILLE_CASE); } - Attendre(100); + AttendreMillisecondes(100); } - -unsigned long Microsecondes() { - return 0; -} - -void ConvertirMicrosecondes(unsigned long microsecondes, int *minutes, int *secondes) { - *minutes = microsecondes / 60000000; - *secondes = (microsecondes % 60000000) / 1000000; -} - -void AttendreMicrosecondes(unsigned long microsecondes) { - clock_t debut = clock(); - while ((clock() - debut) * 1000000 / CLOCKS_PER_SEC < microsecondes); -} - - int main() { int touche,i, j,x=0,direction=1,longueurSerpent=10; - Segment serpent[10]; + Segment* serpent = (Segment*) malloc (longueurSerpent*sizeof(Segment)); couleur c; couleur v; couleur s; @@ -141,25 +153,6 @@ int main() { EcranJeu(); afficherPastilleAleatoire(5); afficherSerpent(serpent, longueurSerpent); - - const unsigned long CYCLE = 1000000L; - - unsigned long suivant = Microsecondes() + CYCLE; - - while (1) { - if (Microsecondes() > suivant) { - - int minutes, secondes; - ConvertirMicrosecondes(Microsecondes(), &minutes, &secondes); - - printf("Temps écoulé : %02d:%02d\n", minutes, secondes); - - suivant = Microsecondes() + CYCLE; - } - - - AttendreMicrosecondes(1000); - } while(1){ @@ -187,9 +180,8 @@ int main() { } } - - deplacerSerpent(longueurSerpent, serpent, direction); - } + deplacerSerpent(longueurSerpent, serpent, direction); +}