Home | History | Annotate | Download | only in app
      1 //
      2 // Copyright 2005 The Android Open Source Project
      3 //
      4 // Simple class to hold an image that can be loaded and unloaded.
      5 //
      6 
      7 // For compilers that support precompilation, include "wx/wx.h".
      8 #include "wx/wxprec.h"
      9 
     10 // Otherwise, include all standard headers
     11 #ifndef WX_PRECOMP
     12 # include "wx/wx.h"
     13 #endif
     14 #include "wx/image.h"   // needed for Windows build
     15 
     16 #include "LoadableImage.h"
     17 #include "AssetStream.h"
     18 #include "MyApp.h"
     19 
     20 #include "utils.h"
     21 
     22 #include <stdio.h>
     23 
     24 
     25 /*
     26  * Load the image.
     27  */
     28 bool LoadableImage::Create(const char* fileName, int x, int y)
     29 {
     30     if (fileName == NULL || x < 0 || y < 0) {
     31         fprintf(stderr, "bad params to %s\n", __PRETTY_FUNCTION__);
     32         return false;
     33     }
     34 
     35     delete[] mName;
     36     mName = android::strdupNew(fileName);
     37 
     38     mX = x;
     39     mY = y;
     40 
     41     return true;
     42 }
     43 
     44 /*
     45  * Load the bitmap.
     46  */
     47 bool LoadableImage::LoadResources(void)
     48 {
     49     if (mName == NULL)
     50         return false;
     51 
     52     if (mpBitmap != NULL)       // already loaded?
     53         return true;
     54 
     55     //printf("LoadResources: '%s'\n", (const char*) mName);
     56 #ifdef BEFORE_ASSET
     57     wxImage img(mName);
     58 #else
     59     android::AssetManager* pAssetMgr = ((MyApp*)wxTheApp)->GetAssetManager();
     60     android::Asset* pAsset;
     61 
     62     pAsset = pAssetMgr->open(mName, android::Asset::ACCESS_RANDOM);
     63     if (pAsset == NULL) {
     64         fprintf(stderr, "ERROR: unable to load '%s'\n", mName);
     65         return false;
     66     } else {
     67         //printf("--- opened asset '%s'\n",
     68         //    (const char*) pAsset->getAssetSource());
     69     }
     70     AssetStream astr(pAsset);
     71 
     72     wxImage img(astr);
     73 #endif
     74 
     75     mWidth = img.GetWidth();
     76     mHeight = img.GetHeight();
     77     if (mWidth <= 0 || mHeight <= 0) {
     78         /* image failed to load or decode */
     79         fprintf(stderr, "ERROR: unable to load/decode '%s'\n", mName);
     80         //delete img;
     81         return false;
     82     }
     83 
     84     mpBitmap = new wxBitmap(img);
     85 
     86     //delete img;
     87 
     88     return true;
     89 }
     90 
     91 /*
     92  * Unload the bitmap.
     93  */
     94 bool LoadableImage::UnloadResources(void)
     95 {
     96     delete mpBitmap;
     97     mpBitmap = NULL;
     98     return true;
     99 }
    100 
    101