From b265f4b7d2560d387cea46488e92e06b3d20b7a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophyl=20Hoguet?= Date: Sun, 20 Mar 2022 06:22:29 +0100 Subject: [PATCH] portage fini :D --- .vscode/settings.json | 3 +- android/jni/src/Android.mk | 2 +- main.c | 1 + model.c | 98 +++++++++++++++++++++++++------------- 4 files changed, 70 insertions(+), 34 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 9073867..457b638 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,7 +5,8 @@ "game.h": "c", "game_private.h": "c", "history.h": "c", - "game_tools.h": "c" + "game_tools.h": "c", + "chrono": "c" }, "editor.formatOnSave": true, "C_Cpp.clang_format_path": "/usr/bin/clang-format", diff --git a/android/jni/src/Android.mk b/android/jni/src/Android.mk index a99fe96..440a012 100644 --- a/android/jni/src/Android.mk +++ b/android/jni/src/Android.mk @@ -10,7 +10,7 @@ LOCAL_CFLAGS += -g -std=c99 LOCAL_C_INCLUDES := $(LOCAL_PATH)/$(SDL_PATH)/include -YOUR_SRC_FILES= game_aux.c game_ext.c game_private.c game_tools.c game.c history.c main.c model.c +YOUR_SRC_FILES= main.c game_aux.c game_ext.c game_private.c game_tools.c game.c history.c model.c LOCAL_SRC_FILES := $(SDL_PATH)/src/main/android/SDL_android_main.c $(YOUR_SRC_FILES) diff --git a/main.c b/main.c index bd9625c..624ff07 100644 --- a/main.c +++ b/main.c @@ -10,6 +10,7 @@ /* **************************************************************** */ int main(int argc, char* argv[]) { + PRINT("CA MARCHE\n"); /* initialize SDL2 and some extensions */ if (SDL_Init(SDL_INIT_VIDEO) != 0) ERROR("Error: SDL_Init VIDEO (%s)", SDL_GetError()); diff --git a/model.c b/model.c index dfa1d36..8347341 100644 --- a/model.c +++ b/model.c @@ -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 => Charge la partie sur le fichier \n", argv[0]); + PRINT("Usage : %s => Joue la partie par défaut\n", argv[0]); + PRINT(" %s => Charge la partie sur le fichier \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;