diff --git a/src/image/image-private.h b/src/image/image-private.h index 41fa8b6c..236a0619 100644 --- a/src/image/image-private.h +++ b/src/image/image-private.h @@ -21,7 +21,7 @@ namespace lol class ImageCodec { - friend class ImageLoader; + friend class ImageBank; friend class ImageData; public: @@ -69,7 +69,7 @@ private: class ImageData { friend class Image; - friend class ImageLoader; + friend class ImageBank; public: inline ImageData() diff --git a/src/image/image.cpp b/src/image/image.cpp index 18ba7e60..ddd0e951 100644 --- a/src/image/image.cpp +++ b/src/image/image.cpp @@ -58,29 +58,27 @@ static bool RegisterAllLoaders() * Our static image loader */ -static class ImageLoader +static class ImageBank { public: void Init(); Image *Create(char const *path); - Image *Get(char const *path); - Image *Load(char const *path); - Image *Store(char const *path, Image *image); - void Destroy(Image *img); + + void Unref(Image *img); private: Map m_images; } -image_loader; +g_image_bank; -void ImageLoader::Init() +void ImageBank::Init() { /* Initialise loaders (see above) */ static bool init = RegisterAllLoaders(); UNUSED(init); } -Image *ImageLoader::Create(char const *path) +Image *ImageBank::Create(char const *path) { Init(); @@ -93,7 +91,8 @@ Image *ImageLoader::Create(char const *path) } else { - m_images[path] = Load(path); + m_images[path] = new Image(); + m_images[path]->m_data = ImageCodec::Load(path); img = m_images[path]; } @@ -101,35 +100,7 @@ Image *ImageLoader::Create(char const *path) return img; } -Image *ImageLoader::Get(char const *path) -{ - /* 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]; - ++img->m_data->m_refcount; - return img; - } - - return nullptr; -} - -Image *ImageLoader::Load(char const *path) -{ - Image* img = new Image(); - img->m_data = ImageCodec::Load(path); - return img; -} - -Image *ImageLoader::Store(char const *path, Image *image) -{ - m_images[path] = image; - ++image->m_data->m_refcount; - return image; -} - -void ImageLoader::Destroy(Image *img) +void ImageBank::Unref(Image *img) { ASSERT(img->m_data->m_refcount > 0); if (--img->m_data->m_refcount == 0) @@ -151,22 +122,7 @@ void ImageLoader::Destroy(Image *img) Image *Image::Create(char const *path) { - return image_loader.Create(path); -} - -Image *Image::Store(char const *path, Image *img) -{ - return image_loader.Store(path, img); -} - -Image *Image::Load(char const *path) -{ - return image_loader.Load(path); -} - -void Image::Destroy(Image *img) -{ - return image_loader.Destroy(img); + return g_image_bank.Create(path); } /* @@ -178,6 +134,11 @@ Image::Image() { } +void Image::Destroy() +{ + g_image_bank.Unref(this); +} + bool Image::Save(char const *path) { return m_data->Save(path); diff --git a/src/lol/image/image.h b/src/lol/image/image.h index 3246e4e7..a065f017 100644 --- a/src/lol/image/image.h +++ b/src/lol/image/image.h @@ -23,15 +23,14 @@ namespace lol class Image { - friend class ImageLoader; + friend class ImageBank; public: static Image *Create(char const *path); - static Image *Store(char const *path, Image *img); - static Image *Load(char const *path); - static void Destroy(Image *img); bool Save(char const *path); + void Destroy(); + ivec2 GetSize() const; PixelFormat GetFormat() const; uint8_t *GetData() const; diff --git a/src/tileset.cpp b/src/tileset.cpp index 4c62f5ac..319bcdf0 100644 --- a/src/tileset.cpp +++ b/src/tileset.cpp @@ -190,7 +190,7 @@ void TileSet::TickDraw(float seconds) { if (m_data->m_image) { - Image::Destroy(m_data->m_image); + m_data->m_image->Destroy(); m_data->m_image = nullptr; } else @@ -223,7 +223,7 @@ void TileSet::TickDraw(float seconds) if (pixels != m_data->m_image->GetData()) delete[] pixels; - Image::Destroy(m_data->m_image); + m_data->m_image->Destroy(); m_data->m_image = nullptr; } } diff --git a/test/unit/image.cpp b/test/unit/image.cpp index 7f7d03ad..37557133 100644 --- a/test/unit/image.cpp +++ b/test/unit/image.cpp @@ -41,7 +41,7 @@ LOLUNIT_FIXTURE(ImageTest) LOLUNIT_ASSERT_EQUAL((int)data[255 * 4 + 1], 0xff); LOLUNIT_ASSERT_EQUAL((int)data[255 * 4 + 2], 0xff); - Image::Destroy(image); + image->Destroy(); } };