Update SAE
This commit is contained in:
parent
a904234b67
commit
c69bc3df20
@ -14,4 +14,7 @@ graph_sup.o : graph_sup.c graph_sup.h
|
|||||||
gcc -c graph_sup.c
|
gcc -c graph_sup.c
|
||||||
|
|
||||||
clean :
|
clean :
|
||||||
rm -f *.o
|
rm -f *.o
|
||||||
|
|
||||||
|
run :
|
||||||
|
./taquin.out
|
@ -7,16 +7,16 @@
|
|||||||
|
|
||||||
#define FPS 15
|
#define FPS 15
|
||||||
|
|
||||||
//La liste des bouttons et leur nombre
|
/*La liste des bouttons et leur nombre*/
|
||||||
button* Buttons;
|
button* Buttons;
|
||||||
int BT_Count = 0;
|
int BT_Count = 0;
|
||||||
|
|
||||||
//Des variables afin de faciliter le calcul du temps entre chaque image pour int DrawNextFrame().
|
/*Des variables afin de faciliter le calcul du temps entre chaque image pour int DrawNextFrame().*/
|
||||||
double delta = (1/FPS)*1000000;
|
double delta = (1/FPS)*1000000;
|
||||||
unsigned long suivant = (1/FPS)*1000000;
|
unsigned long suivant = (1/FPS)*1000000;
|
||||||
|
|
||||||
//Un set de fonction permettant de facilement mettre en place des boutons, pouvoir les manipuler et les supprimer
|
/*Un set de fonction permettant de facilement mettre en place des boutons, pouvoir les manipuler et les supprimer
|
||||||
//Ces fonctions utilisent la structure struct Button.
|
Ces fonctions utilisent la structure struct Button.*/
|
||||||
void ClearButtons() {
|
void ClearButtons() {
|
||||||
BT_Count = 0;
|
BT_Count = 0;
|
||||||
Buttons = (button*)realloc(Buttons, sizeof(button) * 1);
|
Buttons = (button*)realloc(Buttons, sizeof(button) * 1);
|
||||||
@ -39,7 +39,7 @@ int GetButton(int x, int y) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Des fonctions appelant celles de la bibliothèque graphique afin de réduire la longueur des noms..
|
/*Des fonctions appelant celles de la bibliothèque graphique afin de réduire la longueur des noms..*/
|
||||||
couleur GetColorN(char* name) {
|
couleur GetColorN(char* name) {
|
||||||
return CouleurParNom(name);
|
return CouleurParNom(name);
|
||||||
}
|
}
|
||||||
@ -60,7 +60,7 @@ void SetColorN(char* name) {
|
|||||||
ChoisirCouleurDessin(CouleurParNom(name));
|
ChoisirCouleurDessin(CouleurParNom(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
//Permet de faire tourner la partie graphique à X images par secondes définit par la constante FPS.
|
/*Permet de faire tourner la partie graphique à X images par secondes définit par la constante FPS.*/
|
||||||
int DrawNextFrame() {
|
int DrawNextFrame() {
|
||||||
if (Microsecondes() >= suivant) {
|
if (Microsecondes() >= suivant) {
|
||||||
suivant = Microsecondes() + delta;
|
suivant = Microsecondes() + delta;
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
InitialiserGraphique();
|
InitialiserGraphique();
|
||||||
CreerFenetre((Maxx() - WIDTH) / 2, (Maxy() - HEIGHT) / 2, 1200, 700);
|
CreerFenetre((Maxx() - WIDTH) / 2, (Maxy() - HEIGHT) / 2, 1200, 700);
|
||||||
ChoisirTitreFenetre("Taquin"); /* Ben alors Denis, on nous cache des fonctions ? */
|
ChoisirTitreFenetre("Taquin");
|
||||||
while (1) {
|
while (1) {
|
||||||
if (!DrawMenu()) break;
|
if (!DrawMenu()) break;
|
||||||
};
|
};
|
||||||
|
@ -9,20 +9,21 @@
|
|||||||
void UpdateSelection(char* selection, int param) {
|
void UpdateSelection(char* selection, int param) {
|
||||||
int MenuHeight = HEIGHT - 120, i;
|
int MenuHeight = HEIGHT - 120, i;
|
||||||
if (selection == "lines") {
|
if (selection == "lines") {
|
||||||
SetColor(0, 0, 0);
|
char str[30];
|
||||||
for (i = 0; i <= 5; i++) DessinerRectangle(WIDTH - 200, 75 + i * 70, 50, 50); //Lignes
|
for (i = 3; i <= 8; i++) {
|
||||||
SetColor(255, 0, 0);
|
sprintf(str, "./images/assets/sel_%d.png", i);
|
||||||
DessinerRectangle(WIDTH - 200, 75 + (param-3) * 70, 50, 50);
|
ChargerImage(str, 1001, 88 + 81 * (i-3), 0, 0, 61, 61);
|
||||||
|
}
|
||||||
|
ChargerImage("./images/assets/sel_halo.png", 1001, 88 + 81 * (param-3), 0, 0, 61, 61);
|
||||||
} else if (selection == "columns") {
|
} else if (selection == "columns") {
|
||||||
SetColor(0, 0, 0);
|
char str[30];
|
||||||
for (i = 0; i <= 5; i++) DessinerRectangle(WIDTH/2 + 150, 75 + i * 70, 50, 50); //Colonnes
|
for (i = 3; i <= 8; i++) {
|
||||||
SetColor(255, 0, 0);
|
sprintf(str, "./images/assets/sel_%d.png", i);
|
||||||
DessinerRectangle(WIDTH/2 + 150, 75 + (param-3) * 70, 50, 50);
|
ChargerImage(str, 737, 88 + 81 * (i-3), 0, 0, 61, 61);
|
||||||
|
}
|
||||||
|
ChargerImage("./images/assets/sel_halo.png", 737, 88 + 81 * (param-3), 0, 0, 61, 61);
|
||||||
} else if (selection == "image"){
|
} else if (selection == "image"){
|
||||||
SetColor(0, 0, 0);
|
ChargerImage("./images/assets/img_sel.png", 0, MenuHeight/3 * param, 0, 0, 603, 198);
|
||||||
for (i=0; i<3; i++) DessinerRectangle(0, MenuHeight/3 * i, WIDTH/2, MenuHeight/3);
|
|
||||||
SetColor(255,0,0);
|
|
||||||
DessinerRectangle(0, MenuHeight/3 * param, WIDTH/2, MenuHeight/3);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -32,46 +33,26 @@ int DrawMenu() {
|
|||||||
|
|
||||||
int MenuHeight = HEIGHT - 120;
|
int MenuHeight = HEIGHT - 120;
|
||||||
int Lines = 3, Columns = 3, i;
|
int Lines = 3, Columns = 3, i;
|
||||||
int X_image, Y_image;
|
int X_image = 0, Y_image = 0;
|
||||||
|
|
||||||
char filename[100];
|
char filename[100];
|
||||||
|
|
||||||
SetColor(0, 0, 0);
|
|
||||||
char str[3];
|
char str[3];
|
||||||
EcrireTexte(WIDTH/2 +120, 50 , "Colonnes", 2);
|
|
||||||
EcrireTexte(WIDTH - 220, 50 , "Lignes", 2);
|
|
||||||
for (i = 0; i <= 5; i++) {
|
for (i = 0; i <= 5; i++) {
|
||||||
AddButton(WIDTH/2 + 150, 75 + i * 70, 50, 50, 10 + i); //Colonnes
|
AddButton(737, 88 + 81 * i, 61, 61, 10 + i); //Colonnes
|
||||||
AddButton(WIDTH - 200, 75 + i * 70, 50, 50, 20 + i); //Lignes
|
AddButton(1001, 88 + 81 * i, 61, 61, 20 + i); //Lignes
|
||||||
|
|
||||||
sprintf(str, "%d", i+3);
|
|
||||||
EcrireTexte(WIDTH/2 + 175, 100 + i * 70, str, 2);
|
|
||||||
EcrireTexte(WIDTH - 175, 100 + i * 70, str, 2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DessinerRectangle(WIDTH/4 *3 - 100, HEIGHT - 100, 200, 80);
|
ChargerImage("./images/assets/menu_background.png", 0, 0, 0, 0, WIDTH, HEIGHT);
|
||||||
AddButton(WIDTH/4 *3 - 100, HEIGHT - 100, 200, 80, 1);
|
AddButton(WIDTH/4 * 3 - 100, HEIGHT - 100, 200, 80, 1); //Lancer le Taquin
|
||||||
EcrireTexte(WIDTH/4 *3 - 75 , HEIGHT - 50, "Start taquin", 2);
|
AddButton(WIDTH/4 - 100, HEIGHT -100, 200, 80, 2); //Quitter
|
||||||
|
|
||||||
DessinerRectangle(WIDTH/4 -100, HEIGHT - 100, 200, 80);
|
|
||||||
AddButton(WIDTH/4 - 100, HEIGHT -100, 200, 80, 2);
|
|
||||||
EcrireTexte(WIDTH/4 - 90, HEIGHT - 50, "Quitter menu", 2);
|
|
||||||
|
|
||||||
UpdateSelection("lines", Lines);
|
UpdateSelection("lines", Lines);
|
||||||
UpdateSelection("columns", Columns);
|
UpdateSelection("columns", Columns);
|
||||||
|
|
||||||
SetColor(0, 0, 0);
|
AddButton(0, 0, WIDTH/2, MenuHeight/3, 3); //Luna
|
||||||
DessinerRectangle(0, 0, WIDTH/2, MenuHeight/3);
|
AddButton(0, MenuHeight/3, WIDTH/2, MenuHeight/3, 4); //Zanitsu
|
||||||
AddButton(0, 0, WIDTH/2, MenuHeight/3, 3);
|
AddButton(0, MenuHeight/3 *2, WIDTH/2, MenuHeight/3, 5); //Bonsaï
|
||||||
|
|
||||||
DessinerRectangle(0, MenuHeight/3, WIDTH/2, MenuHeight/3);
|
|
||||||
AddButton(0, MenuHeight/3, WIDTH/2, MenuHeight/3, 4);
|
|
||||||
|
|
||||||
DessinerRectangle(0, MenuHeight/3 * 2, WIDTH/2, MenuHeight/3);
|
|
||||||
AddButton(0, MenuHeight/3 *2, WIDTH/2, MenuHeight/3, 5);
|
|
||||||
|
|
||||||
DessinerSegment(WIDTH/2, 0, WIDTH/2, MenuHeight);
|
|
||||||
DessinerRectangle(0, MenuHeight, WIDTH, HEIGHT - MenuHeight);
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
if (DrawNextFrame()) {
|
if (DrawNextFrame()) {
|
||||||
@ -84,30 +65,34 @@ int DrawMenu() {
|
|||||||
} else if (BT_ID >= 20 && BT_ID <= 25) {
|
} else if (BT_ID >= 20 && BT_ID <= 25) {
|
||||||
Lines = BT_ID - 17;
|
Lines = BT_ID - 17;
|
||||||
UpdateSelection("lines", Lines);
|
UpdateSelection("lines", Lines);
|
||||||
} else if (BT_ID >= 3 && BT_ID <= 5){
|
} else if (BT_ID >= 3 && BT_ID <= 5){
|
||||||
|
ChargerImage("./images/assets/menu_background.png", 0, 0, 0, 0, WIDTH, HEIGHT);
|
||||||
|
UpdateSelection("lines", Lines);
|
||||||
|
UpdateSelection("columns", Columns);
|
||||||
UpdateSelection("image", BT_ID - 3);
|
UpdateSelection("image", BT_ID - 3);
|
||||||
if (BT_ID == 3){
|
|
||||||
X_image = 500;
|
if (BT_ID == 3){
|
||||||
Y_image = 445;
|
X_image = 500;
|
||||||
strcpy(filename, "./images/luna.png");
|
Y_image = 445;
|
||||||
} else if (BT_ID == 4){
|
strcpy(filename, "./images/luna.png");
|
||||||
X_image = 300;
|
} else if (BT_ID == 4){
|
||||||
Y_image = 168;
|
X_image = 450;
|
||||||
strcpy(filename, "./images/zenitsu.jpg");
|
Y_image = 311;
|
||||||
} else if (BT_ID == 5){
|
strcpy(filename, "./images/zenitsu.png");
|
||||||
X_image = 400;
|
} else if (BT_ID == 5){
|
||||||
Y_image = 400;
|
X_image = 400;
|
||||||
strcpy(filename, "./images/banzai.png");
|
Y_image = 400;
|
||||||
}
|
strcpy(filename, "./images/bonsai.png");
|
||||||
} else {
|
}
|
||||||
if (BT_ID == 1) {
|
} else {
|
||||||
|
if (BT_ID == 1 && X_image != 0) {
|
||||||
ClearButtons();
|
ClearButtons();
|
||||||
EffacerEcran(GetColor(255, 255, 255));
|
EffacerEcran(GetColor(255, 255, 255));
|
||||||
CreateTaquin(filename, X_image, Y_image, Lines, Columns);
|
CreateTaquin(filename, X_image, Y_image, Lines, Columns);
|
||||||
return 1;
|
return 1;
|
||||||
} else if (BT_ID == 2) {
|
} else if (BT_ID == 2) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,10 +17,12 @@ int OffsetX, OffsetY;
|
|||||||
int ImageX, ImageY, PieceX, PieceY;
|
int ImageX, ImageY, PieceX, PieceY;
|
||||||
|
|
||||||
int Lines, Columns;
|
int Lines, Columns;
|
||||||
|
int MoveCount;
|
||||||
unsigned char* Taquin;
|
unsigned char* Taquin;
|
||||||
|
|
||||||
char Filename[100];
|
char Filename[100];
|
||||||
|
|
||||||
|
/* Dessine la partie de l'image correspondante à l'indice donné */
|
||||||
void DrawPiece(int index, couleur color) {
|
void DrawPiece(int index, couleur color) {
|
||||||
int CoordX = (index % Columns) * PieceX;
|
int CoordX = (index % Columns) * PieceX;
|
||||||
int CoordY = (index / Columns) * PieceY;
|
int CoordY = (index / Columns) * PieceY;
|
||||||
@ -64,6 +66,14 @@ int MovePiece(int index, int should_update) {
|
|||||||
if (should_update) {
|
if (should_update) {
|
||||||
DrawPiece(moves[i], black);
|
DrawPiece(moves[i], black);
|
||||||
DrawPiece(index, selected);
|
DrawPiece(index, selected);
|
||||||
|
|
||||||
|
ChargerImage("./images/assets/move_count.png", 497, 600, 0, 0, 205, 85);
|
||||||
|
SetColor(192, 205, 228);
|
||||||
|
|
||||||
|
char str[5];
|
||||||
|
sprintf(str, "%d", MoveCount);
|
||||||
|
EcrireTexte(620, 650, str, 2);
|
||||||
|
MoveCount++;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -73,6 +83,49 @@ int MovePiece(int index, int should_update) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Randomise le Taquin */
|
||||||
|
void RandomizeTaquin() {
|
||||||
|
int index = 0, last_move = -1, move, i;
|
||||||
|
int moves[4] = {1, -1, Columns, -Columns};
|
||||||
|
int piece_count = Lines * Columns;
|
||||||
|
/* L'aléatoire n'est pas idéal, afin de mieux exploiter
|
||||||
|
la fonction, on devrait éviter de faire des coups inutiles
|
||||||
|
du genre essayer de sortir du Taquin mais elle marche
|
||||||
|
relativement bien au delà de ces problèmes. */
|
||||||
|
MoveCount = 0;
|
||||||
|
|
||||||
|
for (i = 0; i < RANDOM_ITER; i++) {
|
||||||
|
srand(Microsecondes());
|
||||||
|
move = rand() % 4;
|
||||||
|
if (move != last_move) {
|
||||||
|
move = moves[move];
|
||||||
|
if (index + move >= 0 && index + move < piece_count) {
|
||||||
|
MovePiece(index, 0);
|
||||||
|
index += move;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ChargerImage("./images/assets/move_count.png", 497, 600, 0, 0, 205, 85);
|
||||||
|
SetColor(192, 205, 228);
|
||||||
|
|
||||||
|
char str[5];
|
||||||
|
sprintf(str, "%d", MoveCount);
|
||||||
|
EcrireTexte(620, 650, str, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Renvoie 1 si le taquin est en ordre */
|
||||||
|
int CheckForVictory() {
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < Lines * Columns; i++) {
|
||||||
|
if (Taquin[i] != i) return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Bloque le programme pendant la partie, appelle les fonctions liées au coups
|
||||||
|
et aussi celles afin de randomiser et quitter le taquin. */
|
||||||
int TaquinRenderLoop() {
|
int TaquinRenderLoop() {
|
||||||
int button_id = -1, last_id = -1;
|
int button_id = -1, last_id = -1;
|
||||||
int controller = 0, key;
|
int controller = 0, key;
|
||||||
@ -83,8 +136,14 @@ int TaquinRenderLoop() {
|
|||||||
if (SourisCliquee()) {
|
if (SourisCliquee()) {
|
||||||
controller = 0;
|
controller = 0;
|
||||||
button_id = GetButton(_X, _Y);
|
button_id = GetButton(_X, _Y);
|
||||||
if (button_id >= 0) MovePiece(button_id, 1);
|
if (button_id >= 0) {
|
||||||
else if (button_id == -3) printf("RandomizeTaquin()");
|
MovePiece(button_id, 1);
|
||||||
|
if (CheckForVictory()) return 1;
|
||||||
|
}
|
||||||
|
else if (button_id == -3) {
|
||||||
|
RandomizeTaquin();
|
||||||
|
UpdateTaquin();
|
||||||
|
}
|
||||||
else if (button_id == -2) return 0;
|
else if (button_id == -2) return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,6 +175,7 @@ int TaquinRenderLoop() {
|
|||||||
}
|
}
|
||||||
} else if (key == XK_space) {
|
} else if (key == XK_space) {
|
||||||
MovePiece(button_id, 1);
|
MovePiece(button_id, 1);
|
||||||
|
if (CheckForVictory()) return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (button_id >= 0) DrawPiece(button_id, selected);
|
if (button_id >= 0) DrawPiece(button_id, selected);
|
||||||
@ -138,32 +198,31 @@ int TaquinRenderLoop() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RandomizeTaquin() {
|
/* L'écran de victoire */
|
||||||
int index = 0, last_move = -1, move, i;
|
void ShowVictoryScreen() {
|
||||||
int moves[4] = {1, -1, Columns, -Columns};
|
ClearButtons();
|
||||||
int piece_count = Lines * Columns;
|
AddButton(WIDTH/4 + 60, HEIGHT/4 + 250, 230, 78, 1);
|
||||||
/* L'aléatoire n'est pas idéal, afin de mieux exploiter
|
AddButton(WIDTH/4 + 312, HEIGHT/4 + 250, 230, 78, 2);
|
||||||
la fonction, on devrait éviter de faire des coups inutiles
|
|
||||||
du genre essayer de sortir du Taquin mais elle marche
|
|
||||||
relativement bien au delà de ces problèmes. */
|
|
||||||
|
|
||||||
for (i = 0; i < RANDOM_ITER; i++) {
|
int BT_ID;
|
||||||
srand(Microsecondes());
|
|
||||||
move = rand() % 4;
|
while (1) {
|
||||||
if (move != last_move) {
|
if (DrawNextFrame()) {
|
||||||
move = moves[move];
|
ChargerImage("./images/assets/win_screen.png", WIDTH/4, HEIGHT/4, 0, 0, WIDTH/2, HEIGHT/2);
|
||||||
if (index + move >= 0 && index + move < piece_count) {
|
|
||||||
MovePiece(index, 0);
|
if (SourisCliquee()) {
|
||||||
index += move;
|
BT_ID = GetButton(_X, _Y);
|
||||||
|
if (BT_ID == 1) {
|
||||||
|
return;
|
||||||
|
} else if (BT_ID == 2) {
|
||||||
|
CreateTaquin(Filename, ImageX, ImageY, Lines, Columns);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ShowVictoryScreen() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Crée le Taquin et lance les fonctions liées à la logique et au graphisme du Taquin */
|
/* Crée le Taquin et lance les fonctions liées à la logique et au graphisme du Taquin */
|
||||||
void CreateTaquin(char * filename, int ImageWidth, int ImageHeight, int lines, int columns) {
|
void CreateTaquin(char * filename, int ImageWidth, int ImageHeight, int lines, int columns) {
|
||||||
/* On externalise les valeurs données afin de pouvoir facilement les utiliser plus tard. */
|
/* On externalise les valeurs données afin de pouvoir facilement les utiliser plus tard. */
|
||||||
@ -189,20 +248,11 @@ void CreateTaquin(char * filename, int ImageWidth, int ImageHeight, int lines, i
|
|||||||
Taquin[i] = i;
|
Taquin[i] = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
SetColor(0, 0, 0);
|
ChargerImage("./images/assets/background_taquin.png", 0, 0, 0, 0, 1200, 700);
|
||||||
RemplirRectangle(48, OffsetY - 2, ImageX + 4, ImageY + 4);
|
|
||||||
RemplirRectangle(OffsetX - 1, OffsetY - 1, ImageX + 2, ImageY + 1);
|
|
||||||
ChargerImage(Filename, 50, OffsetY, 0, 0, ImageX, ImageY);
|
ChargerImage(Filename, 50, OffsetY, 0, 0, ImageX, ImageY);
|
||||||
|
|
||||||
AddButton(75, HEIGHT - 100, 250, 60, -2); //Bouton Quitter
|
AddButton(WIDTH/4 - 100, HEIGHT -100, 200, 80, -2); //Bouton Quitter
|
||||||
EcrireTexte(160, HEIGHT -60, "Menu", 2);
|
AddButton(WIDTH/4 * 3 - 100, HEIGHT - 100, 200, 80, -3); //Bouton Randomiser
|
||||||
|
|
||||||
AddButton(WIDTH - 325, HEIGHT - 100, 250, 60, -3); //Bouton rejouer
|
|
||||||
EcrireTexte(WIDTH - 300, HEIGHT - 60, "Recommencer", 2);
|
|
||||||
|
|
||||||
SetColor(0, 0, 0);
|
|
||||||
DessinerRectangle(75, HEIGHT - 100, 250, 60);
|
|
||||||
DessinerRectangle(WIDTH - 325, HEIGHT - 100, 250, 60);
|
|
||||||
|
|
||||||
RandomizeTaquin();
|
RandomizeTaquin();
|
||||||
UpdateTaquin();
|
UpdateTaquin();
|
||||||
|
Loading…
Reference in New Issue
Block a user