Merge branch 'main' of gitlab.emi.u-bordeaux.fr:pt2/teams-s4/tm07/lightup-07c

This commit is contained in:
Mohamed Fatih committed 2022-03-17 16:29:56 +01:00
commit 5ed6e97511
1 file changed
+29 -46
+29 -46
View File
@@ -43,13 +43,16 @@ struct Env_t {
SDL_Texture* text_undo; SDL_Texture* text_undo;
SDL_Texture* text_redo; SDL_Texture* text_redo;
SDL_Texture* text_solve; SDL_Texture* text_solve;
bool pressed_restart;
bool pressed_undo;
bool pressed_redo;
bool pressed_solve;
SDL_Rect* rec_redo; // rectangle of each buttons SDL_Rect* rec_redo; // rectangle of each buttons
SDL_Rect* rec_undo; SDL_Rect* rec_undo;
SDL_Rect* rec_restart; SDL_Rect* rec_restart;
SDL_Rect* rec_solve; SDL_Rect* rec_solve;
SDL_Rect* rec_game; // rectangle of the grid SDL_Rect* rec_game; // rectangle of the grid
SDL_Rect** tab_rec_game; // array of rectangles containing each square of the grid uint size_game; // contain the number of squares in the game
uint size_game; // contain the number of squares in the game
}; };
/* **************************************************************** */ /* **************************************************************** */
@@ -288,58 +291,38 @@ bool process(SDL_Window* win, SDL_Renderer* ren, Env* env, SDL_Event* e) {
} else if (e->type == SDL_MOUSEMOTION) { } else if (e->type == SDL_MOUSEMOTION) {
SDL_Point mouse; SDL_Point mouse;
SDL_GetMouseState(&mouse.x, &mouse.y); SDL_GetMouseState(&mouse.x, &mouse.y);
if (SDL_PointInRect(mouse, env->rec_restart)) { if (SDL_PointInRect(&mouse, env->rec_restart)) {
env->text_restart = IMG_LoadTexture(ren, RESTART_DOWN); env->pressed_restart = true;
if (!env->text_restart) } else if (SDL_PointInRect(&mouse, env->rec_undo)) {
ERROR("IMG_LoadTexture: %s\n", RESTART_DOWN); env->pressed_undo = true;
} else if (SDL_PointInRect(mouse, env->rec_undo)) { } else if (SDL_PointInRect(&mouse, env->rec_redo)) {
env->text_undo = IMG_LoadTexture(ren, UNDO_DOWN); env->pressed_redo;
if (!env->text_undo) } else if (SDL_PointInRect(&mouse, env->rec_solve)) {
ERROR("IMG_LoadTexture: %s\n", UNDO_DOWN); env->pressed_solve = true;
} else if (SDL_PointInRect(mouse, env->rec_redo)) {
env->text_redo = IMG_LoadTexture(ren, REDO_DOWN);
if (!env->text_redo)
ERROR("IMG_LoadTexture: %s\n", REDO_DOWN);
} else if (SDL_PointInRect(mouse, env->rec_sove)) {
env->text_solve = IMG_LoadTexture(ren, SOLVE_DOWN);
if (!env->text_solve)
ERROR("IMG_LoadTexture: %s\n", SOLVE_DOWN);
} else { } else {
env->text_restart = IMG_LoadTexture(ren, RESTART_UP); env->pressed_restart = false;
if (!env->text_restart) env->pressed_undo = false;
ERROR("IMG_LoadTexture: %s\n", RESTART_UP); env->pressed_redo = false;
env->text_undo = IMG_LoadTexture(ren, UNDO_UP); env->pressed_solve = false;
if (!env->text_undo)
ERROR("IMG_LoadTexture: %s\n", UNDO_UP);
env->text_redo = IMG_LoadTexture(ren, REDO_UP);
if (!env->text_redo)
ERROR("IMG_LoadTexture: %s\n", REDO_UP);
env->text_solve = IMG_LoadTexture(ren, SOLVE_UP);
if (!env->text_solve)
ERROR("IMG_LoadTexture: %s\n", SOLVE_UP);
} }
} else if (e->type == SDL_MOUSEBUTTONDOWN) { } else if (e->type == SDL_MOUSEBUTTONDOWN) {
SDL_Point mouse; SDL_Point mouse;
SDL_GetMouseState(&mouse.x, &mouse.y); SDL_GetMouseState(&mouse.x, &mouse.y);
if (SDL_PointInRect(mouse, env->rec_restart)) { if (SDL_PointInRect(&mouse, env->rec_restart)) {
game_restart(env->g); game_restart(env->g);
} else if (SDL_PointInRect(mouse, env->rec_undo)) { } else if (SDL_PointInRect(&mouse, env->rec_undo)) {
game_undo(env->g); game_undo(env->g);
} else if (SDL_PointInRect(mouse, env->rec_redo)) { } else if (SDL_PointInRect(&mouse, env->rec_redo)) {
game_redo(env->g); game_redo(env->g);
} else if (SDL_PointInRect(mouse, env->rec_sove)) { } else if (SDL_PointInRect(&mouse, env->rec_solve)) {
game_solve(env->g); game_solve(env->g);
} else if (SDL_PointInRect(mouse, env->rec_game)) { } else if (SDL_PointInRect(&mouse, env->rec_game)) {
for (uint ind = 0; i < env->size; ind++) { uint i = mouse.y - ((h - (env->rec_game->y)) / 2) / (env->g->height);
uint i = ind % (env->g->height); uint j = mouse.x - ((w - (env->rec_game->x)) / 2) / (env->g->width);
uint j = ind / (env->g->width); if (game_is_blank(env->g, i, j)) {
if (SDL_PointInRect(mouse, env->tab_rec_game[ind])) { game_play_move(env->g, i, j, S_LIGHTBULB);
if (game_is_blank(env->g, i, j)) { } else if (game_is_lightbulb(env->g, i, j)) {
game_play_move(env->g, i, j, S_LIGHTBULB); game_play_move(env->g, i, j, S_BLANK);
} else if (game_is_lightbulb(env->g, i, j)) {
game_play_move(env->g, i, j, S_BLANK);
}
}
} }
} }
} }