Skip to content

Commit

Permalink
cache images
Browse files Browse the repository at this point in the history
  • Loading branch information
crowriot committed Jan 26, 2013
1 parent 81de027 commit a254188
Showing 1 changed file with 59 additions and 29 deletions.
88 changes: 59 additions & 29 deletions modules/cuttherope.c
Expand Up @@ -69,9 +69,9 @@ typedef struct {

#define MAX_IMAGES 200
typedef struct {
char* name;
char filename[PATH_MAX];
jint id;
image_t image;
image_t* image;
} Image;


Expand Down Expand Up @@ -262,15 +262,35 @@ cuttherope_CallObjectMethodV(JNIEnv *env, jobject p1, jmethodID p2, va_list p3)
return GLOBAL_J(env);
}

static void
cuttherope_CallVoidMethodV(JNIEnv* env, jobject p1, jmethodID p2, va_list p3)
static image_t*
cuttherope_loadImage(JNIEnv *env, const char *filename )
{
if (!p2)
return;
int i;
Image* img = 0;
for (i=0; i<MAX_IMAGES;i++) {
img = &cuttherope_priv.images[i];
if (img->filename==0 || img->filename[0]==0) {
break;
}
if (strcmp(img->filename,filename)==0) {
MODULE_DEBUG_PRINTF("cuttherope_loadImage: returning cached image %s\n",filename);
return img->image;
}
}

MODULE_DEBUG_PRINTF("cuttherope_CallVoidMethodV %s\n",p2->name);
if (i>=MAX_IMAGES) {
MODULE_DEBUG_PRINTF("cuttherope_loadImage: Images exceeding limits: %s\n",filename);
return NULL;
}

if (strcmp(p2->name,"loadImage")==0) {//public void loadImage(String paramString, int paramInt)
strcpy(img->filename,filename);
img->image = NULL;

char *buf;
size_t buf_size;
char filepath[PATH_MAX]; sprintf(filepath,"assets/%s",filename);

if (GLOBAL_J(env)->read_file(filepath, &buf, &buf_size)) {

imageloadersettings_t loadsettings =
{
Expand All @@ -280,32 +300,38 @@ cuttherope_CallVoidMethodV(JNIEnv* env, jobject p1, jmethodID p2, va_list p3)
.swaprb = 1,
};

struct dummy_jstring *filename = va_arg(p3, struct dummy_jstring*);
jint fileId = va_arg(p3, jint);
image_t* image = 0;
if (strstr(filepath,".png")!=0) {
img->image = loadpng_mem(buf,buf_size,loadsettings);
}
else
if (strstr(filepath,".jpeg")!=0) {
img->image = loadjpeg_mem(buf,buf_size,loadsettings);
}
}

return img->image;
}

MODULE_DEBUG_PRINTF("cuttherope_CallVoidMethodV(%s): filename=%s fileId=%d\n",p2->name,filename->data,fileId);
static void
cuttherope_CallVoidMethodV(JNIEnv* env, jobject p1, jmethodID p2, va_list p3)
{
if (!p2)
return;

char *buf;
size_t buf_size;
MODULE_DEBUG_PRINTF("cuttherope_CallVoidMethodV %s\n",p2->name);

char filepath[PATH_MAX]; sprintf(filepath,"assets/%s",filename->data);
if (strcmp(p2->name,"loadImage")==0) {//public void loadImage(String paramString, int paramInt)

if (GLOBAL_J(env)->read_file(filepath, &buf, &buf_size)) {
struct dummy_jstring *filename = va_arg(p3, struct dummy_jstring*);
jint fileId = va_arg(p3, jint);

MODULE_DEBUG_PRINTF("cuttherope_CallVoidMethodV(%s): filename=%s ok\n",p2->name,filename->data);
image_t* image = 0;
if (strstr(filepath,".png")!=0) {
image = loadpng_mem(buf,buf_size,loadsettings);
}
else
if (strstr(filepath,".jpeg")!=0) {
image = loadjpeg_mem(buf,buf_size,loadsettings);
}
if (image!=NULL) {
MODULE_DEBUG_PRINTF("cuttherope_CallVoidMethodV(%s): imageLoaded=%s (%d,%d,%d) (%x)\n",p2->name,filename->data,image->width,image->height,image->bpp,image);
cuttherope_priv.imageLoaded(ENV(cuttherope_priv.global),cuttherope_priv.global,fileId,image,image->width,image->height);
}
MODULE_DEBUG_PRINTF("cuttherope_CallVoidMethodV(%s): filename=%s fileId=%d\n",p2->name,filename->data,fileId);

image_t *image = cuttherope_loadImage(env,filename->data);
if (image!=NULL) {
MODULE_DEBUG_PRINTF("cuttherope_CallVoidMethodV(%s): imageLoaded=%s (%d,%d,%d) (%x)\n",p2->name,filename->data,image->width,image->height,image->bpp,image);
cuttherope_priv.imageLoaded(ENV(cuttherope_priv.global),cuttherope_priv.global,fileId,image,image->width,image->height);
}
}
else
Expand Down Expand Up @@ -619,12 +645,16 @@ cuttherope_init(struct SupportModule *self, int width, int height, const char *h
void *billingInterface = (void*)0xF07;
void *remoteDataManager = (void*)0xF08;

memset(self->priv->images,0,sizeof(self->priv->images));

self->priv->JNI_OnLoad(VM_M, NULL);

#ifdef PANDORA
self->priv->nativeResize(ENV_M, GLOBAL_M, height, width); //$$$ landscape-to-portrait pandora fix
self->priv->global->module_hacks->gles_landscape_to_portrait = 1;
if (self->priv->global->module_hacks->gles_landscape_to_portrait)
self->priv->nativeResize(ENV_M, GLOBAL_M, height, width);
else
self->priv->nativeResize(ENV_M, GLOBAL_M, width, height);
self->priv->global->module_hacks->gles_downscale_images = 1;
#else
self->priv->nativeResize(ENV_M, GLOBAL_M, width, height);
Expand Down

0 comments on commit a254188

Please sign in to comment.