|
|
@@ -20,18 +20,18 @@ using namespace std; |
|
|
|
namespace lol |
|
|
|
{ |
|
|
|
|
|
|
|
/* HACK: We cannot make this an ImageCodec member function, because the |
|
|
|
* REGISTER_IMAGE_LOADER macro forward-declares free functions from |
|
|
|
* the "lol" namespace. An apparent bug in Visual Studio's compiler |
|
|
|
* makes it think these functions are actually in the top-level |
|
|
|
* namespace when the forward declaration is in a class member function. |
|
|
|
* To avoid the problem, we make the forward declaration in a free |
|
|
|
* function. |
|
|
|
* The bug was reported to Microsoft and fixed by them, but the fix |
|
|
|
* is not yet available. |
|
|
|
* https://connect.microsoft.com/VisualStudio/feedback/details/730878/ */ |
|
|
|
static bool RegisterAllLoaders() |
|
|
|
{ |
|
|
|
/* HACK: We cannot make this an ImageLoader member function, because the |
|
|
|
* REGISTER_IMAGE_LOADER macro forward-declares free functions from |
|
|
|
* the "lol" namespace. An apparent bug in Visual Studio's compiler |
|
|
|
* makes it think these functions are actually in the top-level |
|
|
|
* namespace when the forward declaration is in a class member function. |
|
|
|
* To avoid the problem, we make the forward declaration in a free |
|
|
|
* function. |
|
|
|
* The bug was reported to Microsoft and fixed by them, but the fix |
|
|
|
* is not yet available. |
|
|
|
* https://connect.microsoft.com/VisualStudio/feedback/details/730878/ */ |
|
|
|
#if defined __ANDROID__ |
|
|
|
REGISTER_IMAGE_LOADER(AndroidImageData) |
|
|
|
#endif |
|
|
@@ -53,23 +53,72 @@ static bool RegisterAllLoaders() |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* Static image loader |
|
|
|
* Our static image loader |
|
|
|
*/ |
|
|
|
|
|
|
|
Image *Image::Create(char const *path) |
|
|
|
static class ImageLoader |
|
|
|
{ |
|
|
|
public: |
|
|
|
Image *Create(char const *path); |
|
|
|
void Destroy(Image *img); |
|
|
|
|
|
|
|
private: |
|
|
|
Map<String, Image *> m_images; |
|
|
|
} |
|
|
|
image_loader; |
|
|
|
|
|
|
|
Image *ImageLoader::Create(char const *path) |
|
|
|
{ |
|
|
|
/* Initialise loaders (see above) */ |
|
|
|
static bool init = RegisterAllLoaders(); |
|
|
|
UNUSED(init); |
|
|
|
|
|
|
|
Image *ret = new Image(); |
|
|
|
ret->m_data = ImageLoader::Load(path); |
|
|
|
return ret; |
|
|
|
/* Is our image already in the bank? If so, no need to create it. */ |
|
|
|
Image *img; |
|
|
|
|
|
|
|
if (m_images.HasKey(path)) |
|
|
|
{ |
|
|
|
img = m_images[path]; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
img = new Image(); |
|
|
|
img->m_data = ImageCodec::Load(path); |
|
|
|
m_images[path] = img; |
|
|
|
} |
|
|
|
|
|
|
|
++img->m_data->m_refcount; |
|
|
|
return img; |
|
|
|
} |
|
|
|
|
|
|
|
void ImageLoader::Destroy(Image *img) |
|
|
|
{ |
|
|
|
ASSERT(img->m_data->m_refcount > 0); |
|
|
|
if (--img->m_data->m_refcount == 0) |
|
|
|
{ |
|
|
|
/* FIXME: unload images etc. here */ |
|
|
|
/* XXX: we’re gonna hit a problem here because we didn’t keep |
|
|
|
* the image path within the object, so unless we store it |
|
|
|
* ourselves we’re good for a O(n) lookup… which we can’t do |
|
|
|
* on Map objects anyway. */ |
|
|
|
/* TODO: 1. remove image from Map |
|
|
|
2. delete img; */ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
* Static image methods |
|
|
|
*/ |
|
|
|
|
|
|
|
Image *Image::Create(char const *path) |
|
|
|
{ |
|
|
|
return image_loader.Create(path); |
|
|
|
} |
|
|
|
|
|
|
|
void Image::Destroy(Image *img) |
|
|
|
{ |
|
|
|
delete img; |
|
|
|
return image_loader.Destroy(img); |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|