diff --git a/assets/Roboto-Regular.ttf b/assets/Roboto-Regular.ttf new file mode 100644 index 0000000..3d6861b Binary files /dev/null and b/assets/Roboto-Regular.ttf differ diff --git a/assets/redo_down.png b/assets/redo_down.png deleted file mode 100644 index 4a4fe3d..0000000 Binary files a/assets/redo_down.png and /dev/null differ diff --git a/assets/redo_up.png b/assets/redo_up.png deleted file mode 100644 index ef1b2a4..0000000 Binary files a/assets/redo_up.png and /dev/null differ diff --git a/assets/restart_down.png b/assets/restart_down.png deleted file mode 100644 index 867c87b..0000000 Binary files a/assets/restart_down.png and /dev/null differ diff --git a/assets/restart_up.png b/assets/restart_up.png deleted file mode 100644 index 0068027..0000000 Binary files a/assets/restart_up.png and /dev/null differ diff --git a/assets/solve_down.png b/assets/solve_down.png deleted file mode 100644 index 06b8154..0000000 Binary files a/assets/solve_down.png and /dev/null differ diff --git a/assets/solve_up.png b/assets/solve_up.png deleted file mode 100644 index 03740bb..0000000 Binary files a/assets/solve_up.png and /dev/null differ diff --git a/assets/undo_down.png b/assets/undo_down.png deleted file mode 100644 index 6bbe99d..0000000 Binary files a/assets/undo_down.png and /dev/null differ diff --git a/assets/undo_up.png b/assets/undo_up.png deleted file mode 100644 index 288cf1b..0000000 Binary files a/assets/undo_up.png and /dev/null differ diff --git a/assets/win.mp3 b/assets/win.mp3 new file mode 100644 index 0000000..2fc77cc Binary files /dev/null and b/assets/win.mp3 differ diff --git a/main.c b/main.c index ea40ef4..c1f69d6 100644 --- a/main.c +++ b/main.c @@ -38,11 +38,12 @@ int main(int argc, char* argv[]) { /* main render loop */ SDL_Event e; bool quit = false; + SDL_Event prec_e; while (!quit) { /* manage events */ while (SDL_PollEvent(&e)) { /* process your events */ - quit = process(win, env, &e); + quit = process(ren, win, env, &e, &prec_e); if (quit) break; } diff --git a/model.c b/model.c index 9522df8..0f35698 100644 --- a/model.c +++ b/model.c @@ -16,17 +16,8 @@ /* **************************************************************** */ -#define RESTART_UP "restart_up.png" -#define RESTART_DOWN "restart_down.png" -#define UNDO_UP "undo_up.png" -#define UNDO_DOWN "undo_down.png" -#define REDO_UP "redo_up.png" -#define REDO_DOWN "redo_down.png" -#define SOLVE_UP "solve_up.png" -#define SOLVE_DOWN "solve_down.png" #define LIGHTBULB_WHITE "lightbulb_white.png" #define LIGHTBULB_RED "lightbulb_red.png" -#define FONT "arial.ttf" #define LB1 "lb1.mp3" #define LB2 "lb2.mp3" #define LB3 "lb3.mp3" @@ -36,28 +27,29 @@ #define MARK1 "mark1.wav" #define MARK2 "mark2.wav" #define MARK3 "mark3.wav" +#define FONT "Roboto-Regular.ttf" #define FONTSIZE 200 #ifdef __ANDROID__ static void copy_asset(char* src, char* dst) { SDL_RWops* file = SDL_RWFromFile(src, "r"); if (!file) - ERROR("%s", "[ERROR] SDL_RWFromFile: %s\n", src); + ERROR("[ERROR] SDL_RWFromFile: %s\n", src); int size = SDL_RWsize(file); - PRINT("%s", "copy file %s (%d bytes) into %s\n", src, size, dst); + PRINT("copy file %s (%d bytes) into %s\n", src, size, dst); char* buf = (char*)malloc(size + 1); if (!buf) ERROR("%s", "[ERROR] malloc\n"); int r = SDL_RWread(file, buf, 1, size); - PRINT("%s", "read %d\n", r); + PRINT("read %d\n", r); if (r != size) - ERROR("%s", "[ERROR] fail to read all file (%d bytes)\n", r); + ERROR("[ERROR] fail to read all file (%d bytes)\n", r); FILE* out = fopen(dst, "w+"); if (!out) - ERROR("%s", "[ERROR] fail to create file %s\n", dst); + ERROR("[ERROR] fail to create file %s\n", dst); int w = fwrite(buf, 1, r, out); if (r != w) - ERROR("%s", "[ERROR] fail to write all file (%d bytes)\n", w); + ERROR("[ERROR] fail to write all file (%d bytes)\n", w); fclose(out); SDL_RWclose(file); free(buf); @@ -102,6 +94,114 @@ void usage(char* argv[]) { exit(EXIT_FAILURE); } +void render_blended_text(SDL_Renderer* ren, Env* env) { +#ifdef __ANDROID__ + const char* dir = SDL_AndroidGetInternalStoragePath(); + char font_path[1024]; + sprintf(font_path, "%s/%s", dir, FONT); + copy_asset(FONT, font_path); + TTF_Font* font = TTF_OpenFont(font_path, FONTSIZE); +#else + TTF_Font* font = TTF_OpenFont(FONT, FONTSIZE); +#endif + if (!font) + ERROR("TTF_OpenFont: %s\n", FONT); + TTF_SetFontStyle(font, TTF_STYLE_BOLD); + // color of 0 in black wall + 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}; + + /*rendu restart*/ + SDL_Surface* surf = TTF_RenderText_Blended(font, "Restart", color_b); + env->text_restart[0] = SDL_CreateTextureFromSurface(ren, surf); + SDL_FreeSurface(surf); + + surf = TTF_RenderText_Blended(font, "Restart", color_w); + env->text_restart[1] = SDL_CreateTextureFromSurface(ren, surf); + SDL_FreeSurface(surf); + + /*rendu undo*/ + surf = TTF_RenderText_Blended(font, "Undo", color_b); + env->text_undo[0] = SDL_CreateTextureFromSurface(ren, surf); + SDL_FreeSurface(surf); + + surf = TTF_RenderText_Blended(font, "Undo", color_w); + env->text_undo[1] = SDL_CreateTextureFromSurface(ren, surf); + SDL_FreeSurface(surf); + + /*rerndu redo*/ + surf = TTF_RenderText_Blended(font, "Redo", color_b); + env->text_redo[0] = SDL_CreateTextureFromSurface(ren, surf); + SDL_FreeSurface(surf); + + surf = TTF_RenderText_Blended(font, "Redo", color_w); + env->text_redo[1] = SDL_CreateTextureFromSurface(ren, surf); + SDL_FreeSurface(surf); + + /*rendu solve*/ + surf = TTF_RenderText_Blended(font, "Solve", color_b); + env->text_solve[0] = SDL_CreateTextureFromSurface(ren, surf); + SDL_FreeSurface(surf); + + surf = TTF_RenderText_Blended(font, "Solve", color_w); + env->text_solve[1] = SDL_CreateTextureFromSurface(ren, surf); + SDL_FreeSurface(surf); + + /* init zero texture double tab*/ + // if the case has not error + surf = TTF_RenderText_Blended(font, "0", color_w); + env->zero[0] = SDL_CreateTextureFromSurface(ren, surf); + SDL_FreeSurface(surf); + // if the case has error + surf = TTF_RenderText_Blended(font, "0", color_r); + env->zero[1] = SDL_CreateTextureFromSurface(ren, surf); + SDL_FreeSurface(surf); + + /* init one texture double tab*/ + // if the case has not error + surf = TTF_RenderText_Blended(font, "1", color_w); + env->one[0] = SDL_CreateTextureFromSurface(ren, surf); + SDL_FreeSurface(surf); + // if the case has error + surf = TTF_RenderText_Blended(font, "1", color_r); + env->one[1] = SDL_CreateTextureFromSurface(ren, surf); + SDL_FreeSurface(surf); + + /* init two texture double tab*/ + // if the case has not error + surf = TTF_RenderText_Blended(font, "2", color_w); + env->two[0] = SDL_CreateTextureFromSurface(ren, surf); + SDL_FreeSurface(surf); + // if the case has error + surf = TTF_RenderText_Blended(font, "2", color_r); + env->two[1] = SDL_CreateTextureFromSurface(ren, surf); + SDL_FreeSurface(surf); + + /* init three texture double tab*/ + // if the case has not error + surf = TTF_RenderText_Blended(font, "3", color_w); + env->three[0] = SDL_CreateTextureFromSurface(ren, surf); + SDL_FreeSurface(surf); + // if the case has error + surf = TTF_RenderText_Blended(font, "3", color_r); + env->three[1] = SDL_CreateTextureFromSurface(ren, surf); + SDL_FreeSurface(surf); + + /* init four texture double tab*/ + // if the case has not error + surf = TTF_RenderText_Blended(font, "4", color_w); + env->four[0] = SDL_CreateTextureFromSurface(ren, surf); + SDL_FreeSurface(surf); + // if the case has error + surf = TTF_RenderText_Blended(font, "4", color_r); + env->four[1] = SDL_CreateTextureFromSurface(ren, surf); + SDL_FreeSurface(surf); + TTF_CloseFont(font); +} + Env* init(SDL_Renderer* ren, int argc, char* argv[]) { Env* env = malloc(sizeof(struct Env_t)); env->pressed_restart = false; @@ -121,87 +221,30 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) { } else { usage(argv); } -#ifdef __ANDROID__ - const char* dir = SDL_AndroidGetInternalStoragePath(); - char font_path[1024]; - sprintf(font_path, "%s/%s", dir, FONT); - copy_asset(FONT, font_path); - TTF_Font* font = TTF_OpenFont(font_path, FONTSIZE); -#else - TTF_Font* font = TTF_OpenFont(FONT, FONTSIZE); -#endif - if (!font) - ERROR("TTF_OpenFont: %s\n", FONT); - TTF_SetFontStyle(font, TTF_STYLE_BOLD); - // color of 0 in black wall - 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 */ - /* init zero texture double tab*/ env->zero = malloc(sizeof(SDL_Texture*) * 2); if (env->zero == NULL) ERROR("%s", "NOT ENOUGTH MEMORY\n"); - // if the case has not error - SDL_Surface* surf = TTF_RenderText_Blended(font, "0", color_w); - env->zero[0] = SDL_CreateTextureFromSurface(ren, surf); - SDL_FreeSurface(surf); - // if the case has error - surf = TTF_RenderText_Blended(font, "0", color_r); - env->zero[1] = SDL_CreateTextureFromSurface(ren, surf); - SDL_FreeSurface(surf); /* init one texture double tab*/ env->one = malloc(sizeof(SDL_Texture*) * 2); if (env->one == NULL) ERROR("%s", "NOT ENOUGTH MEMORY\n"); - // if the case has not error - surf = TTF_RenderText_Blended(font, "1", color_w); - env->one[0] = SDL_CreateTextureFromSurface(ren, surf); - SDL_FreeSurface(surf); - // if the case has error - surf = TTF_RenderText_Blended(font, "1", color_r); - env->one[1] = SDL_CreateTextureFromSurface(ren, surf); - SDL_FreeSurface(surf); /* init two texture double tab*/ env->two = malloc(sizeof(SDL_Texture*) * 2); if (env->two == NULL) ERROR("%s", "NOT ENOUGTH MEMORY\n"); - // if the case has not error - surf = TTF_RenderText_Blended(font, "2", color_w); - env->two[0] = SDL_CreateTextureFromSurface(ren, surf); - SDL_FreeSurface(surf); - // if the case has error - surf = TTF_RenderText_Blended(font, "2", color_r); - env->two[1] = SDL_CreateTextureFromSurface(ren, surf); - SDL_FreeSurface(surf); /* init three texture double tab*/ env->three = malloc(sizeof(SDL_Texture*) * 2); if (env->three == NULL) ERROR("%s", "NOT ENOUGTH MEMORY\n"); - // if the case has not error - surf = TTF_RenderText_Blended(font, "3", color_w); - env->three[0] = SDL_CreateTextureFromSurface(ren, surf); - SDL_FreeSurface(surf); - // if the case has error - surf = TTF_RenderText_Blended(font, "3", color_r); - env->three[1] = SDL_CreateTextureFromSurface(ren, surf); - SDL_FreeSurface(surf); /* init four texture double tab*/ env->four = malloc(sizeof(SDL_Texture*) * 2); if (env->four == NULL) ERROR("%s", "NOT ENOUGTH MEMORY\n"); - // if the case has not error - surf = TTF_RenderText_Blended(font, "4", color_w); - env->four[0] = SDL_CreateTextureFromSurface(ren, surf); - SDL_FreeSurface(surf); - // if the case has error - surf = TTF_RenderText_Blended(font, "4", color_r); - env->four[1] = SDL_CreateTextureFromSurface(ren, surf); - SDL_FreeSurface(surf); /* init lightbulb texture double tab*/ env->lightbulb = malloc(sizeof(SDL_Texture*) * 2); @@ -220,53 +263,23 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) { env->text_restart = malloc(sizeof(SDL_Texture*) * 2); if (env->text_restart == NULL) ERROR("%s", "NOT ENOUGTH MEMORY\n"); - // if the case has not error - env->text_restart[0] = IMG_LoadTexture(ren, RESTART_UP); - if (!env->text_restart[0]) - ERROR("IMG_LoadTexture: %s\n", RESTART_UP); - // if the case has error - env->text_restart[1] = IMG_LoadTexture(ren, RESTART_DOWN); - if (!env->text_restart[1]) - ERROR("IMG_LoadTexture: %s\n", RESTART_DOWN); /* init text_undo texture double tab*/ env->text_undo = malloc(sizeof(SDL_Texture*) * 2); if (env->text_undo == NULL) ERROR("%s", "NOT ENOUGTH MEMORY\n"); - // if the case has not error - env->text_undo[0] = IMG_LoadTexture(ren, UNDO_UP); - if (!env->text_undo[0]) - ERROR("IMG_LoadTexture: %s\n", UNDO_UP); - // if the case has error - env->text_undo[1] = IMG_LoadTexture(ren, UNDO_DOWN); - if (!env->text_undo[1]) - ERROR("IMG_LoadTexture: %s\n", UNDO_DOWN); /* init text_redo texture double tab*/ env->text_redo = malloc(sizeof(SDL_Texture*) * 2); if (env->text_redo == NULL) ERROR("%s", "NOT ENOUGTH MEMORY\n"); - // if the case has not error - env->text_redo[0] = IMG_LoadTexture(ren, REDO_UP); - if (!env->text_redo[0]) - ERROR("IMG_LoadTexture: %s\n", REDO_UP); - // if the case has error - env->text_redo[1] = IMG_LoadTexture(ren, REDO_DOWN); - if (!env->text_redo[1]) - ERROR("IMG_LoadTexture: %s\n", UNDO_DOWN); /* init text_solve texture double tab*/ env->text_solve = malloc(sizeof(SDL_Texture*) * 2); if (env->text_solve == NULL) ERROR("%s", "NOT ENOUGTH MEMORY\n"); - // if the case has not error - env->text_solve[0] = IMG_LoadTexture(ren, SOLVE_UP); - if (!env->text_solve[0]) - ERROR("IMG_LoadTexture: %s\n", SOLVE_UP); - // if the case has error - env->text_solve[1] = IMG_LoadTexture(ren, SOLVE_DOWN); - if (!env->text_solve[1]) - ERROR("IMG_LoadTexture: %s\n", SOLVE_DOWN); + + render_blended_text(ren, env); env->rec_redo = malloc(sizeof(SDL_Rect)); if (env->rec_redo == NULL) @@ -334,7 +347,6 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) { if (env->mark_music[2] == NULL) ERROR("Cannot load music %s\n", MARK3); env->mark_music_cpt = 0; - return env; } @@ -430,8 +442,21 @@ void render_mark(SDL_Renderer* ren, SDL_Rect* rec, bool lighted) { } void render(SDL_Window* win, SDL_Renderer* ren, Env* env) { - int w, h; - SDL_GetWindowSize(win, &w, &h); + int win_w, win_h, h, w; + SDL_GetWindowSize(win, &win_w, &win_h); + + if (win_h > + win_w) { // sert a avoir un rapport de 1/1 pour la game et les boutons et ne pas avoir de compressions sur quelconques axes des images des boutons + h = win_w; + w = win_w; + } else { + h = win_h; + w = win_h; + } + + int marge_h = (win_h - h) / 2; + int marge_w = (win_w - w) / 2; + h = h - h / 10; // set background color SDL_SetRenderDrawColor(ren, 24, 26, 27, SDL_ALPHA_OPAQUE); @@ -440,19 +465,20 @@ void render(SDL_Window* win, SDL_Renderer* ren, Env* env) { int size_rec = int_min(w / game_nb_cols(env->g), h / game_nb_rows(env->g)); rec.h = size_rec; rec.w = size_rec; - int rec_x = w / 2 - size_rec * game_nb_cols(env->g) / 2; - int rec_y = h / 2 - size_rec * game_nb_rows(env->g) / 2 + h / 10; + int rec_x = w / 2 - size_rec * game_nb_cols(env->g) / 2 + marge_w; + int rec_y = h / 2 - size_rec * game_nb_rows(env->g) / 2 + h / 10 + marge_h; rec.x = rec_x; rec.y = rec_y; env->rec_game->x = rec.x; env->rec_game->y = rec.y; env->rec_game->h = size_rec * game_nb_rows(env->g); env->rec_game->w = size_rec * game_nb_cols(env->g); + // buttons SDL_Rect buttons; buttons.w = w / 5; buttons.h = h / 10 / 1.5; - buttons.y = (h / 10 - buttons.h) / 2; - buttons.x = (w / 4 - w / 5) / 2; + buttons.y = (h / 10 - buttons.h) / 2 + marge_h; + buttons.x = (w / 4 - w / 5) / 2 + marge_w; *(env->rec_undo) = buttons; SDL_RenderCopy(ren, env->text_undo[env->pressed_undo ? 1 : 0], NULL, &buttons); buttons.x = buttons.x + buttons.w + (w / 4 - w / 5); @@ -504,14 +530,88 @@ void ToggleFullscreen(SDL_Window* Window) { SDL_ShowCursor(IsFullscreen); } -bool process(SDL_Window* win, Env* env, SDL_Event* e) { +bool process(SDL_Renderer* ren, SDL_Window* win, Env* env, SDL_Event* e, SDL_Event* prec_e) { int w, h; SDL_GetWindowSize(win, &w, &h); if (e->type == SDL_QUIT) { 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 + if (prec_e != NULL) { + prec_e = NULL; + } if (e->type == SDL_MOUSEMOTION) { SDL_Point mouse; SDL_GetMouseState(&mouse.x, &mouse.y); @@ -579,6 +679,8 @@ bool process(SDL_Window* win, Env* env, SDL_Event* e) { const Uint8* state = SDL_GetKeyboardState(NULL); if (state[SDL_SCANCODE_F11]) ToggleFullscreen(win); + } else if (e->type == SDL_WINDOWEVENT) { + render_blended_text(ren, env); } #endif return false; diff --git a/model.h b/model.h index 056c8ce..f92c105 100644 --- a/model.h +++ b/model.h @@ -45,7 +45,8 @@ typedef struct Env_t Env; Env* init(SDL_Renderer* ren, int argc, char* argv[]); void render(SDL_Window* win, SDL_Renderer* ren, Env* env); void clean(Env* env); -bool process(SDL_Window* win, Env* env, SDL_Event* e); +bool process(SDL_Renderer* ren, SDL_Window* win, Env* env, SDL_Event* e, SDL_Event* prec_e); +void render_blended_numbered_wall(SDL_Renderer* ren, Env* env); /* **************************************************************** */