From 6f4148bacbb923ec5a5bdc1ed2c8f9b890ac8006 Mon Sep 17 00:00:00 2001 From: valentin carmona Date: Thu, 24 Mar 2022 11:07:31 +0100 Subject: [PATCH] =?UTF-8?q?add=20shortcuts=20and=20optimize=20process=20(e?= =?UTF-8?q?n=20plus=20j'ai=20pas=20oubli=C3=A9=20le=20clang)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model.c | 162 ++++++++++++++++++++++---------------------------------- model.h | 2 + 2 files changed, 65 insertions(+), 99 deletions(-) diff --git a/model.c b/model.c index 1af6d42..a197431 100644 --- a/model.c +++ b/model.c @@ -583,95 +583,33 @@ bool process(SDL_Window* win, Env* env, SDL_Event* e, SDL_Event* prec_e) { const Uint8* state = SDL_GetKeyboardState(NULL); if (state[SDL_SCANCODE_F11]) ToggleFullscreen(win); + if (state[SDL_SCANCODE_Z]) + game_undo(env->g); + if (state[SDL_SCANCODE_Y]) + game_redo(env->g); + if (state[SDL_SCANCODE_S]) + game_solve(env->g); + if (state[SDL_SCANCODE_R] || state[SDL_SCANCODE_F5]) + game_restart(env->g); } if (game_is_over(env->g) && !env->won) { Mix_PlayMusic(env->win_music, 1); env->won = true; env->won_timestamp = SDL_GetTicks(); + } else if (env->won) { if (SDL_GetTicks() - env->won_timestamp >= 5000) return true; - } -#ifdef _ANDROID_ - if (e->type == SDL_FINGERDOWN) { - SDL_Point coord; - coord.x = e->x; - coord.y = e->y; - if (SDL_PointInRect(&coord, env->rec_restart)) { - env->pressed_restart = true; - env->pressed_undo = false; - env->pressed_redo = false; - env->pressed_solve = false; - } else if (SDL_PointInRect(&coord, env->rec_undo)) { - env->pressed_undo = true; - env->pressed_restart = false; - env->pressed_redo = false; - env->pressed_solve = false; - } else if (SDL_PointInRect(&coord, env->rec_redo)) { - env->pressed_redo = true; - env->pressed_undo = false; - env->pressed_restart = false; - env->pressed_solve = false; - } else if (SDL_PointInRect(&coord, env->rec_solve)) { - env->pressed_solve = true; - env->pressed_undo = false; - env->pressed_redo = false; - env->pressed_restart = false; - } else if (SDL_PointInRect(&coord, env->rec_solve)) { - env->pressed_restart = false; - env->pressed_undo = false; - env->pressed_redo = false; - env->pressed_solve = false; - if (e->type != prec_e->type) { - prec_e->type = e->type; - prec_e->timestamp = e->timestamp; - } - } else { - env->pressed_restart = false; - env->pressed_undo = false; - env->pressed_redo = false; - env->pressed_solve = false; - } - } else if (e->type == SDL_FINGERUP) { - SDL_Point coord; - coord.x = e->x; - coord.y = e->y; - if (SDL_PointInRect(&coord, env->rec_restart)) { - game_restart(env->g); - } else if (SDL_PointInRect(&coord, env->rec_undo)) { - game_undo(env->g); - } else if (SDL_PointInRect(&coord, env->rec_redo)) { - game_redo(env->g); - } else if (SDL_PointInRect(&coord, env->rec_solve)) { - game_solve(env->g); - } else if (SDL_PointInRect(&coord, env->rec_game)) { - uint i = (((float)coord.y - (float)env->rec_game->y) / (float)env->rec_game->h * game_nb_rows(env->g)) - - 0.00001 /*to avoid if clicked exacly on the bottom right corner to result a 7*/; - uint j = (((float)coord.x - (float)env->rec_game->x) / (float)env->rec_game->w * game_nb_cols(env->g)) - - 0.00001 /*to avoid if clicked exacly on the bottom right corner to result a 7*/; - if ((e->timestamp - prec_e->timestamp) < 300) { - if (game_is_blank(env->g, i, j) || game_is_marked(env->g, i, j)) { - game_play_move(env->g, i, j, S_LIGHTBULB); - } else if (game_is_lightbulb(env->g, i, j)) { - game_play_move(env->g, i, j, S_BLANK); - } - } else { - if (game_is_blank(env->g, i, j) || game_is_lightbulb(env->g, i, j)) { - game_play_move(env->g, i, j, S_MARK); - } else if (game_is_marked(env->g, i, j)) { - game_play_move(env->g, i, j, S_BLANK); - } - } - } - } -#else - else { - if (prec_e != NULL) { - prec_e = NULL; - } - if (e->type == SDL_MOUSEMOTION) { + + } else { + if (e->type == SDL_MOUSEMOTION || e->type == SDL_FINGERDOWN) { SDL_Point mouse; - SDL_GetMouseState(&mouse.x, &mouse.y); + if (e->type == SDL_MOUSEMOTION) { + SDL_GetMouseState(&mouse.x, &mouse.y); + } else { + mouse.x = e->tfinger.x; + mouse.y = e->tfinger.y; + } if (SDL_PointInRect(&mouse, env->rec_restart)) { env->pressed_restart = true; env->pressed_undo = false; @@ -698,9 +636,22 @@ bool process(SDL_Window* win, Env* env, SDL_Event* e, SDL_Event* prec_e) { env->pressed_redo = false; env->pressed_solve = false; } - } else if (e->type == SDL_MOUSEBUTTONDOWN) { +#ifdef _ANDROID_ + if (SDL_PointInRect(&mouse, env->rec_game)) { + if (e->type != prec_e->type) { + prec_e->type = e->type; + prec_e->tfinger.timestamp = e->tfinger.timestamp; + } + } +#endif + } else if (e->type == SDL_MOUSEBUTTONDOWN || e->type == SDL_FINGERUP) { SDL_Point mouse; - SDL_GetMouseState(&mouse.x, &mouse.y); + if (e->type == SDL_MOUSEBUTTONDOWN) { + SDL_GetMouseState(&mouse.x, &mouse.y); + } else { + mouse.x = e->tfinger.x; + mouse.y = e->tfinger.y; + } if (SDL_PointInRect(&mouse, env->rec_restart)) { game_restart(env->g); } else if (SDL_PointInRect(&mouse, env->rec_undo)) { @@ -714,30 +665,43 @@ bool process(SDL_Window* win, Env* env, SDL_Event* e, SDL_Event* prec_e) { 0.00001 /*to avoid if clicked exacly on the bottom right corner to result a 7*/; uint j = (((float)mouse.x - (float)env->rec_game->x) / (float)env->rec_game->w * game_nb_cols(env->g)) - 0.00001 /*to avoid if clicked exacly on the bottom right corner to result a 7*/; - if (e->button.button == SDL_BUTTON_LEFT) { - if (game_is_blank(env->g, i, j) || game_is_marked(env->g, i, j)) { - game_play_move(env->g, i, j, S_LIGHTBULB); - play_Music(env, true, true, false); - } else if (game_is_lightbulb(env->g, i, j)) { - game_play_move(env->g, i, j, S_BLANK); - play_Music(env, true, true, false); - } - } else if (e->button.button == SDL_BUTTON_RIGHT) { - if (game_is_blank(env->g, i, j) || game_is_lightbulb(env->g, i, j)) { - game_play_move(env->g, i, j, S_MARK); - play_Music(env, false, true, true); - } else if (game_is_marked(env->g, i, j)) { - game_play_move(env->g, i, j, S_BLANK); - play_Music(env, false, true, true); - } + + if (e->type == SDL_MOUSEBUTTONDOWN && e->button.button == SDL_BUTTON_LEFT) { // only execute when type == SDL_MOUSEBUTTONDOWN + play_light(i, j, env); + } else if (e->type == SDL_MOUSEBUTTONDOWN && e->button.button == SDL_BUTTON_RIGHT) { + play_mark(i, j, env); + } + + if (e->type == SDL_FINGERUP && (e->tfinger.timestamp - prec_e->tfinger.timestamp) < 300) { // only execute when type == SDL_FINGERUP + play_light(i, j, env); + } else if (e->type == SDL_FINGERUP) { + play_mark(i, j, env); } } } } -#endif return false; } +void play_light(uint i, uint j, Env* env) { + if (game_is_blank(env->g, i, j) || game_is_marked(env->g, i, j)) { + game_play_move(env->g, i, j, S_LIGHTBULB); + play_Music(env, true, true, false); + } else if (game_is_lightbulb(env->g, i, j)) { + game_play_move(env->g, i, j, S_BLANK); + play_Music(env, true, true, false); + } +} + +void play_mark(uint i, uint j, Env* env) { + if (game_is_blank(env->g, i, j) || game_is_lightbulb(env->g, i, j)) { + game_play_move(env->g, i, j, S_MARK); + play_Music(env, false, true, true); + } else if (game_is_marked(env->g, i, j)) { + game_play_move(env->g, i, j, S_BLANK); + play_Music(env, false, true, true); + } +} /* **************************************************************** */ void clean(Env* env) { diff --git a/model.h b/model.h index edbfde3..7d999dc 100644 --- a/model.h +++ b/model.h @@ -47,6 +47,8 @@ void render(SDL_Window* win, SDL_Renderer* ren, Env* env); void clean(Env* env); bool process(SDL_Window* win, Env* env, SDL_Event* e, SDL_Event* prec_e); void render_blended_numbered_wall(SDL_Renderer* ren, Env* env); +void play_light(unsigned int i, unsigned int j, Env* env); +void play_mark(unsigned int i, unsigned int j, Env* env); /* **************************************************************** */