english + update buttons with keyboard + w -> z qwerty sdl
This commit is contained in:
1 parent
8ec28d653b
commit
4bd0977bc7
1 file changed
+41
-35
@@ -65,8 +65,8 @@ static void copy_asset(char* src, char* dst) {
|
|||||||
|
|
||||||
/* **************************************************************** */
|
/* **************************************************************** */
|
||||||
|
|
||||||
/* Fonction Min */
|
/* Fonction Minimum */
|
||||||
int int_min_intero(int a, int b) {
|
int int_min_intero(int a, int b) { /*interopérabilité*/
|
||||||
if (a < b)
|
if (a < b)
|
||||||
return a;
|
return a;
|
||||||
return b;
|
return b;
|
||||||
@@ -86,16 +86,16 @@ struct Env_t {
|
|||||||
game g;
|
game g;
|
||||||
|
|
||||||
SDL_Texture** zero; // array of two texture one white and one red for each number (red color = r:255 g:50 b:50)
|
SDL_Texture** zero; // array of two texture one white and one red for each number (red color = r:255 g:50 b:50)
|
||||||
SDL_Texture** one;
|
SDL_Texture** one; // Same
|
||||||
SDL_Texture** two;
|
SDL_Texture** two; // Same
|
||||||
SDL_Texture** three;
|
SDL_Texture** three; // Same
|
||||||
SDL_Texture** four;
|
SDL_Texture** four; // Same
|
||||||
SDL_Texture** lightbulb; // array of the two lightbulb images first white second red
|
SDL_Texture** lightbulb; // array of the two lightbulb images first white second red
|
||||||
SDL_Texture** text_restart; // up fist then down
|
SDL_Texture** text_restart; // array of two textures one when the button is up (the first one) and one when the button is down (the second one)
|
||||||
SDL_Texture** text_undo; // array of two texture one white and one black
|
SDL_Texture** text_undo; // array of two texture one white (the first one) and one black (the second one)
|
||||||
SDL_Texture** text_redo;
|
SDL_Texture** text_redo; // Same
|
||||||
SDL_Texture** text_solve;
|
SDL_Texture** text_solve; // Same
|
||||||
SDL_Texture** text_save; // this one have three textures one white, one black and one green
|
SDL_Texture** text_save; // This one have three textures one white, one black and one green
|
||||||
|
|
||||||
bool pressed_restart;
|
bool pressed_restart;
|
||||||
bool pressed_undo;
|
bool pressed_undo;
|
||||||
@@ -107,15 +107,15 @@ struct Env_t {
|
|||||||
|
|
||||||
SDL_Rect* rec_game; // rectangle of the grid
|
SDL_Rect* rec_game; // rectangle of the grid
|
||||||
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_save;
|
SDL_Rect* rec_save; // ^^
|
||||||
|
|
||||||
Mix_Music** lb_music;
|
Mix_Music** lb_music; // array of three musics for each action
|
||||||
Mix_Music** err_music;
|
Mix_Music** err_music; // Same
|
||||||
Mix_Music** mark_music;
|
Mix_Music** mark_music; // Same
|
||||||
Mix_Music* win_music;
|
Mix_Music* win_music; // Only one music when wining
|
||||||
|
|
||||||
uint lb_music_cpt;
|
uint lb_music_cpt;
|
||||||
uint err_music_cpt;
|
uint err_music_cpt;
|
||||||
@@ -129,7 +129,7 @@ void usage(char* argv[]) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Fonction for return a texture of a given text */
|
/* Fonction to return the texture of the given path */
|
||||||
SDL_Texture* render_blended_text(SDL_Renderer* ren, SDL_Color color, char* text) {
|
SDL_Texture* render_blended_text(SDL_Renderer* ren, SDL_Color color, char* text) {
|
||||||
TTF_Font* font = TTF_OpenFont(FONT, FONTSIZE); // TO EDIT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! (ANDROID)
|
TTF_Font* font = TTF_OpenFont(FONT, FONTSIZE); // TO EDIT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! (ANDROID)
|
||||||
if (!font)
|
if (!font)
|
||||||
@@ -144,7 +144,7 @@ SDL_Texture* render_blended_text(SDL_Renderer* ren, SDL_Color color, char* text)
|
|||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Fonction for allocating automaticly */
|
/* Fonction to allocate each array of env */
|
||||||
void init_malloc(void** tab[], unsigned long* sizeof_tab, uint size_tab) {
|
void init_malloc(void** tab[], unsigned long* sizeof_tab, uint size_tab) {
|
||||||
for (uint i = 0; i < size_tab; i++) {
|
for (uint i = 0; i < size_tab; i++) {
|
||||||
*(tab[i]) = malloc(sizeof_tab[i]); // malloc with the corressponding size
|
*(tab[i]) = malloc(sizeof_tab[i]); // malloc with the corressponding size
|
||||||
@@ -222,11 +222,11 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
|
|||||||
((SDL_Rect*)(*(env_tab[i])))->x = 0;
|
((SDL_Rect*)(*(env_tab[i])))->x = 0;
|
||||||
((SDL_Rect*)(*(env_tab[i])))->y = 0;
|
((SDL_Rect*)(*(env_tab[i])))->y = 0;
|
||||||
}
|
}
|
||||||
// if the case has not error
|
// the normal lightbulb
|
||||||
env->lightbulb[0] = IMG_LoadTexture(ren, LIGHTBULB_WHITE);
|
env->lightbulb[0] = IMG_LoadTexture(ren, LIGHTBULB_WHITE);
|
||||||
if (!env->lightbulb[0])
|
if (!env->lightbulb[0])
|
||||||
ERROR("IMG_LoadTexture: %s\n", LIGHTBULB_WHITE);
|
ERROR("IMG_LoadTexture: %s\n", LIGHTBULB_WHITE);
|
||||||
// if the case has error
|
// the error one
|
||||||
env->lightbulb[1] = IMG_LoadTexture(ren, LIGHTBULB_RED);
|
env->lightbulb[1] = IMG_LoadTexture(ren, LIGHTBULB_RED);
|
||||||
if (!env->lightbulb[1])
|
if (!env->lightbulb[1])
|
||||||
ERROR("IMG_LoadTexture: %s\n", LIGHTBULB_RED);
|
ERROR("IMG_LoadTexture: %s\n", LIGHTBULB_RED);
|
||||||
@@ -369,8 +369,7 @@ void render(SDL_Window* win, SDL_Renderer* ren, Env* env) {
|
|||||||
int win_w, win_h, h, w;
|
int win_w, win_h, h, w;
|
||||||
SDL_GetWindowSize(win, &win_w, &win_h);
|
SDL_GetWindowSize(win, &win_w, &win_h);
|
||||||
|
|
||||||
if (win_h >
|
if (win_h > win_w) { // used to keep the ratio of the buttons
|
||||||
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;
|
h = win_w;
|
||||||
w = win_w;
|
w = win_w;
|
||||||
} else {
|
} else {
|
||||||
@@ -400,7 +399,7 @@ void render(SDL_Window* win, SDL_Renderer* ren, Env* env) {
|
|||||||
// buttons
|
// buttons
|
||||||
SDL_Rect buttons;
|
SDL_Rect buttons;
|
||||||
buttons.w = w / (NB_BUTTONS + 1);
|
buttons.w = w / (NB_BUTTONS + 1);
|
||||||
buttons.h = h / 10 / 1.5; // 1.5 = use to keep margin around buttons
|
buttons.h = h / 10 / 1.5; // 1.5 => used to keep margin around buttons
|
||||||
buttons.y = (h / 10 - buttons.h) / 2 + marge_h;
|
buttons.y = (h / 10 - buttons.h) / 2 + marge_h;
|
||||||
buttons.x = (w / NB_BUTTONS - w / (NB_BUTTONS + 1)) / 2 + marge_w;
|
buttons.x = (w / NB_BUTTONS - w / (NB_BUTTONS + 1)) / 2 + marge_w;
|
||||||
*(env->rec_undo) = buttons;
|
*(env->rec_undo) = buttons;
|
||||||
@@ -494,34 +493,41 @@ bool process(SDL_Window* win, Env* env, SDL_Event* e, SDL_Event* prec_e, int* nb
|
|||||||
if (e->type == SDL_QUIT) {
|
if (e->type == SDL_QUIT) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
/*Tests of the differents types of inputs for interact with the game*/
|
// Tests of the differents types of inputs to interact with the game
|
||||||
if (e->type == SDL_KEYDOWN) { // differents shortcuts for differents types of actions
|
if (e->type == SDL_KEYDOWN) { // differents shortcuts for differents types of actions
|
||||||
const Uint8* state = SDL_GetKeyboardState(NULL);
|
const Uint8* state = SDL_GetKeyboardState(NULL);
|
||||||
if (state[SDL_SCANCODE_F11]) { // F11 forr toggle fullscreen (pretty obvious)
|
if (state[SDL_SCANCODE_F11]) { // F11 to toggle fullscreen (pretty obvious)
|
||||||
ToggleFullscreen(win);
|
ToggleFullscreen(win);
|
||||||
} else if (state[SDL_SCANCODE_Z]) { // Z for undo a move only if you have done at least 1 move before
|
} else if (state[SDL_SCANCODE_W]) { // (W becuase sdl2 is in QWERTY) Z to undo a move only if you have done at least 1 move before
|
||||||
if ((*nb_coups) > 0) {
|
if ((*nb_coups) > 0) {
|
||||||
game_undo(env->g);
|
game_undo(env->g);
|
||||||
(*nb_undo)++;
|
(*nb_undo)++;
|
||||||
(*nb_coups)--;
|
(*nb_coups)--;
|
||||||
|
update_pressed(env, true, false, false, false, false, false);
|
||||||
}
|
}
|
||||||
} else if (state[SDL_SCANCODE_Y]) { // Y for redo a move can only be used if u heve done one or many undo before
|
} else if (state[SDL_SCANCODE_Y]) { // Y to redo a move can only if you have done at least 1 undo before
|
||||||
if ((*nb_undo) > 0) {
|
if ((*nb_undo) > 0) {
|
||||||
game_redo(env->g);
|
game_redo(env->g);
|
||||||
(*nb_undo)--;
|
(*nb_undo)--;
|
||||||
(*nb_coups)++;
|
(*nb_coups)++;
|
||||||
|
update_pressed(env, false, true, false, false, false, false);
|
||||||
}
|
}
|
||||||
} else if (state[SDL_SCANCODE_S]) { // S for solving the game immediatly
|
} else if (state[SDL_SCANCODE_S]) { // S to solving the game immediately
|
||||||
game_solve(env->g);
|
game_solve(env->g);
|
||||||
(*nb_undo) = 0;
|
(*nb_undo) = 0;
|
||||||
(*nb_coups)++;
|
(*nb_coups)++;
|
||||||
} else if (state[SDL_SCANCODE_R] || state[SDL_SCANCODE_F5]) { // R or F5 for restart the game
|
update_pressed(env, false, false, false, true, false, false);
|
||||||
|
} else if (state[SDL_SCANCODE_R] || state[SDL_SCANCODE_F5]) { // R or F5 to restart the game
|
||||||
game_restart(env->g);
|
game_restart(env->g);
|
||||||
(*nb_undo) = 0;
|
(*nb_undo) = 0;
|
||||||
(*nb_coups) = 0;
|
(*nb_coups) = 0;
|
||||||
} else if (state[SDL_SCANCODE_W]) { // W for save the game in the save.txt file
|
update_pressed(env, false, false, true, false, false, false);
|
||||||
|
} else if (state[SDL_SCANCODE_Z]) { // (Z becuase sdl2 is in QWERTY) W to save the game in the file save.txt
|
||||||
game_save(env->g, "save.txt");
|
game_save(env->g, "save.txt");
|
||||||
|
update_pressed(env, false, false, false, false, false, true);
|
||||||
}
|
}
|
||||||
|
} else if (e->type == SDL_KEYUP) {
|
||||||
|
update_pressed(env, false, false, false, false, false, false);
|
||||||
}
|
}
|
||||||
if (game_is_over(env->g) && !env->win) {
|
if (game_is_over(env->g) && !env->win) {
|
||||||
Mix_PlayMusic(env->win_music, 1);
|
Mix_PlayMusic(env->win_music, 1);
|
||||||
@@ -601,9 +607,9 @@ bool process(SDL_Window* win, Env* env, SDL_Event* e, SDL_Event* prec_e, int* nb
|
|||||||
update_pressed(env, false, false, false, false, false, true);
|
update_pressed(env, false, false, false, false, false, true);
|
||||||
} else if (SDL_PointInRect(&mouse, env->rec_game)) {
|
} 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)) -
|
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*/;
|
0.00001 /*to avoid if clicked exacly on the bottom right corner to result a 7 (in case of a game 7x7)*/;
|
||||||
uint j = (((float)mouse.x - (float)env->rec_game->x) / (float)env->rec_game->w * game_nb_cols(env->g)) -
|
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*/;
|
0.00001 /*to avoid if clicked exacly on the bottom right corner to result a 7 (in case of a game 7x7)*/;
|
||||||
|
|
||||||
if (e->type == SDL_MOUSEBUTTONDOWN && e->button.button == SDL_BUTTON_LEFT) { // only execute when type == SDL_MOUSEBUTTONDOWN
|
if (e->type == SDL_MOUSEBUTTONDOWN && e->button.button == SDL_BUTTON_LEFT) { // only execute when type == SDL_MOUSEBUTTONDOWN
|
||||||
play_light(i, j, env);
|
play_light(i, j, env);
|
||||||
|
|||||||
Reference in new issue
Block a user