APL/APL1.1/SAE11_2021/lib_taquin/taquin.c
2021-11-26 12:14:37 +01:00

92 lines
2.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <graph.h>
#include <string.h>
#include "utils.h"
#include "taquin.h"
#include "../graph_sup.h"
#define OFFSET_X 650
#define OFFSET_Y 100
//int ImageX, ImageY;
int Rows, Columns;
char TaquinFilename[500];
int ImageX = 500;
int ImageY = 445;
int SizeX, SizeY;
//Met à jour le taquin à chaque mouvement.
void UpdateTaquin(char* filename, int** Taquin, int rows, int columns) {
int x, y, i_x, i_y, id;
int i = 0;
char str_id[20];
for (int cur_row = 0; cur_row < columns; cur_row++) {
for (int cur_col = 0; cur_col < rows; cur_col++) {
id = Taquin[cur_row][cur_col];
if (id != 0) {
x = i % columns;
y = i / columns;
i_x = id % columns;
i_y = id / columns;
AddButton(OFFSET_X + SizeX*x, OFFSET_Y + SizeY*y + (500-ImageY)/2, SizeX, SizeY, id);
ChargerImage(filename, OFFSET_X + SizeX*x + 1, OFFSET_Y + SizeY*y + (500-ImageY)/2 + 1, SizeX*i_x+1, SizeY*i_y+1, SizeX-2, SizeY-2);
}
i++;
}
}
}
void UpdatePiece(char* filename, int x, int y, couleur color) {
ChoisirCouleurDessin(color);
RemplirRectangle(OFFSET_X + SizeX * x - 1, OFFSET_Y + SizeY * y + (500-ImageY)/2 - 1, SizeX + 2, SizeY + 2);
ChargerImage(filename, OFFSET_X + SizeX*x + 1, OFFSET_Y + SizeY*y + (500-ImageY)/2 + 1, SizeX*x+1, SizeY*y+1, SizeX-2, SizeY-2);
}
//Initialise le Taquin, la fonction appelle la création graphique et logique du Taquin.
void CreateTaquin(char * filename, int rows, int columns) {
clearButtons();
Rows = rows;
Columns = columns;
strcpy(TaquinFilename, filename);
SizeX = ImageX/columns;
SizeY = ImageY/rows;
int ** Taquin;
Taquin = calloc(columns, sizeof(int*));
for (int i = 0; i < columns; i++) {
Taquin[i] = calloc(rows, sizeof(int));
}
for (int i = 0; i < rows * columns; i++) {
int row = i/rows;
int column = i % rows;
Taquin[row][column] = i;
}
UpdateTaquin(filename, Taquin, rows, columns);
int last_id = -1;
int id = -1;
while (1) {
//if (SourisCliquee()) {
SourisPosition();
id = GetButton(_X, _Y);
if (id != last_id) {
if (id != -1) UpdatePiece(filename, id % columns, id / columns, GetColorN("red"));
UpdatePiece(filename, last_id % columns, last_id / columns, GetColorN("black"));
last_id = id;
}
//}
}
}