init&cleanSDL (closes #16)

This commit is contained in:
Mohamed Fatih committed 2022-03-18 16:44:01 +01:00
1 parent e5767a0e04
commit bdcdbf41d7
1 file changed
+44 -28
+44 -28
View File
@@ -63,10 +63,10 @@ void usage(char* argv[]) {
Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
Env* env = malloc(sizeof(struct Env_t));
printf("To win the game, you must satisfy the following conditions:\n");
printf("To win the game, you must satisfy the following conditions:\n\n");
printf(
"All non-black squares are lit.\nNo light is lit by another light.\nEach numbered black square must be orthogonally adjacent to exactly the given "
"number of lights.\nNon-numbered black squares may have any number of lights adjacent to them.\n");
"-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 "
"number of lights.\n-Non-numbered black squares may have any number of lights adjacent to them.\n");
if (argc == 1) {
// Create new game
env->g = game_default();
@@ -157,10 +157,12 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
fprintf(stderr, "NOT ENOUGTH MEMORY\n");
// if the case has not error
env->lightbulb[0] = IMG_LoadTexture(ren, LIGHTBULB_WHITE);
if (!env->lightbulb[0]) ERROR("IMG_LoadTexture: %s\n", LIGHTBULB_WHITE);
if (!env->lightbulb[0])
ERROR("IMG_LoadTexture: %s\n", LIGHTBULB_WHITE);
// if the case has error
env->lightbulb[1] = IMG_LoadTexture(ren, LIGHTBULB_BLACK);
if (!env->lightbulb[1]) ERROR("IMG_LoadTexture: %s\n", LIGHTBULB_BLACK);
if (!env->lightbulb[1])
ERROR("IMG_LoadTexture: %s\n", LIGHTBULB_BLACK);
/* init text_restart texture double tab*/
env->text_restart = malloc(sizeof(SDL_Texture*) * 2);
@@ -168,10 +170,12 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
fprintf(stderr, "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 (!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);
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);
@@ -179,10 +183,12 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
fprintf(stderr, "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 (!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);
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);
@@ -190,10 +196,12 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
fprintf(stderr, "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 (!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);
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);
@@ -201,10 +209,12 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
fprintf(stderr, "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 (!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);
if (!env->text_solve[1])
ERROR("IMG_LoadTexture: %s\n", SOLVE_DOWN);
env->rec_redo = malloc(sizeof(SDL_Rect));
if (env->rec_redo == NULL)
@@ -409,6 +419,7 @@ bool process(SDL_Window* win, Env* env, SDL_Event* e) {
/* **************************************************************** */
void clean(Env* env) {
game_delete(env->g);
SDL_DestroyTexture(env->zero[0]);
SDL_DestroyTexture(env->zero[1]);
free(env->zero);
@@ -421,21 +432,26 @@ void clean(Env* env) {
SDL_DestroyTexture(env->four[0]);
SDL_DestroyTexture(env->four[1]);
free(env->four);
/*SDL_DestroyTexture(env->one);
SDL_DestroyTexture(env->two);
SDL_DestroyTexture(env->three);
SDL_DestroyTexture(env->four);
SDL_DestroyTexture(env->lightbulb);
SDL_DestroyTexture(env->text_restart);
SDL_DestroyTexture(env->text_undo);
SDL_DestroyTexture(env->text_redo);
SDL_DestroyTexture(env->text_solve);
SDL_DestroyTexture(env->but_redo_up);
SDL_DestroyTexture(env->but_redo_down);
SDL_DestroyTexture(env->but_solve_up);
SDL_DestroyTexture(env->but_solve_down);
*/
SDL_DestroyTexture(env->lightbulb[0]);
SDL_DestroyTexture(env->lightbulb[1]);
free(env->lightbulb);
SDL_DestroyTexture(env->text_restart[0]);
SDL_DestroyTexture(env->text_restart[1]);
free(env->text_restart);
SDL_DestroyTexture(env->text_undo[0]);
SDL_DestroyTexture(env->text_undo[1]);
free(env->text_undo);
SDL_DestroyTexture(env->text_redo[0]);
SDL_DestroyTexture(env->text_redo[1]);
free(env->text_redo);
SDL_DestroyTexture(env->text_solve[0]);
SDL_DestroyTexture(env->text_solve[1]);
free(env->text_solve);
free(env->rec_redo);
free(env->rec_undo);
free(env->rec_restart);
free(env->rec_solve);
free(env->rec_game);
free(env);
}