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
|
||||
|
||||
clean :
|
||||
rm -f *.o
|
||||
rm -f *.o
|
||||
|
||||
run :
|
||||
./taquin.out
|
@ -7,16 +7,16 @@
|
||||
|
||||
#define FPS 15
|
||||
|
||||
//La liste des bouttons et leur nombre
|
||||
/*La liste des bouttons et leur nombre*/
|
||||
button* Buttons;
|
||||
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;
|
||||
unsigned long suivant = (1/FPS)*1000000;
|
||||
|
||||
//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.
|
||||
/*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.*/
|
||||
void ClearButtons() {
|
||||
BT_Count = 0;
|
||||
Buttons = (button*)realloc(Buttons, sizeof(button) * 1);
|
||||
@ -39,7 +39,7 @@ int GetButton(int x, int y) {
|
||||
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) {
|
||||
return CouleurParNom(name);
|
||||
}
|
||||
@ -60,7 +60,7 @@ void SetColorN(char* 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() {
|
||||
if (Microsecondes() >= suivant) {
|
||||
suivant = Microsecondes() + delta;
|
||||
|
@ -8,7 +8,7 @@
|
||||
int main(int argc, char* argv[]) {
|
||||
InitialiserGraphique();
|
||||
CreerFenetre((Maxx() - WIDTH) / 2, (Maxy() - HEIGHT) / 2, 1200, 700);
|
||||
ChoisirTitreFenetre("Taquin"); /* Ben alors Denis, on nous cache des fonctions ? */
|
||||
ChoisirTitreFenetre("Taquin");
|
||||
while (1) {
|
||||
if (!DrawMenu()) break;
|
||||
};
|
||||
|
@ -9,20 +9,21 @@
|
||||
void UpdateSelection(char* selection, int param) {
|
||||
int MenuHeight = HEIGHT - 120, i;
|
||||
if (selection == "lines") {
|
||||
SetColor(0, 0, 0);
|
||||
for (i = 0; i <= 5; i++) DessinerRectangle(WIDTH - 200, 75 + i * 70, 50, 50); //Lignes
|
||||
SetColor(255, 0, 0);
|
||||
DessinerRectangle(WIDTH - 200, 75 + (param-3) * 70, 50, 50);
|
||||
char str[30];
|
||||
for (i = 3; i <= 8; i++) {
|
||||
sprintf(str, "./images/assets/sel_%d.png", i);
|
||||
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") {
|
||||
SetColor(0, 0, 0);
|
||||
for (i = 0; i <= 5; i++) DessinerRectangle(WIDTH/2 + 150, 75 + i * 70, 50, 50); //Colonnes
|
||||
SetColor(255, 0, 0);
|
||||
DessinerRectangle(WIDTH/2 + 150, 75 + (param-3) * 70, 50, 50);
|
||||
char str[30];
|
||||
for (i = 3; i <= 8; i++) {
|
||||
sprintf(str, "./images/assets/sel_%d.png", i);
|
||||
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"){
|
||||
SetColor(0, 0, 0);
|
||||
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);
|
||||
ChargerImage("./images/assets/img_sel.png", 0, MenuHeight/3 * param, 0, 0, 603, 198);
|
||||
}
|
||||
};
|
||||
|
||||
@ -32,46 +33,26 @@ int DrawMenu() {
|
||||
|
||||
int MenuHeight = HEIGHT - 120;
|
||||
int Lines = 3, Columns = 3, i;
|
||||
int X_image, Y_image;
|
||||
int X_image = 0, Y_image = 0;
|
||||
|
||||
char filename[100];
|
||||
|
||||
SetColor(0, 0, 0);
|
||||
char str[3];
|
||||
EcrireTexte(WIDTH/2 +120, 50 , "Colonnes", 2);
|
||||
EcrireTexte(WIDTH - 220, 50 , "Lignes", 2);
|
||||
for (i = 0; i <= 5; i++) {
|
||||
AddButton(WIDTH/2 + 150, 75 + i * 70, 50, 50, 10 + i); //Colonnes
|
||||
AddButton(WIDTH - 200, 75 + i * 70, 50, 50, 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);
|
||||
AddButton(737, 88 + 81 * i, 61, 61, 10 + i); //Colonnes
|
||||
AddButton(1001, 88 + 81 * i, 61, 61, 20 + i); //Lignes
|
||||
}
|
||||
|
||||
DessinerRectangle(WIDTH/4 *3 - 100, HEIGHT - 100, 200, 80);
|
||||
AddButton(WIDTH/4 *3 - 100, HEIGHT - 100, 200, 80, 1);
|
||||
EcrireTexte(WIDTH/4 *3 - 75 , HEIGHT - 50, "Start taquin", 2);
|
||||
|
||||
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);
|
||||
ChargerImage("./images/assets/menu_background.png", 0, 0, 0, 0, WIDTH, HEIGHT);
|
||||
AddButton(WIDTH/4 * 3 - 100, HEIGHT - 100, 200, 80, 1); //Lancer le Taquin
|
||||
AddButton(WIDTH/4 - 100, HEIGHT -100, 200, 80, 2); //Quitter
|
||||
|
||||
UpdateSelection("lines", Lines);
|
||||
UpdateSelection("columns", Columns);
|
||||
|
||||
SetColor(0, 0, 0);
|
||||
DessinerRectangle(0, 0, WIDTH/2, MenuHeight/3);
|
||||
AddButton(0, 0, WIDTH/2, MenuHeight/3, 3);
|
||||
|
||||
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);
|
||||
AddButton(0, 0, WIDTH/2, MenuHeight/3, 3); //Luna
|
||||
AddButton(0, MenuHeight/3, WIDTH/2, MenuHeight/3, 4); //Zanitsu
|
||||
AddButton(0, MenuHeight/3 *2, WIDTH/2, MenuHeight/3, 5); //Bonsaï
|
||||
|
||||
while (1) {
|
||||
if (DrawNextFrame()) {
|
||||
@ -84,30 +65,34 @@ int DrawMenu() {
|
||||
} else if (BT_ID >= 20 && BT_ID <= 25) {
|
||||
Lines = BT_ID - 17;
|
||||
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);
|
||||
if (BT_ID == 3){
|
||||
X_image = 500;
|
||||
Y_image = 445;
|
||||
strcpy(filename, "./images/luna.png");
|
||||
} else if (BT_ID == 4){
|
||||
X_image = 300;
|
||||
Y_image = 168;
|
||||
strcpy(filename, "./images/zenitsu.jpg");
|
||||
} else if (BT_ID == 5){
|
||||
X_image = 400;
|
||||
Y_image = 400;
|
||||
strcpy(filename, "./images/banzai.png");
|
||||
}
|
||||
} else {
|
||||
if (BT_ID == 1) {
|
||||
|
||||
if (BT_ID == 3){
|
||||
X_image = 500;
|
||||
Y_image = 445;
|
||||
strcpy(filename, "./images/luna.png");
|
||||
} else if (BT_ID == 4){
|
||||
X_image = 450;
|
||||
Y_image = 311;
|
||||
strcpy(filename, "./images/zenitsu.png");
|
||||
} else if (BT_ID == 5){
|
||||
X_image = 400;
|
||||
Y_image = 400;
|
||||
strcpy(filename, "./images/bonsai.png");
|
||||
}
|
||||
} else {
|
||||
if (BT_ID == 1 && X_image != 0) {
|
||||
ClearButtons();
|
||||
EffacerEcran(GetColor(255, 255, 255));
|
||||
CreateTaquin(filename, X_image, Y_image, Lines, Columns);
|
||||
return 1;
|
||||
return 1;
|
||||
} else if (BT_ID == 2) {
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,10 +17,12 @@ int OffsetX, OffsetY;
|
||||
int ImageX, ImageY, PieceX, PieceY;
|
||||
|
||||
int Lines, Columns;
|
||||
int MoveCount;
|
||||
unsigned char* Taquin;
|
||||
|
||||
char Filename[100];
|
||||
|
||||
/* Dessine la partie de l'image correspondante à l'indice donné */
|
||||
void DrawPiece(int index, couleur color) {
|
||||
int CoordX = (index % Columns) * PieceX;
|
||||
int CoordY = (index / Columns) * PieceY;
|
||||
@ -64,6 +66,14 @@ int MovePiece(int index, int should_update) {
|
||||
if (should_update) {
|
||||
DrawPiece(moves[i], black);
|
||||
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;
|
||||
}
|
||||
@ -73,6 +83,49 @@ int MovePiece(int index, int should_update) {
|
||||
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 button_id = -1, last_id = -1;
|
||||
int controller = 0, key;
|
||||
@ -83,8 +136,14 @@ int TaquinRenderLoop() {
|
||||
if (SourisCliquee()) {
|
||||
controller = 0;
|
||||
button_id = GetButton(_X, _Y);
|
||||
if (button_id >= 0) MovePiece(button_id, 1);
|
||||
else if (button_id == -3) printf("RandomizeTaquin()");
|
||||
if (button_id >= 0) {
|
||||
MovePiece(button_id, 1);
|
||||
if (CheckForVictory()) return 1;
|
||||
}
|
||||
else if (button_id == -3) {
|
||||
RandomizeTaquin();
|
||||
UpdateTaquin();
|
||||
}
|
||||
else if (button_id == -2) return 0;
|
||||
}
|
||||
|
||||
@ -116,6 +175,7 @@ int TaquinRenderLoop() {
|
||||
}
|
||||
} else if (key == XK_space) {
|
||||
MovePiece(button_id, 1);
|
||||
if (CheckForVictory()) return 1;
|
||||
}
|
||||
|
||||
if (button_id >= 0) DrawPiece(button_id, selected);
|
||||
@ -138,32 +198,31 @@ int TaquinRenderLoop() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
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. */
|
||||
/* L'écran de victoire */
|
||||
void ShowVictoryScreen() {
|
||||
ClearButtons();
|
||||
AddButton(WIDTH/4 + 60, HEIGHT/4 + 250, 230, 78, 1);
|
||||
AddButton(WIDTH/4 + 312, HEIGHT/4 + 250, 230, 78, 2);
|
||||
|
||||
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;
|
||||
int BT_ID;
|
||||
|
||||
while (1) {
|
||||
if (DrawNextFrame()) {
|
||||
ChargerImage("./images/assets/win_screen.png", WIDTH/4, HEIGHT/4, 0, 0, WIDTH/2, HEIGHT/2);
|
||||
|
||||
if (SourisCliquee()) {
|
||||
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 */
|
||||
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. */
|
||||
@ -189,20 +248,11 @@ void CreateTaquin(char * filename, int ImageWidth, int ImageHeight, int lines, i
|
||||
Taquin[i] = i;
|
||||
}
|
||||
|
||||
SetColor(0, 0, 0);
|
||||
RemplirRectangle(48, OffsetY - 2, ImageX + 4, ImageY + 4);
|
||||
RemplirRectangle(OffsetX - 1, OffsetY - 1, ImageX + 2, ImageY + 1);
|
||||
ChargerImage("./images/assets/background_taquin.png", 0, 0, 0, 0, 1200, 700);
|
||||
ChargerImage(Filename, 50, OffsetY, 0, 0, ImageX, ImageY);
|
||||
|
||||
AddButton(75, HEIGHT - 100, 250, 60, -2); //Bouton Quitter
|
||||
EcrireTexte(160, HEIGHT -60, "Menu", 2);
|
||||
|
||||
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);
|
||||
AddButton(WIDTH/4 - 100, HEIGHT -100, 200, 80, -2); //Bouton Quitter
|
||||
AddButton(WIDTH/4 * 3 - 100, HEIGHT - 100, 200, 80, -3); //Bouton Randomiser
|
||||
|
||||
RandomizeTaquin();
|
||||
UpdateTaquin();
|
||||
|
Loading…
Reference in New Issue
Block a user