From 831efb6ecc0f8a756ada5269e3488d70973f8e16 Mon Sep 17 00:00:00 2001 From: xianjimli Date: Wed, 31 Jul 2019 17:17:59 +0800 Subject: [PATCH] improve for android --- src/base/assets_manager.c | 40 ++++++++++++++++++++++++------- src/tkc/types_def.h | 7 +++++- src/vgcanvas/vgcanvas_nanovg_gl.c | 4 ++-- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/base/assets_manager.c b/src/base/assets_manager.c index 1fe6dba97..475ad8096 100644 --- a/src/base/assets_manager.c +++ b/src/base/assets_manager.c @@ -80,8 +80,8 @@ static const char* assets_manager_get_res_root(assets_manager_t* am) { } } -static asset_info_t* load_asset(uint16_t type, uint16_t subtype, uint32_t size, const char* path, - const char* name) { +static asset_info_t* asset_info_create(uint16_t type, uint16_t subtype, const char* name, + int32_t size) { asset_info_t* info = TKMEM_ALLOC(sizeof(asset_info_t) + size); return_value_if_fail(info != NULL, NULL); @@ -93,10 +93,37 @@ static asset_info_t* load_asset(uint16_t type, uint16_t subtype, uint32_t size, info->is_in_rom = FALSE; strncpy(info->name, name, TK_NAME_LEN); + return info; +} + +#ifdef WITH_SDL +#include +static asset_info_t* load_asset(uint16_t type, uint16_t subtype, const char* path, + const char* name) { + SDL_RWops* rwops = SDL_RWFromFile(path, "r"); + return_value_if_fail(rwops != NULL, NULL); + + int32_t size = rwops->size(rwops); + asset_info_t* info = asset_info_create(type, subtype, name, size); + if (info != NULL) { + rwops->read(rwops, info->data, size, 1); + } + rwops->close(rwops); + + return info; +} +#else +static asset_info_t* load_asset(uint16_t type, uint16_t subtype, const char* path, + const char* name) { + int32_t size = file_get_size(path); + asset_info_t* info = asset_info_create(type, subtype, name, size); + return_value_if_fail(info != NULL, NULL); + ENSURE(file_read_part(path, info->data, size, 0) == size); return info; } +#endif /*WITH_SDL*/ static ret_t build_path(assets_manager_t* am, char* path, uint32_t size, bool_t ratio_sensitive, const char* subpath, const char* name, const char* extname) { @@ -157,8 +184,7 @@ static asset_info_t* try_load_image(assets_manager_t* am, const char* name, NULL); if (file_exist(path)) { - size_t size = file_get_size(path); - asset_info_t* info = load_asset(ASSET_TYPE_IMAGE, subtype, size, path, name); + asset_info_t* info = load_asset(ASSET_TYPE_IMAGE, subtype, path, name); return info; } @@ -205,8 +231,7 @@ static asset_info_t* try_load_assets(assets_manager_t* am, const char* name, con return_value_if_fail(build_path(am, path, MAX_PATH, FALSE, subpath, name, extname) == RET_OK, NULL); if (file_exist(path)) { - int32_t size = file_get_size(path); - return load_asset(type, subtype, size, path, name); + return load_asset(type, subtype, path, name); } return NULL; @@ -239,11 +264,10 @@ static uint16_t subtype_from_extname(const char* extname) { asset_info_t* assets_manager_load_file(assets_manager_t* am, asset_type_t type, const char* path) { if (file_exist(path)) { - int32_t size = file_get_size(path); const char* extname = strrchr(path, '.'); uint16_t subtype = subtype_from_extname(extname); - return load_asset(type, subtype, size, path, path); + return load_asset(type, subtype, path, path); } return NULL; diff --git a/src/tkc/types_def.h b/src/tkc/types_def.h index 210cb6bd1..5a64039b6 100644 --- a/src/tkc/types_def.h +++ b/src/tkc/types_def.h @@ -161,7 +161,12 @@ typedef enum _ret_t { RET_BAD_PARAMS } ret_t; -#ifdef WIN32 +#ifdef ANDROID +#define log_debug(format, ...) __android_log_print(ANDROID_LOG_DEBUG, "AWTK", format, __VA_ARGS__) +#define log_info(format, ...) __android_log_print(ANDROID_LOG_INFO, "AWTK", format, __VA_ARGS__) +#define log_warn(format, ...) __android_log_print(ANDROID_LOG_WARN, "AWTK", format, __VA_ARGS__) +#define log_error(format, ...) __android_log_print(ANDROID_LOG_ERROR, "AWTK", format, __VA_ARGS__) +#elif defined(WIN32) #include #define random rand #define srandom srand diff --git a/src/vgcanvas/vgcanvas_nanovg_gl.c b/src/vgcanvas/vgcanvas_nanovg_gl.c index ae36e21e3..0c1d88cf1 100644 --- a/src/vgcanvas/vgcanvas_nanovg_gl.c +++ b/src/vgcanvas/vgcanvas_nanovg_gl.c @@ -41,7 +41,7 @@ #include "GLES/gl.h" #include "GLES2/gl2.h" #include "GLES2/gl2ext.h" -#endif/*WITHOUT_GLAD*/ +#endif /*WITHOUT_GLAD*/ #include "nanovg.h" #include "tkc/utf8.h" @@ -75,7 +75,7 @@ static ret_t vgcanvas_init_gl(vgcanvas_nanovg_t* nanovg, SDL_Window* win) { SDL_GL_SetSwapInterval(1); #ifndef WITHOUT_GLAD gladLoadGL(); -#endif/*WITHOUT_GLAD*/ +#endif /*WITHOUT_GLAD*/ glEnable(GL_ALPHA_TEST); glEnable(GL_STENCIL_TEST);