From a24c65451a10e9edb1766034fbea899e45bacf8a Mon Sep 17 00:00:00 2001 From: Pere Pujal i Carabantes Date: Thu, 18 Jan 2018 00:41:38 +0100 Subject: [PATCH] Data like brushes, starters,... now is loaded from assets in the android port. --- src/android_assets.c | 114 +++++++++++++++++++++++++++++++++++++++++++ src/android_assets.h | 50 +++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 src/android_assets.c create mode 100644 src/android_assets.h diff --git a/src/android_assets.c b/src/android_assets.c new file mode 100644 index 000000000..3aca6406c --- /dev/null +++ b/src/android_assets.c @@ -0,0 +1,114 @@ +/* + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + (See COPYING.txt) +*/ + +#include "android_assets.h" + +AAssetManager * asset_manager = NULL; + +AAssetDir * open_asset_dir(char * dirname) +{ + return(AAssetManager_openDir(asset_manager, dirname)); + +} +void load_assets_dir(char * dirname, tp_ftw_str ** ffilenames, unsigned * num_file_names) +{ + AAssetDir* assetDir = AAssetManager_openDir(asset_manager, dirname); + const char* filename = (const char*)NULL; + tp_ftw_str * filenames = NULL; + + unsigned max_file_names = 0; + int fulllen = 0; + *num_file_names = 0; + + + while ((filename = AAssetDir_getNextFileName(assetDir)) != NULL) + { + char *cp; + + fulllen = strlen(filename) + 1; + + if (*num_file_names == max_file_names) + { + max_file_names = max_file_names * 5 / 4 + 30; + filenames = realloc(filenames, max_file_names * sizeof *filenames); + } + cp = malloc(fulllen + 1); + memcpy(cp, filename, fulllen); + filenames[*num_file_names].str = cp; + filenames[*num_file_names].len = fulllen; + *num_file_names = *num_file_names + 1; + } + + AAssetDir_close(assetDir); + + *ffilenames = filenames; +} + +JNIEXPORT jboolean Java_org_tuxpaint_tuxpaintActivity_managertojni(JNIEnv * env, jclass clazz, jobject mgr) +{ + asset_manager = AAssetManager_fromJava(env, mgr); + + if (asset_manager == NULL) + return 0; + else + return 1; +} + + + +void load_brushes_from_assets(SDL_Surface * screen, SDL_Texture *texture, SDL_Renderer *renderer, const char * dirname, void (*fn) (SDL_Surface * screen, + SDL_Texture * texture, + SDL_Renderer * renderer, + const char *restrict const dir, + unsigned dirlen, tp_ftw_str * files, + unsigned count, const char *restrict const locale)) +{ + unsigned num_file_names = 0; + char * dir = "data/brushes"; + char buf[TP_FTW_PATHSIZE]; + unsigned dirlen = strlen(dirname); + + memcpy(buf, dir, dirlen); + + tp_ftw_str * filenames = NULL; + + load_assets_dir(dirname, &filenames, &num_file_names); + fn(screen, texture, renderer, dir, dirlen, filenames, num_file_names, NULL); +} + + + + +void load_from_assets(SDL_Surface * screen, SDL_Texture *texture, SDL_Renderer *renderer, const char * dirname, void (*fn) (SDL_Surface * screen, + SDL_Texture * texture, + SDL_Renderer * renderer, + const char *restrict const dir, + unsigned dirlen, tp_ftw_str * files, + unsigned count, const char *restrict const locale)) +{ + unsigned num_file_names = 0; + // char * dir = "data/stamps/cartoon/tux"; + char buf[TP_FTW_PATHSIZE]; + unsigned dirlen = strlen(dirname); + + memcpy(buf, dirname, dirlen); + + tp_ftw_str * filenames = NULL; + + load_assets_dir(dirname, &filenames, &num_file_names); + fn(screen, texture, renderer, dirname, dirlen, filenames, num_file_names, NULL); +} diff --git a/src/android_assets.h b/src/android_assets.h new file mode 100644 index 000000000..7210b33f9 --- /dev/null +++ b/src/android_assets.h @@ -0,0 +1,50 @@ +/* + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + (See COPYING.txt) +*/ +#ifndef TP_ANDROID_ASSETS + +#define TP_ANDROID_ASSETS +#include +#include "debug.h" +#include "dirwalk.h" +#include "progressbar.h" +#include + +/* AAssetManager_openDir() only lists files, not directories, so we can't relay on + directory exploring and have to keep track of what we put on the assets dir */ +#define ASSETS_BRUSHES_DIR "data/brushes" +#define ASSETS_STAMPS_DIR "stamps/cartoon/tux" + +AAssetDir * open_asset_dir(char * dirname); + +void load_brushes_from_assets(SDL_Surface * screen, SDL_Texture *texture, SDL_Renderer *renderer, const char * dirname, void (*fn) (SDL_Surface * screen, + SDL_Texture * texture, + SDL_Renderer * renderer, + const char *restrict const dir, + unsigned dirlen, tp_ftw_str * files, + unsigned count, const char *restrict const locale) ); + +void load_from_assets(SDL_Surface * screen, SDL_Texture *texture, SDL_Renderer *renderer, const char * dirname, void (*fn) (SDL_Surface * screen, + SDL_Texture * texture, + SDL_Renderer * renderer, + const char *restrict const dir, + unsigned dirlen, tp_ftw_str * files, + unsigned count, const char *restrict const locale) ); + +JNIEXPORT jboolean Java_org_tuxpaint_tuxpaintActivity_managertojni(JNIEnv * env, jclass clazz, jobject mgr); + + +#endif