Data like brushes, starters,... now is loaded from assets in the android port, failed to merge some stuff...

This commit is contained in:
Pere Pujal i Carabantes 2018-01-19 08:53:49 +01:00
parent c6117bf501
commit df13e8d1bc
3 changed files with 160 additions and 28 deletions

View file

@ -39,6 +39,9 @@
#include "dirwalk.h" #include "dirwalk.h"
#include "progressbar.h" #include "progressbar.h"
#ifdef __ANDROID__
#include "android_assets.h"
#endif
/* /*
The following section renames global variables defined in SDL_Pango.h to avoid errors during linking. The following section renames global variables defined in SDL_Pango.h to avoid errors during linking.
@ -278,20 +281,69 @@ void tp_ftw(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer
tp_ftw_str *dir_names = NULL; tp_ftw_str *dir_names = NULL;
int d_namlen; int d_namlen;
int add_rsrc; int add_rsrc;
unsigned dlen;
dir[dirlen++] = '/'; dir[dirlen++] = '/';
dir[dirlen] = '\0'; dir[dirlen] = '\0';
//printf("processing directory %s %d\n", dir, dirlen); //printf("processing directory %s %d\n", dir, dirlen);
dlen = dirlen;
/* Open the directory: */ /* Open the directory: */
d = opendir(dir); d = opendir(dir);
#ifdef __ANDROID__
char *di;
AAssetDir *adir;
if (!d) /* Fallback into assets */
{
/* Remove the trailing '/' */
dlen = strlen(dir) - 1;
di = strndup(dir, dlen);
adir = open_asset_dir(di);
if (!adir)
return;
}
#else
if (!d) if (!d)
return; return;
#endif
for (;;) for (;;)
{ {
struct dirent *f = readdir(d); struct dirent *f;
if (d)
f = readdir(d);
int filetype = TP_FTW_UNKNOWN; int filetype = TP_FTW_UNKNOWN;
#ifdef __ANDROID__
char *afilename;
if (!d)
{
afilename = AAssetDir_getNextFileName(adir);
if (afilename)
{
f = malloc(sizeof(struct dirent));
strncpy(f->d_name, afilename, sizeof(f->d_name));
f->d_type = DT_REG;
/* There is not _DIRENT_HAVE_D_NAMLEN currently on Android 4.3, but who knows in the future... */
#if defined(_DIRENT_HAVE_D_NAMLEN)
f->d_namlen = strlen(f->d_name);
#endif
/* AAssetDir_getNextFileName() only lists files, not (sub)dirs,
and we don't put any device or special file inside assets,
so it is a regular file */
filetype = TP_FTW_NORMAL;
}
else
break;
}
#endif
if (!f) if (!f)
break; break;
if (f->d_name[0] == '.') if (f->d_name[0] == '.')
@ -393,6 +445,11 @@ void tp_ftw(SDL_Surface * screen, SDL_Texture * texture, SDL_Renderer * renderer
} }
free(file_names); free(file_names);
#else #else
#ifdef __ANDROID__
if (dlen != dirlen) /* First case only happens in Android files coming from assets */
fn(screen, texture, renderer, di, dlen, file_names, num_file_names, locale);
else
#endif
fn(screen, texture, renderer, dir, dirlen, file_names, num_file_names, locale); fn(screen, texture, renderer, dir, dirlen, file_names, num_file_names, locale);
#endif #endif
} }

View file

