portage fini :D

This commit is contained in:
Théophyl Hoguet committed 2022-03-20 06:22:29 +01:00
1 parent 4f75d464e8
commit b265f4b7d2
4 files changed
+70 -34

No files matched your search

+66 -32
View File
@@ -15,19 +15,46 @@
/* **************************************************************** */
#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 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 FONTSIZE 200
#ifdef __ANDROID__
static void copy_asset(char* src, char* dst) {
SDL_RWops* file = SDL_RWFromFile(src, "r");
if (!file)
ERROR("[ERROR] SDL_RWFromFile: %s\n", src);
int size = SDL_RWsize(file);
PRINT("copy file %s (%d bytes) into %s\n", src, size, dst);
char* buf = (char*)malloc(size + 1);
if (!buf)
ERROR("[ERROR] malloc\n");
int r = SDL_RWread(file, buf, 1, size);
PRINT("read %d\n", r);
if (r != size)
ERROR("[ERROR] fail to read all file (%d bytes)\n", r);
FILE* out = fopen(dst, "w+");
if (!out)
ERROR("[ERROR] fail to create file %s\n", dst);
int w = fwrite(buf, 1, r, out);
if (r != w)
ERROR("[ERROR] fail to write all file (%d bytes)\n", w);
fclose(out);
SDL_RWclose(file);
free(buf);
}
#endif
/* **************************************************************** */
struct Env_t {
@@ -55,8 +82,8 @@ struct Env_t {
/* **************************************************************** */
void usage(char* argv[]) {
printf("Usage : %s => Joue la partie par défaut\n", argv[0]);
printf(" %s <filename> => Charge la partie sur le fichier <filename>\n", argv[0]);
PRINT("Usage : %s => Joue la partie par défaut\n", argv[0]);
PRINT(" %s <filename> => Charge la partie sur le fichier <filename>\n", argv[0]);
exit(EXIT_FAILURE);
}
@@ -66,8 +93,8 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
env->pressed_undo = false;
env->pressed_redo = false;
env->pressed_solve = false;
printf("To win the game, you must satisfy the following conditions:\n\n");
printf(
PRINT("To win the game, you must satisfy the following conditions:\n\n");
PRINT(
"-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) {
@@ -79,10 +106,17 @@ 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)
fprintf(stderr, "TTF_OpenFont: %s\n", 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};
@@ -92,7 +126,7 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
/* init zero texture double tab*/
env->zero = malloc(sizeof(SDL_Texture*) * 2);
if (env->zero == NULL)
fprintf(stderr, "NOT ENOUGTH MEMORY\n");
ERROR("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);
@@ -105,7 +139,7 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
/* init one texture double tab*/
env->one = malloc(sizeof(SDL_Texture*) * 2);
if (env->one == NULL)
fprintf(stderr, "NOT ENOUGTH MEMORY\n");
ERROR("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);
@@ -118,7 +152,7 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
/* init two texture double tab*/
env->two = malloc(sizeof(SDL_Texture*) * 2);
if (env->two == NULL)
fprintf(stderr, "NOT ENOUGTH MEMORY\n");
ERROR("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);
@@ -131,7 +165,7 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
/* init three texture double tab*/
env->three = malloc(sizeof(SDL_Texture*) * 2);
if (env->three == NULL)
fprintf(stderr, "NOT ENOUGTH MEMORY\n");
ERROR("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);
@@ -144,7 +178,7 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
/* init four texture double tab*/
env->four = malloc(sizeof(SDL_Texture*) * 2);
if (env->four == NULL)
fprintf(stderr, "NOT ENOUGTH MEMORY\n");
ERROR("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);
@@ -157,7 +191,7 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
/* init lightbulb texture double tab*/
env->lightbulb = malloc(sizeof(SDL_Texture*) * 2);
if (env->lightbulb == NULL)
fprintf(stderr, "NOT ENOUGTH MEMORY\n");
ERROR("NOT ENOUGTH MEMORY\n");
// if the case has not error
env->lightbulb[0] = IMG_LoadTexture(ren, LIGHTBULB_WHITE);
if (!env->lightbulb[0])
@@ -170,7 +204,7 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
/* init text_restart texture double tab*/
env->text_restart = malloc(sizeof(SDL_Texture*) * 2);
if (env->text_restart == NULL)
fprintf(stderr, "NOT ENOUGTH MEMORY\n");
ERROR("NOT ENOUGTH MEMORY\n");
// if the case has not error
env->text_restart[0] = IMG_LoadTexture(ren, RESTART_UP);
if (!env->text_restart[0])
@@ -183,7 +217,7 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
/* init text_undo texture double tab*/
env->text_undo = malloc(sizeof(SDL_Texture*) * 2);
if (env->text_undo == NULL)
fprintf(stderr, "NOT ENOUGTH MEMORY\n");
ERROR("NOT ENOUGTH MEMORY\n");
// if the case has not error
env->text_undo[0] = IMG_LoadTexture(ren, UNDO_UP);
if (!env->text_undo[0])
@@ -196,7 +230,7 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
/* init text_redo texture double tab*/
env->text_redo = malloc(sizeof(SDL_Texture*) * 2);
if (env->text_redo == NULL)
fprintf(stderr, "NOT ENOUGTH MEMORY\n");
ERROR("NOT ENOUGTH MEMORY\n");
// if the case has not error
env->text_redo[0] = IMG_LoadTexture(ren, REDO_UP);
if (!env->text_redo[0])
@@ -209,7 +243,7 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
/* init text_solve texture double tab*/
env->text_solve = malloc(sizeof(SDL_Texture*) * 2);
if (env->text_solve == NULL)
fprintf(stderr, "NOT ENOUGTH MEMORY\n");
ERROR("NOT ENOUGTH MEMORY\n");
// if the case has not error
env->text_solve[0] = IMG_LoadTexture(ren, SOLVE_UP);
if (!env->text_solve[0])
@@ -221,23 +255,23 @@ Env* init(SDL_Renderer* ren, int argc, char* argv[]) {
env->rec_redo = malloc(sizeof(SDL_Rect));
if (env->rec_redo == NULL)
fprintf(stderr, "NOT ENOUGTH MEMORY\n");
ERROR("NOT ENOUGTH MEMORY\n");
env->rec_game = malloc(sizeof(SDL_Rect));
if (env->rec_game == NULL)
fprintf(stderr, "NOT ENOUGTH MEMORY\n");
ERROR("NOT ENOUGTH MEMORY\n");
env->rec_restart = malloc(sizeof(SDL_Rect));
if (env->rec_restart == NULL)
fprintf(stderr, "NOT ENOUGTH MEMORY\n");
ERROR("NOT ENOUGTH MEMORY\n");
env->rec_solve = malloc(sizeof(SDL_Rect));
if (env->rec_solve == NULL)
fprintf(stderr, "NOT ENOUGTH MEMORY\n");
ERROR("NOT ENOUGTH MEMORY\n");
env->rec_undo = malloc(sizeof(SDL_Rect));
if (env->rec_undo == NULL)
fprintf(stderr, "NOT ENOUGTH MEMORY\n");
ERROR("NOT ENOUGTH MEMORY\n");
TTF_CloseFont(font);
return env;