From 021d9031add754f414fdfedffbb2cf7bc0ea961e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophyl=20Hoguet?= Date: Fri, 25 Mar 2022 15:35:17 +0100 Subject: [PATCH 1/2] update button save --- model.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/model.c b/model.c index 70ac8c7..575fd5b 100644 --- a/model.c +++ b/model.c @@ -85,15 +85,16 @@ struct Env_t { SDL_Texture** four; SDL_Texture** lightbulb; // array of the two lightbulb images first white second red SDL_Texture** text_restart; // up fist then down - SDL_Texture** text_undo; + SDL_Texture** text_undo; // array of two texture one white and one black SDL_Texture** text_redo; SDL_Texture** text_solve; - SDL_Texture** text_save; + SDL_Texture** text_save; // this one have three textures one white, one black and one green bool pressed_restart; bool pressed_undo; bool pressed_redo; bool pressed_solve; bool pressed_save; + bool pressed_savED; SDL_Rect* rec_game; // rectangle of the grid SDL_Rect* rec_redo; // rectangle of each buttons SDL_Rect* rec_undo; @@ -172,6 +173,7 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) { env->pressed_redo = false; env->pressed_solve = false; env->pressed_save = false; + env->pressed_savED = false; PRINT("%s", "To win the game, you must satisfy the following conditions:\n\n"); PRINT("%s", "-All non-black squares are lit.\n-No light is lit by another light.\n-Each numbered black square must be orthogonally adjacent to exactly the given " @@ -455,12 +457,13 @@ void play_mark(uint i, uint j, Env* env) { } } -void update_pressed(Env* env, bool undo, bool redo, bool restart, bool solve, bool save) { +void update_pressed(Env* env, bool undo, bool redo, bool restart, bool solve, bool save, bool savED) { env->pressed_undo = undo; env->pressed_redo = redo; env->pressed_restart = restart; env->pressed_solve = solve; env->pressed_save = save; + env->pressed_savED = savED; } bool process(SDL_Window* win, Env* env, SDL_Event* e, SDL_Event* prec_e, int* nb_coups, int* nb_undo) { @@ -512,25 +515,25 @@ bool process(SDL_Window* win, Env* env, SDL_Event* e, SDL_Event* prec_e, int* nb mouse.y = e->tfinger.y; } if (SDL_PointInRect(&mouse, env->rec_restart)) { - update_pressed(env, false, false, true, false, false); + update_pressed(env, false, false, true, false, false, false); } else if (SDL_PointInRect(&mouse, env->rec_undo)) { if ((*nb_coups) > 0) - update_pressed(env, true, false, false, false, false); + update_pressed(env, true, false, false, false, false, false); else - update_pressed(env, false, false, false, false, false); + update_pressed(env, false, false, false, false, false, false); } else if (SDL_PointInRect(&mouse, env->rec_redo)) { if ((*nb_undo) > 0) - update_pressed(env, false, true, false, false, false); + update_pressed(env, false, true, false, false, false, false); else - update_pressed(env, false, false, false, false, false); + update_pressed(env, false, false, false, false, false, false); } else if (SDL_PointInRect(&mouse, env->rec_solve)) { - update_pressed(env, false, false, false, true, false); + update_pressed(env, false, false, false, true, false, false); } else if (SDL_PointInRect(&mouse, env->rec_save)) { - update_pressed(env, false, false, false, false, true); + update_pressed(env, false, false, false, false, true, false); } else { - update_pressed(env, false, false, false, false, false); + update_pressed(env, false, false, false, false, false, false); } #ifdef _ANDROID_ if (SDL_PointInRect(&mouse, env->rec_game)) { @@ -571,6 +574,7 @@ bool process(SDL_Window* win, Env* env, SDL_Event* e, SDL_Event* prec_e, int* nb (*nb_coups)++; } else if (SDL_PointInRect(&mouse, env->rec_save)) { game_save(env->g, "save.txt"); + update_pressed(env, false, false, false, false, false, true); } else if (SDL_PointInRect(&mouse, env->rec_game)) { uint i = (((float)mouse.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*/; From b8505b5a127d0928636f912144a022a4f8f52562 Mon Sep 17 00:00:00 2001 From: Nessar Date: Fri, 25 Mar 2022 15:42:47 +0100 Subject: [PATCH 2/2] save button beautiful --- model.c | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/model.c b/model.c index 575fd5b..b28734e 100644 --- a/model.c +++ b/model.c @@ -187,16 +187,20 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) { } else { usage(argv); } - void** env_tab[] = {(void**)&(env->zero), (void**)&(env->one), (void**)&(env->two), (void**)&(env->three), - (void**)&(env->four), (void**)&(env->lightbulb), (void**)&(env->text_redo), (void**)&(env->text_restart), - (void**)&(env->text_solve), (void**)&(env->text_undo), (void**)&(env->text_save), (void**)&(env->rec_game), - (void**)&(env->rec_redo), (void**)&(env->rec_restart), (void**)&(env->rec_solve), (void**)&(env->rec_undo), + uint sizeof_array[] = {2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1, 1}; // size of each element to allocate + uint cpt = 0; + void** env_tab[] = {(void**)&(env->zero), (void**)&(env->one), (void**)&(env->two), (void**)&(env->three), + (void**)&(env->four), (void**)&(env->text_restart), (void**)&(env->text_undo), (void**)&(env->text_redo), + (void**)&(env->text_solve), (void**)&(env->text_save), (void**)&(env->lightbulb), (void**)&(env->rec_game), + (void**)&(env->rec_redo), (void**)&(env->rec_restart), (void**)&(env->rec_solve), (void**)&(env->rec_undo), (void**)&(env->rec_save)}; - unsigned long sizeof_env_tab[] = {sizeof(SDL_Texture*) * 2, sizeof(SDL_Texture*) * 2, sizeof(SDL_Texture*) * 2, sizeof(SDL_Texture*) * 2, - sizeof(SDL_Texture*) * 2, sizeof(SDL_Texture*) * 2, sizeof(SDL_Texture*) * 2, sizeof(SDL_Texture*) * 2, - sizeof(SDL_Texture*) * 2, sizeof(SDL_Texture*) * 2, sizeof(SDL_Texture*) * 2, sizeof(SDL_Rect), - sizeof(SDL_Rect), sizeof(SDL_Rect), sizeof(SDL_Rect), sizeof(SDL_Rect), - sizeof(SDL_Rect)}; + unsigned long sizeof_env_tab[] = { + sizeof(SDL_Texture*) * sizeof_array[cpt++], sizeof(SDL_Texture*) * sizeof_array[cpt++], sizeof(SDL_Texture*) * sizeof_array[cpt++], + sizeof(SDL_Texture*) * sizeof_array[cpt++], sizeof(SDL_Texture*) * sizeof_array[cpt++], sizeof(SDL_Texture*) * sizeof_array[cpt++], + sizeof(SDL_Texture*) * sizeof_array[cpt++], sizeof(SDL_Texture*) * sizeof_array[cpt++], sizeof(SDL_Texture*) * sizeof_array[cpt++], + sizeof(SDL_Texture*) * sizeof_array[cpt++], sizeof(SDL_Texture*) * sizeof_array[cpt++], sizeof(SDL_Rect) * sizeof_array[cpt++], + sizeof(SDL_Rect) * sizeof_array[cpt++], sizeof(SDL_Rect) * sizeof_array[cpt++], sizeof(SDL_Rect) * sizeof_array[cpt++], + sizeof(SDL_Rect) * sizeof_array[cpt++], sizeof(SDL_Rect) * sizeof_array[cpt++]}; init_malloc(env_tab, sizeof_env_tab, sizeof(env_tab) / sizeof(env_tab[0])); // if the case has not error env->lightbulb[0] = IMG_LoadTexture(ren, LIGHTBULB_WHITE); @@ -224,22 +228,20 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) { #endif /*Creation of the differents textures and place them in env*/ - SDL_Color color_w = {255, 255, 255, 255}; // color of 0 in black wall with error - SDL_Color color_r = {255, 50, 50, 255}; /* blue color in RGBA */ - SDL_Color color_b = {0, 0, 0, 255}; /* black color in RGBA*/ + SDL_Color color_w = {255, 255, 255, SDL_ALPHA_OPAQUE}; // color of 0 in black wall with error + SDL_Color color_r = {255, 50, 50, SDL_ALPHA_OPAQUE}; /* blue color in RGBA */ + SDL_Color color_g = {0, 150, 0, SDL_ALPHA_OPAQUE}; /* green color in RGBA */ + SDL_Color color_b = {0, 0, 0, SDL_ALPHA_OPAQUE}; /* black color in RGBA*/ char* tab_texts_double[] = {"0", "1", "2", "3", "4", "Restart", "Undo", "Redo", "Solve", "Save"}; - + SDL_Color color_of_texts[] = {color_w, color_r, color_w, color_r, color_w, color_r, color_w, color_r, color_w, color_r, color_w, + color_b, color_w, color_b, color_w, color_b, color_w, color_b, color_w, color_b, color_g}; SDL_Texture** tab_textures_double[] = {env->zero, env->one, env->two, env->three, env->four, env->text_restart, env->text_undo, env->text_redo, env->text_solve, env->text_save}; - - SDL_Color color_tab[] = {color_w, color_r}; + cpt = 0; for (uint i = 0; i < sizeof(tab_textures_double) / sizeof(tab_textures_double[0]); i++) { - if (i > 4) { - color_tab[1] = color_b; - } - for (int j = 0; j < 2; j++) { - tab_textures_double[i][j] = render_blended_text(ren, color_tab[j], tab_texts_double[i]); + for (uint j = 0; j < sizeof_array[i]; j++) { + tab_textures_double[i][j] = render_blended_text(ren, color_of_texts[cpt++], tab_texts_double[i]); } } return env; @@ -394,7 +396,7 @@ void render(SDL_Window* win, SDL_Renderer* ren, Env* env) { SDL_RenderCopy(ren, env->text_solve[env->pressed_solve ? 1 : 0], NULL, &buttons); buttons.x += buttons.w + (w / NB_BUTTONS - w / (NB_BUTTONS + 1)); *(env->rec_save) = buttons; - SDL_RenderCopy(ren, env->text_save[env->pressed_save ? 1 : 0], NULL, &buttons); + SDL_RenderCopy(ren, env->text_save[env->pressed_savED ? 2 : env->pressed_save ? 1 : 0], NULL, &buttons); // render cases for (uint i = 0; i < game_nb_rows(env->g); i++) { for (uint j = 0; j < game_nb_cols(env->g); j++) {