@ -45,7 +45,7 @@ void show_progress_bar_(SDL_Surface * screen, SDL_Texture * texture, SDL_Rendere
return; return;
newtime = SDL_GetTicks(); newtime = SDL_GetTicks();
if (newtime > oldtime + 15) // trying not to eat some serious CPU time! if (newtime > oldtime + 15) /* trying not to eat some serious CPU time! */
{ {
for (x = 0; x < screen->w; x = x + 65) for (x = 0; x < screen->w; x = x + 65)
{ {
@ -62,8 +62,6 @@ void show_progress_bar_(SDL_Surface * screen, SDL_Texture * texture, SDL_Rendere
prog_bar_ctr++; prog_bar_ctr++;
// FIXME SDL2
// SDL_UpdateRect(screen, 0, screen->h - 24, screen->w, 24);
r.x = 0; r.x = 0;
r.y = screen->h - 24; r.y = screen->h - 24;
r.w = screen->w; r.w = screen->w;
@ -71,11 +69,9 @@ void show_progress_bar_(SDL_Surface * screen, SDL_Texture * texture, SDL_Rendere
SDL_UpdateTexture(texture, &r, screen->pixels + ((screen->h - 24) * screen->pitch), screen->pitch); SDL_UpdateTexture(texture, &r, screen->pixels + ((screen->h - 24) * screen->pitch), screen->pitch);
// NOTE docs says one should clear the renderer, however this means a refresh of the whole thing. /* Docs says one should clear the renderer, even if this means a refresh of the whole thing. */
// SDL_RenderClear(renderer); SDL_RenderClear(renderer);
// SDL_RenderCopy(renderer, texture, NULL, NULL); SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderCopy(renderer, texture, &r, &r);
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
} }
oldtime = newtime; oldtime = newtime;

View file

@ -306,6 +306,7 @@ typedef struct safer_dirent
#define AUTOSAVE_GOING_BACKGROUND #define AUTOSAVE_GOING_BACKGROUND
#include "android_print.h" #include "android_print.h"
#include "android_assets.h"
#else #else
@ -688,6 +689,8 @@ static SDL_Rect r_ttoolopt; /* was 96x40 @ 544,0 (title for tool options) */
static SDL_Rect r_tuxarea; /* was 640x56 */ static SDL_Rect r_tuxarea; /* was 640x56 */
static SDL_Rect r_label; static SDL_Rect r_label;
static SDL_Rect old_dest; static SDL_Rect old_dest;
static SDL_Rect r_tir; /* Text input rectangle */
static float render_scale; /* Scale factor for the render */
static int button_w; /* was 48 */ static int button_w; /* was 48 */
static int button_h; /* was 48 */ static int button_h; /* was 48 */
@ -2241,6 +2244,10 @@ static void mainloop(void)
valhat_y = 0; valhat_y = 0;
done = 0; done = 0;
keyglobal = 0; keyglobal = 0;
r_tir.x = 0;
r_tir.y = 0;
r_tir.w = 0;
r_tir.h = 0;
if (NUM_TOOLS > 14 + TOOLOFFSET) if (NUM_TOOLS > 14 + TOOLOFFSET)
{ {
@ -2703,6 +2710,15 @@ static void mainloop(void)
charsize(event.key.keysym.unicode), iswprint(key_unicode) ? L"" : L"not ", key_down); charsize(event.key.keysym.unicode), iswprint(key_unicode) ? L"" : L"not ", key_down);
#endif #endif
#endif #endif
/* Set the text input rectangle for system onscreen keyboards */
if (onscreen_keyboard && !kbd)
{
r_tir.y = (float)cursor_y / render_scale;
r_tir.x = (float)cursor_x / render_scale;
SDL_SetTextInputRect(&r_tir);
SDL_StartTextInput();
}
/* Discard previous # of redraw characters */ /* Discard previous # of redraw characters */
if ((int)texttool_len <= redraw) if ((int)texttool_len <= redraw)
@ -3026,7 +3042,15 @@ static void mainloop(void)
PROMPT_TIP_LEFTCLICK_YES, PROMPT_TIP_LEFTCLICK_YES,
"", img_mouse, img_mouse_click, NULL, 1, event.button.x, event.button.y); "", img_mouse, img_mouse_click, NULL, 1, event.button.x, event.button.y);
if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL)
{
if (onscreen_keyboard && !kbd)
{
r_tir.y = (float)event.button.y / render_scale;
SDL_SetTextInputRect(&r_tir);
SDL_StartTextInput();
}
do_render_cur_text(0); do_render_cur_text(0);
}
draw_tux_text(TUX_BORED, "", 0); draw_tux_text(TUX_BORED, "", 0);
} }
} }
@ -3156,6 +3180,8 @@ static void mainloop(void)
if (onscreen_keyboard && !kbd) if (onscreen_keyboard && !kbd)
{ {
r_tir.y = (float)event.button.y / render_scale;
SDL_SetTextInputRect(&r_tir);
SDL_StartTextInput(); SDL_StartTextInput();
} }
if (!font_thread_done) if (!font_thread_done)
@ -4465,6 +4491,13 @@ static void mainloop(void)
} }
else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL) else if (cur_tool == TOOL_TEXT || cur_tool == TOOL_LABEL)
{ {
if (onscreen_keyboard && !kbd)
{
r_tir.y = (float)old_y / render_scale;
SDL_SetTextInputRect(&r_tir);
SDL_StartTextInput();
}
/* Text and Label Tools! */ /* Text and Label Tools! */
if (cur_tool == TOOL_LABEL && cur_label == LABEL_SELECT) if (cur_tool == TOOL_LABEL && cur_label == LABEL_SELECT)
{ {
@ -4515,6 +4548,8 @@ static void mainloop(void)
if (onscreen_keyboard && !kbd) if (onscreen_keyboard && !kbd)
{ {
r_tir.y = (float)old_y / render_scale;
SDL_SetTextInputRect(&r_tir);
SDL_StartTextInput(); SDL_StartTextInput();
} }
do_render_cur_text(0); do_render_cur_text(0);
@ -4595,6 +4630,8 @@ static void mainloop(void)
if (onscreen_keyboard && !kbd) if (onscreen_keyboard && !kbd)
{ {
r_tir.y = (float)cursor_y / render_scale;
SDL_SetTextInputRect(&r_tir);
SDL_StartTextInput(); SDL_StartTextInput();
} }
} }
@ -7617,7 +7654,11 @@ static void load_stamps(SDL_Surface * screen)
default_stamp_size = compute_default_scale_factor(1.0); default_stamp_size = compute_default_scale_factor(1.0);
load_stamp_dir(screen, homedirdir); load_stamp_dir(screen, homedirdir);
#ifndef __ANDROID__
load_stamp_dir(screen, DATA_PREFIX "stamps"); load_stamp_dir(screen, DATA_PREFIX "stamps");
#else
load_stamp_dir(screen, ASSETS_STAMPS_DIR);
#endif
#ifdef __APPLE__ #ifdef __APPLE__
load_stamp_dir(screen, "Resources/stamps"); load_stamp_dir(screen, "Resources/stamps");
load_stamp_dir(screen, "/Library/Application Support/TuxPaint/stamps"); load_stamp_dir(screen, "/Library/Application Support/TuxPaint/stamps");
@ -13487,8 +13528,8 @@ static void do_png_embed_data(png_structp png_ptr)
for (y = 0; y < current_node->save_height; y++) for (y = 0; y < current_node->save_height; y++)
{ {
pix = pix =
getpixels[current_node->label_node_surface->format-> getpixels[current_node->label_node_surface->format->BytesPerPixel] (current_node->
BytesPerPixel] (current_node->label_node_surface, x, y); label_node_surface, x, y);
SDL_GetRGBA(pix, current_label_node->label_node_surface->format, &r, &g, &b, &a); SDL_GetRGBA(pix, current_label_node->label_node_surface->format, &r, &g, &b, &a);
fwrite(&a, alpha_size, 1, lfi); fwrite(&a, alpha_size, 1, lfi);
@ -16390,6 +16431,8 @@ static void do_render_cur_text(int do_blit)
w = tmp_surf->w; w = tmp_surf->w;
h = tmp_surf->h; h = tmp_surf->h;
r_tir.h = (float)tmp_surf->h / render_scale;
r_tir.w = (float)tmp_surf->w / render_scale;
cursor_textwidth = w; cursor_textwidth = w;
} }
@ -18503,6 +18546,33 @@ static int do_new_dialog(void)
closedir(d); closedir(d);
} }
#ifdef __ANDROID__
else if (dirname[places_to_look][0] != "/")
{
/* Try inside android assets only if it is a relative path */
AAssetDir *ad = open_asset_dir(dirname[places_to_look]);
const char *afilename = (const char *)NULL;
while ((afilename = AAssetDir_getNextFileName(ad)) != NULL)
{
f = malloc(sizeof(struct dirent));
strncpy(f->d_name, afilename, sizeof(f->d_name));
memcpy(&(fs[num_files_in_dirs].f), f, sizeof(struct dirent));
fs[num_files_in_dirs].place = places_to_look;
free(f);
num_files_in_dirs++;
if (num_files_in_dirs >= things_alloced)
{
things_alloced = things_alloced + 32;
fs = (struct dirent2 *)realloc(fs, sizeof(struct dirent2) * things_alloced);
}
}
}
#endif
} }
@ -21968,12 +22038,14 @@ static void parse_file_options(struct cfginfo *restrict tmpcfg, const char *file
*arg++ = '\0'; *arg++ = '\0';
#ifdef __linux__ #ifdef __linux__
#ifndef __ANDROID__
/* Perform shell expansion */ /* Perform shell expansion */
wordexp_t result; wordexp_t result;
wordexp(arg, &result, 0); wordexp(arg, &result, 0);
arg = strdup(result.we_wordv[0]); arg = strdup(result.we_wordv[0]);
wordfree(&result); wordfree(&result);
#endif
#endif #endif
/* FIXME: leaking mem here, but the trouble is that these /* FIXME: leaking mem here, but the trouble is that these
@ -21982,7 +22054,9 @@ static void parse_file_options(struct cfginfo *restrict tmpcfg, const char *file
of the strings end up being kept around */ of the strings end up being kept around */
parse_one_option(tmpcfg, str, strdup(arg), filename); parse_one_option(tmpcfg, str, strdup(arg), filename);
#ifdef __linux__ #ifdef __linux__
#ifndef __ANDROID__
free(arg); free(arg);
#endif
#endif #endif
} }
fclose(fi); fclose(fi);
@ -23004,6 +23078,7 @@ static void setup(void)
SDL_Thread *fontconfig_thread; SDL_Thread *fontconfig_thread;
#endif #endif
render_scale = 1.0;
#ifdef _WIN32 #ifdef _WIN32
@ -23027,9 +23102,7 @@ static void setup(void)
if (disable_screensaver == 0) if (disable_screensaver == 0)
{ {
putenv((char *)"SDL_VIDEO_ALLOW_SCREENSAVER=1"); putenv((char *)"SDL_VIDEO_ALLOW_SCREENSAVER=1");
if (SDL_MAJOR_VERSION < 1 || if (SDL_MAJOR_VERSION < 2 || (SDL_MAJOR_VERSION == 2 && SDL_MINOR_VERSION == 0 && SDL_PATCHLEVEL < 2))
(SDL_MAJOR_VERSION >= 1 && SDL_MINOR_VERSION < 2) ||
(SDL_MAJOR_VERSION >= 1 && SDL_MINOR_VERSION >= 2 && SDL_PATCHLEVEL < 12))
{ {
fprintf(stderr, "Note: 'allowscreensaver' requires SDL 1.2.12 or higher\n"); fprintf(stderr, "Note: 'allowscreensaver' requires SDL 1.2.12 or higher\n");
} }
@ -23245,6 +23318,7 @@ static void setup(void)
/* Keep things squared */ /* Keep things squared */
ww = window_scale_w * ww; ww = window_scale_w * ww;
hh = window_scale_w * hh; hh = window_scale_w * hh;
render_scale = window_scale_w;
SDL_RenderSetScale(renderer, window_scale_w, window_scale_w); SDL_RenderSetScale(renderer, window_scale_w, window_scale_w);
} }
@ -23252,6 +23326,7 @@ static void setup(void)
{ {
ww = window_scale_h * ww; ww = window_scale_h * ww;
hh = window_scale_h * hh; hh = window_scale_h * hh;
render_scale = window_scale_h;
SDL_RenderSetScale(renderer, window_scale_h, window_scale_h); SDL_RenderSetScale(renderer, window_scale_h, window_scale_h);
} }
@ -23267,7 +23342,6 @@ static void setup(void)
screen = SDL_CreateRGBSurface(0, ww, hh, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000); screen = SDL_CreateRGBSurface(0, ww, hh, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
if (screen == NULL) if (screen == NULL)
{ {
fprintf(stderr, fprintf(stderr,
@ -24167,9 +24241,14 @@ int main(int argc, char *argv[])
/* do not add code (slowness) here unless required for scanning fonts */ /* do not add code (slowness) here unless required for scanning fonts */
progname = argv[0]; progname = argv[0];
#if defined(DEBUG) && defined(__APPLE__) #if defined(DEBUG)
#if defined(__APPLE__)
/* EP added block to log messages */ /* EP added block to log messages */
freopen("/tmp/tuxpaint.log", "w", stdout); /* redirect stdout to a file */ freopen("/tmp/tuxpaint.log", "w", stdout); /* redirect stdout to a file */
#elif defined (__ANDROID__)
freopen("/mnt/sdcard/tuxpaint/tuxpaint.log", "w", stdout); /* redirect stdout to a file */
#endif
dup2(fileno(stdout), fileno(stderr)); /* redirect stderr to stdout */ dup2(fileno(stdout), fileno(stderr)); /* redirect stderr to stdout */
setvbuf(stdout, NULL, _IONBF, 0); /* we don't want buffering to avoid de-sync'ing stdout and stderr */ setvbuf(stdout, NULL, _IONBF, 0); /* we don't want buffering to avoid de-sync'ing stdout and stderr */
setvbuf(stderr, NULL, _IONBF, 0); /* we don't want buffering to avoid de-sync'ing stdout and stderr */ setvbuf(stderr, NULL, _IONBF, 0); /* we don't want buffering to avoid de-sync'ing stdout and stderr */
@ -24192,7 +24271,7 @@ int main(int argc, char *argv[])
* file may not exist on the runtime system, however, so we copy the file * file may not exist on the runtime system, however, so we copy the file
* into our app bundle at compile time, and tell Fontconfig here to look for * into our app bundle at compile time, and tell Fontconfig here to look for
* the file within the app bundle. */ * the file within the app bundle. */
putenv((char*) "FONTCONFIG_PATH=Resources/etc"); putenv((char *)"FONTCONFIG_PATH=Resources/etc");
#endif #endif
#ifdef FORKED_FONTS #ifdef FORKED_FONTS