Update SAE
This commit is contained in:
parent
fb76f7bd0f
commit
2221d2b0f7
@ -9,6 +9,10 @@
|
||||
/* La marge en pixel entre chaque pièce du Taquin */
|
||||
#define MARGIN 1
|
||||
|
||||
/* Définit le nombre d'itérations de la fonction random à faire
|
||||
afin de randomiser le taquin */
|
||||
#define RANDOM_ITER 50000
|
||||
|
||||
int OffsetX, OffsetY;
|
||||
int ImageX, ImageY, PieceX, PieceY;
|
||||
|
||||
@ -24,7 +28,11 @@ void DrawPiece(int index, couleur color) {
|
||||
int ImCoordX = (Taquin[index] % Columns) * PieceX;
|
||||
int ImCoordY = (Taquin[index] / Columns) * PieceY;
|
||||
|
||||
ChargerImage(Filename, CoordX + OffsetX, CoordY + OffsetY, ImCoordX, ImCoordY, PieceX, PieceY);
|
||||
if (Taquin[index] != 0) ChargerImage(Filename, CoordX + OffsetX, CoordY + OffsetY, ImCoordX, ImCoordY, PieceX, PieceY);
|
||||
else {
|
||||
SetColor(255, 255, 255);
|
||||
RemplirRectangle(CoordX + OffsetX, CoordY + OffsetY, PieceX, PieceY);
|
||||
}
|
||||
SetColorC(color);
|
||||
DessinerRectangle(CoordX + OffsetX, CoordY + OffsetY, PieceX - 1, PieceY - 1);
|
||||
}
|
||||
@ -46,7 +54,7 @@ int MovePiece(int index, int should_update) {
|
||||
};
|
||||
|
||||
int i;
|
||||
couleur color = GetColor(0, 0, 0);
|
||||
couleur selected = GetColor(255, 0, 0), black = GetColor(0, 0, 0);
|
||||
for (i = 0; i < 4; i++) {
|
||||
if (moves[i] >= 0 && moves[i] < Columns * Lines) {
|
||||
if (Taquin[moves[i]] == 0) {
|
||||
@ -54,8 +62,8 @@ int MovePiece(int index, int should_update) {
|
||||
Taquin[index] = 0;
|
||||
|
||||
if (should_update) {
|
||||
DrawPiece(moves[i], color);
|
||||
DrawPiece(index, color);
|
||||
DrawPiece(moves[i], black);
|
||||
DrawPiece(index, selected);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@ -65,6 +73,91 @@ int MovePiece(int index, int should_update) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int TaquinRenderLoop() {
|
||||
int button_id = -1, last_id = -1;
|
||||
int controller = 0, key;
|
||||
|
||||
couleur selected = GetColor(255, 0, 0), black = GetColor(0, 0, 0);
|
||||
|
||||
while (1) {
|
||||
if (SourisCliquee()) {
|
||||
controller = 0;
|
||||
button_id = GetButton(_X, _Y);
|
||||
if (button_id >= 0) MovePiece(button_id, 1);
|
||||
}
|
||||
|
||||
if (ToucheEnAttente()) {
|
||||
controller = 1;
|
||||
|
||||
key = Touche();
|
||||
if (button_id < 0) button_id = 0;
|
||||
|
||||
if (key == XK_Left || key == XK_q) {
|
||||
if (button_id - 1 >= 0) {
|
||||
last_id = button_id;
|
||||
button_id -= 1;
|
||||
}
|
||||
} else if (key == XK_Right || key == XK_d) {
|
||||
if (button_id + 1 < Columns * Lines) {
|
||||
last_id = button_id;
|
||||
button_id += 1;
|
||||
}
|
||||
} else if (key == XK_Up || key == XK_z) {
|
||||
if (button_id - Columns >= 0) {
|
||||
last_id = button_id;
|
||||
button_id -= Columns;
|
||||
}
|
||||
} else if (key == XK_Down || key == XK_s) {
|
||||
if (button_id + Columns < Lines * Columns) {
|
||||
last_id = button_id;
|
||||
button_id += Columns;
|
||||
}
|
||||
} else if (key == XK_space) {
|
||||
MovePiece(button_id, 1);
|
||||
}
|
||||
|
||||
if (button_id >= 0) DrawPiece(button_id, selected);
|
||||
if (last_id >= 0) DrawPiece(last_id, black);
|
||||
}
|
||||
|
||||
if (DrawNextFrame()) {
|
||||
if (controller == 0) {
|
||||
SourisPosition();
|
||||
button_id = GetButton(_X, _Y);
|
||||
if (button_id != last_id) {
|
||||
if (button_id != -1) DrawPiece(button_id, selected);
|
||||
if (last_id != -1) DrawPiece(last_id, black);
|
||||
last_id = button_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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. */
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Crée le Taquin et lance les fonctions liées à la logique et au graphisme du Taquin */
|
||||
void CreateTaquin(char * filename, int image_width, int image_height, int lines, int columns) {
|
||||
/* On externalise les valeurs données afin de pouvoir facilement les utiliser plus tard. */
|
||||
@ -82,17 +175,22 @@ void CreateTaquin(char * filename, int image_width, int image_height, int lines,
|
||||
OffsetX = WIDTH - 50 - ImageX;
|
||||
OffsetY = (HEIGHT - ImageY) / 2 - 50;
|
||||
|
||||
/* On alloue l'espace mémoire nécéssaire au Taquin et on l'initialise en premier résolu. */
|
||||
/* On alloue l'espace mémoire nécéssaire au Taquin et on l'initialise en premier résolu.
|
||||
On ajoute aussi les boutons servant à vérifier les clics souris.*/
|
||||
Taquin = calloc(Lines * Columns, sizeof(unsigned char));
|
||||
for (i = 0; i < Lines * Columns; i++) Taquin[i] = i;
|
||||
for (i = 0; i < Lines * Columns; i++) {
|
||||
AddButton(OffsetX + (i % Columns) * PieceX, OffsetY +(i / Columns) * PieceY, PieceX, PieceY, 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(Filename, 50, OffsetY, 0, 0, ImageX, ImageY);
|
||||
|
||||
/*RandomizeTaquin()*/
|
||||
RandomizeTaquin();
|
||||
UpdateTaquin();
|
||||
/*TaquinRenderLoop()*/
|
||||
/*ShowVictoryScreen()*/
|
||||
if (TaquinRenderLoop()) {
|
||||
/*ShowVictoryScreen()*/
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user